:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #13202e;
  --muted: #5b6b7c;
  --border: #dde3ea;
  --brand: #1d3d5c;
  --brand-contrast: #ffffff;
  --accent: #b8860b;
  --up: #1a7f4b;
  --down: #c0392b;
  --neutral: #5b6b7c;
  --warn-bg: #fff4d6;
  --warn-text: #7a5300;
  --err-bg: #fde8e6;
  --err-text: #8f2318;
  --ok-bg: #e3f4ea;
  --ok-text: #135c37;
  --chart-line: #1d3d5c;
  --chart-area: rgba(29, 61, 92, 0.10);
  --band-outer: rgba(184, 134, 11, 0.16);
  --band-inner: rgba(184, 134, 11, 0.30);
  --grid: #e3e8ee;
  --shadow: 0 1px 2px rgba(19, 32, 46, 0.06), 0 2px 8px rgba(19, 32, 46, 0.05);
  --radius: 14px;
  --tap: 48px;
  --nav-h: 60px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Noto Sans", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d141c;
    --surface: #151f2a;
    --surface-2: #1c2836;
    --text: #e8eef5;
    --muted: #93a3b5;
    --border: #263546;
    --brand: #8fb4dc;
    --brand-contrast: #0d141c;
    --accent: #e0b44a;
    --up: #45c07e;
    --down: #ff7b6b;
    --neutral: #93a3b5;
    --warn-bg: #3a2f10;
    --warn-text: #f3d27a;
    --err-bg: #3d1c19;
    --err-text: #ffb3a8;
    --ok-bg: #12301f;
    --ok-text: #8fe0b2;
    --chart-line: #8fb4dc;
    --chart-area: rgba(143, 180, 220, 0.12);
    --band-outer: rgba(224, 180, 74, 0.16);
    --band-inner: rgba(224, 180, 74, 0.30);
    --grid: #223040;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
}
button { font: inherit; color: inherit; }
a { color: inherit; }
.num { font-variant-numeric: tabular-nums; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) 0 max(8px, env(safe-area-inset-left, 0px));
  min-height: calc(56px + env(safe-area-inset-top, 0px));
  background: var(--brand);
  color: var(--brand-contrast);
}
@media (prefers-color-scheme: dark) { .topbar { background: var(--surface); color: var(--text); border-bottom: 1px solid var(--border); } }
.topbar h1 { flex: 1; margin: 0; font-size: 1.15rem; font-weight: 650; letter-spacing: 0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  min-width: var(--tap); min-height: var(--tap);
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 0; border-radius: 12px; cursor: pointer; color: inherit;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.15); }
.icon-btn svg { width: 24px; height: 24px; }
.icon-btn[hidden] { display: none; }
.spin svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Layout */
main {
  max-width: 1180px; margin: 0 auto;
  padding: 12px max(12px, env(safe-area-inset-right, 0px)) 24px max(12px, env(safe-area-inset-left, 0px));
}
.grid { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 720px) { .grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 1100px) { .grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.section-title { margin: 20px 4px 8px; font-size: 0.82rem; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.card h2 { margin: 0 0 4px; font-size: 1.05rem; font-weight: 650; }
.card .sub { color: var(--muted); font-size: 0.88rem; }
a.card { display: block; text-decoration: none; }
a.card:active, .row:active { background: var(--surface-2); }

.hero { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 6px 0 2px; }
.hero .value { font-size: 2.1rem; font-weight: 700; letter-spacing: -0.01em; }
.hero .unit { color: var(--muted); font-size: 1rem; }
.change { font-weight: 650; }
.change.up { color: var(--up); }
.change.down { color: var(--down); }
.change.flat { color: var(--neutral); }

.meta { display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: center; margin-top: 10px; font-size: 0.86rem; color: var(--muted); }
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px;
  font-size: 0.78rem; font-weight: 650; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok-text); }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge.err { background: var(--err-bg); color: var(--err-text); }
.badge.up { color: var(--up); }
.badge.down { color: var(--down); }

/* Stock rows */
.list { padding: 4px 0; }
.row {
  display: grid; grid-template-columns: minmax(0, 1fr) 76px auto; align-items: center; gap: 10px;
  min-height: 64px; padding: 10px 16px; text-decoration: none; border-top: 1px solid var(--border);
}
.list .row:first-child { border-top: 0; }
.row .name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .company { color: var(--muted); font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .price { text-align: right; }
.row .price strong { display: block; font-size: 1.05rem; }
.row .flags { display: flex; gap: 4px; margin-top: 3px; flex-wrap: wrap; }
.spark { width: 76px; height: 32px; display: block; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 14px; margin: 0; }
@media (min-width: 560px) { .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.stats div { min-width: 0; }
.stats dt { color: var(--muted); font-size: 0.8rem; }
.stats dd { margin: 2px 0 0; font-weight: 650; }

/* Charts */
.chart-wrap { position: relative; width: 100%; margin-top: 8px; touch-action: pan-y; }
.chart-wrap svg { display: block; width: 100%; overflow: visible; }
.chart-wrap .axis { fill: var(--muted); font-size: 11px; }
.chart-wrap .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart-wrap .line { fill: none; stroke: var(--chart-line); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-wrap .area { fill: var(--chart-area); }
.chart-wrap .band-outer { fill: var(--band-outer); }
.chart-wrap .band-inner { fill: var(--band-inner); }
.chart-wrap .median { fill: none; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; }
.chart-wrap .cursor { stroke: var(--muted); stroke-width: 1; }
.chart-wrap .dot { fill: var(--chart-line); stroke: var(--surface); stroke-width: 2; }
.tooltip {
  position: absolute; top: 0; pointer-events: none; transform: translateX(-50%);
  background: var(--text); color: var(--bg); border-radius: 8px; padding: 4px 8px; font-size: 0.8rem; white-space: nowrap;
}
.tooltip[hidden] { display: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 8px; font-size: 0.8rem; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -2px; margin-right: 5px; }

.segmented { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; overflow-x: auto; scrollbar-width: none; }
.segmented button {
  flex: 1 0 auto; min-height: 40px; min-width: 52px; padding: 0 10px; border: 0; border-radius: 9px;
  background: transparent; color: var(--muted); font-weight: 600; cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* States */
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s ease-in-out infinite; border-radius: 8px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton, .spin svg { animation: none; } }
.skeleton.line { height: 14px; margin: 8px 0; }
.skeleton.big { height: 36px; width: 60%; margin: 10px 0; }
.skeleton.chart { height: 180px; margin-top: 12px; }
.state { padding: 14px; border-radius: 12px; font-size: 0.92rem; }
.state.error { background: var(--err-bg); color: var(--err-text); }
.state.empty { background: var(--surface-2); color: var(--muted); }
.state button, .btn {
  margin-top: 10px; min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid currentColor;
  background: transparent; font-weight: 650; cursor: pointer;
}
.banner { margin: 0 0 12px; padding: 12px 14px; border-radius: 12px; font-size: 0.9rem; }
.banner.offline { background: var(--warn-bg); color: var(--warn-text); }
.banner.auth { background: var(--err-bg); color: var(--err-text); }
.banner strong { display: block; }
.banner[hidden] { display: none; }

.kv { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.kv td { padding: 9px 0; border-top: 1px solid var(--border); vertical-align: top; }
.kv tr:first-child td { border-top: 0; }
.kv td:last-child { text-align: right; padding-left: 12px; }
.note { color: var(--muted); font-size: 0.8rem; margin-top: 10px; }
.dealer { padding: 10px 0; border-top: 1px solid var(--border); }
.dealer:first-child { border-top: 0; }
.dealer .top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }

/* Bottom navigation */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--tap); text-decoration: none; color: var(--muted); font-size: 0.72rem; font-weight: 600;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a[aria-current="page"] { color: var(--brand); }
@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .tabbar { position: sticky; top: calc(56px + env(safe-area-inset-top, 0px)); bottom: auto; height: 52px; padding: 0; max-width: none; }
  .tabbar a { flex-direction: row; gap: 8px; font-size: 0.92rem; }
}
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.legend .sw-outer { background: var(--band-outer); }
.legend .sw-inner { background: var(--band-inner); }
.legend .sw-median { background: var(--accent); }
.pad { padding: 4px 16px 10px; }
.kv.spaced { margin-top: 12px; }
#back[hidden] + h1 { padding-left: 8px; }
.product-card { padding: 18px; }
.product-card > p { line-height: 1.6; overflow-wrap: anywhere; }
.product-card label { display: block; margin: 16px 0; font-weight: 600; }
.product-card select { display: block; width: 100%; max-width: 100%; min-height: 44px; margin-top: 6px; padding: 8px; color: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.product-card details { margin: 16px 0; overflow-x: auto; }
.product-card summary { cursor: pointer; min-height: 44px; padding: 8px 0; }
.product-card textarea { width: 100%; box-sizing: border-box; font: inherit; color: inherit; background: inherit; }
.product-card table { width: 100%; font-size: 0.85rem; }
.product-card th, .product-card td { padding: 8px; text-align: left; }
.product-card .btn { margin: 5px 4px 5px 0; min-height: 44px; }
