:root {
  --bg: #f7f3ec;
  --surface: #ffffff;
  --surface-2: #efe8dd;
  --text: #211b16;
  --muted: #6e6359;
  --line: #e2d8ca;
  --accent: #b8322a;
  --accent-text: #ffffff;
  --gold: #e3b52b;
  --ok: #2f7d4f;
  --radius: 12px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15110e;
    --surface: #211b17;
    --surface-2: #2d251f;
    --text: #f3ece3;
    --muted: #a8998b;
    --line: #3a3029;
    --accent: #e0574b;
    --accent-text: #1a0f0d;
    --gold: #f0c541;
    --ok: #5cc287;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.2; }
h2 { font-size: 1.05rem; margin: 1.6rem 0 .6rem; }
h3 { font-size: .95rem; margin: 1.2rem 0 .4rem; }
.muted { color: var(--muted); }
.error { color: var(--accent); min-height: 1em; margin: .4rem 0 0; }
.empty { color: var(--muted); padding: 2rem 0; text-align: center; grid-column: 1 / -1; }

button, input, select {
  font: inherit;
  color: inherit;
}
input, select {
  width: 100%;
  min-height: 44px;
  padding: .5rem .7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}
input[type="checkbox"] { width: 1.3rem; min-height: 0; height: 1.3rem; accent-color: var(--accent); }
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
label { display: grid; gap: .25rem; font-size: .85rem; color: var(--muted); }
label > input, label > select { color: var(--text); font-size: 1rem; }

button {
  min-height: 44px;
  padding: .5rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.ghost { background: transparent; }
.danger { color: var(--accent); }

/* login */
#login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login { width: min(340px, 100%); display: grid; gap: 1rem; text-align: center; }
.login img { margin: 0 auto; }
.login h1 { font-size: 1.3rem; margin: 0; }
.login label { text-align: left; }

/* header */
.top {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 1rem;
  align-items: center;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.who { display: flex; gap: .4rem; flex: 0 1 auto; min-width: 0; }
.who select { font-weight: 700; font-size: 1.05rem; border-color: transparent; background: var(--surface-2); max-width: 55vw; }
.who button { padding: 0 .9rem; font-size: 1.3rem; }
.overall { flex: 1; min-width: 0; }
.overall-nums { display: flex; justify-content: space-between; gap: .5rem; align-items: baseline; }
.overall-nums strong { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.overall-nums span { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--gold)); border-radius: inherit; transition: width .3s; }

.tabs { display: flex; gap: .3rem; padding: 10px 16px 0; max-width: 1200px; margin: 0 auto; }
.tabs button { flex: 1; border: none; background: transparent; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; border-radius: 0; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

.tab { padding: 12px 16px 96px; max-width: 1200px; margin: 0 auto; }

/* collection */
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.filters #q { grid-column: 1 / -1; }
@media (min-width: 760px) { .filters { grid-template-columns: 2fr 1fr 1fr 1fr 1fr; } .filters #q { grid-column: auto; } }
.shown { color: var(--muted); font-size: .85rem; margin: .7rem 0; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
@media (max-width: 480px) { .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; } }

.tile { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.tile .open { padding: 0; border: none; border-radius: 0; background: var(--surface-2); aspect-ratio: 5 / 7; display: block; width: 100%; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile:not(.owned) img, .tile:not(.owned) .noimg { filter: grayscale(1); opacity: .45; }
.tile.owned { border-color: var(--ok); }
.check {
  position: absolute; top: 6px; right: 6px;
  width: 40px; height: 40px; min-height: 0; padding: 0;
  border-radius: 50%; font-weight: 800; font-size: 1.1rem;
  background: color-mix(in srgb, var(--surface) 85%, transparent); color: var(--muted);
  border: 2px solid var(--line);
}
.check[aria-pressed="true"] { background: var(--ok); border-color: var(--ok); color: #fff; }
.meta { padding: .5rem .6rem .6rem; display: grid; gap: .15rem; font-size: .8rem; }
.meta b { font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta > span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badges { display: flex; gap: .3rem; flex-wrap: wrap; }
.badges i, i.lang { font-style: normal; font-size: .7rem; padding: .1rem .4rem; border-radius: 6px; background: var(--surface-2); color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
i.lang { background: var(--gold); color: #2a2000; font-weight: 700; }

.noimg { display: grid; place-content: center; text-align: center; height: 100%; padding: .5rem; color: var(--muted); font-weight: 600; gap: .2rem; }
.noimg small { font-weight: 400; font-size: .75rem; }
.noimg.small { width: 48px; height: 67px; background: var(--surface-2); border-radius: 4px; }

.fab {
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 4;
  border-radius: 99px; padding: .8rem 1.3rem; box-shadow: 0 6px 20px rgb(0 0 0 / .25);
}

/* progress */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 760px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; display: grid; }
.stat span { color: var(--muted); font-size: .8rem; }
.stat strong { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
table.progress { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; font-size: .9rem; }
.progress th, .progress td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); text-align: left; }
.progress thead th { font-size: .75rem; color: var(--muted); font-weight: 600; }
.progress tbody tr { cursor: pointer; }
.progress tbody tr:hover { background: var(--surface-2); }
.progress tbody th { font-weight: 600; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.barcell { width: 30%; }
@media (max-width: 480px) { .barcell { display: none; } .progress th, .progress td { padding: .5rem .4rem; } }
.missing { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.missing button { width: 100%; text-align: left; border: none; background: var(--surface); display: block; font-size: .9rem; }

/* scan */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.panel p { margin-top: 0; color: var(--muted); }
#scanStatus { display: block; margin-top: .5rem; }
.scan-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0; position: sticky; top: 72px; z-index: 3; background: var(--bg); padding: .4rem 0; }
.scan-actions .primary { margin-left: auto; }
.scan-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.scan-list label { display: flex; align-items: center; gap: .7rem; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .5rem .7rem; color: var(--text); font-size: .85rem; cursor: pointer; }
.scan-list img { width: 48px; border-radius: 4px; }
.scan-list label:has(:checked) { border-color: var(--accent); }

/* dialogs */
dialog {
  border: none; padding: 0; border-radius: 16px;
  background: var(--surface); color: var(--text);
  width: min(760px, calc(100% - 32px)); max-height: calc(100dvh - 32px);
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
dialog form { display: flex; flex-direction: column; max-height: calc(100dvh - 32px); }
@media (max-width: 600px) {
  dialog { width: 100%; max-width: 100%; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  dialog form { height: 100dvh; max-height: 100dvh; }
}
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: max(8px, env(safe-area-inset-top)) 16px 8px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; font-size: 1.1rem; }
.dlg-body { padding: 16px; overflow-y: auto; flex: 1; display: grid; gap: 16px; }
@media (min-width: 601px) { #entryDialog .dlg-body { grid-template-columns: 240px 1fr; } }
.dlg-foot { display: flex; gap: .5rem; padding: 12px 16px max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); flex-wrap: wrap; }
.dlg-foot .primary { margin-left: auto; min-width: 110px; }
.photo { display: grid; gap: .5rem; align-content: start; justify-items: center; }
#entryImg { width: min(240px, 70vw); aspect-ratio: 5 / 7; border-radius: 10px; overflow: hidden; background: var(--surface-2); }
#entryImg img { width: 100%; height: 100%; object-fit: contain; }
.upload { border: 1px dashed var(--line); border-radius: 10px; padding: .6rem 1rem; color: var(--text); cursor: pointer; display: block; }
.fields { display: grid; gap: .8rem; align-content: start; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.own { display: flex; align-items: center; gap: .6rem; font-size: 1rem; color: var(--text); font-weight: 600; padding: .6rem .8rem; background: var(--surface-2); border-radius: 10px; }
.owned-fields { display: grid; gap: .8rem; padding-left: .4rem; border-left: 3px solid var(--ok); }
.owned-fields.off { opacity: .45; border-left-color: var(--line); }
.owned-fields p { margin: 0; font-size: .85rem; }

#toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 10px; z-index: 20; max-width: calc(100% - 32px);
}

/* printable binder placeholders: 3×3 real card size (63×88mm) per A4 */
.shown-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; }
#print { display: none; }
@media print {
  @page { size: A4; margin: 8mm; }
  body { background: #fff; color: #000; padding: 0; }
  #app, #login, dialog, #toast { display: none !important; }
  #print { display: block; }
  .sheet { display: grid; grid-template-columns: repeat(3, 63mm); grid-auto-rows: 88mm; gap: 2mm; justify-content: center; break-after: page; }
  .sheet:last-child { break-after: auto; }
  .slot {
    position: relative; overflow: hidden; border: .3mm dashed #777; border-radius: 3mm;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2mm;
    padding: 6mm 4mm; text-align: center; font-family: system-ui, sans-serif;
  }
  .slot > * { position: relative; }
  .slot img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .15; filter: grayscale(1); }
  .pos { position: absolute; top: 2mm; left: 3mm; font-size: 6.5pt; color: #555; }
  .ph-variant { font-size: 15pt; font-weight: 800; text-transform: uppercase; line-height: 1.1; border: .6mm solid #000; border-radius: 2mm; padding: 1.5mm 3mm; background: #fff; }
  .ph-extra { font-size: 9pt; font-weight: 700; background: #fff; padding: .5mm 2mm; border-radius: 1mm; }
  .ph-card { display: grid; gap: .8mm; font-size: 8pt; background: #fff; padding: 1.5mm 2mm; border-radius: 1.5mm; margin-top: 2mm; }
  .ph-card b { font-size: 10pt; }
  .ph-lang { font-weight: 700; }
}

.scan-list li { display: flex; gap: .4rem; align-items: stretch; }
.scan-list li label { flex: 1; min-width: 0; }
.scan-list li > button { padding: 0 .7rem; color: var(--muted); }
h3 small { font-weight: 400; }
.hidden-list { margin-top: 1.5rem; }
.hidden-list summary { cursor: pointer; color: var(--muted); padding: .5rem 0; }

.shown-actions { display: flex; gap: .3rem; }
i.cameo { background: var(--accent); color: var(--accent-text); font-weight: 700; }
label.inline { display: flex; align-items: center; gap: .5rem; color: var(--text); font-size: .9rem; }
.row-search { display: flex; gap: .5rem; }
.cameo-pick { display: flex; align-items: center; gap: .7rem; width: 100%; text-align: left; padding: .5rem .7rem; font-size: .85rem; }
.cameo-pick img, .scan-list img { width: 48px; aspect-ratio: 5 / 7; object-fit: cover; border-radius: 4px; background: var(--surface-2); }
#cameoDialog .dlg-body { align-content: start; }

.shop { margin: 0; font-size: .9rem; color: var(--muted); }
.shop a { color: var(--accent); font-weight: 600; }

.tile .open, #entryImg { position: relative; }
.standin {
  position: absolute; left: 6px; bottom: 6px; max-width: calc(100% - 12px);
  font-size: .68rem; font-weight: 700; padding: .15rem .4rem; border-radius: 6px;
  background: rgb(0 0 0 / .7); color: #fff;
}
#entryImg .standin { font-weight: 600; font-size: .75rem; }
#findImages { margin-left: .3rem; }
