/* Look: the parts inventory page at the back of a LEGO instruction booklet.
   Each part sits in a pale blue callout with its count ("12x") underneath.
   Everything else stays flat and quiet. */
:root {
  --page: #eef3f7;
  --callout: #dde9f3;
  --callout-line: #9db4c8;
  --surface: #ffffff;
  --ink: #1f2a33;
  --muted: #5e6f7d;
  --line: #cdd9e3;
  --red: #d01012;
  --red-dark: #a50c0e;
  --focus: #ffcd03;
  --ok-bg: #dcefdc;
  --ok-ink: #1d4d22;
  --err-bg: #f9dedb;
  --err-ink: #85180f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --page: #141b21;
    /* Part pictures have white backgrounds, so the callouts stay booklet-paper light. */
    --callout: #c9d9e6;
    --callout-line: #6f8aa1;
    --surface: #1a232b;
    --ink: #e4ebf0;
    --muted: #93a4b2;
    --line: #2c3a46;
    --ok-bg: #1b3a20;
    --ok-ink: #bfe5c2;
    --err-bg: #4a1a16;
    --err-ink: #ffccc5;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 "Rubik", system-ui, sans-serif;
  background: var(--page);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; }
main { max-width: 1120px; margin: 0 auto; padding: 8px 16px 32px; }
h1 { font-size: 26px; font-weight: 600; letter-spacing: -.01em; margin: 8px 0 18px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Header */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  max-width: 1120px; margin: 0 auto;
  padding: calc(14px + var(--safe-top)) 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 19px; text-decoration: none; }
.stud {
  width: 20px; height: 20px; border-radius: 50%; flex: none;
  background: var(--red); box-shadow: inset -2px -3px 0 rgba(0, 0, 0, .22);
}

/* Form controls. 16px text stops iOS zooming in on focus. */
input, select, button, textarea { font: inherit; color: inherit; }
input, select {
  width: 100%; min-height: 44px; padding: 9px 12px;
  border: 1.5px solid var(--line); border-radius: 6px; background: var(--surface);
}
input:focus, select:focus { outline: none; border-color: var(--ink); }
button {
  min-height: 44px; padding: 9px 16px; border: 1.5px solid var(--line); border-radius: 6px;
  background: var(--surface); cursor: pointer;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; min-height: 44px;
  padding: 9px 18px; border: 0; border-radius: 6px;
  background: var(--red); color: #fff; font-weight: 500; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--red-dark); }
.btn.secondary { background: transparent; color: var(--ink); border: 1.5px solid var(--line); }
.btn.secondary:hover { border-color: var(--ink); }
button.link { min-height: 0; border: 0; background: none; padding: 0; text-decoration: underline; }
form.inline { display: inline; }

.flash { padding: 11px 14px; border-radius: 6px; margin-bottom: 16px; background: var(--ok-bg); color: var(--ok-ink); }
.flash.error { background: var(--err-bg); color: var(--err-ink); }

/* Inventory filters */
.filters { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; margin-bottom: 14px; }
.filters .choices { display: flex; gap: 8px; }
.filters select { width: auto; }
.js .filters .apply { display: none; }
.clear { align-self: center; color: var(--muted); }

.summary { display: flex; align-items: baseline; gap: 16px; color: var(--muted); margin: 0 0 18px; }
.summary strong { color: var(--ink); font-weight: 500; }
.summary .export { margin-left: auto; }

/* The parts grid */
.lots {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 28px 20px;
}
.lot { min-width: 0; }
.callout {
  display: flex; align-items: center; justify-content: center; aspect-ratio: 5 / 4;
  background: var(--callout); border: 1.5px solid var(--callout-line); border-radius: 8px;
  padding: 10px; position: relative;
}
.callout img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  mix-blend-mode: multiply; /* Rebrickable images have white backgrounds */
}
.callout, .callout:hover { text-decoration: none; }
.noimg { color: #5e6f7d; font-size: 14px; }

.qty { display: flex; align-items: center; gap: 4px; margin: 8px 0 2px; }
.count { font-size: 26px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; margin-right: auto; }
.count small { font-size: 18px; font-weight: 600; margin-left: 1px; }
.qty button {
  width: 36px; min-height: 36px; height: 36px; padding: 0;
  border-radius: 50%; font-size: 20px; line-height: 1; color: var(--muted);
}
.qty button:hover { color: var(--ink); border-color: var(--ink); }
.lot-name { display: block; font-weight: 500; line-height: 1.3; text-decoration: none; }
.lot-name:hover { text-decoration: underline; }
.lot-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 4px; font-size: 14px; color: var(--muted); }
.lot-meta a { text-decoration: none; }
.lot-meta a:hover { text-decoration: underline; }
.place {
  display: inline-block; margin-top: 8px; padding: 1px 8px; border: 1.5px solid var(--line); border-radius: 4px;
  font-size: 14px; text-decoration: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.place:hover { border-color: var(--ink); }
.notes { font-size: 14px; color: var(--muted); margin-top: 6px; }

.swatch { display: inline-block; width: 13px; height: 13px; border-radius: 50%; border: 1px solid rgba(0, 0, 0, .3); vertical-align: -1px; flex: none; }
@media (prefers-color-scheme: dark) { .swatch { border-color: rgba(255, 255, 255, .4); } }
.swatch.big { width: 44px; height: 44px; border-radius: 6px; }
.swatch.trans { opacity: .55; }
.swatch.none { background: repeating-linear-gradient(45deg, var(--line) 0 3px, transparent 3px 6px); }

/* Gallery / list switch */
.views { display: flex; gap: 14px; margin-left: auto; }
.views a { color: var(--muted); text-decoration: none; }
.views a:hover { color: var(--ink); }
.views a[aria-current] { color: var(--ink); font-weight: 500; text-decoration: underline; text-underline-offset: 4px; text-decoration-thickness: 2px; }
.summary .views + .export { margin-left: 0; }

/* List view */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.inv { width: 100%; border-collapse: collapse; }
.inv th { text-align: left; font-weight: 500; font-size: 14px; color: var(--muted); padding: 0 12px 8px; border-bottom: 1.5px solid var(--line); }
.inv td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.inv th:first-child, .inv td:first-child { padding-left: 0; }
.inv th:last-child, .inv td:last-child { padding-right: 0; }
.inv .pic { width: 76px; }
.inv .callout { width: 64px; aspect-ratio: 5 / 4; padding: 4px; border-radius: 6px; }
.inv .pnum { font-size: 14px; color: var(--muted); text-decoration: none; }
.inv .pnum:hover { text-decoration: underline; }
.inv .col { white-space: nowrap; }
.inv .place { margin-top: 0; }
.inv .num { text-align: right; width: 1%; }
.inv .qty { margin: 0; gap: 6px; justify-content: flex-end; }
.inv .count { font-size: 20px; min-width: 2.6em; text-align: right; margin-right: 6px; }
.inv .count small { font-size: 15px; }

.empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.empty p { margin: 0 0 16px; }
.pager { display: flex; gap: 20px; justify-content: center; align-items: center; margin-top: 32px; }

/* Add / edit */
.editor {
  display: grid; gap: 24px 36px; align-items: start;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  grid-template-areas: "form preview" "form search";
}
.editor form { grid-area: form; }
.editor .preview { grid-area: preview; }
.editor .finder { grid-area: search; }
.editor label { display: block; font-weight: 500; margin: 18px 0 6px; }
.editor form > label:first-of-type { margin-top: 0; }
.preview .callout { aspect-ratio: auto; height: 240px; }
.preview .count { display: block; margin-top: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row button { flex: none; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.combo { position: relative; flex: 1; min-width: 0; }
.combo-list {
  position: absolute; z-index: 10; left: 0; right: 0; top: calc(100% + 4px);
  max-height: 300px; overflow-y: auto; margin: 0; padding: 4px 0; list-style: none;
  background: var(--surface); border: 1.5px solid var(--ink); border-radius: 6px;
}
.combo-list [role="option"] { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 8px 12px; cursor: pointer; }
.combo-list [role="option"]:hover, .combo-list .active { background: var(--line); }
.combo-list [aria-selected="true"] { font-weight: 500; }
.combo-list .swatch { width: 16px; height: 16px; border-radius: 4px; }
.combo-group { padding: 10px 12px 4px; font-size: 13px; color: var(--muted); }
.combo-empty { padding: 10px 12px; color: var(--muted); }

.hint { font-size: 14px; color: var(--muted); margin: 6px 0 0; }
.hint.error { color: var(--err-ink); }
.buttons { display: flex; gap: 10px; align-items: center; margin-top: 24px; flex-wrap: wrap; }
.buttons a { margin-left: 6px; color: var(--muted); }
.finder label { margin-top: 0; }
.results { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 10px; margin-top: 12px; max-height: 460px; overflow-y: auto; }
.result { display: flex; flex-direction: column; gap: 4px; padding: 0; border: 0; background: none; text-align: left; font-size: 13px; line-height: 1.3; }
.result img, .result .noimg {
  width: 100%; height: 72px; object-fit: contain; padding: 6px;
  background: var(--callout); border: 1.5px solid var(--callout-line); border-radius: 6px;
}
.result:hover img { border-color: var(--ink); }
.result small { color: var(--muted); }
.danger { margin-top: 28px; color: var(--err-ink); }

/* Sign in */
.login-page { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login { width: 100%; max-width: 320px; }
.login h1 { display: flex; align-items: center; gap: 10px; font-size: 22px; margin-bottom: 24px; }
.login label { display: block; font-weight: 500; margin: 14px 0 6px; }
.login .btn { width: 100%; margin-top: 22px; }

.foot {
  max-width: 1120px; margin: 0 auto; padding: 24px 16px calc(28px + var(--safe-bottom));
  display: flex; flex-wrap: wrap; gap: 8px 20px; font-size: 14px; color: var(--muted);
}
.foot .signout { margin-left: auto; }

/* Phones */
@media (max-width: 760px) {
  h1 { font-size: 22px; }
  .filters { grid-template-columns: 1fr; }
  .filters .choices { overflow-x: auto; margin: 0 -16px; padding: 0 16px; scrollbar-width: none; }
  .filters .choices::-webkit-scrollbar { display: none; }
  .filters select { flex: none; max-width: 70vw; }
  .lots { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px 14px; }
  .count { font-size: 22px; }
  .qty button { width: 40px; height: 40px; min-height: 40px; }
  .editor { grid-template-columns: 1fr; grid-template-areas: "preview" "form" "search"; }
  .preview { display: grid; grid-template-columns: 120px 1fr; gap: 0 14px; align-items: end; }
  .preview .callout { height: 96px; grid-row: 1 / span 2; }
  .preview .count { margin: 0 0 4px; }
  .preview #rb-link { grid-column: 2; }
  .buttons .btn { flex: 1 1 auto; }
  .combo-list { max-height: 45vh; }
  .foot .signout { margin-left: 0; }
  .summary { flex-wrap: wrap; row-gap: 6px; }
  .summary > span:first-child { flex-basis: 100%; }
  .views { margin-left: 0; }
  .summary .views + .export { margin-left: auto; }

  /* List rows become compact two-line entries */
  .inv thead { display: none; }
  .inv, .inv tbody { display: block; }
  .inv tr {
    display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 2px 12px; align-items: center;
    padding: 12px 0; border-bottom: 1px solid var(--line);
  }
  .inv td { display: block; padding: 0; border: 0; }
  .inv .pic { width: auto; grid-row: 1 / span 3; }
  .inv .part { grid-column: 2; }
  .inv .col { grid-column: 2; font-size: 14px; color: var(--muted); }
  .inv .loc { grid-column: 2; }
  .inv .num { grid-column: 3; grid-row: 1 / span 3; width: auto; }
  .inv .qty { flex-wrap: wrap; justify-content: center; width: 86px; gap: 6px; }
  .inv .qty .count { flex-basis: 100%; margin: 0; min-width: 0; text-align: center; font-size: 18px; }
  .inv .place { font-size: 13px; margin-top: 2px; }
}
