:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --bubble-in: #e9ecf1;
  --bubble-owner: #2563eb;
  --bubble-agent: #7c3aed;
  --ok: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  --radius: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120; --card: #131c2e; --ink: #e5e7eb; --muted: #94a3b8; --line: #1f2a40;
    --accent: #3b82f6; --bubble-in: #1f2a40; --bubble-owner: #2563eb; --bubble-agent: #6d28d9;
    --ok: #4ade80; --warn: #fbbf24; --bad: #f87171; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --card: #131c2e; --ink: #e5e7eb; --muted: #94a3b8; --line: #1f2a40;
  --accent: #3b82f6; --bubble-in: #1f2a40; --bubble-owner: #2563eb; --bubble-agent: #6d28d9;
  --ok: #4ade80; --warn: #fbbf24; --bad: #f87171; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { background: none; border: 0; font-size: 30px; line-height: 1; padding: 0 6px 4px 0; cursor: pointer; color: var(--accent); }
.mode-pill { font-size: 12px; padding: 3px 9px; border-radius: 999px; background: #f59e0b22; color: var(--warn); font-weight: 600; }
.mode-pill.live { background: #16a34a22; color: var(--ok); }

main { max-width: 760px; margin: 0 auto; padding: 12px 16px 24px; }

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
.tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-decoration: none; color: var(--muted); font-size: 12px; position: relative; gap: 2px;
}
.tabs a .ico { font-size: 20px; }
.tabs a.active { color: var(--accent); font-weight: 600; }
.badge { position: absolute; top: 8px; left: calc(50% + 6px); background: var(--bad); color: #fff; font-size: 11px; border-radius: 999px; padding: 0 6px; min-width: 18px; text-align: center; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 14px; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }

.list { background: var(--card); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); }
.row { display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); text-decoration: none; color: inherit; align-items: flex-start; }
.row:last-child { border-bottom: 0; }
.row:active { background: var(--bg); }
.avatar { flex: none; width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 16px; }
.row .main { flex: 1; min-width: 0; }
.row .top { display: flex; gap: 8px; align-items: baseline; }
.row .name { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .time { color: var(--muted); font-size: 13px; flex: none; }
.row .biz { font-size: 12px; color: var(--muted); }
.row .preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.unread .name::after { content: ""; display: inline-block; width: 8px; height: 8px; background: var(--accent); border-radius: 50%; margin-left: 6px; vertical-align: 2px; }
.row.unread .preview { color: var(--ink); }

.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; margin-right: 4px; vertical-align: 1px; }
.tag.missed { background: #dc262622; color: var(--bad); }
.tag.booked { background: #16a34a22; color: var(--ok); }
.tag.human { background: #f59e0b22; color: var(--warn); }
.tag.agent { background: #7c3aed22; color: #8b5cf6; }
.tag.owner { background: #2563eb22; color: var(--accent); }
.tag.ch-ig { background: linear-gradient(90deg, #f58529, #dd2a7b, #8134af); color: #fff; }
.tag.ch-fb { background: #0866ff; color: #fff; }
.tag.ch-sms { background: #64748b22; color: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* thread */
.thread-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.thread-head .who { font-size: 14px; color: var(--muted); }
.thread-head a { color: var(--accent); }
.agent-toggle { display: flex; align-items: center; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }
.agent-toggle select { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 6px; background: var(--bg); }
.bubbles { display: flex; flex-direction: column; gap: 6px; padding-bottom: 90px; }
.bubble { max-width: 82%; padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; }
.bubble.in { align-self: flex-start; background: var(--bubble-in); border-bottom-left-radius: 6px; }
.bubble.owner { align-self: flex-end; background: var(--bubble-owner); color: #fff; border-bottom-right-radius: 6px; }
.bubble.agent { align-self: flex-end; background: var(--bubble-agent); color: #fff; border-bottom-right-radius: 6px; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 4px 6px; }
.meta.right { align-self: flex-end; }
.callcard { align-self: center; width: 100%; max-width: 420px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; text-decoration: none; color: inherit; display: block; }
.callcard .t { font-weight: 600; }
.callcard .s { font-size: 14px; color: var(--muted); margin-top: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }

.composer {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom)); z-index: 4;
  background: var(--card); border-top: 1px solid var(--line); padding: 8px 12px;
}
.composer form { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 18px; padding: 8px 14px; background: var(--bg); max-height: 120px; }
.composer .from { font-size: 11px; color: var(--muted); max-width: 760px; margin: 0 auto 4px; }

.btn { border: 0; background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 9px 16px; font-weight: 600; cursor: pointer; text-decoration: none; display: inline-block; }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--bad); border: 1px solid var(--line); }
.btn:disabled { opacity: .5; }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 17px; margin: 0 0 4px; }
.card h3 { font-size: 13px; margin: 14px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.card .sub { color: var(--muted); font-size: 14px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.stat { background: var(--bg); border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 11px; color: var(--muted); }
.statusline { font-size: 14px; margin-top: 8px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.transcript { font-size: 14px; white-space: pre-wrap; background: var(--bg); border-radius: 10px; padding: 10px; max-height: 360px; overflow: auto; }
audio { width: 100%; margin-top: 6px; }

/* forms */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 3px; }
.field input, .field select, .field textarea { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; background: var(--bg); }
.hours { display: grid; grid-template-columns: 52px 1fr 1fr 44px; gap: 6px 8px; align-items: center; font-size: 14px; }
.hours input[type=time] { border: 1px solid var(--line); border-radius: 8px; padding: 5px; background: var(--bg); width: 100%; min-width: 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.note { font-size: 13px; color: var(--muted); background: var(--bg); border-radius: 10px; padding: 10px; margin: 8px 0; }
.note.warn { color: var(--warn); }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabs-h) + 80px + env(safe-area-inset-bottom)); background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 999px; font-size: 14px; z-index: 20; max-width: calc(100% - 32px); }
