/* Cars Driven — the whole stylesheet.
 *
 * ONE global stylesheet in its own file rather than a <style> block, which is worth doing
 * beyond tidiness: ../tomica-tracker-cf hit four bugs that came from a single global sheet
 * (a progress bar given the class `.bar` collapsed the header, which already owned `.bar`;
 * `[hidden]` lost to a later `display` rule and needed `!important`). Rules in one file make
 * "does this class already exist?" a grep instead of a scroll. Both of those traps are
 * pre-empted below.
 */

/* Light only, on purpose — but for a DIFFERENT reason than tomica's.
   There, the photos were product shots on a white backdrop, so the photo panel had to be white.
   Here the photos are whatever I happened to snap of a real car in a real car park: mixed
   lighting, no consistent backdrop, frequently none at all. So the photo panel is a NEUTRAL
   grey that flatters neither and clashes with nothing, and the table — not the photos — is the
   thing the design is built around. */
:root {
  --bg: #f1f2f4;          /* page */
  --panel: #ffffff;       /* table, cards, header, modals */
  --photo: #e4e5e9;       /* behind a photo, and the placeholder for a row that has none */
  --meta: #f6f6f8;        /* the caption strip under a card photo */
  --line: #e0e1e6;
  --line-soft: #eeeff2;   /* row rules inside the table, lighter than a panel border */
  --ink: #16171c;
  --muted: #6a6c78;
  --accent: #1b4dbf;
  --accent-ink: #fff;
  --chip: #edeef1;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 18px rgba(0,0,0,.06);
  --radius: 12px;
}

* { box-sizing: border-box; }
/* A later `display` rule outranks the UA stylesheet's [hidden], which bit
   library-card-tracker. Make hidden win outright. */
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }

/* ---- header */
header {
  position: sticky; top: 0; z-index: 20; background: var(--panel);
  border-bottom: 1px solid var(--line); padding: 10px 16px;
}
.bar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; max-width: 1500px; margin: 0 auto; }
.brand {
  font-weight: 600; letter-spacing: -.01em; display: flex; align-items: center; gap: 9px;
  text-decoration: none; color: inherit; font-size: 16px;
}
.brand .mark { width: 26px; height: 26px; display: block; flex: 0 0 auto; }
.brand b { font-weight: 800; }
.count { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }
.grow { flex: 1 1 40px; }

.tabs { display: flex; gap: 2px; background: var(--chip); border-radius: 999px; padding: 3px; }
.tabs button {
  border: 0; background: none; padding: 6px 14px; border-radius: 999px; cursor: pointer;
  color: var(--muted); font-weight: 600; font-size: 13px;
}
.tabs button[aria-selected="true"] { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }

.btn {
  border: 1px solid var(--line); background: var(--panel); padding: 7px 12px;
  border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: #c02626; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.sm { padding: 4px 9px; font-size: 12px; }

.filters { max-width: 1500px; margin: 0 auto; display: flex; gap: 8px; flex-wrap: wrap; padding-top: 10px; align-items: center; }
.filters input, .filters select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; font-size: 13px;
}
.filters input[type="search"] { flex: 1 1 220px; min-width: 0; }
/* A filter holding a non-default value is outlined, so it's obvious at a glance why the count
   says "37 of 192" — six selects side by side are otherwise easy to lose track of. */
.filters select.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }

main { max-width: 1500px; margin: 0 auto; padding: 16px; }

/* ---- the column picker (table view) */
.colWrap { position: relative; }
.colMenu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 8px;
  min-width: 180px; max-height: 60vh; overflow: auto;
}
.colMenu label {
  display: flex; align-items: center; gap: 8px; padding: 5px 6px; font-size: 13px;
  border-radius: 6px; cursor: pointer;
}
.colMenu label:hover { background: var(--chip); }
.colMenu .sep { border-top: 1px solid var(--line); margin: 6px 0; }

/* ---- table
   The main view. Horizontal scroll is expected, not a failure mode: 13 columns of car spec
   don't fit a laptop, and the alternative — wrapping cells — makes a scan-down-the-column read
   impossible, which is the entire reason this app is a table rather than a gallery. */
.tableWrap {
  overflow-x: auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
th, td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--line-soft); white-space: nowrap; }
th {
  position: sticky; top: 0; z-index: 2; background: var(--panel); cursor: pointer;
  font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase;
  letter-spacing: .04em; border-bottom: 1px solid var(--line);
  /* `position: sticky` on a <th> inside a scrolling container leaves the cell background
     transparent at the moment rows slide under it in Safari; the explicit background above is
     what stops the first row showing through the header. */
}
/* Edge gutters. Without these the first column sits flush against the panel border — only the
   generic 10px of cell padding between the value and the rounded edge, which reads as clipped
   rather than as a margin. `:first-child`/`:last-child` (specificity 0,1,1) outrank the plain
   `.tcell` class rule below, so the photo cell picks the gutter up too when it is present. */
th:first-child, td:first-child { padding-left: 18px; }
th:last-child, td:last-child { padding-right: 18px; }
th:hover { color: var(--ink); }
th.sorted { color: var(--accent); }
/* Model and Trim aren't sortable (see COLS in derive.js). The header must stop LOOKING
   clickable, or it reads as a broken control rather than a deliberate one — the click handler
   is genuinely absent, so a pointer cursor here would be a lie. */
th.nosort { cursor: default; }
th.nosort:hover { color: var(--muted); }
th .arrow { font-size: 9px; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--chip); }
td.num { font-variant-numeric: tabular-nums; text-align: right; }
th.num { text-align: right; }
/* The Note column is the only one allowed to be long, so it is the only one that truncates.
   `max-width` on a table cell needs `table-layout: auto` to be treated as a hint, which it is
   here — the cell shrinks to this when the row is crowded and grows when it isn't. */
td.wide { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
/* Empty cells get a muted dash rather than nothing: a blank run of Trim cells reads as a
   rendering fault, while a column of dashes reads as "not recorded", which is the truth. */
td.nil { color: #b9bac2; }
td.flag { text-align: center; }
.yes { color: var(--accent); font-weight: 700; }

/* The photo cell. `cover`, not `contain` — at 44x33 a letterboxed photo is mostly background,
   and unlike a product shot a snapshot of a real car has nothing worth preserving at the
   edges. The detail modal uses `contain`, where the whole frame matters. */
.tcell { width: 44px; padding-right: 0; }
.tcell .ph {
  width: 44px; height: 33px; border-radius: 4px; background: var(--photo); overflow: hidden;
  display: block;
}
.tcell img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ---- grouped view */
.grpHdr {
  display: flex; align-items: baseline; gap: 8px; margin: 22px 0 10px;
  font-size: 13px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase;
  color: var(--muted); border-bottom: 1px solid var(--line); padding-bottom: 6px;
}
.grpHdr:first-child { margin-top: 0; }
.grpHdr b { color: var(--ink); font-size: 15px; text-transform: none; letter-spacing: 0; }
.grpHdr .n { font-variant-numeric: tabular-nums; }

.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); cursor: pointer; display: flex; flex-direction: column;
}
.card:hover { border-color: var(--muted); }
.card .thumb {
  aspect-ratio: 4 / 3; background: var(--photo); display: grid; place-items: center; overflow: hidden;
}
.card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.card .thumb .none { color: var(--muted); font-size: 12px; }
.card .meta { padding: 9px 11px 11px; background: var(--meta); border-top: 1px solid var(--line); flex: 1 1 auto; }
.card .t { font-weight: 650; font-size: 14px; line-height: 1.3; }
.card .s { color: var(--muted); font-size: 12px; margin-top: 3px; }
.tag {
  display: inline-block; background: var(--chip); color: var(--muted); border-radius: 999px;
  padding: 2px 8px; font-size: 11px; font-weight: 600; margin: 7px 4px 0 0;
}
.tag.nora { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
/* Monospace is what distinguishes a spec chip from a word chip beside it — "Compact Sedan" next
   to "2.0L I-4 Turbo" would otherwise look like the same kind of thing. */
.tag.spec { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0; white-space: nowrap; }

/* ---- stats view */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); margin-bottom: 18px; }
.tile {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 13px 14px; box-shadow: var(--shadow);
}
.tile .n { font-size: 26px; font-weight: 750; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.tile .l { font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 3px; }
.tile .sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.panels { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 13px 14px; box-shadow: var(--shadow);
}
.panel h3 {
  margin: 0 0 10px; font-size: 12px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
}
/* A bar list, not a chart library: one row per value, the bar drawn as a background width.
   `grid-template-columns` with a fixed last track keeps the counts in a straight column
   regardless of label length, which is what makes the list scannable. */
.bars { display: grid; gap: 4px; }
.barRow {
  display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center;
  font-size: 13px; padding: 3px 7px; border-radius: 6px; position: relative;
  background: linear-gradient(to right, color-mix(in srgb, var(--accent) 13%, transparent) var(--w, 0%), transparent var(--w, 0%));
  cursor: pointer; border: 1px solid transparent;
}
.barRow:hover { border-color: var(--accent); }
.barRow .k { position: relative; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barRow .v { position: relative; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 600; }
.barRow .unknown { color: var(--muted); font-style: italic; }

/* ---- modal */
dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink);
  padding: 0; max-width: 680px; width: calc(100vw - 32px); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
.dlgHead { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.dlgHead h2 { margin: 0; font-size: 16px; }
.dlgBody { padding: 16px; max-height: min(72vh, 620px); overflow: auto; }
.dlgFoot { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 12px; }
.fields label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.fields label.wide { grid-column: 1 / -1; }
.fields label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 13px; color: var(--ink); }
.fields input, .fields select, .fields textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 14px; color: var(--ink);
}
.fields textarea { min-height: 62px; resize: vertical; }

/* Name is DERIVED from Make · Model · Trim (90% of stored names are exactly that), shown
   read-only with an opt-in override — the 20 rows where it differs carry text no other field
   holds, like `Mazda 3i Sedan` from model `3` and trim `i`. */
.nameRow {
  grid-column: 1 / -1; display: flex; align-items: baseline; gap: 8px;
  padding: 8px 10px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
.nameRow .lbl { font-size: 12px; color: var(--muted); font-weight: 600; }
.nameRow #nameShown { flex: 1 1 auto; font-size: 14px; min-width: 0; }
.nameRow #nameShown:empty::before { content: 'from Make / Model / Trim'; color: var(--muted); }
.nameRow input { flex: 1 1 auto; min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; }
.nameRow .btn { padding: 4px 9px; font-size: 12px; }

/* A datalist-backed input, used for every field with a settled vocabulary (Make, Type, Engine,
   Transmission, Drivetrain, Displacement). Deliberately an <input list=…> and NOT a <select>:
   the options are the values already in the collection, so a select would make a car with a
   genuinely new engine layout unenterable, while the datalist suggests without constraining. */
.fields input[list] { background-image: none; }

.detail { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.detail .ph {
  grid-column: 1 / -1; background: var(--photo); border: 1px solid var(--line); border-radius: 8px;
  aspect-ratio: 16/10; display: grid; place-items: center; overflow: hidden;
}
/* `contain` here, unlike the table thumbnail: the modal is where you actually look at the
   photo, so cropping it to fill the frame would be hiding the thing you opened. */
.detail .ph img { width: 100%; height: 100%; object-fit: contain; }
.detail .ph .msg { padding: 10px; }
.kv { font-size: 13px; margin: 0; }
.kv dt { color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.kv dd { margin: 2px 0 10px; }

.msg { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.msg.err { color: #c02626; }
.empty { text-align: center; color: var(--muted); padding: 60px 16px; }

/* ---- photo upload (edit modal)
   There is no URL importer and no wiki, so this is the ONLY way a photo gets in: a file picker
   plus a drop target. The chosen file is re-encoded in the browser before it is sent, because a
   Worker cannot resize an image and there is no CDN to ask for a scaled copy. */
.photoRow { display: flex; gap: 12px; align-items: flex-start; margin: 0 0 14px; }
.photoRow .pv {
  width: 148px; height: 111px; flex: 0 0 auto; background: var(--photo); border-radius: 8px;
  display: grid; place-items: center; overflow: hidden; border: 1px solid var(--line);
}
.photoRow .pv img { width: 100%; height: 100%; object-fit: cover; }
.photoRow .pv span { color: var(--muted); font-size: 11px; text-align: center; padding: 6px; }
.photoRow .side { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 7px; }
.photoRow .row { display: flex; gap: 6px; flex-wrap: wrap; }
.drop {
  border: 1.5px dashed var(--line); border-radius: 8px; padding: 10px 12px; font-size: 12px;
  color: var(--muted); text-align: center; cursor: pointer;
}
.drop:hover, .drop.over { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, transparent); }

/* NOT `.bar` — the header's top row already owns that class, and styling it as a 6px
   overflow:hidden pill collapsed the whole header in ../tomica-tracker-cf. */
.pbar { height: 6px; background: var(--chip); border-radius: 999px; overflow: hidden; margin: 8px 0; }
.pbar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }

@media (max-width: 620px) {
  .fields, .detail { grid-template-columns: 1fr; }
  main { padding: 12px 16px; }
  .photoRow { flex-direction: column; }
  .photoRow .pv { width: 100%; height: 150px; }
}
