:root {
  --bg: #f4f0ea;
  --surface: #fffdf9;
  --surface-2: #f8f3ec;
  --ink: #1f1813;
  --ink-2: #5b4e44;
  --muted: #8d7f72;
  --line: #e8dfd4;
  --line-strong: #d8ccbf;
  --accent: #3b2416;
  --accent-ink: #fbf3e8;
  --good: #0ca30c;
  --bad: #d03b3b;
  /* Duration tiers: one hue (coffee), light -> dark = short -> long (validated ordinal ramp) */
  --t1: #d9a35e;
  --t2: #b9762f;
  --t3: #8a4f1f;
  --t4: #5a2f12;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(60 40 20 / .05), 0 4px 16px rgb(60 40 20 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12100e;
    --surface: #1c1815;
    --surface-2: #231e1a;
    --ink: #f3ece4;
    --ink-2: #cdbfb2;
    --muted: #9c8d80;
    --line: #2e2722;
    --line-strong: #3c332c;
    --accent: #e0a458;
    --accent-ink: #1c1209;
    /* Dark surface: short -> long goes dim -> bright (validated ordinal ramp) */
    --t1: #7a4a22;
    --t2: #a8692f;
    --t3: #d08f45;
    --t4: #f0c27f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 "Plus Jakarta Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-feature-settings: "tnum" 1;
}
h1, h2, h3, p { margin: 0; }
h2 { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.meta { color: var(--muted); font-size: 12px; }
.panel-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.hidden { display: none !important; }
kbd { font: 600 11px/1 inherit; background: var(--surface-2); border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 5px; padding: 2px 5px; }

/* ---------------------------------------------------------------- header */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px max(16px, calc((100vw - 1440px) / 2 + 24px));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.mark {
  width: 34px; height: 34px; border-radius: 10px; background: var(--accent); position: relative; flex: none;
}
.mark::before { /* a cup seen from above = a ring */
  content: ""; position: absolute; inset: 8px; border-radius: 50%; border: 3px solid var(--t1);
}
.mark::after { /* clock hand */
  content: ""; position: absolute; left: 16px; top: 12px; width: 2.5px; height: 6px; border-radius: 2px; background: var(--t1);
}
.name { font-weight: 800; font-size: 16px; letter-spacing: -0.02em; line-height: 1.1; }
.shop { color: var(--muted); font-size: 12px; }
.top-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.live { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; padding: 5px 11px; border-radius: 99px; background: var(--surface); border: 1px solid var(--line); }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live b { font-weight: 600; }
.live.ok i { background: var(--good); animation: pulse 2s infinite; }
.live.warn i { background: #fab219; }
.live.bad i { background: var(--bad); }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgb(12 163 12 / .45); } 50% { box-shadow: 0 0 0 5px rgb(12 163 12 / 0); } }
.clock { font-weight: 700; font-size: 15px; padding: 0 6px; }

.btn {
  appearance: none; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  padding: 7px 13px; border-radius: 10px; font: 600 13px/1.2 inherit; cursor: pointer; text-decoration: none;
  transition: border-color .15s, background .15s;
}
.btn:hover { border-color: var(--ink-2); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { border-color: var(--accent); filter: brightness(1.1); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ------------------------------------------------------------------ page */
.page { max-width: 1440px; margin: 0 auto; padding: 20px 24px 40px; display: flex; flex-direction: column; gap: 20px; }
.page > * { min-width: 0; }
@media (max-width: 700px) { .page { padding: 16px 16px 32px; gap: 16px; } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 18px; }

/* --------------------------------------------------------------- summary */
.summary { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 1100px) { .summary { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 640px) { .summary { grid-template-columns: repeat(2, minmax(0, 1fr)); } .stat-hero { grid-column: 1 / -1; } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; box-shadow: var(--shadow); min-width: 0; }
.stat-label { color: var(--ink-2); font-size: 12.5px; font-weight: 500; }
.stat-value { font-size: 28px; font-weight: 800; letter-spacing: -0.03em; line-height: 1.15; white-space: nowrap; }
.stat-value small { font-size: 15px; font-weight: 600; color: var(--ink-2); margin: 0 3px 0 1px; letter-spacing: 0; }
.stat-note { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-hero { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.stat-hero .stat-label, .stat-hero .stat-note, .stat-hero .stat-value small { color: color-mix(in srgb, var(--accent-ink) 75%, transparent); }

/* ------------------------------------------------------------- main grid */
.main-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 420px); gap: 20px; align-items: start; }
.main-grid.editing { grid-template-columns: minmax(0, 1fr); }
.main-grid.editing .camera { order: -1; }
@media (max-width: 1000px) { .main-grid { grid-template-columns: minmax(0, 1fr); } }
.panel-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.panel-head h2 { font-size: 20px; }

.legend { list-style: none; margin: 0; padding: 0; display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 12px; height: 12px; border-radius: 4px; flex: none; display: inline-block; }
.sw.t1, .bar.t1 { background: var(--t1); } .sw.t2, .bar.t2 { background: var(--t2); }
.sw.t3, .bar.t3 { background: var(--t3); } .sw.t4, .bar.t4 { background: var(--t4); }

/* ----------------------------------------------------------- table cards */
.table-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); gap: 12px; }
.empty-note { color: var(--muted); padding: 32px 16px; text-align: center; border: 1.5px dashed var(--line-strong); border-radius: var(--radius); grid-column: 1 / -1; }
.tcard {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); min-width: 0; position: relative; overflow: hidden;
}
.tcard::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--tier, transparent); }
.tcard.off { background: var(--surface-2); box-shadow: none; }
.tc-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tc-head h3 { font-size: 17px; font-weight: 800; letter-spacing: -0.02em; }
.chip { font-size: 11.5px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); white-space: nowrap; }
.chip.on { color: var(--good); border-color: color-mix(in srgb, var(--good) 35%, transparent); background: color-mix(in srgb, var(--good) 9%, var(--surface)); }
.chip.on::before { content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--good); margin-right: 6px; vertical-align: 1px; }

.tc-label { color: var(--muted); font-size: 12px; font-weight: 500; }
.tc-time { font-size: 38px; font-weight: 800; letter-spacing: -0.04em; line-height: 1.05; }
.tc-time small { font-size: 16px; font-weight: 600; color: var(--ink-2); letter-spacing: 0; margin: 0 4px 0 2px; }
.tcard.off .tc-time { font-size: 22px; color: var(--muted); font-weight: 700; }
.tc-sub { color: var(--ink-2); font-size: 12.5px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.tier-name { font-weight: 700; }

.meter { position: relative; height: 8px; border-radius: 99px; background: var(--line); margin: 4px 0 16px; }
.meter .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 99px; background: var(--tier); transition: width .5s ease; }
.meter .tick { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--surface); }
.meter .tick span { position: absolute; top: 15px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--muted); white-space: nowrap; }

.visitors { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.visitors li { display: grid; grid-template-columns: 12px 1fr auto; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); font-size: 13px; }
.visitors .who { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1.25; }
.visitors .who em { display: block; font-style: normal; color: var(--muted); font-size: 11px; }
.visitors .dur { font-weight: 700; }
.visitors li.away { opacity: .6; }
.visitors li.new .sw { background: transparent; border: 1.5px dashed var(--muted); }
.tc-foot { margin-top: auto; padding-top: 10px; border-top: 1px dashed var(--line-strong); font-size: 12px; color: var(--ink-2); display: flex; gap: 4px 12px; flex-wrap: wrap; }
.tc-foot b { color: var(--ink); }

/* ---------------------------------------------------------------- camera */
.camera .card-head { margin-bottom: 10px; align-items: center; }
.stage { position: relative; background: #0d0b0a; border-radius: 12px; overflow: hidden; aspect-ratio: 16 / 9; }
.stage canvas { width: 100%; height: 100%; display: block; object-fit: contain; }
.stage.editing canvas { cursor: crosshair; }
.placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #b9ab9e; text-align: center; padding: 16px; font-size: 13px; }
.cam-foot { margin-top: 8px; }
.editbar { margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--line-strong); }
.steps { margin: 0 0 8px; padding-left: 18px; font-size: 13px; color: var(--ink-2); }
.steps li { margin-bottom: 2px; }
.zone-list { list-style: none; padding: 0; margin: 12px 0; display: flex; flex-wrap: wrap; gap: 6px; }
.zone-list li { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-2); border: 1px solid var(--line); padding: 3px 4px 3px 11px; border-radius: 99px; font-size: 12.5px; font-weight: 600; }
.zone-list button { border: 0; background: transparent; color: var(--muted); cursor: pointer; font: 600 12px inherit; padding: 2px 6px; border-radius: 99px; }
.zone-list button:hover { color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, transparent); }

/* -------------------------------------------------------------- timeline */
.timeline { width: 100%; overflow-x: auto; }
.timeline svg { display: block; }
.timeline .row-label { fill: var(--ink); font-size: 12.5px; font-weight: 700; }
.timeline .row-sub { fill: var(--muted); font-size: 11px; }
.timeline .axis { fill: var(--muted); font-size: 11px; }
.timeline .grid { stroke: var(--line); }
.timeline .lane-bg { fill: var(--surface-2); }
.timeline .now { stroke: var(--bad); stroke-width: 1.5; }
.timeline .now-label { fill: var(--bad); font-size: 11px; font-weight: 700; }
.timeline rect.seg { stroke: var(--surface); stroke-width: 2; paint-order: stroke; cursor: default; }
.timeline rect.seg.t1 { fill: var(--t1); } .timeline rect.seg.t2 { fill: var(--t2); }
.timeline rect.seg.t3 { fill: var(--t3); } .timeline rect.seg.t4 { fill: var(--t4); }
.timeline rect.seg.live { stroke: var(--good); stroke-width: 1.5; stroke-dasharray: 3 2; }
.timeline rect.seg:hover { filter: brightness(1.12); }
.timeline .empty { fill: var(--muted); font-size: 13px; }

/* ---------------------------------------------------------------- bottom */
.bottom-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; }
@media (max-width: 1000px) { .bottom-grid { grid-template-columns: minmax(0, 1fr); } }
.dist { display: flex; flex-direction: column; gap: 10px; }
.dist-row { display: grid; grid-template-columns: 150px 1fr 70px; align-items: center; gap: 10px; font-size: 13px; }
.dist-row .lbl b { display: block; font-weight: 700; }
.dist-row .lbl span { color: var(--muted); font-size: 11.5px; }
.dist-row .track { height: 22px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.dist-row .bar { height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; transition: width .5s ease; }
.dist-row .val { text-align: right; font-weight: 700; }
.dist-row .val span { color: var(--muted); font-weight: 500; font-size: 11.5px; display: block; }
@media (max-width: 520px) { .dist-row { grid-template-columns: 110px 1fr 56px; } }
.sub-title { font-size: 13px; font-weight: 700; margin: 22px 0 6px; }

.scroll-x { overflow-x: auto; }
.tbl-data { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl-data th { text-align: left; color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; padding: 8px; border-bottom: 1px solid var(--line-strong); }
.tbl-data td { padding: 9px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl-data tr:last-child td { border-bottom: 0; }
.tbl-data .num { text-align: right; }
.tbl-data td b { font-weight: 700; }
.tbl-data .sw { margin-right: 6px; vertical-align: -1px; }
.tbl-data .muted { color: var(--muted); text-align: center; padding: 20px; }

/* --------------------------------------------------------------- tooltip */
.tip { position: fixed; z-index: 50; pointer-events: none; background: var(--ink); color: var(--bg); padding: 8px 11px; border-radius: 9px; font-size: 12.5px; line-height: 1.45; box-shadow: 0 6px 20px rgb(0 0 0 / .2); max-width: 260px; }
.tip b { font-weight: 700; }

/* ---------------------------------------------------------------- dialog */
.dlg { border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--ink); width: min(580px, calc(100vw - 32px)); padding: 22px; max-height: calc(100vh - 32px); }
.dlg::backdrop { background: rgb(20 12 6 / .5); backdrop-filter: blur(2px); }
.dlg h2 { font-size: 18px; margin-bottom: 16px; }
.dlg label { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; font-size: 13px; font-weight: 600; }
.dlg small { color: var(--muted); font-weight: 400; font-size: 11.5px; }
.dlg input, .dlg select { font: 500 14px inherit; padding: 9px 11px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--bg); color: var(--ink); min-width: 0; }
.dlg input:focus, .dlg select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.dlg fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px 2px; margin: 0 0 14px; }
.dlg legend { font-size: 12px; font-weight: 700; color: var(--ink-2); padding: 0 6px; }
.dlg details { margin-top: 4px; }
.dlg summary { cursor: pointer; font-weight: 600; font-size: 13px; color: var(--ink-2); margin-bottom: 12px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.three { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 12px; }
@media (max-width: 520px) { .two, .three { grid-template-columns: 1fr; } }
