:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --surface-2: #f0f0f3;
    --text: #1c1c1e;
    --muted: #6e6e73;
    --line: #e3e3e8;
    --accent: #0a84ff;
    --accent-text: #ffffff;
    --bubble-in: #e9e9ee;
    --bubble-in-text: #1c1c1e;
    --good: #30a14e;
    --bad: #d93a3a;
    --warn: #c98a00;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 8px 24px rgba(0, 0, 0, 0.06);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #000000;
        --surface: #111113;
        --surface-2: #1c1c1f;
        --text: #f2f2f7;
        --muted: #98989f;
        --line: #2a2a2e;
        --accent: #0a84ff;
        --bubble-in: #26262a;
        --bubble-in-text: #f2f2f7;
        --shadow: none;
    }
}

* { box-sizing: border-box; }
html, body, #app { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.4; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 22px; }
h3 { font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---------- sign-in ---------- */
.signin { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.signin-card { width: min(380px, 100%); background: var(--surface); border-radius: 22px; padding: 36px 28px; text-align: center; box-shadow: var(--shadow); }
.signin-mark { width: 64px; height: 64px; margin: 0 auto 18px; border-radius: 18px; background: #1a2140; display: grid; place-items: center; }
.signin-mark svg { width: 34px; height: 34px; fill: #fff; }
.signin-card h1 { font-size: 26px; margin-bottom: 6px; }
.signin-card p { margin: 0 0 24px; color: var(--muted); }
.signin-card .signin-form { text-align: left; }
.signin-error { color: #ff453a !important; margin: -8px 0 16px !important; font-size: 14px; }
.signin-or { display: flex; align-items: center; gap: 12px; margin: 18px 0; color: var(--muted); font-size: 13px; }
.signin-or::before, .signin-or::after { content: ''; flex: 1; height: 1px; background: var(--border, rgba(128,128,128,.25)); }

/* ---------- buttons & fields ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 16px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-text); font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.block { width: 100%; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: transparent; color: var(--bad); border: 1px solid var(--line); }
.btn.small { padding: 6px 12px; font-size: 13px; border-radius: 9px; }
.icon-btn { width: 34px; height: 34px; border-radius: 50%; border: 0; background: var(--surface-2); cursor: pointer; display: grid; place-items: center; }
.icon-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 600; }
input[type=text], input[type=tel], input[type=password], input[type=search], select, textarea {
    width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }

/* ---------- shell ---------- */
.shell { height: 100%; display: grid; grid-template-columns: 340px 1fr; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 16px 8px; }
.sidebar-search { padding: 4px 16px 10px; }
.convo-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.convo { display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center; width: 100%; text-align: left; padding: 10px 10px; border: 0; background: none; border-radius: 12px; cursor: pointer; }
.convo:hover { background: var(--surface-2); }
.convo.active { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.convo-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-preview { color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-time { color: var(--muted); font-size: 12px; align-self: start; padding-top: 2px; }
.convo-main { min-width: 0; }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 650; font-size: 16px; position: relative; flex: none; }
.avatar.sm { width: 34px; height: 34px; font-size: 13px; }
.avatar .dot { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--surface); }
.dot.on { background: var(--good); } .dot.off { background: var(--bad); }
.nav { display: flex; border-top: 1px solid var(--line); padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
.nav a { flex: 1; display: grid; justify-items: center; gap: 2px; padding: 6px 4px; border-radius: 10px; color: var(--muted); text-decoration: none; font-size: 11.5px; font-weight: 600; }
.nav a svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav a.active { color: var(--accent); }

.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.page { overflow-y: auto; padding: 28px clamp(16px, 4vw, 40px); max-width: 820px; width: 100%; margin: 0 auto; }
.page > * + * { margin-top: 22px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card > h3 + * { margin-top: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.list-item:first-child { border-top: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.chip { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.chip.good { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.chip.bad { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* ---------- thread ---------- */
.thread-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--line); }
.thread-head .grow { flex: 1; min-width: 0; }
.thread-title { font-weight: 650; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 18px clamp(12px, 4vw, 40px); display: flex; flex-direction: column; gap: 3px; }
.day { align-self: center; margin: 14px 0 8px; font-size: 12px; color: var(--muted); font-weight: 600; }
.bubble { max-width: min(520px, 78%); padding: 8px 13px; border-radius: 19px; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.in { align-self: flex-start; background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 6px; }
.bubble.out { align-self: flex-end; background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 6px; }
.bubble.pending { opacity: 0.6; }
.who { align-self: flex-end; font-size: 11.5px; color: var(--muted); margin: 6px 6px 1px; }
.who.in { align-self: flex-start; }
.stamp { font-size: 11px; color: var(--muted); margin: 1px 8px 4px; }
.stamp.out { align-self: flex-end; } .stamp.in { align-self: flex-start; }
.composer { display: flex; align-items: flex-end; gap: 10px; padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; min-height: 38px; max-height: 140px; border-radius: 20px; padding: 8px 14px; line-height: 20px; }
.send { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; cursor: pointer; display: grid; place-items: center; flex: none; }
.send:disabled { background: var(--surface-2); color: var(--muted); cursor: default; }
.send svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.hero { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }

/* ---------- stats ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 28px; letter-spacing: -0.02em; }
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 10px; align-items: end; height: 150px; }
.bar { display: grid; align-items: end; justify-items: center; gap: 6px; height: 100%; font-size: 11.5px; color: var(--muted); }
.bar-stack { width: 100%; display: flex; flex-direction: column-reverse; justify-content: flex-start; border-radius: 8px; overflow: hidden; min-height: 3px; }
.bar-stack i { display: block; width: 100%; }
.bar-stack .a { background: var(--accent); } .bar-stack .b { background: color-mix(in srgb, var(--accent) 35%, var(--surface-2)); }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }

/* ---------- modal / toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); display: grid; place-items: center; padding: 16px; z-index: 20; }
.sheet { width: min(440px, 100%); max-height: 80vh; background: var(--surface); border-radius: 20px; padding: 18px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); }
.sheet-list { overflow-y: auto; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 30; max-width: 90vw; }

/* ---------- mobile: one pane at a time ---------- */
@media (max-width: 760px) {
    .shell { grid-template-columns: 1fr; }
    .shell[data-pane="main"] .sidebar { display: none; }
    .shell[data-pane="list"] .main { display: none; }
    .back { display: grid; }
}

.secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
input[type=number] { padding: 6px 8px; }

/* ---------- calls ---------- */
.call-overlay { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.55); display: grid; place-items: center; padding: 16px; backdrop-filter: blur(6px); }
.call-card { width: min(340px, 100%); background: var(--surface); border-radius: 26px; padding: 30px 24px 24px; text-align: center; display: grid; gap: 8px; justify-items: center; box-shadow: var(--shadow); }
.call-card .avatar { width: 84px; height: 84px; font-size: 30px; margin-bottom: 6px; }
.call-name { font-size: 22px; font-weight: 650; }
.call-status { color: var(--muted); font-variant-numeric: tabular-nums; min-height: 22px; }
.call-buttons { display: flex; gap: 14px; margin-top: 18px; }
.call-btn { min-width: 96px; padding: 12px 18px; border-radius: 999px; border: 0; font-weight: 650; cursor: pointer; background: var(--surface-2); color: var(--text); }
.call-btn.on { background: var(--text); color: var(--bg); }
.call-btn.accept { background: var(--good); color: #fff; }
.call-btn.decline { background: var(--bad); color: #fff; }
