:root {
      --bg: #0d1117;
      --surface: #161b22;
      --surface-2: #21262d;
      --surface-3: #30363d;
      --text: #e6edf3;
      --text-muted: #8b949e;
      --text-subtle: #6e7681;
      --border: #30363d;
      --accent: #3fb950;
      --accent-fg: #0d1117;
      --accent-muted: rgba(63,185,80,0.12);
      --blue: #1f6feb;
      --blue-muted: rgba(31,111,235,0.12);
      --purple: #a371f7;
      --purple-muted: rgba(163,113,247,0.12);
      --danger: #f85149;
      --danger-muted: rgba(248,81,73,0.12);
      --warning: #e3b341;
      --warning-muted: rgba(227,179,65,0.12);
      --radius: 12px;
      --radius-sm: 8px;
      --nav: 84px;
      --sidebar: 300px;
      --topbar: 52px;
      --shadow: 0 8px 32px rgba(0,0,0,0.4);
    }
    body.light {
      --bg: #f6f8fa;
      --surface: #ffffff;
      --surface-2: #f6f8fa;
      --surface-3: #eaeef2;
      --text: #1f2328;
      --text-muted: #656d76;
      --text-subtle: #8c959f;
      --border: #d0d7de;
      --accent: #1a7f37;
      --accent-fg: #ffffff;
      --blue: #0969da;
      --danger: #d1242f;
      --warning: #9a6700;
    }
    *, *::before, *::after { box-sizing: border-box; }
    html, body { height: 100%; overflow: hidden; }
    body {
      margin: 0;
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
      background: var(--bg);
      color: var(--text);
      font-size: 14px;
    }
    button, input, select, textarea { font: inherit; }

    /* ── Buttons ──────────────────────────────────────────────────────────── */
    .btn {
      display: inline-flex; align-items: center; justify-content: center; gap: 6px;
      padding: 7px 14px; border-radius: var(--radius-sm);
      border: 1px solid var(--border);
      background: var(--surface-2); color: var(--text);
      font-weight: 500; cursor: pointer; white-space: nowrap;
      transition: all 0.15s; text-decoration: none; font-size: 13px;
    }
    .btn:hover { background: var(--surface-3); border-color: var(--text-muted); }
    .btn:disabled { opacity: 0.5; cursor: not-allowed; }
    .btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 600; }
    .btn.primary:hover { opacity: 0.9; }
    .btn.blue { background: var(--blue); color: #fff; border-color: var(--blue); font-weight: 600; }
    .btn.blue:hover { opacity: 0.9; }
    .btn.danger { background: var(--danger-muted); color: var(--danger); border-color: rgba(248,81,73,0.3); }
    .btn.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
    .btn.ghost { background: transparent; border-color: transparent; }
    .btn.ghost:hover { background: var(--surface-3); border-color: var(--border); }
    .btn.sm { padding: 5px 10px; font-size: 12px; }
    .btn.icon { padding: 7px; width: 34px; }

    /* ── Forms ────────────────────────────────────────────────────────────── */
    .field { display: flex; flex-direction: column; gap: 5px; }
    .field label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
    input, select, textarea {
      width: 100%; padding: 8px 11px;
      border: 1px solid var(--border); border-radius: var(--radius-sm);
      background: var(--surface-2); color: var(--text);
      outline: none; transition: border-color 0.15s; font-size: 14px;
    }
    input:focus, textarea:focus, select:focus { border-color: var(--accent); }
    input[type="file"] { padding: 6px 10px; cursor: pointer; }
    .error-msg {
      padding: 10px 14px; border-radius: var(--radius-sm);
      background: var(--danger-muted); color: var(--danger);
      border: 1px solid rgba(248,81,73,0.3); font-size: 13px;
    }

    /* ── Layout ───────────────────────────────────────────────────────────── */
    .hidden { display: none !important; }

    /* Login */
    .login-page {
      height: 100vh; display: grid; place-items: center;
      background: radial-gradient(ellipse at 50% 0%, rgba(63,185,80,0.08), transparent 60%), var(--bg);
    }
    .login-card {
      width: 100%; max-width: 400px; padding: 32px;
      background: var(--surface); border: 1px solid var(--border);
      border-radius: 16px; box-shadow: var(--shadow);
    }
    .login-logo {
      width: 68px; height: 62px; border-radius: 12px;
      background: linear-gradient(135deg, #3fb950, #1a7f37);
      display: grid; place-items: center; margin-bottom: 20px;
    }
    .login-card h1 { font-size: 22px; font-weight: 700; margin-bottom: 6px; }
    .login-card p { color: var(--text-muted); margin-bottom: 24px; font-size: 14px; }
    .login-form { display: flex; flex-direction: column; gap: 14px; }

    /* App shell */
    .app {
      display: grid;
      grid-template-columns: var(--nav) var(--sidebar) 1fr;
      grid-template-rows: var(--topbar) 1fr;
      height: 100vh; overflow: hidden;
    }

    /* Topbar */
    .topbar {
      grid-column: 1 / -1; grid-row: 1;
      padding: 0 16px; display: flex; align-items: center; gap: 12px;
      border-bottom: 1px solid var(--border);
      background: var(--surface);
      z-index: 10;
    }
    .topbar-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; min-width: 0; }
    .brand-dot {
      width: 28px; height: 28px; border-radius: 8px;
      background: linear-gradient(135deg, #3fb950, #1a7f37);
      flex-shrink: 0;
    }
    .topbar-user {
      display: flex; align-items: center; gap: 8px;
      margin-left: auto; color: var(--text-muted); font-size: 13px;
    }
    .user-badge {
      width: 28px; height: 28px; border-radius: 50%;
      background: var(--purple-muted); border: 1.5px solid var(--purple);
      display: grid; place-items: center; font-size: 12px; font-weight: 700;
      color: var(--purple); overflow: hidden; flex-shrink: 0;
    }
    .user-badge img { width: 100%; height: 100%; object-fit: cover; }
    .topbar-actions { display: flex; align-items: center; gap: 6px; }

    /* Sidebar */
    .sidebar {
      grid-column: 2; grid-row: 2;
      border-right: 1px solid var(--border);
      background: var(--surface);
      display: flex; flex-direction: column;
      overflow: hidden; min-height: 0;
    }

    /* Nav rail */
    .nav-rail {
      grid-column: 1; grid-row: 2;
      background: var(--surface);
      border-right: 1px solid var(--border);
      display: flex; flex-direction: column;
      align-items: center; padding: 8px 0; gap: 2px;
      overflow: hidden; z-index: 5;
    }
    .nav-item {
      width: 68px; height: 62px; border-radius: var(--radius-sm);
      border: none; background: transparent; color: var(--text-muted);
      display: flex; flex-direction: column; align-items: center; justify-content: center;
      gap: 3px; cursor: pointer; transition: all 0.15s; position: relative;
      font-size: 12px; font-weight: 700; letter-spacing: 0.02em;
    }
    .nav-item .nav-icon { font-size: 25px; line-height: 1; }
    .nav-item:hover { background: var(--surface-2); color: var(--text); }
    .nav-item.active { background: var(--accent-muted); color: var(--accent); }
    .nav-item .nav-badge {
      position: absolute; top: 4px; right: 4px;
      min-width: 16px; height: 16px; padding: 0 4px;
      border-radius: 999px; background: var(--danger);
      color: #fff; font-size: 9px; font-weight: 700;
      display: flex; align-items: center; justify-content: center;
      line-height: 1;
    }
    .nav-spacer { flex: 1; }
    .nav-divider { width: 44px; height: 1px; background: var(--border); margin: 4px 0; }

    .sidebar-tabs {
      display: flex; gap: 4px; padding: 10px 12px;
      border-bottom: 1px solid var(--border);
    }
    .sidebar-tab {
      flex: 1; padding: 7px 10px; border-radius: var(--radius-sm);
      background: transparent; border: none; color: var(--text-muted);
      font-weight: 500; cursor: pointer; font-size: 13px; transition: all 0.15s;
    }
    .sidebar-tab.active { background: var(--accent-muted); color: var(--accent); }
    .sidebar-tab:hover:not(.active) { background: var(--surface-2); color: var(--text); }

    /* Stats strip */
    .stats-strip {
      display: grid; grid-template-columns: repeat(3, 1fr);
      gap: 1px; background: var(--border);
      border-bottom: 1px solid var(--border);
    }
    .stat-cell {
      padding: 10px 12px; background: var(--surface);
      text-align: center;
    }
    .stat-num { font-size: 18px; font-weight: 700; }
    .stat-num.orange { color: var(--warning); }
    .stat-num.red { color: var(--danger); }
    .stat-num.green { color: var(--accent); }
    .stat-lbl { font-size: 10px; color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 1px; }

    /* Filter tabs */
    .filter-row {
      display: flex; gap: 4px; padding: 8px 10px;
      border-bottom: 1px solid var(--border); align-items: center;
    }
    .filter-btn {
      flex: 1; padding: 6px 8px; border-radius: var(--radius-sm);
      background: transparent; border: 1px solid transparent;
      color: var(--text-muted); font-size: 12px; font-weight: 500;
      cursor: pointer; transition: all 0.15s; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .filter-btn.active { background: var(--accent-muted); color: var(--accent); border-color: rgba(63,185,80,0.3); }
    .filter-btn.call { color: var(--danger); }
    .filter-btn.call.active { background: var(--danger-muted); border-color: rgba(248,81,73,0.3); }
    .filter-btn.call.flash { animation: flashCall 1s ease infinite; }
    @keyframes flashCall { 0%,100% { background: var(--danger-muted); } 50% { background: var(--danger); color: #fff; } }
    .refresh-btn { padding: 6px; width: 30px; flex-shrink: 0; font-size: 16px; }

    /* Conv list */
    .conv-list { flex: 1; overflow-y: auto; min-height: 0; }
    .conv-list::-webkit-scrollbar { width: 4px; }
    .conv-list::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 3px; }
    .conv-empty { padding: 40px 20px; text-align: center; color: var(--text-subtle); font-size: 13px; }
    .conv-item {
      padding: 12px 14px; border-bottom: 1px solid var(--border);
      cursor: pointer; transition: background 0.1s;
      position: relative;
    }
    .conv-item:hover { background: var(--surface-2); }
    .conv-item.active { background: var(--accent-muted); }
    .conv-item.call-req { border-left: 3px solid var(--danger); }
    .conv-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 6px; }
    .conv-title { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
    .conv-badges { display: flex; gap: 4px; align-items: center; flex-shrink: 0; }
    .badge {
      font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 999px;
      letter-spacing: 0.03em; text-transform: uppercase; white-space: nowrap;
    }
    .badge.new { background: var(--warning-muted); color: var(--warning); border: 1px solid rgba(227,179,65,0.3); }
    .badge.agent { background: var(--accent-muted); color: var(--accent); border: 1px solid rgba(63,185,80,0.3); }
    .badge.ai { background: var(--blue-muted); color: #58a6ff; border: 1px solid rgba(31,111,235,0.3); }
    .badge.call { background: var(--danger-muted); color: var(--danger); border: 1px solid rgba(248,81,73,0.3); }
    .conv-meta { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
    .conv-preview {
      font-size: 12.5px; color: var(--text-subtle); margin-top: 5px;
      display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
    }
    .conv-actions { display: flex; gap: 6px; margin-top: 8px; }
    .conv-new-banner {
      font-size: 10px; font-weight: 700; color: var(--warning);
      text-transform: uppercase; letter-spacing: 0.06em;
      margin-bottom: 5px;
    }
    .pagination-bar {
      padding: 8px 12px; border-top: 1px solid var(--border);
      display: flex; align-items: center; justify-content: space-between;
      font-size: 12px; color: var(--text-muted);
    }

    /* Main content */
    .main-content {
      grid-column: 2 / -1; grid-row: 2;
      display: flex; flex-direction: column;
      min-height: 0; overflow: hidden;
      background: var(--bg);
    }
    .main-content.with-sidebar {
      grid-column: 3;
    }
    .empty-state {
      flex: 1; display: flex; flex-direction: column;
      align-items: center; justify-content: center; gap: 12px;
      color: var(--text-subtle); text-align: center; padding: 40px;
    }
    .empty-icon { font-size: 48px; opacity: 0.4; }
    .empty-state p { max-width: 300px; line-height: 1.6; }

    /* Chat view */
    .chat-view {
      display: flex; flex-direction: column; height: 100%; min-height: 0; overflow: hidden;
    }
    .chat-topbar {
      padding: 12px 18px; border-bottom: 1px solid var(--border);
      background: var(--surface); display: flex; align-items: flex-start;
      justify-content: space-between; gap: 12px; flex-shrink: 0;
    }
    .chat-info h2 { font-size: 16px; font-weight: 700; margin-bottom: 2px; }
    .chat-info .meta { font-size: 12px; color: var(--text-muted); }
    .chat-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

    /* Messages */
    .messages-area {
      flex: 1; min-height: 0; overflow-y: auto; padding: 16px 18px;
    }
    .messages-area::-webkit-scrollbar { width: 5px; }
    .messages-area::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }
    .msg-group { margin-bottom: 16px; }
    .msg-group.from-customer { display: flex; flex-direction: column; align-items: flex-end; }
    .msg-group.from-ai, .msg-group.from-agent, .msg-group.from-system { display: flex; flex-direction: column; align-items: flex-start; }
    .msg-group.from-system { align-items: center; }
    .msg-row { display: flex; gap: 8px; align-items: flex-end; }
    .msg-group.from-customer .msg-row { flex-direction: row-reverse; }
    .msg-group.from-system .msg-row { justify-content: center; }
    .avatar {
      width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0;
      display: grid; place-items: center; font-size: 10px; font-weight: 700;
      overflow: hidden; border: 1.5px solid var(--border);
    }
    .avatar img { width: 100%; height: 100%; object-fit: cover; }
    .avatar.customer { background: linear-gradient(135deg, #1f6feb, #1158c7); color: #fff; }
    .avatar.ai { background: linear-gradient(135deg, #3fb950, #1a7f37); color: #0d1117; }
    .avatar.agent { background: linear-gradient(135deg, #a371f7, #7c3aed); color: #fff; }
    .avatar.system { background: var(--surface-3); color: var(--text-muted); }
    .msg-bubble {
      max-width: 72%; padding: 10px 13px; border-radius: 16px;
      font-size: 14px; line-height: 1.55; white-space: pre-wrap;
      overflow-wrap: break-word; word-break: break-word;
    }
    .msg-group.from-customer .msg-bubble { background: var(--blue); color: #fff; border-bottom-right-radius: 4px; }
    .msg-group.from-ai .msg-bubble { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
    .msg-group.from-agent .msg-bubble { background: var(--surface-2); color: var(--text); border: 1px solid rgba(163,113,247,0.25); border-bottom-left-radius: 4px; }
    .msg-group.from-system .msg-bubble { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 12.5px; text-align: center; max-width: 85%; }
    .msg-group.from-note { display: flex; flex-direction: column; align-items: flex-start; width: 100%; }
    .msg-group.from-note .msg-row { width: 100%; justify-content: flex-start; }
    .msg-group.from-note .msg-bubble {
      background: rgba(227,179,65,0.08); color: var(--text);
      border: 1px dashed rgba(227,179,65,0.45); border-radius: var(--radius);
      font-size: 13.5px; font-style: italic;
      max-width: min(78%, 720px); min-width: 160px; width: fit-content;
      white-space: pre-wrap; word-break: normal; overflow-wrap: break-word;
    }
    .msg-group.from-note .msg-sender { color: var(--warning); }
    .msg-sender { font-size: 11.5px; font-weight: 600; color: var(--text-muted); margin-bottom: 4px; padding-left: 2px; }
    .msg-group.from-customer .msg-sender { text-align: right; padding-right: 2px; }
    .msg-time { font-size: 10.5px; color: var(--text-subtle); margin-top: 3px; padding-left: 2px; }
    .msg-group.from-customer .msg-time { text-align: right; padding-right: 2px; }
    .new-msg-sep {
      display: flex; align-items: center; gap: 10px;
      font-size: 12px; font-weight: 700; color: var(--warning);
      text-transform: uppercase; letter-spacing: 0.06em; margin: 12px 0;
    }
    .new-msg-sep::before, .new-msg-sep::after { content: ''; flex: 1; height: 1px; background: rgba(227,179,65,0.3); }

    /* Composer */
    .composer {
      padding: 10px 14px 12px; border-top: 1px solid var(--border);
      background: var(--surface); display: flex; flex-direction: column; gap: 8px;
      flex-shrink: 0;
    }
    .composer-tabs { display: flex; gap: 6px; }
    .composer-row { display: flex; gap: 8px; align-items: flex-end; }
    .composer textarea {
      flex: 1; min-height: 52px; max-height: 160px; resize: none;
      line-height: 1.5; font-size: 14px;
    }
    .composer-send { flex-shrink: 0; align-self: flex-end; height: 52px; min-width: 72px; border-radius: var(--radius-sm); }
    .composer-hint { font-size: 11px; color: var(--text-subtle); padding: 0 2px 2px; }
    .composer.note-mode { background: linear-gradient(180deg, rgba(227,179,65,0.06), var(--surface)); }
    .composer.note-mode #adminAttachBtn { display: none; }
    .composer.note-mode #agentMsgInput { min-height: 42px; }
    .composer.note-mode .composer-hint::before { content: 'Internal note mode · '; color: var(--warning); font-weight: 700; }

    /* Users panel */
    .users-panel { height: 100%; overflow-y: auto; padding: 24px 28px; width: 100%; }
    .users-panel::-webkit-scrollbar { width: 5px; }
    .users-panel::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }
    .panel-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; }
    .panel-header h2 { font-size: 20px; font-weight: 700; }
    .security-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 20px; }
    .stat-card {
      padding: 14px 16px; background: var(--surface); border: 1px solid var(--border);
      border-radius: var(--radius); display: flex; flex-direction: column; gap: 4px;
    }
    .stat-card .num { font-size: 24px; font-weight: 700; }
    .stat-card .lbl { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
    .two-col { display: grid; grid-template-columns: 340px 1fr; gap: 16px; align-items: start; }
    .card {
      background: var(--surface); border: 1px solid var(--border);
      border-radius: var(--radius); padding: 18px;
    }
    .card h3 { font-size: 15px; font-weight: 700; margin-bottom: 16px; }
    .form-stack { display: flex; flex-direction: column; gap: 12px; }
    .form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
    .security-note {
      margin-top: 12px; padding: 10px 12px; font-size: 12px;
      background: var(--accent-muted); color: var(--text-muted);
      border: 1px solid rgba(63,185,80,0.2); border-radius: var(--radius-sm);
      line-height: 1.5;
    }
    .search-row { display: grid; grid-template-columns: 1fr 130px 130px; gap: 8px; margin-bottom: 12px; }
    .user-cards { display: flex; flex-direction: column; gap: 10px; }
    .user-card {
      background: var(--surface-2); border: 1px solid var(--border);
      border-radius: var(--radius); padding: 14px;
    }
    .user-card.removed { border-color: rgba(248,81,73,0.2); }
    .uc-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 10px; }
    .uc-identity { display: flex; align-items: center; gap: 10px; min-width: 0; }
    .uc-avatar {
      width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
      display: grid; place-items: center; font-size: 12px; font-weight: 700;
      background: var(--purple-muted); color: var(--purple);
      border: 1.5px solid rgba(163,113,247,0.3); overflow: hidden;
    }
    .uc-avatar img { width: 100%; height: 100%; object-fit: cover; }
    .uc-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .uc-email { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .uc-pills { display: flex; gap: 5px; flex-wrap: wrap; }
    .pill {
      font-size: 10.5px; font-weight: 700; padding: 3px 8px; border-radius: 999px;
      text-transform: uppercase; letter-spacing: 0.03em;
    }
    .pill.admin { background: var(--blue-muted); color: #58a6ff; border: 1px solid rgba(31,111,235,0.3); }
    .pill.agent { background: var(--purple-muted); color: var(--purple); border: 1px solid rgba(163,113,247,0.3); }
    .pill.active { background: var(--accent-muted); color: var(--accent); border: 1px solid rgba(63,185,80,0.3); }
    .pill.removed { background: var(--danger-muted); color: var(--danger); border: 1px solid rgba(248,81,73,0.3); }
    .uc-edit { display: grid; grid-template-columns: 1fr 110px; gap: 8px; margin-bottom: 10px; }
    .uc-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
    .uc-meta { font-size: 11px; color: var(--text-subtle); }
    .audit-list { display: flex; flex-direction: column; gap: 7px; max-height: 260px; overflow-y: auto; }
    .audit-item {
      padding: 9px 11px; background: var(--surface-2); border: 1px solid var(--border);
      border-radius: var(--radius-sm);
    }
    .audit-item strong { font-size: 13px; }
    .audit-item .meta { font-size: 11.5px; color: var(--text-muted); margin-top: 2px; }
    .ai-profile { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
    .ai-avatar-wrap {
      width: 44px; height: 44px; border-radius: 50%;
      background: linear-gradient(135deg, #3fb950, #1a7f37);
      display: grid; place-items: center; overflow: hidden; flex-shrink: 0;
    }
    .ai-avatar-wrap img { width: 100%; height: 100%; object-fit: cover; }

    /* Responsive */
    @media (max-width: 1000px) {
      .app { grid-template-columns: var(--nav) 260px 1fr; }
    }
    @media (max-width: 700px) {
      :root { --nav: 60px; --sidebar: 240px; }
      .app { grid-template-columns: var(--nav) var(--sidebar) 1fr; }
      .nav-item { width: 54px; height: 54px; }
      .nav-item .nav-label { display: none; }
      .two-col, .security-stats, .form-row-2, .search-row { grid-template-columns: 1fr; }
      .msg-bubble { max-width: 86%; }
    }
    @media (max-width: 520px) {
      .app { grid-template-columns: var(--nav) 1fr; grid-template-rows: var(--topbar) 1fr; }
      .sidebar { display: none; }
      .sidebar.mobile-open { display: flex; position: fixed; top: var(--topbar); left: var(--nav); width: 280px; height: calc(100vh - var(--topbar)); z-index: 100; box-shadow: 4px 0 20px rgba(0,0,0,0.4); }
      .main-content { grid-column: 2 / -1; }
    }
