:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f2f4;
  --border: #e0e2e6;
  --border-strong: #c9ccd2;
  --text: #1e1f21;
  --text-2: #4b4c50;
  --muted: #6d6e72;
  --accent: #4573d2;
  --accent-weak: rgba(69, 115, 210, .12);
  --danger: #d1395a;
  --warn: #9a6a00;
  --grid: #e6e7ea;
  --axis: #c9ccd2;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 1px 3px rgba(0,0,0,.07);
  --shadow-lg: 0 8px 28px rgba(0,0,0,.16), 0 2px 6px rgba(0,0,0,.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1d1f; --surface: #26272a; --surface-2: #2f3033;
    --border: #3a3b3f; --border-strong: #4c4d52; --text: #f1f0ef; --text-2: #c9c8ca; --muted: #a2a1a4;
    --accent: #6f95ea; --accent-weak: rgba(111,149,234,.16); --danger: #ee6f8a; --warn: #e0b04a;
    --grid: #333437; --axis: #4c4d52;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
    --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 10px 30px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1d1f; --surface: #26272a; --surface-2: #2f3033;
  --border: #3a3b3f; --border-strong: #4c4d52; --text: #f1f0ef; --text-2: #c9c8ca; --muted: #a2a1a4;
  --accent: #6f95ea; --accent-weak: rgba(111,149,234,.16); --danger: #ee6f8a; --warn: #e0b04a;
  --grid: #333437; --axis: #4c4d52;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
  --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 10px 30px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

.wrap, #top, #filters > .inner, #main { max-width: 1240px; margin: 0 auto; padding-left: 20px; padding-right: 20px; }

/* ---------- Top bar ---------- */
#top { display: flex; align-items: center; gap: 12px; height: 54px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 15px; }
.brand img { width: 22px; height: 22px; display: block; }
.asof { color: var(--muted); font-size: 12.5px; margin-left: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.demo-badge { font-size: 11.5px; font-weight: 600; color: var(--warn); border: 1px solid currentColor; border-radius: 999px; padding: 1px 8px; white-space: nowrap; }
.spacer { flex: 1; }
.userbtn { display: flex; align-items: center; gap: 8px; padding: 5px 8px 5px 5px; border-radius: 999px; max-width: 46vw; }
.userbtn:hover, .userbtn[aria-expanded="true"] { background: var(--surface-2); }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 12px; flex: none; }
.userbtn .email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; color: var(--text-2); }

/* ---------- Filters ---------- */
#filters { position: sticky; top: 0; z-index: 20; background: var(--bg); border-bottom: 1px solid transparent; }
#filters.stuck { border-bottom-color: var(--border); }
#filters > .inner { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; padding-top: 8px; padding-bottom: 10px; }
.fgroup { display: flex; align-items: center; gap: 6px; min-width: 0; }
.flabel { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-right: 2px; white-space: nowrap; }
.chips { display: flex; gap: 4px; flex-wrap: wrap; }
.chip {
  padding: 5px 11px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  white-space: nowrap; font-size: 13px; line-height: 1.3; color: var(--text-2);
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.dates { display: flex; align-items: center; gap: 6px; }
.dates input { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 4px 8px; font-size: 13px; }
.picker { display: flex; align-items: center; gap: 6px; padding: 5px 10px 5px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; max-width: 320px; }
.picker:hover, .picker[aria-expanded="true"] { border-color: var(--border-strong); }
.picker.on { border-color: var(--accent); background: var(--accent-weak); }
.picker .val { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.picker svg { flex: none; opacity: .6; }

/* ---------- Main ---------- */
#main { padding-top: 8px; padding-bottom: 48px; display: grid; gap: 14px; transition: opacity .15s; }
#main.loading { opacity: .55; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); min-width: 0; }

#tiles { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
.tile {
  display: block; text-align: left; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 13px 15px 14px; box-shadow: var(--shadow); min-width: 0;
}
button.tile:hover { border-color: var(--border-strong); }
.tile .lab { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.tile .key { width: 14px; height: 0; border-top: 3px solid var(--c); border-radius: 2px; flex: none; }
.tile[aria-pressed="false"] .key { border-top-style: dotted; opacity: .55; }
.tile[aria-pressed="false"] .val { color: var(--text-2); }
.tile .val { font-size: 25px; font-weight: 650; line-height: 1.2; margin-top: 5px; letter-spacing: -.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .val.multi { font-size: 15px; line-height: 1.4; white-space: normal; letter-spacing: 0; }
.tile .val.multi .muted { font-size: 12px; font-weight: 400; }
.tile .sub { font-size: 12px; color: var(--muted); margin-top: 1px; min-height: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Chart ---------- */
#chart { padding: 14px 16px 10px; position: relative; }
#chart:focus-visible { outline-offset: -2px; border-radius: 12px; }
.panel + .panel { margin-top: 10px; border-top: 1px solid var(--grid); padding-top: 12px; }
.panel-head { display: flex; align-items: center; gap: 14px; font-size: 12.5px; color: var(--text-2); margin-bottom: 4px; flex-wrap: wrap; }
.panel-head .ttl { font-weight: 600; color: var(--text); }
.legend { display: flex; gap: 14px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 0; border-top: 2px solid var(--c); display: inline-block; }
.chart-note { font-size: 12px; color: var(--muted); margin-left: auto; }
#chart svg { display: block; width: 100%; overflow: visible; }
#chart svg text { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.empty { padding: 44px 20px; text-align: center; color: var(--muted); }
.empty b { display: block; color: var(--text); font-size: 15px; margin-bottom: 4px; }

#tip {
  position: fixed; z-index: 60; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow-lg); padding: 8px 11px; font-size: 12.5px; min-width: 150px;
}
#tip .d { color: var(--muted); margin-bottom: 4px; }
#tip .r { display: flex; align-items: center; gap: 8px; }
#tip .r i { width: 12px; height: 0; border-top: 2px solid var(--c); flex: none; }
#tip .r b { font-weight: 650; font-variant-numeric: tabular-nums; }
#tip .r span { color: var(--text-2); }

/* ---------- Table ---------- */
.tbar { display: flex; align-items: center; gap: 10px 14px; padding: 8px 12px 0 8px; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 2px; align-self: flex-end; }
.tab { padding: 9px 12px 10px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab:hover { color: var(--text); }
.tab.on { color: var(--text); border-bottom-color: var(--accent); }
.tab .count { font-weight: 500; color: var(--muted); margin-left: 5px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; }
.seg button { padding: 4px 10px; border-radius: 6px; font-size: 13px; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.tbar .tools { margin-left: auto; display: flex; align-items: center; gap: 8px; padding-bottom: 7px; flex-wrap: wrap; }
.search { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 5px 10px; font-size: 13px; width: 200px; max-width: 46vw; }
.btn { padding: 6px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; white-space: nowrap; }
.btn:hover { border-color: var(--border-strong); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 4px 9px; font-size: 12.5px; }

.tscroll { overflow-x: auto; border-radius: 0 0 12px 12px; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { padding: 8px 14px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
thead th { font-size: 12px; font-weight: 600; color: var(--muted); user-select: none; background: var(--surface); }
thead th button { font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }
thead th button:hover { color: var(--text); }
thead th .arr { width: 8px; display: inline-block; opacity: 0; }
thead th.sorted { color: var(--text); }
thead th.sorted .arr { opacity: 1; }
td { font-variant-numeric: tabular-nums; }
tr.total td { font-weight: 650; background: var(--surface-2); }
tr.total td:first-child { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:not(.total):hover td { background: var(--accent-weak); }
td.left, th.left { text-align: left; }
.prod { max-width: 460px; white-space: normal; line-height: 1.35; }
.prod .asin { font-size: 12px; color: var(--muted); }
.more { padding: 10px; text-align: center; }

/* ---------- Popover ---------- */
#popover { position: fixed; z-index: 50; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg); min-width: 240px; max-width: calc(100vw - 16px); overflow: hidden; }
.pop-search { padding: 8px; border-bottom: 1px solid var(--border); }
.pop-search input { width: 100%; border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 6px 10px; }
.pop-list { max-height: 320px; overflow-y: auto; padding: 4px; }
.opt { display: flex; align-items: center; gap: 9px; width: 100%; text-align: left; padding: 7px 9px; border-radius: 8px; cursor: pointer; }
.opt:hover { background: var(--surface-2); }
.opt input { margin: 0; accent-color: var(--accent); width: 15px; height: 15px; }
.opt .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opt .c { color: var(--muted); font-size: 12px; }
.pop-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; border-top: 1px solid var(--border); font-size: 12.5px; color: var(--muted); }
.menu { padding: 5px; min-width: 220px; }
.menu .who { padding: 8px 10px 9px; border-bottom: 1px solid var(--border); margin-bottom: 5px; font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.menu button { display: flex; width: 100%; justify-content: space-between; gap: 12px; text-align: left; padding: 8px 10px; border-radius: 8px; }
.menu button:hover { background: var(--surface-2); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 5px 0; }

/* ---------- Modal ---------- */
.modal-back { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.42); display: grid; place-items: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); width: min(860px, 100%); max-height: min(720px, 100%); display: flex; flex-direction: column; overflow: hidden; }
.modal.narrow { width: min(620px, 100%); }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 650; flex: 1; }
.x { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; color: var(--muted); font-size: 20px; line-height: 1; }
.x:hover { background: var(--surface-2); color: var(--text); }
.modal-body { overflow-y: auto; padding: 16px 18px; flex: 1; min-height: 0; }
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 12px; color: var(--muted); }
.inp { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 10px; width: 100%; min-width: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.hint { font-size: 12.5px; color: var(--muted); }
.err { color: var(--danger); font-size: 13px; }

.access { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 0; min-height: 380px; }
.people { border-right: 1px solid var(--border); padding-right: 12px; margin-right: 16px; }
.person { display: block; width: 100%; text-align: left; padding: 8px 10px; border-radius: 8px; }
.person:hover { background: var(--surface-2); }
.person.on { background: var(--accent-weak); }
.person .e { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; font-size: 13px; }
.person .s { font-size: 12px; color: var(--muted); }
.checks { border: 1px solid var(--border); border-radius: 10px; max-height: 210px; overflow-y: auto; padding: 4px; }
.radio { display: flex; align-items: center; gap: 8px; padding: 4px 0; cursor: pointer; }
.radio input { accent-color: var(--accent); margin: 0; }

.drop { border: 1.5px dashed var(--border-strong); border-radius: 12px; padding: 26px 16px; text-align: center; color: var(--muted); cursor: pointer; display: block; }
.drop.over { border-color: var(--accent); background: var(--accent-weak); color: var(--text); }
.drop b { color: var(--text); display: block; font-size: 14.5px; margin-bottom: 3px; }
.file { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-top: 12px; }
.file h3 { margin: 0 0 2px; font-size: 14px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tag { font-size: 11.5px; font-weight: 600; border-radius: 999px; padding: 1px 8px; background: var(--surface-2); color: var(--text-2); }
.tag.ok { background: var(--accent-weak); color: var(--accent); }
.tag.bad { color: var(--danger); }
.file .cols { font-size: 12.5px; color: var(--text-2); margin: 6px 0 10px; }
.file .cols s { color: var(--muted); }
.file .row .inp { width: auto; flex: 1 1 130px; }
details.del { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 12px; }
details.del summary { cursor: pointer; color: var(--text-2); font-size: 13px; }

/* ---------- Auth ---------- */
#auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.authbox { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 34px 30px 30px; width: min(380px, 100%); text-align: center; }
.authbox img { width: 40px; height: 40px; }
.authbox h1 { font-size: 20px; margin: 12px 0 6px; }
.authbox p { color: var(--muted); margin: 0 0 22px; }
.gbtn { display: inline-flex; align-items: center; justify-content: center; gap: 10px; width: 100%; padding: 11px 16px; border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface); font-weight: 600; font-size: 14.5px; }
.gbtn:hover { background: var(--surface-2); }
.authbox .err { margin-top: 14px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 70; background: #1e1f21; color: #fff; padding: 9px 16px; border-radius: 10px; box-shadow: var(--shadow-lg); font-size: 13.5px; max-width: calc(100vw - 32px); }
#toast.bad { background: #b3243f; }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  #tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .wrap, #top, #filters > .inner, #main { padding-left: 16px; padding-right: 16px; }
  #tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile .val { font-size: 21px; }
  .asof { display: none; }
  #filters { position: static; }
  .prod { min-width: 150px; }
  .fgroup { width: 100%; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin-right: -16px; padding-right: 16px; }
  .chips::-webkit-scrollbar { display: none; }
  .userbtn .email { display: none; }
  .userbtn { padding: 4px; }
  .access { grid-template-columns: 1fr; }
  .people { border-right: 0; border-bottom: 1px solid var(--border); padding: 0 0 10px; margin: 0 0 14px; }
  th, td { padding: 8px 10px; }
  .tbar .tools { margin-left: 0; width: 100%; }
}
