:root {
  --bg: #f3f1ec;
  --card: #ffffff;
  --ink: #1b1f2a;
  --muted: #6b7080;
  --line: #e3e0d8;
  --soft: #f6f4ef;
  --accent: #1f3a8a;
  --accent-ink: #ffffff;
  --accent-soft: #e5ebfb;
  --warn-bg: #fff6e0;
  --warn-ink: #7a5300;
  --err-bg: #fde8e8;
  --err-ink: #9b1c1c;
  --stage: #f8f6f1;
  --shadow: 0 1px 2px rgba(20, 24, 40, .05), 0 6px 20px rgba(20, 24, 40, .06);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141a;
    --card: #1b1e26;
    --ink: #eceef3;
    --muted: #9aa0b0;
    --line: #2c303b;
    --soft: #232731;
    --accent: #6d8ef5;
    --accent-ink: #0d1020;
    --accent-soft: #232c4a;
    --warn-bg: #3a2f12;
    --warn-ink: #f3d38a;
    --err-bg: #3d1b1b;
    --err-ink: #f7b4b4;
    --stage: #eeebe4;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }

.top { display: flex; align-items: center; gap: 12px; max-width: 1240px; margin: 0 auto; padding: 18px 20px 6px; }
.mark { border-radius: 9px; }
.top h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
.sub { margin: 0; color: var(--muted); font-size: 13px; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 420px) minmax(0, 1fr);
  gap: 18px;
  max-width: 1240px;
  margin: 0 auto;
  padding: 12px 20px 40px;
  align-items: start;
}
.output { position: sticky; top: 12px; }
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 10px 16px 32px; }
  .output { position: static; }
}

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h2 { font-size: 15px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.step { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 700; }

/* picker */
.picker { display: block; border: 2px dashed var(--line); border-radius: 12px; cursor: pointer; transition: border-color .15s, background .15s; }
.picker:hover { border-color: var(--accent); }
.picker input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.picker-empty { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 26px 12px; color: var(--muted); text-align: center; }
.picker-empty strong { color: var(--accent); font-size: 17px; }
.picker-full { display: flex; align-items: center; gap: 14px; padding: 10px; }
.picker-full img { width: 84px; height: 84px; object-fit: contain; border-radius: 8px; background: repeating-conic-gradient(#e9e9e9 0 25%, #fff 0 50%) 0 0/14px 14px; }
.picker-meta { display: flex; flex-direction: column; min-width: 0; }
.picker-meta strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-meta small { color: var(--muted); }

/* segmented control */
.seg { display: inline-flex; background: var(--soft); border: 1px solid var(--line); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; font-size: 14px; color: var(--muted); min-height: 34px; }
.seg button.on { background: var(--card); color: var(--ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.small button { padding: 4px 12px; min-height: 30px; }

/* size */
.dims { display: flex; align-items: flex-end; gap: 8px; }
.field { flex: 1; min-width: 0; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.num { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--soft); }
.num input { flex: 1; min-width: 0; width: 100%; border: 0; background: transparent; text-align: center; font-size: 18px; font-weight: 600; padding: 8px 0; -moz-appearance: textfield; }
.num input::-webkit-outer-spin-button, .num input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.num input:focus { outline: none; }
.num:focus-within { border-color: var(--accent); }
.nudge { border: 0; background: transparent; width: 40px; font-size: 20px; color: var(--accent); }
.nudge:active { background: var(--accent-soft); }
.times { color: var(--muted); padding-bottom: 10px; }
.hoops { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.lbl { font-size: 12px; color: var(--muted); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 5px 11px; font-size: 13px; min-height: 32px; }
.chip:active, .chip.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.hint { margin: 10px 0 0; font-size: 13px; color: var(--muted); min-height: 1em; }

/* palette */
.select select { border: 1px solid var(--line); background: var(--soft); border-radius: 10px; padding: 6px 10px; min-height: 34px; }
.palette .empty { color: var(--muted); margin: 0; font-size: 14px; }
.pal-row { display: grid; grid-template-columns: 36px 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.pal-row:first-child { border-top: 0; }
.swatch { width: 36px; height: 36px; border-radius: 9px; border: 1px solid rgba(0,0,0,.15); position: relative; }
.swatch i { position: absolute; right: -4px; bottom: -4px; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--card); }
.pal-name { min-width: 0; }
.pal-name strong { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pal-name small { color: var(--muted); font-size: 12px; }
.pal-row.off .swatch { opacity: .35; }
.pal-row.bg .pal-name small { color: var(--accent); }
.pal-row.off .pal-name strong { color: var(--muted); text-decoration: line-through; }
.pal-row .seg button { padding: 5px 9px; font-size: 13px; min-height: 30px; }
.quant { margin-top: 10px; display: flex; gap: 10px; align-items: center; font-size: 12px; color: var(--muted); }
.quant img { width: 64px; height: 64px; object-fit: contain; border-radius: 6px; border: 1px solid var(--line); background: #ebebeb; }

.more { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.more summary { cursor: pointer; font-size: 14px; color: var(--accent); font-weight: 600; padding: 4px 0; }
.setting { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-top: 10px; font-size: 14px; }
.setting small { color: var(--muted); }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }

/* result */
.stage { position: relative; background: var(--stage); border-radius: 12px; min-height: 280px; display: grid; place-items: center; overflow: hidden; }
.stage img { display: block; max-width: 100%; max-height: 62vh; }
.placeholder { text-align: center; color: #9a9686; padding: 30px; }
.placeholder p { margin: 8px 0 0; font-size: 14px; }
.busy { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center; background: rgba(248, 246, 241, .78); color: #4b4f5c; font-size: 14px; font-weight: 600; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #d5d1c6; border-top-color: #1f3a8a; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error { margin-top: 12px; background: var(--err-bg); color: var(--err-ink); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.warnings { list-style: none; margin: 12px 0 0; padding: 0; }
.warnings li { background: var(--warn-bg); color: var(--warn-ink); border-radius: 10px; padding: 9px 12px; font-size: 14px; margin-top: 6px; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.stats > div { background: var(--soft); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; min-width: 0; }
.stats span { font-size: 12px; color: var(--muted); }
.stats strong { font-size: 18px; white-space: nowrap; }
.stats small { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 560px) { .stats { grid-template-columns: repeat(2, 1fr); } }

.threads h3 { font-size: 14px; margin: 16px 0 6px; }
.threads ol { list-style: none; margin: 0; padding: 0; counter-reset: t; }
.threads li { counter-increment: t; display: flex; align-items: center; gap: 10px; padding: 6px 0; font-size: 14px; border-top: 1px solid var(--line); }
.threads li:first-child { border-top: 0; }
.threads li::before { content: counter(t); width: 22px; text-align: center; color: var(--muted); font-size: 13px; font-weight: 600; }
.threads .dot { width: 20px; height: 20px; border-radius: 50%; border: 1px solid rgba(0,0,0,.18); flex: none; }
.threads .tn { flex: 1; }
.threads .tc { color: var(--muted); font-size: 13px; }

.actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.btn { flex: 1 1 200px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 50px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font-weight: 600; text-decoration: none; font-size: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn:active { transform: scale(.99); }
.fineprint { margin: 12px 0 0; font-size: 12px; color: var(--muted); text-align: center; }
[hidden] { display: none !important; }
