:root {
  --bg: #f3f5fa;
  --panel: #ffffff;
  --ink: #18202e;
  --muted: #667085;
  --line: #dde2ec;
  --brand: #1f4fd1;
  --brand-ink: #ffffff;
  --warn: #b54708;
  --warn-bg: #fff6e8;
  --ok: #067647;
  --radius: 12px;
  color-scheme: light;
}
/* Dark theme: chosen explicitly, or "system" resolved by js/theme.js */
html[data-theme="dark"] { --bg: #0f131a; --panel: #171c25; --ink: #e8ecf4; --muted: #98a2b3; --line: #2a3140; --brand: #4f7dff; --warn: #fdb022; --warn-bg: #2b2112; --ok: #47cd89; color-scheme: dark; }
/* Fallback before/without JavaScript */
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) { --bg: #0f131a; --panel: #171c25; --ink: #e8ecf4; --muted: #98a2b3; --line: #2a3140; --brand: #4f7dff; --warn: #fdb022; --warn-bg: #2b2112; --ok: #47cd89; color-scheme: dark; }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; margin-bottom: 12px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 20px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; gap: 10px; align-items: center; color: inherit; text-decoration: none; }
.foot a { color: inherit; }
.brand h1 { font-size: 1.15rem; }
.brand p { margin: 0; font-size: .8rem; color: var(--muted); }
.top-actions { display: flex; gap: 8px; }

.layout {
  display: grid; gap: 16px; padding: 16px;
  grid-template-columns: minmax(320px, 460px) 1fr;
  max-width: 1280px; margin: 0 auto; align-items: start;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; padding: 10px; } }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.preview-panel { position: sticky; top: 76px; }
@media (max-width: 860px) { .preview-panel { position: static; } }
.section + .section { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }

label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); margin-bottom: 10px; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--ink); }
input[type=number], select {
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 0; width: 100%;
}
input[type=range] { width: 100%; }
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.row.end { justify-content: flex-end; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 18px; }
.hint, .muted { color: var(--muted); font-size: .85rem; }
.hint { margin: 4px 0 10px; }

.btn {
  font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 9px 14px; border-radius: 9px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  margin: 0; flex-direction: row;
}
.btn:hover { border-color: var(--brand); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 9px; font-size: .8rem; border-radius: 7px; }
.btn.wide { width: 100%; margin-top: 4px; border-style: dashed; }

/* ---- items */
.item { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; background: var(--bg); }
.item-head { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.item-head select { font-weight: 600; background: var(--panel); }
.dims { display: grid; grid-template-columns: 1fr auto 1fr 76px auto; gap: 6px; align-items: end; }
.dims .x { padding-bottom: 18px; color: var(--muted); }
.dims .btn { margin-bottom: 10px; height: 38px; }
.dims input, .dims select, .item-opts input, .item-opts select { background: var(--panel); }
.item-opts { display: flex; flex-wrap: wrap; gap: 0 14px; align-items: end; margin-top: 10px; }
.item-opts label:not(.check) { width: 130px; }
.item-opts label.check { margin-bottom: 18px; }
.fill-info { margin: 0; font-size: .82rem; color: var(--muted); }
.fill-info b { color: var(--ink); }
.linkish { font: inherit; background: none; border: none; padding: 0; color: var(--brand); text-decoration: underline; cursor: pointer; }

.slots { display: grid; gap: 10px; }
.slots.two { grid-template-columns: 1fr 1fr; }
@media (max-width: 420px) { .slots.two { grid-template-columns: 1fr; } }
.slot { border-radius: 10px; min-width: 0; }
.slot.empty { border: 2px dashed var(--line); background: var(--panel); max-height: 220px; }
.slot.empty.drag, .slot.drag { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 8%, var(--panel)); }
.slot-drop { height: 100%; margin: 0; cursor: pointer; align-items: center; justify-content: center; text-align: center; color: var(--ink); padding: 8px; }
.slot-drop small { color: var(--muted); font-size: .75rem; }
.slot-icon { font-size: 1.6rem; color: var(--brand); line-height: 1; }
.slot.filled img { width: 100%; display: block; border-radius: 8px; object-fit: fill; box-shadow: 0 1px 4px rgba(0,0,0,.2); background: #fff; }
.slot-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px; margin-top: 6px; font-size: .8rem; }
.slot-btns { display: flex; gap: 4px; }
.slot-btns label.btn { margin: 0; color: var(--ink); }
.warn { color: var(--warn); }
.ok { color: var(--ok); }
p.small { font-size: .75rem; margin: 4px 0 0; }

details.advanced { margin-top: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
details.advanced summary { cursor: pointer; font-weight: 600; font-size: .9rem; }

/* ---- preview */
.preview-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.preview {
  display: flex; flex-direction: column; align-items: center; gap: 18px;
  background: color-mix(in srgb, var(--ink) 6%, var(--bg)); border-radius: 10px; padding: 18px 4px;
  max-height: 70vh; overflow: auto;
}
.sheet-holder { position: relative; flex: none; box-shadow: 0 2px 10px rgba(0,0,0,.18); background: #fff; }
.sheet-holder .sheet { transform-origin: 0 0; position: absolute; left: 0; top: 0; }
.page-no { position: absolute; right: 6px; bottom: 4px; font-size: .7rem; color: #888; }
.sheet { position: relative; background: #fff; overflow: hidden; }
.card { position: absolute; overflow: hidden; }
.card img { width: 100%; height: 100%; display: block; object-fit: fill; }
.card.bordered { background: #fff; }
.card.bordered img { position: absolute; object-fit: cover; }
.item-opts label.wide { width: 100%; }
.card.placeholder {
  background: repeating-linear-gradient(45deg, #eef1f7, #eef1f7 2mm, #e3e8f1 2mm, #e3e8f1 4mm);
  color: #8792a8; font: 3mm system-ui, sans-serif; display: flex; align-items: center; justify-content: center;
}
.cut { position: absolute; border-style: dashed; border-color: #8a8a8a; pointer-events: none; }
.warning { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 8px 10px; margin: 0 0 10px; font-size: .85rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.actions .btn.primary { flex: 1 1 140px; font-size: 1rem; }
.tip { font-size: .8rem; color: var(--muted); margin: 10px 0 0; }

.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 6px 16px max(18px, env(safe-area-inset-bottom)); }

/* ---- dialogs */
dialog { border: none; border-radius: 14px; padding: 0; background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(10, 14, 22, .6); }
.help { padding: 20px 22px; max-width: 520px; }
.help ol { padding-left: 20px; }
.help li { margin-bottom: 6px; }

.cropper { width: min(1200px, 100vw); height: min(860px, 100dvh); max-width: 100vw; max-height: 100dvh; }
.crop-wrap { display: flex; flex-direction: column; height: 100%; }
.crop-head { padding: 12px 16px 0; }
.crop-head .hint { margin: 4px 0 8px; }
.crop-body { flex: 1; display: grid; grid-template-columns: 1fr 260px; min-height: 0; }
.crop-stage { position: relative; min-height: 0; min-width: 0; background: #1b1d22; overflow: hidden; }
#crop-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.lab { display: flex; justify-content: space-between; }
.lab output { font-variant-numeric: tabular-nums; color: var(--ink); }
.slot.filled img { cursor: pointer; }
.crop-side { padding: 12px 14px; overflow: auto; border-left: 1px solid var(--line); }
.crop-preview-box { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
#crop-preview { width: 100%; height: auto; border-radius: 6px; box-shadow: 0 1px 4px rgba(0,0,0,.25); background: #fff; }
.crop-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 16px max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
@media (max-width: 760px) {
  .cropper { width: 100vw; height: 100dvh; border-radius: 0; }
  .crop-body { grid-template-columns: 1fr; grid-template-rows: minmax(200px, 1fr) auto; }
  .crop-side { border-left: none; border-top: 1px solid var(--line); max-height: 40dvh; display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; align-content: start; }
  .crop-side .crop-preview-box { grid-row: span 3; }
  .crop-side .row { margin: 4px 0; }
  .crop-side .btn { padding: 7px 9px; font-size: .8rem; }
  .crop-head { padding: 8px 12px 0; }
  .crop-head h3 { font-size: 1rem; }
  .crop-head .hint { font-size: .75rem; margin: 2px 0 6px; }
  .crop-side label { margin-bottom: 6px; }
}

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0;
  background: #1d2433; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  transition: .25s; pointer-events: none; max-width: min(520px, 92vw); z-index: 50; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- calibration sheet */
.calib > div { position: absolute; }
.calib-title { font: 4mm system-ui, sans-serif; color: #000; }
.calib-h { height: 0; border-top: 0.3mm solid #000; }
.calib-vline { width: 0; border-left: 0.3mm solid #000; }
.tick-v { width: 0; border-left: 0.25mm solid #000; }
.tick-h { height: 0; border-top: 0.25mm solid #000; }
.tick-lab { font: 2.6mm system-ui, sans-serif; width: 4mm; text-align: center; color: #000; }
.calib-label { font: 3mm system-ui, sans-serif; color: #000; }
.calib-card { border: 0.3mm dashed #000; border-radius: 3.18mm; font: 3mm system-ui, sans-serif; color: #000; display: flex; align-items: center; justify-content: center; text-align: center; padding: 4mm; box-sizing: border-box; }

/* ---- print */
#print-root { display: none; }
@media print {
  html, body { background: #fff !important; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root .sheet { break-after: page; page-break-after: always; }
  #print-root .sheet:last-child { break-after: auto; page-break-after: auto; }
  .sheet, .card, .card img, .cut { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* theme switcher */
.theme-btn { min-width: 40px; }
.theme-btn .t-ico { font-size: 1rem; line-height: 1; }
@media (max-width: 480px) { .theme-btn .t-lab { display: none; } }
