:root {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --border: #e3e8ef;
  --text: #1a2233;
  --text-dim: #667085;
  --primary: #2f6bff;
  --primary-ink: #ffffff;
  --in-bubble: #eef1f6;
  --in-ink: #1a2233;
  --out-bubble: #2f6bff;
  --out-ink: #ffffff;
  --danger: #e0483b;
  --ok: #16a34a;
  --warn: #d99b16;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1420;
    --surface: #171d2b;
    --surface-2: #1d2434;
    --border: #2a3346;
    --text: #e7ecf5;
    --text-dim: #93a0b5;
    --in-bubble: #262f42;
    --in-ink: #e7ecf5;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 26px; }
.brand h1 { font-size: 17px; margin: 0; font-weight: 700; letter-spacing: -0.2px; }
.subtitle { margin: 0; font-size: 12px; color: var(--text-dim); }
.topbar-right { display: flex; align-items: center; gap: 12px; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-dim);
  text-transform: capitalize;
}
.status-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.status-pill.connected .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(22,163,74,0.15); }
.status-pill.connected { color: var(--ok); }
.status-pill.disconnected .dot, .status-pill.error .dot { background: var(--danger); }
.status-pill.disconnected, .status-pill.error { color: var(--danger); }

/* ---------- buttons ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  font-size: 13.5px;
  padding: 9px 16px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: filter .15s, background .15s, transform .05s;
}
.btn:hover { filter: brightness(0.97); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.btn-icon {
  border: none; background: transparent; cursor: pointer; font-size: 17px;
  color: var(--text-dim); width: 34px; height: 34px; border-radius: 8px;
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); }
.btn-icon.danger:hover { color: var(--danger); }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.search-wrap { padding: 12px; border-bottom: 1px solid var(--border); }
#search {
  width: 100%;
  padding: 10px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 13.5px;
}
#search:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.conv-list { overflow-y: auto; flex: 1; }
.conv {
  display: flex;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  align-items: center;
}
.conv:hover { background: var(--surface-2); }
.conv.active { background: color-mix(in srgb, var(--primary) 10%, var(--surface)); }
.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 600; color: #fff; font-size: 15px;
}
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.conv-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11.5px; color: var(--text-dim); flex-shrink: 0; }
.conv-preview { font-size: 13px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.badge {
  background: var(--primary); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  display: grid; place-items: center; flex-shrink: 0;
}
.conv-empty, .list-empty { padding: 40px 20px; text-align: center; color: var(--text-dim); font-size: 13.5px; }

/* ---------- thread ---------- */
.thread { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.thread-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-dim); text-align: center; padding: 20px;
}
.empty-illustration { font-size: 56px; opacity: 0.5; margin-bottom: 8px; }
.thread-empty h2 { margin: 0 0 4px; color: var(--text); font-size: 18px; }
.thread-empty p { margin: 0; font-size: 14px; }

.thread-open { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.back-btn { display: none; font-size: 24px; }
.thread-peer { flex: 1; min-width: 0; }
.peer-name { display: block; font-weight: 600; font-size: 15px; }
.peer-number { display: block; font-size: 12.5px; color: var(--text-dim); }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 4px; }
.day-sep { text-align: center; margin: 14px 0 8px; }
.day-sep span {
  font-size: 11.5px; color: var(--text-dim); background: var(--surface-2);
  padding: 3px 10px; border-radius: 999px;
}
.bubble-row { display: flex; margin-top: 2px; }
.bubble-row.out { justify-content: flex-end; }
.bubble-row.in { justify-content: flex-start; }
.bubble {
  max-width: min(72%, 560px);
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.42;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: var(--shadow);
}
.bubble-row.in .bubble { background: var(--in-bubble); color: var(--in-ink); border-bottom-left-radius: 5px; }
.bubble-row.out .bubble { background: var(--out-bubble); color: var(--out-ink); border-bottom-right-radius: 5px; }
.meta { font-size: 10.5px; margin-top: 4px; opacity: 0.75; display: flex; gap: 6px; align-items: center; }
.bubble-row.out .meta { justify-content: flex-end; }
.status-tick.failed { color: #ffd7d2; font-weight: 700; }
.bubble.failed { background: color-mix(in srgb, var(--danger) 88%, #000); }

/* ---------- composer ---------- */
.composer {
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.composer-input { display: flex; align-items: flex-end; gap: 10px; }
#composeBody {
  flex: 1; resize: none; font: inherit; font-size: 14px;
  padding: 11px 14px; border-radius: 20px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); max-height: 140px; line-height: 1.4;
}
#composeBody:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.span-field { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); font-weight: 500; }
.span-select {
  font: inherit; font-size: 12.5px; font-weight: 500;
  padding: 5px 8px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer;
}
.span-select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.char-count { font-size: 12px; color: var(--text-dim); min-width: 26px; text-align: right; font-variant-numeric: tabular-nums; }
.char-count.low { color: var(--warn); font-weight: 600; }
.char-count.none { color: var(--danger); font-weight: 600; }
.send-btn { width: 42px; height: 42px; border-radius: 50%; padding: 0; font-size: 16px; flex-shrink: 0; }
.field-hint { display: block; margin-top: 5px; font-size: 11.5px; color: var(--text-dim); font-weight: 400; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16,24,40,0.5);
  display: grid; place-items: center; z-index: 50; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: 16px; padding: 24px;
  width: 100%; max-width: 440px; box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}
.modal h2 { margin: 0 0 16px; font-size: 18px; }
.modal label { display: block; font-size: 13px; font-weight: 500; color: var(--text-dim); margin-bottom: 14px; }
.modal input, .modal textarea {
  width: 100%; margin-top: 6px; padding: 10px 12px; font: inherit; font-size: 14px;
  border-radius: 9px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  resize: vertical;
}
.modal input:focus, .modal textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.modal .span-select { width: 100%; margin-top: 6px; padding: 10px 12px; font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast {
  background: var(--text); color: var(--surface); padding: 10px 18px; border-radius: 10px;
  font-size: 13.5px; box-shadow: 0 6px 20px rgba(0,0,0,0.25); animation: pop .2s ease;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }
@keyframes pop { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- login page ---------- */
.login-body { display: grid; place-items: center; padding: 20px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px; width: 100%; max-width: 380px; box-shadow: 0 10px 40px rgba(16,24,40,0.12);
  text-align: center;
}
.login-logo { font-size: 44px; }
.login-card h1 { font-size: 20px; margin: 8px 0 2px; }
.login-sub { color: var(--text-dim); font-size: 13.5px; margin: 0 0 20px; }
.login-card label { display: block; text-align: left; font-size: 13px; font-weight: 500; color: var(--text-dim); margin-bottom: 14px; }
.login-card input {
  width: 100%; margin-top: 6px; padding: 11px 12px; font: inherit; font-size: 14px;
  border-radius: 9px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.login-btn { width: 100%; padding: 11px; font-size: 15px; margin-top: 4px; }
.login-error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger);
  font-size: 13px; padding: 9px 12px; border-radius: 8px; margin-bottom: 14px; text-align: left; }

/* ---------- user menu ---------- */
.user-menu { display: flex; align-items: center; gap: 6px; padding-left: 6px; border-left: 1px solid var(--border); }
.user-name { font-size: 13px; font-weight: 600; color: var(--text); max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- admin ---------- */
.admin-section { margin-bottom: 24px; }
.admin-section h3 { font-size: 14px; margin: 0 0 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.admin-card {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px;
}
.admin-card .avatar { width: 38px; height: 38px; font-size: 14px; }
.admin-info { flex: 1; min-width: 0; }
.admin-name { font-weight: 600; font-size: 14px; }
.admin-meta { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.admin-actions { display: flex; gap: 4px; }
.pill-tag { font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--text-dim); }
.pill-tag.super { background: color-mix(in srgb, var(--primary) 16%, transparent); color: var(--primary); }
.pill-tag.off { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.perm-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.check-row { display: flex; align-items: center; gap: 9px; font-size: 13.5px; color: var(--text); font-weight: 400; cursor: pointer; }
.check-row input { width: 16px; height: 16px; margin: 0; flex-shrink: 0; }
.check-row .perm-desc { color: var(--text-dim); font-size: 12px; }

/* ---------- view nav ---------- */
.view-nav { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; border: 1px solid var(--border); }
.view-tab {
  font: inherit; font-size: 13px; font-weight: 600; color: var(--text-dim);
  padding: 6px 16px; border: none; background: transparent; border-radius: 7px; cursor: pointer;
}
.view-tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- main / views ---------- */
.main { flex: 1; display: flex; min-height: 0; }
.layout { flex: 1; display: flex; min-height: 0; }

/* ---------- directory ---------- */
.directory { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.dir-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.dir-title h2 { margin: 0; font-size: 17px; }
.dir-sub { font-size: 12.5px; color: var(--text-dim); }
.dir-actions { display: flex; gap: 8px; }
.dir-body { flex: 1; overflow-y: auto; padding: 16px 20px; }
.dir-empty { text-align: center; color: var(--text-dim); padding: 60px 20px; font-size: 14px; }

.dir-group { margin-bottom: 18px; }
.dir-group-head {
  display: flex; align-items: center; gap: 10px; margin-bottom: 8px;
  padding-bottom: 6px; border-bottom: 1px solid var(--border);
}
.dir-group-name { font-weight: 700; font-size: 14px; }
.dir-group-count {
  font-size: 11.5px; color: var(--text-dim); background: var(--surface-2);
  padding: 2px 9px; border-radius: 999px; font-weight: 600;
}
.dir-group-actions { margin-left: auto; display: flex; gap: 4px; }

.contact-card {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px;
}
.contact-info { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; font-size: 14px; }
.contact-number { font-size: 12.5px; color: var(--text-dim); }
.contact-actions { display: flex; gap: 4px; }

/* ---------- call logs ---------- */
.calls-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.calls-table th {
  text-align: left; font-weight: 600; color: var(--text-dim); font-size: 12px;
  padding: 8px 12px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--bg);
}
.calls-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.calls-table tr:hover td { background: var(--surface); }
.call-dir { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; }
.call-dir.in { color: var(--ok); }
.call-dir.out { color: var(--primary); }
.call-disp { font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 999px; display: inline-block; }
.call-disp.answered { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.call-disp.missed { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.call-disp.other { background: var(--surface-2); color: var(--text-dim); }
.num { font-variant-numeric: tabular-nums; }

/* ---------- reports ---------- */
.report-block { margin-bottom: 26px; }
.report-block > h3 {
  font-size: 13px; margin: 0 0 12px; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: 0.5px;
}
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px;
}
.tile .num { font-size: 26px; font-weight: 700; letter-spacing: -0.5px; }
.tile .lbl { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.tile.ok .num { color: var(--ok); }
.tile.bad .num { color: var(--danger); }
.tile.pri .num { color: var(--primary); }

.report-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 720px) { .report-cols { grid-template-columns: 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.panel h4 { margin: 0 0 12px; font-size: 13.5px; }
.barrow { display: grid; grid-template-columns: 1fr auto; gap: 8px 10px; align-items: center; margin-bottom: 9px; }
.barrow .lab { font-size: 13px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barrow .lab .sub { color: var(--text-dim); font-size: 11.5px; }
.barrow .val { font-size: 12.5px; font-variant-numeric: tabular-nums; color: var(--text-dim); text-align: right; white-space: nowrap; }
.barrow .track { grid-column: 1 / -1; height: 7px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.barrow .fill { height: 100%; background: var(--primary); border-radius: 4px; }
.barrow .fill.ok { background: var(--ok); }
.barrow .fill.bad { background: var(--danger); }
.barrow .fill.warn { background: var(--warn); }
.report-empty { color: var(--text-dim); font-size: 13px; padding: 8px 0; }

.colchart { display: flex; align-items: flex-end; gap: 2px; height: 110px; padding-top: 4px; }
.colchart .col { flex: 1 1 0; min-width: 2px; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; transition: opacity .12s; }
.colchart .col:hover { opacity: 0.7; }
.colchart .col.peak { background: var(--warn); }
.colchart-labels { display: flex; gap: 2px; margin-top: 4px; }
.colchart-labels span { flex: 1 1 0; min-width: 2px; text-align: center; font-size: 10px; color: var(--text-dim); white-space: nowrap; overflow: visible; }

/* ---------- print / PDF ---------- */
.print-only { display: none; }
@media print {
  body { height: auto; overflow: visible; display: block; background: #fff; color: #000; }
  .topbar, .toasts, .modal-backdrop, .dir-toolbar { display: none !important; }
  .main, #reportsView, .directory, #reportsBody {
    display: block !important; height: auto !important; overflow: visible !important; min-height: 0 !important; background: #fff;
  }
  #messagesView, #directoryView, #callsView, #auditView, #adminView { display: none !important; }
  .print-only { display: block !important; }
  .report-print-header h1 { font-size: 20px; margin: 0 0 2px; }
  .report-print-header p { font-size: 12px; color: #555; margin: 0 0 16px; }
  .report-block, .tile, .panel, .barrow, .colchart { break-inside: avoid; }
  .tile, .panel { box-shadow: none; border: 1px solid #ccc; }
  .colchart .col, .barrow .fill { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* drag-and-drop */
.drop-hint { font-size: 12px; color: var(--text-dim); margin-top: 8px; text-align: center; }
.dir-body.dropping { outline: 2px dashed var(--primary); outline-offset: -6px; background: color-mix(in srgb, var(--primary) 6%, transparent); }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .sidebar { width: 100%; }
  .layout.thread-active .sidebar { display: none; }
  .layout:not(.thread-active) .thread { display: none; }
  .back-btn { display: grid; }
  .subtitle { display: none; }
  .view-nav { order: 3; }
  .brand h1 { font-size: 15px; }
  .dir-actions .btn { padding: 8px 10px; font-size: 12px; }
}
