/* Garage Inventory — mobile first, big tap targets, readable with dirty hands
   and bad lighting. Dark by default because that's easier on a phone in a
   garage; follows the system setting. */

:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaef;
  --muted: #939aa8;
  --accent: #4c8dff;
  --accent-ink: #ffffff;
  --good: #35c37d;
  --warn: #f5a524;
  --bad: #f2545b;
  --radius: 14px;
  --tap: 48px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eceef2;
    --border: #dcdfe6;
    --text: #14161b;
    --muted: #616a7a;
    --accent: #1c6dd0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: none;
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
}

/* ------------------------------------------------------------------ chrome */

#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
#topbar h1 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.02em; }
#topbar-actions { display: flex; gap: 8px; }

main {
  flex: 1;
  padding: 14px 16px calc(84px + var(--safe-b));
  max-width: 760px; width: 100%; margin: 0 auto;
}

#tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
/* Menu is a button rather than a link — it opens a sheet, not a page — so the
   tab styling has to cover both without either looking like the odd one out. */
#tabs a, #tabs button {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 58px; text-decoration: none; color: var(--muted);
  font-size: 11px; font-weight: 600;
  background: none; border: 0; padding: 0; font-family: inherit; cursor: pointer;
}
#tabs a span, #tabs button span { font-size: 20px; line-height: 1; }
#tabs a.active, #tabs button.active { color: var(--accent); }

.badge {
  position: absolute; top: 8px; right: 50%; transform: translateX(22px);
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--bad); color: #fff;
  font-size: 11px; font-style: normal; line-height: 18px; text-align: center;
}

.bin-banner {
  position: sticky; top: 53px; z-index: 19;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; font-size: 14px;
  background: color-mix(in srgb, var(--accent) 18%, var(--bg));
  border-bottom: 1px solid var(--border);
}
.bin-banner strong { font-weight: 700; }
.bin-banner button { margin-left: auto; }

/* ----------------------------------------------------------------- pieces */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

button, .btn {
  font: inherit; font-weight: 600;
  min-height: var(--tap);
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--accent); color: var(--accent-ink);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none;
}
button:active, .btn:active { transform: scale(0.97); }
button:disabled { opacity: 0.45; cursor: default; }

button.secondary, .btn.secondary {
  background: var(--surface-2); color: var(--text); border-color: var(--border);
}
button.ghost {
  background: transparent; color: var(--muted);
  border-color: transparent; min-height: 36px; padding: 0 10px; font-size: 14px;
}
button.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
button.small { min-height: 38px; padding: 0 14px; font-size: 14px; }

.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.muted { color: var(--muted); }
.small-text { font-size: 13px; }
.center { text-align: center; }
.hidden { display: none !important; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }

input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  min-height: var(--tap);
  /* 16px minimum stops iOS Safari zooming in when a field is focused. */
  font-size: 16px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { margin-bottom: 12px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-size: 12px; font-weight: 600; color: var(--muted);
}
.pill.low  { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--warn); border-color: transparent; }
.pill.out  { background: color-mix(in srgb, var(--bad) 22%, transparent);  color: var(--bad);  border-color: transparent; }
.pill.ok   { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); border-color: transparent; }

/* ---------------------------------------------------------------- scanner */

.scanner {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .reticle {
  position: absolute; inset: 18% 12%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  border-radius: 12px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35);
}
.scanner .reticle.hit { border-color: var(--good); }
.scanner .cam-actions {
  position: absolute; bottom: 10px; left: 10px; right: 10px;
  display: flex; gap: 8px; justify-content: center;
}
.scanner .cam-actions button {
  min-height: 40px; font-size: 14px;
  background: rgba(0, 0, 0, 0.55); color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(8px);
}
.scanner-msg {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px; text-align: center; color: #fff;
  background: #000;
}

/* ------------------------------------------------------------------ lists */

.item-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none; color: inherit;
}
.item-row .thumb {
  width: 46px; height: 46px; flex: 0 0 auto;
  border-radius: 10px; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; overflow: hidden;
}
.item-row .thumb img { width: 100%; height: 100%; object-fit: cover; }
.item-row .body { flex: 1; min-width: 0; }
.item-row .name { font-weight: 600; }
.item-row .meta {
  font-size: 12.5px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item-row .qty { font-size: 18px; font-weight: 700; text-align: right; white-space: nowrap; }
.item-row .chev { flex: 0 0 auto; color: var(--muted); font-size: 22px; line-height: 1; }

/* Marks which field a search hit, so an order-number match explains itself. */
.item-row .why {
  display: inline-block;
  padding: 0 6px; margin-right: 4px;
  border-radius: 5px;
  background: color-mix(in srgb, var(--accent) 20%, transparent);
  color: var(--accent);
  font-size: 11px; font-weight: 700; text-transform: lowercase;
}

/* Running total for whichever list is on screen. */
.totals {
  display: flex; flex-wrap: wrap; gap: 4px 10px;
  padding: 10px 14px; margin-bottom: 10px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13.5px; color: var(--muted);
}
.totals b { color: var(--text); font-size: 15px; }

.statgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: 12px; }
.statgrid > div { display: flex; flex-direction: column; gap: 2px; }
.statgrid b { font-size: 24px; line-height: 1.1; }

/* ---------------------------------------------------------------- gallery */

.gallery {
  display: flex; gap: 8px; overflow-x: auto;
  padding-bottom: 4px; scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.shot {
  position: relative; flex: 0 0 auto;
  width: 92px; height: 92px; padding: 0;
  border-radius: 12px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--border);
  scroll-snap-align: start; cursor: pointer;
}
.shot img { width: 100%; height: 100%; object-fit: cover; display: block; }
.shot-tag {
  position: absolute; left: 4px; bottom: 4px;
  padding: 1px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, 0.65); color: #fff;
  font-size: 10px; font-weight: 700; letter-spacing: 0.03em;
}
.shot.add {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; color: var(--muted); border-style: dashed; background: transparent;
}
.shot.add span { font-size: 24px; line-height: 1; }
.shot.add small { font-size: 11px; font-weight: 600; }

.supplier-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.supplier-row:last-child { border-bottom: 0; }

/* ------------------------------------------------- compact part page ----- */

.hero-thumb {
  flex: 0 0 auto; width: 68px; height: 68px; padding: 0;
  border-radius: 12px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.hero-thumb img { width: 100%; height: 100%; object-fit: cover; }
.hero-thumb.add { border-style: dashed; font-size: 24px; }

/* Jump links to the folded sections below. */
.chips { display: flex; gap: 6px; overflow-x: auto; margin-top: 12px; padding-bottom: 2px; }
.chip {
  flex: 0 0 auto; min-height: 34px; padding: 0 12px;
  border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--border);
}

/* Tighter than a normal card so the whole folded page clears one screen. */
/* ------------------------------------------------- job parts panel ------- */

/*
 * The part search sits above its list and stays put while you scroll the sheet,
 * so adding the fifth part costs the same as the first. 44px clears the sheet's
 * own sticky header.
 */
.parts-search {
  position: sticky; top: 44px; z-index: 3;
  padding: 8px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.results-panel:not(:empty) {
  max-height: 40vh; overflow-y: auto;
  margin-top: 8px; padding: 0 10px;
  background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 12px;
  -webkit-overflow-scrolling: touch;
}

/* Grows freely until it would swamp the form, then scrolls in place. */
.parts-list.scrolls {
  max-height: 46vh; overflow-y: auto;
  padding-right: 4px;
  -webkit-overflow-scrolling: touch;
}

/* A chip acting as a selected option rather than a jump link. */
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.fold { padding: 0; margin-bottom: 8px; }
.fold > .sum-head, details > .sum-head {
  list-style: none; cursor: pointer;
  padding: 14px; font-size: 14px; font-weight: 600;
  display: flex; align-items: center; justify-content: space-between;
}
.fold > .sum-head::-webkit-details-marker, .sum-head::-webkit-details-marker { display: none; }
.sum-head::after { content: '›'; color: var(--muted); font-size: 20px; transition: transform 0.15s; }
details[open] > .sum-head::after { transform: rotate(90deg); }
.fold > div { padding: 0 14px 14px; }
.fold[open] > .sum-head { border-bottom: 1px solid var(--border); }

/* The stepper is the one thing that must never need scrolling to reach. */
.hero .stepper button { min-height: 54px; flex-basis: 62px; }
.hero .stepper .value b { font-size: 27px; }

/* ------------------------------------------------------------ order list - */

.order-line {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.order-line:last-of-type { border-bottom: 0; }
.order-line .name { font-weight: 600; font-size: 14.5px; }

.order-qty { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.order-qty .oq {
  width: 52px; min-height: 38px; padding: 4px 6px;
  text-align: center; font-size: 15px; border-radius: 9px;
}
/* Spinners waste width on a phone; the ± buttons do the same job. */
.order-qty .oq::-webkit-outer-spin-button,
.order-qty .oq::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.order-qty .oq { -moz-appearance: textfield; appearance: textfield; }
/* ------------------------------------------------------------- stocktake - */

.progress {
  height: 6px; margin-top: 10px;
  background: var(--surface-2); border-radius: 3px; overflow: hidden;
}
.progress .bar { height: 100%; background: var(--good); transition: width 0.25s ease; }

/* The count entry reuses the stepper, but the middle is typeable. */
.stepper input.value {
  border-radius: 12px;
  -moz-appearance: textfield; appearance: textfield;
}
.stepper input.value::-webkit-outer-spin-button,
.stepper input.value::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.qty-btn {
  min-height: 38px; width: 34px; padding: 0;
  font-size: 19px; font-weight: 700;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
}

/* --------------------------------------------------------- quantity stepper */

.stepper { display: flex; align-items: stretch; gap: 10px; }
.stepper button { flex: 0 0 68px; font-size: 26px; font-weight: 700; min-height: 60px; }
.stepper .value {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
}
.stepper .value b { font-size: 30px; line-height: 1; font-weight: 800; }
.stepper .value small { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------------------ sheet */

.sheet { position: fixed; inset: 0; z-index: 60; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.55); }
.sheet-panel {
  position: relative; width: 100%; max-height: 88vh; overflow-y: auto;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  border-top: 1px solid var(--border);
  padding: 8px 16px calc(24px + var(--safe-b));
  animation: rise 0.18s ease-out;
}
@keyframes rise { from { transform: translateY(24px); opacity: 0.4; } }
.sheet-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; margin-bottom: 6px;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.sheet-head h2 { margin: 0; font-size: 18px; }

@media (min-width: 700px) {
  .sheet { align-items: center; justify-content: center; }
  .sheet-panel { max-width: 560px; border-radius: 20px; border: 1px solid var(--border); }
}

/* ------------------------------------------------------------------ toast */

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(78px + var(--safe-b)); z-index: 90;
  max-width: 90vw; padding: 12px 18px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  font-size: 14px; font-weight: 600;
}
.toast.good { border-color: var(--good); }
.toast.bad { border-color: var(--bad); }
.toast { display: flex; align-items: center; gap: 12px; }
.toast-action {
  flex: 0 0 auto;
  min-height: 32px; padding: 0 12px;
  border-radius: 8px; font-size: 13px; font-weight: 700;
  background: var(--accent); color: var(--accent-ink); border: 0;
}

.empty { padding: 44px 20px; text-align: center; color: var(--muted); }
.empty .big { font-size: 40px; display: block; margin-bottom: 10px; }

.tree-row { padding-left: calc(var(--depth, 0) * 18px); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
  button:active, .btn:active { transform: none; }
}

/* ------------------------------------------------------- parts drawings */

/*
 * The drawing sits in a scrollable box rather than being squeezed to the phone
 * width: these are dense engineering diagrams, and a 1960px drawing shrunk to
 * 390px is unreadable. It starts fitted, and pinch-zoom works inside it.
 */
.drawing-wrap {
  position: relative;
  overflow: auto;
  max-height: 70vh;
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}
.drawing-wrap img { display: block; width: 100%; height: auto; }

/* The overlay tracks the image exactly, so markers scale with it. */
.drawing-spots { position: absolute; inset: 0; pointer-events: none; }

.drawing-spot {
  position: absolute;
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto;
  font-size: 10px; font-weight: 700; line-height: 1;
  color: transparent;                    /* the number is already drawn on the diagram */
  background: rgba(120, 120, 120, 0.16);
  border-radius: 3px;
  text-decoration: none;
}
.drawing-spot:hover, .drawing-spot:active { background: rgba(25, 115, 25, 0.35); }

/* A position you already stock, so what you own reads off the drawing itself. */
.drawing-spot.owned {
  background: color-mix(in srgb, var(--good) 34%, transparent);
  box-shadow: 0 0 0 1.5px var(--good) inset;
}

/* Tapping a marker lands you on the row; this says which one. */
.flash { background: color-mix(in srgb, var(--good) 16%, transparent); border-radius: 8px; }

/* ------------------------------------------------------------------ login */

#login-screen {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 24px; background: var(--bg);
}
#login-form { width: 100%; max-width: 320px; }
#login-form h1 { margin: 0 0 4px; font-size: 22px; }
#login-form p { margin: 0 0 18px; }

/* A toggled action button — used by the scan-to-add camera in a job. */
button.secondary.on {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}

/*
 * A chip for a section that has nothing in it yet.
 *
 * The section itself is hidden — a row reading "Photos (0)" is a tap that goes
 * nowhere — so the chip is the only way in, and it has to read as "add one"
 * rather than as a count you can't find.
 */
.chip.empty { opacity: 0.5; }
.chip.empty:active { opacity: 1; }
