:root {
  color-scheme: dark;
  --bg: #0d1117;
  --panel: #141a22;
  --panel2: #1a222c;
  --edge: #2a3542;
  --ink: #e6ebf1;
  --muted: #8d9aaa;
  --accent: #e0a94a;
  --accent-ink: #1b1306;
  --focus: #6cb6ff;
  --error: #ff7b72;
  --ok: #7ee2a8;
  --radius: 10px;
  --check-a: #161c24;
  --check-b: #1d2530;
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { height: 100dvh; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 6px 16px; min-height: 56px;
  background: var(--panel); border-bottom: 1px solid var(--edge);
}
.topbar h1 { font-size: 17px; margin: 0; flex: 1; font-weight: 650; letter-spacing: .01em; }
.topbar .ver { color: var(--muted); font-weight: 400; font-size: 13px; }
.topbar a {
  color: var(--ink); text-decoration: none; display: inline-flex; align-items: center;
  min-height: 44px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--edge);
}
.topbar a:hover { background: var(--panel2); }

/* Layout */
.app {
  flex: 1 1 0; display: grid; grid-template-columns: minmax(0, 1fr) 410px;
  grid-template-rows: minmax(0, 1fr); min-height: 0;
}
.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 12px 16px; gap: 10px; }
.stage-bar { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; }
.readout { margin: 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.readout b { color: var(--ink); font-weight: 600; }
.view-opts { display: flex; flex-wrap: wrap; gap: 6px; }

.preview-wrap { flex: 1; min-height: 0; display: grid; place-items: center; container-type: size; }
.preview {
  position: relative; aspect-ratio: 1 / 1;
  width: min(100cqw, 100cqh); height: min(100cqw, 100cqh); max-width: 900px; min-width: 0;
  border-radius: 14px; border: 1px solid var(--edge); overflow: hidden;
  background-color: var(--check-a);
  background-image: conic-gradient(var(--check-b) 25%, transparent 0 50%, var(--check-b) 0 75%, transparent 0);
  background-size: 32px 32px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .35) inset;
}
.preview.light { --check-a: #e9ecf0; --check-b: #dde1e7; }
.preview.placing { cursor: crosshair; }
.preview .art, .preview .overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
.preview .art svg { width: 100%; height: 100%; display: block; }
.preview .overlay { pointer-events: none; }
.preview .error-msg { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; color: var(--error); text-align: center; }

/* Gallery */
.gallery-wrap { border-top: 1px solid var(--edge); padding-top: 8px; }
.gallery-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 10px; margin-bottom: 6px; }
.gallery-head h2 { font-size: 13px; margin: 0; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.gallery { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; }
.cand {
  flex: 0 0 auto; width: 104px; padding: 4px; border-radius: 10px;
  border: 1px solid var(--edge); background: var(--panel); color: var(--ink); cursor: pointer;
  display: flex; flex-direction: column; align-items: stretch; gap: 2px; font: inherit;
}
.cand .thumb {
  aspect-ratio: 1 / 1; border-radius: 7px;
  background-color: var(--check-a);
  background-image: conic-gradient(var(--check-b) 25%, transparent 0 50%, var(--check-b) 0 75%, transparent 0);
  background-size: 16px 16px;
}
.cand .thumb svg { width: 100%; height: 100%; display: block; }
.cand span { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cand:hover { border-color: #3d4b5c; }
.cand[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

/* Panel */
.panel {
  background: var(--panel); border-left: 1px solid var(--edge);
  overflow-y: auto; padding: 4px 0 24px;
}
.sec { border-bottom: 1px solid var(--edge); padding: 0 14px; }
.sec > summary {
  list-style: none; cursor: pointer; min-height: 44px; display: flex; align-items: center;
  font-weight: 650; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: #c5ced9;
}
.sec > summary::-webkit-details-marker { display: none; }
.sec > summary::after { content: "▾"; margin-left: auto; color: var(--muted); transition: transform .15s; }
.sec:not([open]) > summary::after { transform: rotate(-90deg); }
.sec > :last-child { margin-bottom: 12px; }
h3 { font-size: 13px; margin: 0; font-weight: 600; }
.muted, .hint { color: var(--muted); }
.hint { font-size: 12px; margin: 6px 0; }

fieldset { border: 0; margin: 0 0 8px; padding: 0; min-width: 0; }
legend { font-size: 12px; color: var(--muted); padding: 0; margin-bottom: 4px; }

button, select, input[type="number"], input[type="text"], .file-btn {
  font: inherit; color: var(--ink); background: var(--panel2);
  border: 1px solid var(--edge); border-radius: 8px; min-height: 44px;
}
button, .file-btn { padding: 0 14px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
button:hover, .file-btn:hover { border-color: #3d4b5c; background: #202a36; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }
button.primary:hover { background: #ebb85d; }
button:disabled { opacity: .45; cursor: not-allowed; }
select { padding: 0 10px; }
input[type="number"], input[type="text"] { padding: 0 8px; min-width: 0; }
.row:has(#importFile:focus-visible) .file-btn { outline: 2px solid var(--focus); outline-offset: 2px; }

.btns { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.btns.tight { margin: 0; }
.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.row > label:first-child { min-width: 82px; color: var(--muted); }
.row input[type="number"] { flex: 1; }
.seed-row input { font-variant-numeric: tabular-nums; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 12px;
  border: 1px solid var(--edge); border-radius: 22px; background: var(--panel2); cursor: pointer; user-select: none;
}
.chip input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.chip:has(input:checked) { border-color: var(--accent); }
.chip:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.locks { display: flex; flex-wrap: wrap; gap: 6px; }
.locks legend { width: 100%; }

/* Top export row and pinned weapon block */
.export-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.export-bar select { min-width: 92px; }
.sec.top { padding-top: 8px; padding-bottom: 4px; }
.row select { flex: 1; min-width: 0; }

/* Segmented radio buttons (types, style, material) */
.opt { position: relative; display: block; }
.opt input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.opt span {
  display: flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 8px;
  border: 1px solid var(--edge); border-radius: 8px; background: var(--panel2); text-align: center;
  font-size: 13px; line-height: 1.15;
}
.opt:hover span { border-color: #3d4b5c; }
.opt input:checked + span { background: #2a2317; border-color: var(--accent); color: #ffe1a6; font-weight: 600; }
.opt input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.seg { display: grid; grid-template-columns: repeat(auto-fit, minmax(68px, 1fr)); gap: 5px; }

/* Colours */
.colors { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin: 4px 0 12px; }
.swatch { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.swatch label { font-size: 12px; color: var(--muted); }
.swatch input[type="color"] {
  width: 100%; height: 44px; padding: 2px; border: 1px solid var(--edge); border-radius: 8px;
  background: var(--panel2); cursor: pointer;
}
.swatch input[type="text"] { min-height: 32px; font-size: 11px; font-family: ui-monospace, Consolas, monospace; width: 100%; padding: 0 4px; }

/* Sliders */
.sliders { display: grid; gap: 2px; margin-bottom: 8px; }
.ctl {
  display: grid; grid-template-columns: 112px minmax(0, 1fr) 66px 18px; align-items: center; gap: 8px;
  min-height: 44px;
}
.ctl label { font-size: 13px; color: #c5ced9; }
.ctl .num { min-height: 36px; width: 100%; font-variant-numeric: tabular-nums; text-align: right; padding: 0 6px; }
.ctl .unit { font-size: 11px; color: var(--muted); }
.ctl.disabled label { color: var(--muted); }
.ctl.disabled { opacity: .55; }
input[type="range"] { width: 100%; height: 44px; margin: 0; accent-color: var(--accent); background: transparent; cursor: pointer; }

/* Gems */
.gems { border-top: 1px dashed var(--edge); padding-top: 8px; }
.gems-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; }
.gem-list { display: grid; gap: 6px; }
.gem-row {
  display: grid; grid-template-columns: 22px repeat(3, minmax(0, 1fr)) 44px 44px; gap: 5px; align-items: end;
}
.gem-row .idx { color: var(--muted); font-size: 12px; align-self: center; text-align: center; }
.gem-row label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); gap: 2px; }
.gem-row input[type="number"] { width: 100%; }
.gem-row input[type="color"] { width: 44px; height: 44px; padding: 2px; border: 1px solid var(--edge); border-radius: 8px; background: var(--panel2); }
.gem-row button { padding: 0; width: 44px; }

/* Status */
.status { margin: 0; min-height: 20px; font-size: 13px; color: var(--ok); }
.status.error { color: var(--error); }

/* Responsive */
@media (max-width: 920px) {
  body { height: auto; min-height: 100dvh; overflow: visible; }
  .app { display: block; height: auto; flex: 0 0 auto; }
  .panel { border-left: 0; border-top: 1px solid var(--edge); overflow: visible; }
  .preview { width: min(100%, 72vh); min-width: 0; }
  .stage { position: sticky; top: 0; z-index: 2; background: var(--bg); border-bottom: 1px solid var(--edge); }
  .preview-wrap { container-type: normal; flex: 0 0 auto; }
  .preview { width: min(100%, 22vh); height: auto; }
  .readout { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .stage-bar { gap: 2px; }
  .view-opts .chip { min-height: 36px; padding: 0 8px; font-size: 12px; }
  .export-bar { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .export-bar > * { flex-shrink: 0; }
  .gallery-wrap { max-height: 112px; overflow: auto; }
  .gallery-head .hint { display: none; }
  .cand { width: 68px; }
}
@media (max-width: 480px) {
  .topbar { padding: 6px 10px; gap: 8px; }
  .topbar h1 { font-size: 15px; }
  .topbar .guide-link { display: none; }
  .stage { padding: 10px; }
  .sec { padding: 0 10px; }
  .ctl { grid-template-columns: 92px minmax(0, 1fr) 58px 14px; gap: 6px; }
  .colors { grid-template-columns: repeat(3, 1fr); }
  .gem-row { grid-template-columns: 18px repeat(3, minmax(0, 1fr)) 44px 44px; gap: 4px; }
}
