:root {
  --bg: #0B0E12;
  --card: #12161C;
  --ink: #E8E6E1;
  --muted: #8A9099;
  --up: #E0A34A;
  --down: #C4553F;
  --line: #1F252D;
  --line-soft: #181D24;
  --track: #1A1F27;
  --raised: #1B2028;
  --radius: 12px;
  --s1: 8px;
  --s2: 16px;
  --s3: 24px;
  --s4: 32px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Noto Serif SC", "Songti SC", "STSong", SimSun, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, "Roboto Mono", "JetBrains Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; width: 100%; max-width: 100%; margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
img, svg, canvas { max-width: 100%; }
button { font: inherit; color: inherit; }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- shell ---------- */
.app {
  height: 100dvh;
  width: 100%;
  display: flex;
  flex-direction: column;
  touch-action: manipulation;
  overflow: hidden;
}
.topbar {
  flex: none;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--s1) var(--s2);
  padding: calc(env(safe-area-inset-top) + var(--s2)) var(--s2) var(--s2);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.eyebrow {
  margin: 0 0 4px;
  font-size: 11px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--muted);
}
.title {
  margin: 0;
  font-family: var(--serif);
  font-weight: 500;
  font-size: 22px;
  line-height: 28px;
  letter-spacing: .01em;
}
.title em { font-style: normal; color: var(--up); }
.content {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--s2) var(--s2) calc(env(safe-area-inset-bottom) + var(--s3));
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.content::-webkit-scrollbar { display: none; }

/* segmented control */
.seg {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
}
.seg button {
  height: 40px;
  min-width: 56px;
  padding: 0 14px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  transition: background-color .2s ease, color .2s ease;
}
.seg button.is-active { background: var(--raised); color: var(--ink); }
.seg button:focus-visible { outline: 1px solid var(--up); outline-offset: 2px; }

/* ---------- grids ---------- */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s1); }
.grid-main, .grid-bottom { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s1); margin-top: var(--s1); }
@media (min-width: 900px) {
  .topbar { padding: var(--s3) var(--s3) var(--s2); }
  .title { font-size: 28px; line-height: 32px; }
  .content { padding: var(--s3) var(--s3) var(--s4); }
  .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--s2); }
  .grid-main, .grid-bottom { gap: var(--s2); margin-top: var(--s2); }
  .grid-main { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
  .grid-bottom { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
}

/* ---------- cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s1);
  padding: var(--s2) var(--s2) 12px;
  border-bottom: 1px solid var(--line);
}
.card-title { margin: 0; font-size: 15px; font-weight: 600; }
.card-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.readout { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); white-space: nowrap; }
.readout b { font-weight: 500; color: var(--ink); }
.card-body { position: relative; flex: 1; min-height: 0; padding: var(--s1) var(--s2) var(--s2); }
.chart { display: block; width: 100%; height: 100%; overflow: visible; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.chart:focus { outline: none; }
.chart:focus-visible { outline: 1px solid var(--up); outline-offset: 4px; border-radius: 4px; }
.card-line .card-body { height: 264px; }
.card-share .card-body { height: 304px; }
.card-heat .card-body { display: flex; flex-direction: column; gap: var(--s1); height: 328px; }
.card-heat .chart { flex: 1; min-height: 0; }
@media (min-width: 900px) {
  .card-line .card-body { height: 312px; }
  .card-share .card-body { height: 312px; }
  .card-heat .card-body { height: auto; min-height: 328px; }
}

/* ---------- KPI ---------- */
.kpi {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s2);
  display: flex;
  flex-direction: column;
  gap: var(--s1);
  min-width: 0;
}
.kpi-label { margin: 0; font-size: 12px; color: var(--muted); }
.kpi-main { display: flex; align-items: flex-end; min-width: 0; }
.kpi-foot { display: flex; align-items: center; justify-content: space-between; gap: var(--s1); }
.kpi-value { margin: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 26px; line-height: 32px; font-weight: 500; white-space: nowrap; }
.kpi-unit { font-family: var(--sans); font-size: 12px; color: var(--muted); margin-left: 4px; }
.spark { flex: none; width: 60px; height: 20px; display: block; overflow: visible; }
.kpi-delta { margin: 0; display: flex; align-items: center; gap: 4px; font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 12px; white-space: nowrap; min-width: 0; }
@media (max-width: 479px) { .kpi-delta .yoy-label { display: none; } }
.kpi-delta .arrow { width: 12px; height: 12px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.kpi-delta .yoy-label { font-family: var(--sans); color: var(--muted); margin-left: 2px; }
.up { color: var(--up); }
.down { color: var(--down); }
@media (min-width: 900px) { .kpi-value { font-size: 30px; line-height: 36px; } }

/* ---------- SVG chart pieces ---------- */
.grid-line { stroke: var(--line-soft); stroke-width: 1; shape-rendering: crispEdges; }
.axis-text { fill: var(--muted); font-size: 11px; }
.line-area { fill: url(#lineGrad); }
.line-path { fill: none; stroke: var(--up); stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.line-dot { fill: var(--up); }
.crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; shape-rendering: crispEdges; }
.hover-dot { fill: var(--card); stroke: var(--ink); stroke-width: 1.5; }
.bubble-box { fill: var(--ink); }
.bubble-text { fill: var(--bg); font-size: 11px; font-weight: 600; }
.overlay { fill: transparent; }

.bar-row { cursor: default; transition: opacity .2s ease; }
.bar-row:focus { outline: none; }
.bar-label { fill: var(--muted); font-family: var(--sans); font-size: 12px; }
.bar-track { fill: var(--track); }
.bar-fill { fill: var(--up); transition: opacity .2s ease; }
.bar-val-in { fill: var(--bg); font-size: 11px; font-weight: 600; }
.bar-val-out { fill: var(--ink); font-size: 11px; }
.bar-row:focus-visible .bar-track { stroke: var(--ink); stroke-width: 1; }
.chart.has-hot .bar-row:not(.is-hot) .bar-fill { opacity: .45; }
.chart.has-hot .bar-row:not(.is-hot) .bar-label { opacity: .6; }
.chart.has-hot .bar-row:not(.is-hot) .bar-val-out { opacity: .6; }

.heat-cell { transition: opacity .2s ease; }
.heat-sel { fill: none; stroke: var(--ink); stroke-width: 1.5; pointer-events: none; }
.heat-legend { flex: none; display: flex; align-items: center; gap: var(--s1); font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 11px; color: var(--muted); }
.heat-legend .bar { flex: none; width: 120px; height: 8px; border-radius: 2px; background: linear-gradient(90deg, rgba(224,163,74,.08), rgba(224,163,74,1)); }
.heat-legend .lg-title { margin-right: auto; font-family: var(--sans); }

/* ---------- table ---------- */
.tbl-wrap { overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tbl-wrap::-webkit-scrollbar { display: none; }
.tbl { --cols: minmax(96px, 1.5fr) minmax(72px, 1fr) minmax(64px, .8fr) minmax(64px, .9fr); min-width: 300px; position: relative; }
.trow { display: grid; grid-template-columns: var(--cols); align-items: center; }
.thead { border-bottom: 1px solid var(--line); }
.tbody .trow { border-top: 1px solid var(--line-soft); transition: transform .2s ease, opacity .2s ease; will-change: transform; }
.tbody .trow:first-child { border-top: 0; }
.tbody .trow.is-enter { opacity: 0; }
.tbody .trow.is-leave { opacity: 0; position: absolute; left: 0; right: 0; pointer-events: none; }
.th { min-width: 0; }
.th button {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  border-radius: 6px;
  transition: color .2s ease;
}
.th.num button { justify-content: flex-end; }
.th button:focus-visible { outline: 1px solid var(--up); outline-offset: -1px; }
.th[aria-sort="ascending"] button, .th[aria-sort="descending"] button { color: var(--ink); }
.sort-ico { width: 12px; height: 12px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; opacity: .5; }
.sort-ico .asc, .sort-ico .desc { transition: opacity .2s ease; }
.th[aria-sort] .sort-ico { opacity: 1; color: var(--up); }
.th[aria-sort="ascending"] .sort-ico .desc { opacity: 0; }
.th[aria-sort="descending"] .sort-ico .asc { opacity: 0; }
.td { height: 40px; display: flex; align-items: center; min-width: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 13px; }
.td.num { justify-content: flex-end; }
.td.country { font-family: var(--sans); gap: var(--s1); }
.td .rank { font-family: var(--mono); color: var(--muted); font-size: 11px; width: 16px; text-align: right; }
.td .yoy { display: inline-flex; align-items: center; gap: 2px; }
.td .yoy .arrow { width: 10px; height: 10px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.empty { padding: var(--s3) 0; text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- footer / toast ---------- */
.foot { margin: var(--s2) 0 0; font-size: 11px; color: var(--muted); text-align: right; font-family: var(--mono); }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + var(--s3));
  transform: translate(-50%, 8px);
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--raised);
  color: var(--ink);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  max-width: calc(100% - 32px);
  z-index: 10;
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- admin ---------- */
.admin-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s1); }
@media (min-width: 900px) { .admin-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--s2); } .admin-grid .span2 { grid-column: 1 / -1; } }
.field { display: flex; gap: var(--s1); }
.input {
  flex: 1;
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
}
.input:focus { outline: 1px solid var(--up); outline-offset: 0; border-color: var(--up); }
.btn {
  height: 44px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--raised);
  color: var(--ink);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .2s ease, opacity .2s ease;
}
.btn.primary { background: var(--up); border-color: var(--up); color: var(--bg); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }
.btn:focus-visible { outline: 1px solid var(--up); outline-offset: 2px; }
.btn.small { height: 32px; padding: 0 10px; font-size: 12px; }
.btn.danger { color: var(--down); }
.btn.danger.is-arm { background: var(--down); border-color: var(--down); color: var(--ink); }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s1); }
.stat { padding: 12px; border: 1px solid var(--line-soft); border-radius: 10px; background: var(--bg); }
.stat-label { margin: 0 0 4px; font-size: 11px; color: var(--muted); }
.stat-value { margin: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 22px; line-height: 28px; font-weight: 500; }
.pref { display: flex; flex-direction: column; gap: 12px; }
.pref-row { display: grid; grid-template-columns: 40px minmax(0, 1fr) 40px; align-items: center; gap: var(--s1); font-size: 12px; }
.pref-row .pl { color: var(--muted); }
.pref-row .pv { font-family: var(--mono); text-align: right; }
.pref-row .pt { height: 8px; border-radius: 2px; background: var(--track); overflow: hidden; }
.pref-row .pf { height: 100%; width: 0; border-radius: 2px; background: var(--up); transition: width .2s ease; }
.log { display: flex; flex-direction: column; }
.log-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: var(--s1); height: 48px; border-top: 1px solid var(--line-soft); font-size: 13px; }
.log-row:first-child { border-top: 0; }
.log-row .lt { font-family: var(--mono); color: var(--muted); font-size: 12px; }
.log-row .lr { font-family: var(--mono); }
.hint { margin: var(--s1) 0 0; font-size: 12px; color: var(--muted); }
