:root {
  /* Light theme is sand and cream, not white: easier on the eyes over a working day. */
  --bg: #efe7d9;
  --surface: #faf5ec;
  --surface-2: #f3ecdf;
  --surface-3: #e9e0d0;
  --ink: #231e17;
  --ink-2: #5a5146;
  --ink-3: #8a7f70;
  --line: #e4d9c6;
  --line-2: #d6c9b3;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: #e9e5f6;
  --accent-text: #4338ca;
  --good: #11713f;
  --good-soft: #e0eedb;
  --mid: #9a5706;
  --mid-soft: #f6e6c8;
  --bad: #b8291e;
  --bad-soft: #f6ddd5;
  --money: #11713f;
  --bar: #6d63f0;
  --bar-soft: #c9c5fb;
  --shadow: 0 1px 2px rgba(60, 44, 20, .05), 0 1px 3px rgba(60, 44, 20, .07);
  --shadow-lg: 0 12px 32px rgba(60, 44, 20, .16);
  --radius: 14px;
  --radius-sm: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0d13; --surface: #141823; --surface-2: #1a1f2c; --surface-3: #222838;
    --ink: #eef1f6; --ink-2: #b2bacb; --ink-3: #7c869b; --line: #252b3a; --line-2: #313849;
    --accent: #7c74ff; --accent-ink: #0b0d13; --accent-soft: #24214d; --accent-text: #aaa5ff;
    --good: #4fd08f; --good-soft: #10301f; --mid: #f2b04b; --mid-soft: #35270d;
    --bad: #ff7b6e; --bad-soft: #3a1714; --money: #4fd08f; --bar: #8c85ff; --bar-soft: #3a3670;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); --shadow-lg: 0 16px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0d13; --surface: #141823; --surface-2: #1a1f2c; --surface-3: #222838;
  --ink: #eef1f6; --ink-2: #b2bacb; --ink-3: #7c869b; --line: #252b3a; --line-2: #313849;
  --accent: #7c74ff; --accent-ink: #0b0d13; --accent-soft: #24214d; --accent-text: #aaa5ff;
  --good: #4fd08f; --good-soft: #10301f; --mid: #f2b04b; --mid-soft: #35270d;
  --bad: #ff7b6e; --bad-soft: #3a1714; --money: #4fd08f; --bar: #8c85ff; --bar-soft: #3a3670;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3); --shadow-lg: 0 16px 40px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 "Onest", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); }
.hidden { display: none !important; }

/* -- boot / login --------------------------------------------------------- */
.boot { min-height: 100vh; display: grid; place-items: center; }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--line-2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 32px 28px 28px; box-shadow: var(--shadow-lg);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 18px; letter-spacing: -.01em; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; background: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 22px; height: 22px; }
.login-card h1 { font-size: 22px; margin: 26px 0 4px; letter-spacing: -.02em; }
.login-card p.sub { color: var(--ink-2); margin: 0 0 22px; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
.input, select.input, textarea.input {
  width: 100%; height: 42px; padding: 0 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-2); background: var(--surface); outline: none;
  font-size: 16px; transition: border-color .15s, box-shadow .15s;
}
textarea.input { height: auto; min-height: 90px; padding: 10px 12px; font-size: 14px; resize: vertical; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.form-error { color: var(--bad); font-size: 13px; min-height: 18px; margin: 2px 0 10px; }

/* -- buttons ---------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 14px; border-radius: var(--radius-sm); border: 1px solid var(--line-2);
  background: var(--surface); cursor: pointer; font-weight: 600; font-size: 13.5px; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; height: 44px; }
.btn.sm { height: 32px; padding: 0 10px; font-size: 13px; }
.icon-btn { width: 38px; padding: 0; }
.btn svg, .icon { width: 16px; height: 16px; flex: none; }

/* -- layout ----------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar-in {
  padding: 10px 24px; display: flex; align-items: center; gap: 12px;
  min-height: 62px;
}
.store-btn {
  display: flex; align-items: center; gap: 11px; padding: 5px 10px 5px 5px; margin-left: -5px;
  border-radius: 12px; border: 0; background: transparent; cursor: default; text-align: left; min-width: 0;
}
.store-btn.switchable { cursor: pointer; }
.store-btn.switchable:hover { background: var(--surface-2); }
.store-avatar {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; font-size: 20px; flex: none;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .12);
}
.store-name { font-weight: 700; font-size: 15.5px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.store-sub { font-size: 12px; color: var(--ink-3); white-space: nowrap; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; height: 24px; padding: 0 9px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent-text); font-size: 12px; font-weight: 600; white-space: nowrap;
}
.chip.neutral { background: var(--surface-2); color: var(--ink-2); }
.spacer { flex: 1; }
.sync-pill {
  display: inline-flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-size: 13px; color: var(--ink-2); cursor: help;
  white-space: nowrap;
}
.sync-pill b { color: var(--ink); font-weight: 600; }
.sync-pill .short { display: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: none; }
.dot.stale { background: var(--mid); }
.dot.err { background: var(--bad); }
.dot.live { animation: pulse 1.4s ease-in-out infinite; background: var(--accent); }
@keyframes pulse { 50% { opacity: .35; } }

.page { padding: 22px 24px 60px; }
.section { margin-top: 18px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}

/* -- menu / popover --------------------------------------------------------- */
.menu {
  position: absolute; z-index: 50; min-width: 240px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow-lg); padding: 6px;
}
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border: 0; background: transparent;
  border-radius: 9px; cursor: pointer; text-align: left; font-size: 14px;
}
.menu-item:hover, .menu-item.active { background: var(--surface-2); }
.menu-item .store-avatar { width: 28px; height: 28px; font-size: 15px; border-radius: 8px; }
.menu-sep { height: 1px; background: var(--line); margin: 6px 4px; }
.menu-label { font-size: 11.5px; color: var(--ink-3); padding: 6px 10px 4px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }

/* -- period bar --------------------------------------------------------------- */
.period { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; }
.period-nav { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.period-nav .btn { height: 32px; width: 32px; padding: 0; border: 0; }
.period-label { font-weight: 700; padding: 0 10px; min-width: 120px; text-align: center; color: var(--accent-text); white-space: nowrap; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.seg button {
  height: 32px; padding: 0 12px; border-radius: 9px; border: 0; background: transparent; cursor: pointer;
  font-weight: 600; font-size: 13px; color: var(--ink-2); white-space: nowrap;
}
.seg button:hover { color: var(--ink); background: var(--surface-2); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.range { display: flex; align-items: center; gap: 6px; }
.range .input { height: 38px; width: 150px; font-size: 14px; }
.note { color: var(--ink-3); font-size: 12.5px; }
.banner {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-radius: 12px;
  background: var(--mid-soft); color: var(--mid); font-size: 13px; margin-top: 14px;
}
.banner.err { background: var(--bad-soft); color: var(--bad); }

/* -- KPI cards ---------------------------------------------------------------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(178px, 1fr)); gap: 12px; }
.kpi { padding: 14px 16px 14px; position: relative; min-height: 108px; display: flex; flex-direction: column; min-width: 0; }
.kpi-label { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
.kpi-value { font-size: 26px; font-weight: 800; letter-spacing: -.025em; margin-top: 6px; line-height: 1.15; white-space: nowrap; }
.kpi-value.money { color: var(--money); }
.kpi-value.good { color: var(--good); } .kpi-value.mid { color: var(--mid); } .kpi-value.bad { color: var(--bad); }
.kpi-value.muted { color: var(--ink-3); }
.kpi-sub { font-size: 12.5px; color: var(--ink-3); margin-top: auto; padding-top: 6px; }
.kpi .axis { display: inline-flex; gap: 3px; align-self: flex-start; margin-top: 8px; background: var(--surface-2); border-radius: 8px; padding: 2px; }
.kpi .axis button { border: 0; background: transparent; font-size: 11px; font-weight: 600; padding: 3px 7px; border-radius: 6px; cursor: pointer; color: var(--ink-3); }
.kpi .axis button.on { background: var(--surface); color: var(--accent-text); box-shadow: var(--shadow); }
.ripen { font-size: 13px; cursor: help; }
.tip {
  display: inline-grid; place-items: center; width: 16px; height: 16px; border-radius: 50%;
  background: var(--surface-3); color: var(--ink-3); font-size: 10.5px; font-weight: 700; cursor: help; flex: none;
}
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 8px; color: transparent !important; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* -- charts ---------------------------------------------------------------------- */
.panel-head {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px; cursor: pointer; border: 0; width: 100%;
  background: transparent; text-align: left; border-radius: var(--radius);
}
.panel-head h3 { margin: 0; font-size: 15px; letter-spacing: -.01em; }
.panel-head .note { margin-left: 4px; }
.chev { transition: transform .2s; color: var(--ink-3); }
.open .chev { transform: rotate(90deg); }
.chart-body { padding: 0 16px 16px; }
.chart-metrics { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.pill {
  height: 30px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  font-size: 12.5px; font-weight: 600; cursor: pointer; color: var(--ink-2);
}
.pill.on { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }
.chart-wrap { position: relative; }
.chart-svg { width: 100%; height: 240px; display: block; overflow: visible; }
.chart-svg .bar { fill: var(--bar); transition: opacity .15s; }
.chart-svg .bar.dim { opacity: .35; }
.chart-svg .grid { stroke: var(--line); stroke-width: 1; }
.chart-svg .avg { stroke: var(--ink-3); stroke-dasharray: 4 4; stroke-width: 1.2; }
.chart-svg text { fill: var(--ink-3); font-size: 11px; }
.chart-info { display: flex; flex-wrap: wrap; gap: 6px 18px; align-items: baseline; margin-top: 10px; font-size: 13px; color: var(--ink-2); min-height: 22px; }
.chart-info b { color: var(--ink); }

/* -- tabs & tables ---------------------------------------------------------------- */
.tabs { display: inline-flex; gap: 4px; padding: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.tabs button { height: 34px; padding: 0 16px; border: 0; border-radius: 9px; background: transparent; cursor: pointer; font-weight: 600; color: var(--ink-2); }
.tabs button.on { background: var(--surface-2); color: var(--ink); box-shadow: inset 0 0 0 1px var(--line); }
.table-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 16px 0 10px; }
.table-head h3 { margin: 0 6px 0 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.stat-chip { height: 28px; padding: 0 10px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 12.5px; color: var(--ink-3); display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.stat-chip b { color: var(--ink); }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius); }
table.data { width: 100%; border-collapse: separate; border-spacing: 0; min-width: 880px; }
table.data th, table.data td { padding: 10px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
table.data th:first-child, table.data td:first-child { text-align: left; position: sticky; left: 0; z-index: 1; }
table.data tbody td:first-child { background: var(--surface); }
table.data tbody tr.click:hover td:first-child { background: var(--surface-2); }
table.data thead th:first-child, table.data tfoot td:first-child { background: var(--surface-2); }
table.data thead th {
  font-size: 11.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3);
  background: var(--surface-2); cursor: pointer; user-select: none; position: sticky; top: 0;
}
table.data thead th:first-child { z-index: 2; }
table.data thead th.sorted { color: var(--accent-text); }
table.data tbody tr { background: var(--surface); }
table.data tbody tr.click { cursor: pointer; }
table.data tbody tr.click:hover { background: var(--surface-2); }
table.data tfoot td { background: var(--surface-2); font-weight: 700; border-bottom: 0; }
table.data td.name { max-width: 460px; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.pid { color: var(--ink-3); font-size: 12px; font-weight: 600; margin-left: 4px; }
.pm { font-size: 10.5px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--surface-3); color: var(--ink-2); margin-left: 6px; text-transform: uppercase; }
.rate { display: inline-block; padding: 2px 7px; border-radius: 7px; font-weight: 700; font-size: 12.5px; }
.rate.good { background: var(--good-soft); color: var(--good); }
.rate.mid { background: var(--mid-soft); color: var(--mid); }
.rate.bad { background: var(--bad-soft); color: var(--bad); }
.rate.none { color: var(--ink-3); font-weight: 500; }
.m { color: var(--money); font-weight: 700; }
.a { color: var(--accent-text); font-weight: 700; }
.dim { color: var(--ink-3); }
.empty { padding: 40px 16px; text-align: center; color: var(--ink-3); }
.empty b { display: block; color: var(--ink-2); font-size: 15px; margin-bottom: 4px; }
.footnotes { margin-top: 12px; display: grid; gap: 4px; }
.search { position: relative; }
.search .input { height: 36px; padding-left: 34px; font-size: 14px; width: 300px; max-width: 100%; }
.search svg { position: absolute; left: 11px; top: 10px; color: var(--ink-3); }

.office-head { display: flex; align-items: center; gap: 12px; padding: 12px 14px; margin-bottom: 12px; }
.office-head h2 { margin: 0; font-size: 18px; letter-spacing: -.015em; }

/* -- admin -------------------------------------------------------------------------- */
.admin-grid { display: grid; gap: 12px; }
.store-card { padding: 16px; display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: start; }
.store-card h3 { margin: 0; font-size: 16px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--ink-2); font-size: 13px; margin-top: 6px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.err-text { color: var(--bad); font-size: 13px; margin-top: 6px; }
.ok-text { color: var(--good); font-size: 13px; margin-top: 6px; }

.modal-back {
  position: fixed; inset: 0; background: rgba(9, 12, 20, .5); z-index: 100; display: grid; place-items: center;
  padding: 16px; overflow-y: auto;
}
.modal {
  width: 100%; max-width: 640px; background: var(--surface); border-radius: 18px; box-shadow: var(--shadow-lg);
  border: 1px solid var(--line); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
}
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 17px; flex: 1; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 14px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.radio-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.radio-card { flex: 1; min-width: 180px; border: 1px solid var(--line-2); border-radius: 12px; padding: 10px 12px; cursor: pointer; }
.radio-card.on { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.radio-card b { display: block; font-size: 14px; }
.radio-card small { color: var(--ink-3); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.access-row { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.access-row .offices { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.result { padding: 10px 12px; border-radius: 10px; font-size: 13px; margin-top: 4px; }
.result.ok { background: var(--good-soft); color: var(--good); }
.result.bad { background: var(--bad-soft); color: var(--bad); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: 13.5px;
  transition: opacity .2s, transform .2s; z-index: 200; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* -- theme switch: the new theme spreads from the icon as a circle (clip-path driven from app.js) -- */
::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; }

/* -- small screens ------------------------------------------------------------------- */
@media (max-width: 760px) {
  .topbar-in { padding: 8px 16px; gap: 8px; }
  .page { padding: 16px 16px 48px; }
  .store-sub, .sync-pill .when, .sync-pill .long, .hide-sm { display: none; }
  .sync-pill .short { display: inline; }
  .sync-pill { padding: 0 10px; }
  .store-name { font-size: 15px; }
  .kpis { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
  .kpi { padding: 12px; min-height: 96px; }
  .kpi-value { font-size: 21px; }
  .range .input { width: 100%; }
  .range { width: 100%; }
  .grid2 { grid-template-columns: 1fr; }
  .store-card { grid-template-columns: auto 1fr; }
  .store-card .actions { grid-column: 1 / -1; justify-content: flex-start; }
  .search .input { width: 100%; }
  .search { flex: 1; min-width: 0; }
}
