:root {
  --bg: #f6f3ee;
  --panel: #fffdf9;
  --ink: #2a2622;
  --muted: #7b7268;
  --line: #e2dbd0;
  --accent: #9a6b2f;
  --accent-ink: #fff;
  --chip: #ece5da;
  --sel: #2f6fdf;
  --row: 190px;
  --ai: #7a4fc9;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a18;
    --panel: #252220;
    --ink: #eee8df;
    --muted: #a39a8e;
    --line: #3a3531;
    --accent: #d2a463;
    --accent-ink: #1c1a18;
    --chip: #34302b;
    --sel: #6aa0ff;
    --ai: #9d78e6;
    color-scheme: dark;
  }
}
@media (max-width: 700px) { :root { --row: 120px; } }

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.top { position: sticky; top: 0; z-index: 5; background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px 8px; }
.title-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px; }
h1 { font-size: 22px; margin: 0; font-weight: 600; letter-spacing: .01em; }
.show { display: flex; align-items: center; gap: 8px; }
.label, .muted { color: var(--muted); }
.admin-tools { margin-left: auto; display: flex; gap: 8px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 5px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); }
.seg button:disabled { opacity: .35; cursor: default; }
.seg.small button { padding: 2px 9px; font-size: 13px; }
.seg button.ai { color: var(--ai); font-weight: 600; }
.seg button.ai[aria-checked="true"] { background: var(--ai); color: #fff; }

.btn { border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); border-radius: 8px; padding: 5px 12px; cursor: pointer; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.on { background: var(--sel); border-color: var(--sel); color: #fff; }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 2px 4px; text-decoration: underline; }
.icon { border: 0; background: none; font-size: 20px; cursor: pointer; padding: 4px 8px; color: inherit; }

.filter { margin-top: 8px; }
.filter-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
#tagSearch { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 4px 10px; width: 200px; max-width: 100%; }
.count { color: var(--muted); margin-left: auto; font-size: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; max-height: 30vh; overflow-y: auto; }
.chip { border: 1px solid transparent; background: var(--chip); border-radius: 999px; padding: 3px 11px; cursor: pointer; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.chip .n { color: var(--muted); font-size: 12px; }
.chip.on { background: var(--accent); color: var(--accent-ink); }
.chip.on .n { color: inherit; opacity: .75; }
.chip.special { background: transparent; border-color: var(--line); font-style: italic; }
.chip .x { border: 0; background: none; padding: 0 0 0 2px; cursor: pointer; font-size: 14px; line-height: 1; opacity: .7; }
.chip .x:hover { opacity: 1; }

/* Justified rows: every cell grows in proportion to its aspect ratio, so a row shares one height. */
.grid { display: flex; flex-wrap: wrap; gap: 4px; padding: 8px 16px 90px; }
.grid::after { content: ""; flex-grow: 1e6; }
.cell { position: relative; flex-grow: calc(var(--w) / var(--h)); width: calc(var(--row) * var(--w) / var(--h)); aspect-ratio: var(--w) / var(--h); background: var(--chip); border-radius: 3px; overflow: hidden; cursor: zoom-in; padding: 0; border: 0; }
.cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cell .badge { position: absolute; right: 6px; bottom: 6px; background: var(--ai); color: #fff; border-radius: 4px; font-size: 11px; font-weight: 600; padding: 1px 6px; pointer-events: none; box-shadow: 0 0 3px rgba(0,0,0,.4); }
body.admin .cell { cursor: pointer; }
body.admin .cell::before { content: ""; position: absolute; top: 6px; left: 6px; width: 20px; height: 20px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.25); box-shadow: 0 0 2px rgba(0,0,0,.6); }
body.admin .cell.sel { outline: 3px solid var(--sel); outline-offset: -3px; }
body.admin .cell.sel::before { background: var(--sel); border-color: #fff; }
.cell .open { position: absolute; top: 4px; right: 4px; display: none; border: 0; border-radius: 6px; background: rgba(0,0,0,.55); color: #fff; padding: 2px 7px; cursor: zoom-in; font-size: 14px; }
body.admin .cell .open { display: block; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

.bulk { position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 6; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.18); padding: 8px 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; width: max-content; max-width: calc(100vw - 32px); }
.bulk .sep { width: 1px; height: 22px; background: var(--line); }
.bulk input, .bulk select { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 4px 8px; }
.bulk input { width: 170px; }
.bulk select { max-width: 200px; }

.lb { position: fixed; inset: 0; z-index: 10; background: #111; color: #eee; display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: minmax(0, 1fr); }
.lb .muted { color: #aaa; }
.lb-top { display: flex; align-items: center; gap: 14px; padding: 6px 10px 6px 16px; }
.lb-top .name { font-size: 13px; }
.lb-top .icon { margin-left: auto; color: #eee; }
.stage { position: relative; min-height: 0; overflow: hidden; touch-action: pan-y; user-select: none; }
.stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.stage .over { pointer-events: none; }
.divider { position: absolute; top: 0; bottom: 0; width: 0; border-left: 2px solid rgba(255,255,255,.9); cursor: ew-resize; }
.divider span { position: absolute; top: 50%; left: -16px; width: 30px; height: 30px; margin-top: -15px; border-radius: 50%; background: #fff; box-shadow: 0 0 6px rgba(0,0,0,.5); }
.divider span::before { content: "‹ ›"; color: #333; position: absolute; inset: 0; text-align: center; line-height: 30px; font-size: 15px; }
.stage.compare { cursor: ew-resize; }
.nav { position: absolute; top: 50%; transform: translateY(-50%); border: 0; background: rgba(0,0,0,.35); color: #fff; font-size: 34px; width: 46px; height: 70px; cursor: pointer; border-radius: 8px; z-index: 2; }
.nav.prev { left: 8px; }
.nav.next { right: 8px; }
.lb-bottom { padding: 8px 16px 14px; display: flex; flex-wrap: wrap; align-items: flex-start; gap: 10px 24px; }
.lb .seg { border-color: #444; flex-wrap: wrap; max-width: 100%; }
.lb .seg button + button { border-left-color: #444; }
.lb .seg button[aria-checked="true"] { background: #d2a463; color: #111; }
.lb .seg button.ai { color: #b99cf0; }
.lb .seg button.ai[aria-checked="true"] { background: #9d78e6; color: #fff; }
.lb-variants { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; max-width: 100%; }
.ai-note { margin: 0; font-size: 13px; color: #c7b3f2; }
.lb-tags { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; flex: 1; min-width: 240px; }
.lb-tags .chips { margin-top: 0; max-height: none; }
.lb .chip { background: #2c2c2c; color: #eee; }
.add-tag { display: flex; gap: 6px; }
.add-tag input { border: 1px solid #444; background: #1d1d1d; border-radius: 8px; padding: 4px 8px; width: 180px; }

.dlg { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); padding: 14px 18px; width: min(560px, calc(100vw - 32px)); max-height: 80vh; }
.dlg::backdrop { background: rgba(0,0,0,.4); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; }
.dlg h2 { margin: 0; font-size: 18px; }
#tagTable { width: 100%; border-collapse: collapse; }
#tagTable td { padding: 4px 4px; border-top: 1px solid var(--line); }
#tagTable td.n { color: var(--muted); text-align: right; width: 3em; }
#tagTable input { width: 100%; border: 1px solid transparent; background: transparent; border-radius: 6px; padding: 3px 6px; }
#tagTable input:focus { border-color: var(--line); background: var(--bg); }
#tagTable td.act { white-space: nowrap; width: 1%; }

.toast { position: fixed; left: 50%; top: 16px; transform: translateX(-50%); z-index: 20; background: #333; color: #fff; padding: 8px 14px; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.3); }
.toast.err { background: #a3322a; }

@media (max-width: 700px) {
  .top { padding: 8px 12px 6px; position: static; }
  .lb-top .name { display: none; }
  .lb-bottom { padding: 8px 10px 12px; }
  .grid { padding: 6px 8px 110px; gap: 3px; }
  h1 { font-size: 19px; }
  .admin-tools { margin-left: 0; }
  .nav { display: none; }
  .bulk { bottom: 8px; }
}
