:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --fg: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #1d4ed8;
  --accent-fg: #ffffff;
  --accent-soft: #dbeafe;
  --ok: #15803d;
  --warn: #b45309;
  --err: #b91c1c;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 1px 3px rgb(0 0 0 / 0.08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2e;
    --fg: #e5e7eb;
    --muted: #94a3b8;
    --line: #1f2a44;
    --accent: #60a5fa;
    --accent-fg: #0b1220;
    --accent-soft: #1e3a8a;
    --ok: #4ade80;
    --warn: #fbbf24;
    --err: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 18px; margin: 0; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }

.status {
  font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 46vw;
}
@media (max-width: 480px) {
  .status { max-width: 30vw; }
  .brand img { display: none; }
  #install { padding: 8px 6px; }
}
.status::before {
  content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  margin-right: 6px; background: var(--muted); vertical-align: middle;
}
.status[data-state="downloading"]::before, .status[data-state="syncing"]::before { background: var(--warn); }
.status[data-state="idle"]::before { background: var(--ok); }
.status[data-state="error"]::before, .status[data-state="offline"]::before { background: var(--err); }

/* ---------- layout ---------- */
main { max-width: 1200px; margin: 0 auto; padding: 16px; }

.filters {
  display: grid; gap: 12px;
  grid-template-columns: 1fr;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow);
}
@media (min-width: 760px) {
  .filters { grid-template-columns: 2fr auto 1fr 1fr 90px 90px; align-items: end; }
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field input, .field select {
  font: inherit; font-size: 15px; color: var(--fg);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 10px; min-height: 40px; width: 100%;
}
.field input:focus-visible, .field select:focus-visible, .btn:focus-visible, .segmented input:focus-visible + span {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
@media (max-width: 759px) {
  .field-year { display: inline-flex; }
  .filters { grid-template-columns: 1fr 1fr; }
  .field-search, .segmented { grid-column: 1 / -1; }
}

.segmented {
  display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 3px;
  overflow-x: auto;
}
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; text-align: center; padding: 7px 12px; border-radius: 6px;
  font-size: 14px; cursor: pointer; white-space: nowrap; color: var(--muted);
}
.segmented input:checked + span { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); font-weight: 600; }

.summary { display: flex; justify-content: space-between; align-items: center; margin: 14px 2px 8px; color: var(--muted); font-size: 14px; }

.btn {
  font: inherit; font-size: 14px; cursor: pointer; text-decoration: none;
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; min-height: 38px;
  background: var(--surface); color: var(--fg);
  display: inline-flex; align-items: center; justify-content: center;
}
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-ghost { background: transparent; color: var(--accent); border-color: transparent; }

/* ---------- table ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.prices { width: 100%; border-collapse: collapse; }
.prices th, .prices td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.prices th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface); }
.prices tbody tr { cursor: pointer; }
.prices tbody tr:hover { background: var(--bg); }
.prices tbody tr:last-child td { border-bottom: 0; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; }
.price { font-weight: 600; white-space: nowrap; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: var(--muted); }

.sort { all: unset; cursor: pointer; }
.sort[aria-sort="ascending"]::after { content: " ▲"; font-size: 10px; }
.sort[aria-sort="descending"]::after { content: " ▼"; font-size: 10px; }

.empty { padding: 32px 16px; text-align: center; color: var(--muted); margin: 0; }

/* Cards on phones */
@media (max-width: 759px) {
  .prices thead { display: none; }
  .prices, .prices tbody, .prices tr, .prices td { display: block; }
  .prices tr {
    display: grid; grid-template-columns: 1fr auto; gap: 2px 12px;
    padding: 12px 14px; border-bottom: 1px solid var(--line);
  }
  .prices td { padding: 0; border: 0; }
  .prices td.c-model { grid-column: 1; grid-row: 1; font-weight: 600; }
  .prices td.c-price { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
  .prices td.c-brand { grid-column: 1; grid-row: 2; color: var(--muted); font-size: 13px; }
  .prices td.c-meta { grid-column: 1 / -1; font-size: 13px; color: var(--muted); }
  .prices td.c-year, .prices td.c-fuel, .prices td.c-fipe { display: none; }
}
@media (min-width: 760px) { .c-meta { display: none; } }

.pager { display: flex; justify-content: center; align-items: center; gap: 12px; margin: 16px 0 calc(16px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 14px; }

/* ---------- detail dialog ---------- */
dialog {
  width: min(560px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 14px;
  background: var(--surface); color: var(--fg); padding: 20px; box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { margin: 2px 0 4px; font-size: 18px; padding-right: 40px; }
dialog h3 { font-size: 14px; color: var(--muted); margin: 18px 0 8px; font-weight: 600; }
dialog p { margin: 0; }
.dialog-close { position: absolute; top: 10px; right: 10px; }
.big-price { font-size: 28px; font-weight: 700; margin-top: 10px !important; font-variant-numeric: tabular-nums; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; }
.chart .area { fill: var(--accent-soft); opacity: 0.6; }
.chart .dot { fill: var(--accent); }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .grid { stroke: var(--line); }

.footer { text-align: center; font-size: 12px; padding: 8px 16px 24px; }
