* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --green: #00a884;
  --green-dark: #008069;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --border: #e4e7eb;
  --text: #111b21;
  --muted: #667781;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --note: #fff7c2;
  --danger: #dc3545;
}
html, body { height: 100%; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); font-size: 13px; margin-top: 8px; }

.btn { border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.btn:hover { filter: brightness(0.97); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-dark); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.full { width: 100%; }

/* Login */
#login-view { display: flex; align-items: center; justify-content: center; height: 100vh; background: linear-gradient(160deg, var(--green-dark), #075e54 60%, #0b3d36); }
.login-card { background: var(--panel); padding: 40px; border-radius: 16px; width: 360px; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.login-logo { font-size: 42px; }
.brand-logo { width: 260px; max-width: 80%; height: auto; margin: 0 auto 6px; display: block; }
#gate-view { display: flex; align-items: center; justify-content: center; height: 100vh; background: linear-gradient(160deg, var(--green-dark), #075e54 60%, #0b3d36); }
.usage-warning { margin-top: 14px; font-size: 12px; line-height: 1.45; color: #9a6a00; background: #fff6e0; border: 1px solid #f2d98a; border-radius: 8px; padding: 9px 11px; text-align: left; }
.usage-banner { display: flex; align-items: center; gap: 10px; justify-content: center; background: #fff6e0; color: #9a6a00; border-bottom: 1px solid #f2d98a; font-size: 13px; padding: 7px 14px; text-align: center; }
.usage-banner button { margin-left: auto; background: transparent; border: none; color: #9a6a00; cursor: pointer; font-size: 14px; opacity: .7; }
.usage-banner button:hover { opacity: 1; }
.usage-banner.hidden { display: none; }
.login-card h1 { font-size: 20px; margin: 10px 0 4px; }
.login-card form { margin-top: 22px; display: flex; flex-direction: column; gap: 10px; }
.login-card input { padding: 11px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
.login-card #signup-form { margin-top: 22px; display: flex; flex-direction: column; gap: 10px; }
.link { color: var(--green-dark); font-weight: 600; text-decoration: none; }
.link:hover { text-decoration: underline; }
.notice { background: #e9f5f1; color: #0b6e4f; border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.invite-row { display: flex; align-items: center; gap: 8px; }
.invite-row code { background: #f0f2f5; border: 1px dashed var(--border); border-radius: 8px; padding: 7px 14px; font-size: 16px; letter-spacing: 2px; font-weight: 700; }

/* Topbar */
.topbar { display: flex; align-items: center; gap: 16px; background: var(--panel); border-bottom: 1px solid var(--border); padding: 10px 16px; }
.brand { font-weight: 700; font-size: 16px; }
.pill { font-size: 12px; padding: 4px 12px; border-radius: 999px; font-weight: 600; }
.pill.connected { background: #d9fdd3; color: #0b6e4f; }
.pill.disconnected { background: #ffe3e3; color: #b02a37; }
.pill.qr, .pill.connecting { background: #fff3cd; color: #8a6d00; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.me { font-size: 13px; color: var(--muted); }

/* Layout */
#app-view { display: flex; flex-direction: column; height: 100vh; }
.layout { display: flex; flex: 1; min-height: 0; }

/* Sidebar */
.sidebar { width: 340px; min-width: 260px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.tabs { display: flex; gap: 4px; padding: 10px 10px 0; }
.tab { flex: 1; border: none; background: transparent; padding: 8px 4px; font-size: 13px; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tab.active { color: var(--green-dark); border-bottom-color: var(--green); font-weight: 600; }
#chat-search { margin: 10px; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item { display: flex; gap: 10px; padding: 12px 14px; cursor: pointer; border-bottom: 1px solid #f4f5f7; }
.chat-item:hover { background: #f5f6f6; }
.chat-item.active { background: #e9f5f1; }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: var(--green); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
.avatar.group { background: #7986cb; }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .row1 { display: flex; justify-content: space-between; gap: 8px; }
.chat-item .cname { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.chat-item .row2 { display: flex; justify-content: space-between; gap: 8px; margin-top: 3px; }
.chat-item .preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--green); color: #fff; font-size: 11px; border-radius: 999px; padding: 1px 7px; font-weight: 700; flex-shrink: 0; }
.assignee-tag { font-size: 11px; color: #5b21b6; background: #ede9fe; border-radius: 4px; padding: 1px 6px; margin-top: 4px; display: inline-block; }

/* Conversation */
.conversation { flex: 1; display: flex; flex-direction: column; position: relative; min-width: 0; background: #efeae2; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
.empty-icon { font-size: 52px; }
.chat-pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--panel); border-bottom: 1px solid var(--border); padding: 10px 16px; flex-wrap: wrap; }
.chat-title .name { font-weight: 700; }
.chat-actions { display: flex; align-items: center; gap: 8px; }
.chat-actions select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; }

.messages { flex: 1; overflow-y: auto; padding: 18px 8%; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 10px; font-size: 14px; line-height: 1.45; box-shadow: 0 1px 1px rgba(0,0,0,.08); white-space: pre-wrap; word-break: break-word; }
.msg.in { background: var(--bubble-in); align-self: flex-start; border-top-left-radius: 2px; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-top-right-radius: 2px; }
.msg .sender { font-size: 12px; font-weight: 700; color: var(--green-dark); margin-bottom: 2px; }
.msg .agent { font-size: 11px; font-weight: 600; color: #7c3aed; margin-bottom: 2px; }
.msg .time { font-size: 10px; color: var(--muted); text-align: right; margin-top: 3px; }
.day-divider { align-self: center; background: #fff; color: var(--muted); font-size: 11px; padding: 4px 12px; border-radius: 8px; margin: 8px 0; box-shadow: 0 1px 1px rgba(0,0,0,.06); }

.composer { display: flex; gap: 10px; padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--border); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: 14px; font-family: inherit; max-height: 120px; }
.composer .attach { font-size: 18px; padding: 8px 10px; }
.msg img.media { display: block; max-width: 100%; max-height: 300px; border-radius: 8px; margin-bottom: 4px; }
.msg video.media { display: block; max-width: 100%; max-height: 300px; border-radius: 8px; margin-bottom: 4px; }
.msg .media-audio { display: block; width: 240px; margin-bottom: 4px; }
.msg .doc { display: inline-block; background: rgba(0,0,0,.05); border-radius: 8px; padding: 8px 12px; color: var(--green-dark); font-weight: 600; text-decoration: none; margin-bottom: 4px; }
.msg .media-fail { display: inline-block; color: var(--muted); font-size: 13px; font-style: italic; }

/* Notes drawer */
.notes-panel { position: absolute; top: 0; right: 0; bottom: 0; width: 320px; background: var(--note); border-left: 1px solid #e6d97a; padding: 14px; display: flex; flex-direction: column; gap: 8px; z-index: 5; }
.notes-head { display: flex; justify-content: space-between; align-items: center; }
.notes-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.note-item { background: #fffef5; border: 1px solid #eee3a0; border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.note-item .who { font-size: 11px; color: var(--muted); margin-top: 4px; }
#note-form textarea { width: 100%; border: 1px solid #e6d97a; border-radius: 8px; padding: 8px; font-family: inherit; font-size: 13px; margin-bottom: 6px; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { background: var(--panel); border-radius: 14px; padding: 26px; width: 420px; max-width: 92vw; max-height: 88vh; overflow-y: auto; }
.modal-card h2 { font-size: 18px; margin-bottom: 8px; }
.modal-card h3 { font-size: 14px; margin: 16px 0 8px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.qr-box { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 280px; gap: 10px; margin-top: 14px; background: #fafafa; border: 1px dashed var(--border); border-radius: 12px; }
.qr-box img { width: 280px; height: 280px; }
.spinner { width: 30px; height: 30px; border: 3px solid var(--border); border-top-color: var(--green); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.team-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.team-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
.team-row .role { font-size: 11px; background: #e0f2fe; color: #075985; padding: 1px 8px; border-radius: 999px; }
.team-row .del { margin-left: auto; }
.team-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.team-form input, .team-form select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; }
.team-form button { grid-column: span 2; }

@media (max-width: 760px) {
  .sidebar { width: 100%; position: absolute; inset: 0; z-index: 4; }
  .sidebar.chat-open { display: none; }
  .messages { padding: 14px 4%; }
  .notes-panel { width: 100%; }
}
