:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #17202c;
  --muted: #5b6878;
  --border: #dbe1e8;
  --primary: #1d5bbf;
  --primary-hover: #174a9c;
  --on-primary: #ffffff;
  --ok: #18703f;
  --ok-bg: #e2f3e9;
  --warn: #8a5a00;
  --warn-bg: #fcf0d9;
  --bad: #b3261e;
  --bad-bg: #fde6e4;
  --new-bg: #edf3ff;
  --you-bg: #1d5bbf;
  --net-bg: #eef1f5;
  --radius: 10px;
  /* Chart series (validated: blue/orange/aqua, adjacent CVD dE >= 9 on this surface) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --grid: #e8ebef;
  --axis: #c3c8cf;
  --good-text: #006300;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161d;
    --surface: #1a2029;
    --text: #e6ebf1;
    --muted: #9aa6b5;
    --border: #2c3542;
    --primary: #5b93ee;
    --primary-hover: #7aa8f2;
    --on-primary: #0c1320;
    --ok: #6fd39a;
    --ok-bg: #173826;
    --warn: #f0c063;
    --warn-bg: #3a2d12;
    --bad: #f28b82;
    --bad-bg: #3d1c1a;
    --new-bg: #1c2a42;
    --you-bg: #3b6fc4;
    --net-bg: #252d39;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --grid: #262d38;
    --axis: #3a4350;
    --good-text: #0ca30c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1 { font-size: 1.4rem; margin: 0 0 4px; }
h2 { font-size: 1rem; margin: 24px 0 8px; color: var(--muted); font-weight: 600; }

button, input, select, textarea { font: inherit; color: inherit; }

input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

textarea { resize: vertical; }

label { display: block; margin-bottom: 12px; font-weight: 600; font-size: 0.9rem; }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; font-size: 15px; }

button {
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}

button:disabled { opacity: 0.55; cursor: wait; }

button.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); font-weight: 600; }
button.primary:hover:not(:disabled) { background: var(--primary-hover); }

button.link { border: 0; background: none; padding: 0; color: var(--primary); }
button.small { font-size: 0.85rem; }

.muted { color: var(--muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.alert {
  background: var(--bad-bg);
  color: var(--bad);
  border-radius: 8px;
  padding: 8px 12px;
}

/* ---------- Sign-in ---------- */

.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login { width: 100%; max-width: 360px; }
.login button { width: 100%; margin-top: 4px; }

/* ---------- Header ---------- */

.top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { font-weight: 700; font-size: 1.05rem; }

.state { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.state .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.state.warn .dot { background: var(--warn); }
.state.ok .dot { background: var(--ok); }
.state.bad .dot { background: var(--bad); }
.state.bad { color: var(--bad); }

.who { margin-left: auto; display: flex; gap: 12px; align-items: center; font-size: 0.9rem; color: var(--muted); }

main { max-width: 640px; margin: 0 auto; padding: 16px; }


h2 { margin-top: 0; }
.card + .card { margin-top: 16px; }

/* ---------- Go online ---------- */

.start p { margin: 0 0 14px; color: var(--muted); }
.start .alert { margin: 14px 0 0; color: var(--bad); }
button.big { padding: 12px 28px; font-size: 1.05rem; }

/* ---------- Call ---------- */

.call { text-align: center; border-width: 2px; border-color: var(--primary); }
.call.ringing { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px var(--new-bg); } }
@media (prefers-reduced-motion: reduce) { .call.ringing { animation: none; } }

.call-kind { color: var(--muted); font-weight: 600; font-size: 0.9rem; }
.call-number { font-size: 1.8rem; font-weight: 700; letter-spacing: 0.02em; margin: 4px 0 2px; overflow-wrap: anywhere; }
.call-sim { color: var(--muted); }
.call-time { font-variant-numeric: tabular-nums; margin-top: 4px; min-height: 1.45em; }

.call-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 14px; }
.call-actions button { min-width: 110px; padding: 11px 18px; font-weight: 600; }
button.answer { background: var(--ok); border-color: var(--ok); color: var(--surface); }
button.hangup { background: var(--bad); border-color: var(--bad); color: var(--surface); }
button[aria-pressed="true"] { background: var(--warn-bg); border-color: var(--warn); color: var(--warn); }

.keypad { display: grid; grid-template-columns: repeat(3, 64px); gap: 8px; justify-content: center; margin-top: 14px; }
.keypad button { padding: 10px 0; font-size: 1.2rem; font-weight: 600; }

/* ---------- Dialer ---------- */

.dial { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; }
.dial select { width: auto; }
.dial + .alert { margin: 10px 0 0; }
.hint { margin: 10px 0 0; font-size: 0.85rem; color: var(--muted); }
@media (max-width: 520px) {
  .dial { grid-template-columns: 1fr 1fr; }
  .dial input { grid-column: 1 / -1; }
}

/* ---------- Recent calls ---------- */

.head { display: flex; justify-content: space-between; align-items: baseline; }
.calls { list-style: none; margin: 0; padding: 0; }
.calls li { display: grid; grid-template-columns: 22px 1fr auto; gap: 2px 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.calls li:first-child { border-top: 0; }
.calls .dir { grid-row: span 2; font-size: 1.1rem; color: var(--muted); }
.calls .num { border: 0; background: none; padding: 0; font-weight: 600; text-align: left; color: var(--primary); }
.calls .when { color: var(--muted); font-size: 0.85rem; text-align: right; }
.calls .meta { grid-column: 2 / -1; font-size: 0.85rem; color: var(--muted); }
.calls .status { font-weight: 600; }
.calls .status.answered { color: var(--ok); }
.calls .status.missed, .calls .status.nobody, .calls .status.failed { color: var(--bad); }
.empty { color: var(--muted); margin: 0; }
.top-hint { margin: 0 0 10px; }
#callSearch { margin-bottom: 6px; }
.calls button.play { font-size: inherit; font-weight: 600; }
.calls .player { grid-column: 2 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; margin-top: 6px; }
.calls .player audio { width: 100%; max-width: 420px; height: 36px; }
.calls .player a { color: var(--primary); font-size: 0.85rem; }
button.more { width: 100%; margin-top: 10px; }

/* ---------- Reports ---------- */

main.wide { max-width: 1120px; }
.who a { color: var(--primary); }
.report-head { margin: 6px 0 12px; }
.report-head h1 { margin: 0; }
.report-head p { margin: 2px 0 0; }

.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 14px; margin-bottom: 16px; }
.filters label { margin: 0; }
.filters select, .filters input { width: auto; min-width: 150px; }
.filter-actions { display: flex; gap: 8px; margin-left: auto; }
#report { transition: opacity 0.15s; }
#report.loading { opacity: 0.5; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.tile-label { color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.tile-value { font-size: 1.7rem; font-weight: 650; margin: 2px 0; }
.tile-sub { color: var(--muted); font-size: 0.82rem; }
.tile-delta { font-size: 0.82rem; font-weight: 600; margin-top: 6px; color: var(--muted); }
.tile-delta.good { color: var(--good-text); }
.tile-delta.bad { color: var(--bad); }
.tile-delta .muted { font-weight: 400; }

.chart-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 16px; }
.chart-head h2 .muted { font-weight: 400; }
.legend { list-style: none; display: flex; gap: 14px; margin: 0 0 8px; padding: 0; font-size: 0.85rem; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.swatch.s1, .key.s1 { background: var(--series-1); }
.swatch.s2, .key.s2 { background: var(--series-2); }
.swatch.s3, .key.s3 { background: var(--series-3); }
.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .peak { fill: var(--text); font-size: 11px; font-weight: 600; }
.chart .bar.s1 { fill: var(--series-1); }
.chart .bar.s2 { fill: var(--series-2); }
.chart .bar.s3 { fill: var(--series-3); }
.chart .hit { fill: transparent; cursor: default; outline: none; }
.chart .hit:focus-visible { stroke: var(--primary); stroke-width: 2; }
.chart svg.hovering .col:not(.active) .bar { opacity: 0.45; }
.table-toggle, .table-more { margin-top: 8px; font-size: 0.85rem; }
.table-wrap { overflow-x: auto; }
.chart-card .table-wrap { margin-top: 8px; }
#tiles + .card { margin-top: 0; }

.tooltip {
  position: absolute; z-index: 10; pointer-events: none; min-width: 150px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12); padding: 8px 10px; font-size: 0.85rem;
}
.tip-title { color: var(--muted); margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 8px; }
.tip-row .key { display: inline-block; width: 12px; height: 2px; border-radius: 1px; }
.tip-row strong { min-width: 2.5em; text-align: right; font-variant-numeric: tabular-nums; }
.tip-row span { color: var(--muted); }

table.data { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.data th { text-align: left; color: var(--muted); font-weight: 600; font-size: 0.8rem; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.data td { padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data tr:last-child td { border-bottom: 0; }
table.data .n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data a { color: var(--primary); }
@media (max-width: 520px) {
  .filter-actions { margin-left: 0; }
  table.data { font-size: 0.85rem; }
  table.data th, table.data td { padding: 6px 5px; }
}

@media print {
  body { background: #fff; color: #000; font-size: 12px; }
  .top, .filters, .table-toggle, .tooltip, #callbackTable td:last-child, #callbackTable th:last-child { display: none !important; }
  main.wide { max-width: none; padding: 0; }
  .card, .tile { border-color: #ccc; break-inside: avoid; }
  .chart-card .table-wrap[hidden] { display: none !important; }
}
