:root {
  color-scheme: light;
  --plane: #f2f3f0;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6d67;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --s1: #2a78d6;
  --s1-deep: #1c5cab;
  --s1-wash: #e8f1fc;
  --s2: #eb6834;
  --good: #006300;
  --bad: #b02a2a;
  --focus: #2a78d6;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--plane);
  color: var(--ink);
  font: 14px/1.45 var(--font);
}
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 14px;
  padding: 10px max(20px, calc((100vw - 1320px) / 2 + 20px));
  background: rgba(252, 252, 251, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -0.01em; }
.logo { width: 22px; height: 22px; border-radius: 7px; background: linear-gradient(135deg, var(--s1), var(--s2)); flex: none; }
.affchip {
  padding: 3px 10px; border-radius: 999px; background: var(--s1-wash); color: var(--s1-deep);
  font-weight: 600; font-size: 12.5px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.affselect { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 5px 8px; max-width: 220px; }
.spacer { flex: 1; }
.live { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 12.5px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--axis); }
.live.on .dot { background: #0ca30c; box-shadow: 0 0 0 0 rgba(12, 163, 12, 0.5); animation: pulse 2.4s infinite; }
.live.err .dot { background: var(--s2); }
@keyframes pulse { 70% { box-shadow: 0 0 0 8px rgba(12, 163, 12, 0); } 100% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0); } }
.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 6px 12px; border-radius: 8px; font-weight: 550; white-space: nowrap;
}
.brand { white-space: nowrap; }
@media (max-width: 560px) {
  .topbar { padding-inline: 14px; gap: 10px; }
  #liveText { display: none; }
  .btn { padding: 6px 10px; }
}
.btn:hover { background: #f3f3f0; }
.btn.primary { background: var(--s1); border-color: var(--s1); color: #fff; }
.btn.primary:hover { background: var(--s1-deep); }
.btn.danger { color: var(--bad); }
.btn[disabled] { opacity: 0.55; cursor: default; }
.linkbtn { border: 0; background: none; color: var(--s1-deep); padding: 2px 4px; font-weight: 550; }
.linkbtn:hover { text-decoration: underline; }

/* ---------- layout ---------- */
.shell { max-width: 1320px; margin: 0 auto; padding: 18px 20px 48px; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-bottom: 16px; }
.presets { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 3px; flex-wrap: wrap; }
.presets button { border: 0; background: none; padding: 6px 12px; border-radius: 7px; font-weight: 550; color: var(--ink-2); }
.presets button:hover { background: #f0efec; }
.presets button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.custom { display: flex; align-items: center; gap: 8px; }
.custom label { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }
.custom input { border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; background: var(--surface); }
.period-label { color: var(--muted); }
.grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.main { display: grid; gap: 16px; min-width: 0; }
.side { position: sticky; top: 64px; }
@media (max-width: 1100px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .side { position: static; order: -1; }
}

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; min-width: 0; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card h2 { font-size: 15px; font-weight: 650; letter-spacing: -0.005em; }
.card .sub { color: var(--muted); font-size: 12.5px; }
.eyebrow { font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }

/* ---------- hero ---------- */
.hero-value { font-size: clamp(40px, 6vw, 60px); font-weight: 680; letter-spacing: -0.03em; line-height: 1.02; margin: 6px 0 8px; }
.hero-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; color: var(--ink-2); }
.delta { display: inline-flex; align-items: center; gap: 4px; font-weight: 650; font-size: 13px; padding: 2px 8px; border-radius: 999px; }
.delta.up { color: var(--good); background: #e6f4e6; }
.delta.down { color: var(--bad); background: #fbe9e9; }
.delta.flat { color: var(--ink-2); background: #eeede9; }
.split { display: flex; height: 14px; gap: 2px; margin: 18px 0 10px; }
.split span { display: block; min-width: 4px; }
.split .a { background: var(--s1); border-radius: 4px 0 0 4px; }
.split .b { background: var(--s2); border-radius: 0 4px 4px 0; }
.split .only { border-radius: 4px; }
.split-legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px 20px; }
.key { display: flex; gap: 10px; align-items: flex-start; }
.key i { width: 12px; height: 12px; border-radius: 3px; margin-top: 3px; flex: none; }
.key b { display: block; font-size: 17px; font-weight: 650; }
.key span { color: var(--ink-2); font-size: 12.5px; }
.callout { margin-top: 14px; padding: 10px 12px; border-radius: 10px; background: #fdf1eb; color: #6b2c10; font-size: 13px; }

/* ---------- tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { color: var(--ink-2); font-size: 12.5px; }
.tile .value { font-size: 28px; font-weight: 650; letter-spacing: -0.02em; margin: 2px 0; }
.tile .note { color: var(--muted); font-size: 12.5px; }
.tile .delta { font-size: 12px; padding: 1px 6px; margin-left: 6px; vertical-align: 2px; }

/* ---------- charts ---------- */
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 8px; color: var(--ink-2); font-size: 12.5px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; }
.chart { width: 100%; min-height: 200px; }
.chart svg { display: block; overflow: visible; }
.chart text { font: 11.5px var(--font); fill: var(--muted); }
.chart .val { fill: var(--ink-2); font-weight: 600; }
.mark { transition: filter 0.12s; }
.hit:hover + .marks .mark, .colgroup:hover .mark, .colgroup:focus-visible .mark { filter: brightness(1.1); }
.colgroup { outline: none; }
.colgroup:focus-visible .focusring { stroke: var(--focus); }
.focusring { fill: none; stroke: transparent; stroke-width: 2; }
.bars { display: grid; gap: 12px; }
.bar-row { display: grid; grid-template-columns: 150px minmax(0, 1fr) 96px; gap: 12px; align-items: center; }
.bar-row .name { color: var(--ink-2); }
.bar-track { height: 14px; border-radius: 4px; background: var(--s1-wash); overflow: hidden; }
.bar-fill { height: 100%; background: var(--s1); border-radius: 0 4px 4px 0; }
.bar-row .v { text-align: right; font-weight: 600; }
.bar-row .v small { color: var(--muted); font-weight: 500; margin-left: 4px; }
@media (max-width: 520px) { .bar-row { grid-template-columns: 1fr 88px; } .bar-row .bar-track { grid-column: 1 / -1; order: 3; } }
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: 0.02em; }
td.r, th.r { text-align: right; }
.empty { color: var(--muted); padding: 28px 0; text-align: center; }

/* ---------- matrix ---------- */
.matrix th, .matrix td { text-align: right; padding: 6px 8px; }
.matrix th:first-child, .matrix td:first-child { text-align: left; }
.matrix td.cell { min-width: 74px; border-bottom: 2px solid var(--surface); border-right: 2px solid var(--surface); font-variant-numeric: tabular-nums; }
.matrix td.cell.inperiod { box-shadow: inset 0 0 0 1.5px var(--ink); }
.matrix .rowhead small { display: block; color: var(--muted); font-weight: 500; }

/* ---------- live feed ---------- */
.feed-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px; }
.feed-total { font-size: 32px; font-weight: 680; letter-spacing: -0.02em; line-height: 1.1; margin-top: 4px; }
.feed-count { color: var(--ink-2); font-size: 12.5px; }
.feed-list { list-style: none; margin: 14px 0 0; padding: 0; max-height: min(58vh, 560px); overflow-y: auto; }
.feed-item { display: grid; grid-template-columns: 46px minmax(0, 1fr) auto; gap: 10px; align-items: center; padding: 10px 6px; border-top: 1px solid var(--grid); }
.feed-item.new { animation: flash 2.4s ease-out; }
@keyframes flash { from { background: #fff3c4; } to { background: transparent; } }
.feed-time { color: var(--muted); font-size: 12.5px; }
.feed-name { font-weight: 600; overflow-wrap: anywhere; min-width: 0; }
.feed-tag { display: block; color: var(--muted); font-size: 12px; font-weight: 500; }
.feed-amt { font-weight: 680; }
.feed-note { color: var(--muted); font-size: 12px; margin-top: 12px; }

/* ---------- customers ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar input[type="search"], .toolbar select { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 10px; }
.toolbar input[type="search"] { min-width: 220px; flex: 1 1 220px; max-width: 320px; }
.customers th button { border: 0; background: none; font: inherit; color: inherit; padding: 0; letter-spacing: inherit; }
.customers th button:hover { color: var(--ink); }
.customers th[aria-sort] button { color: var(--ink); }
.customers tbody tr { cursor: pointer; }
.customers tbody tr:hover { background: #f5f5f2; }
.customers tbody tr:focus-visible { outline-offset: -2px; }
.uname { font-weight: 600; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 550; border: 1px solid var(--border); color: var(--ink-2); background: #f7f7f4; }
.chip.ret { color: #7a3211; background: #fdf1eb; border-color: #f4d3c3; }
.chip.new { color: var(--s1-deep); background: var(--s1-wash); border-color: #cfe1f7; }
.more { display: flex; justify-content: center; padding-top: 12px; }

/* ---------- drawer & tooltip ---------- */
.scrim { position: fixed; inset: 0; background: rgba(11, 11, 11, 0.32); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(440px, 100%); z-index: 41;
  background: var(--surface); border-left: 1px solid var(--border); padding: 20px; overflow-y: auto;
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.12);
}
.drawer h2 { font-size: 20px; letter-spacing: -0.01em; }
.drawer .close { float: right; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.stat-grid div { background: #f7f7f4; border-radius: 10px; padding: 10px 12px; }
.stat-grid small { display: block; color: var(--muted); font-size: 12px; }
.stat-grid b { font-size: 17px; }
.timeline { list-style: none; margin: 8px 0 0; padding: 0; }
.timeline li { display: flex; justify-content: space-between; padding: 8px 0; border-top: 1px solid var(--grid); }
.tip {
  position: fixed; z-index: 60; pointer-events: none; background: var(--ink); color: #fff; border-radius: 10px;
  padding: 8px 10px; font-size: 12.5px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); max-width: 260px;
}
.tip .t { color: #c9c8c0; margin-bottom: 4px; }
.tip .row { display: flex; align-items: center; gap: 8px; }
.tip .row i { width: 12px; height: 3px; border-radius: 2px; display: block; }
.tip .row b { margin-left: auto; padding-left: 12px; font-size: 13.5px; }
.tip .row span { color: #d8d7cf; }
.reload { opacity: 0.55; transition: opacity 0.15s; }

/* ---------- login & admin ---------- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px; }
.login-card .brand { margin-bottom: 18px; font-size: 18px; }
.login-card h1 { font-size: 22px; letter-spacing: -0.02em; margin-bottom: 4px; }
.login-card p.lead { color: var(--ink-2); margin-bottom: 18px; }
.field { display: grid; gap: 5px; margin-bottom: 14px; }
.field label { font-weight: 600; font-size: 13px; }
.field input, .field select { border: 1px solid #cfcdc4; background: #fff; border-radius: 9px; padding: 10px 12px; width: 100%; }
.field .hint { color: var(--muted); font-size: 12.5px; }
.error { color: var(--bad); background: #fbe9e9; border-radius: 9px; padding: 9px 12px; margin-bottom: 14px; font-size: 13px; }
.okmsg { color: var(--good); background: #e6f4e6; border-radius: 9px; padding: 9px 12px; margin-bottom: 14px; font-size: 13px; }
.login-card .btn.primary { width: 100%; padding: 11px; }

.admin-grid { display: grid; gap: 16px; }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 550; }
.status i { width: 8px; height: 8px; border-radius: 50%; background: #0ca30c; display: block; }
.status.off i { background: var(--axis); }
.status.bad i { background: var(--s2); }
.errtext { color: var(--bad); font-size: 12.5px; white-space: normal; max-width: 320px; }
dialog { border: 1px solid var(--border); border-radius: 16px; padding: 22px; width: min(560px, calc(100vw - 24px)); background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgba(11, 11, 11, 0.4); }
dialog h2 { margin-bottom: 14px; font-size: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-grid .wide { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

@media (prefers-reduced-motion: reduce) {
  .live.on .dot, .feed-item.new { animation: none; }
}
