@font-face {
  font-family: 'Archivo Black';
  src: url('../fonts/archivo-black.woff') format('woff');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'DM Sans';
  src: url('../fonts/dm-sans.woff') format('woff');
  font-weight: 100 1000;
  font-display: swap;
}

:root {
  --desk: #e4eaf5;
  --grid: #d3dcee;
  --ink: #1d2b53;
  --ink-soft: #56638a;
  --line: #9aa7c6;        /* slider tracks, progress */
  --line-strong: #7d8bb0; /* control borders, 3.4:1 on white */
  --paper: #fff;
  --crayon: #ffd23f;
  --crayon-deep: #f2b705;
  --focus: #3b6fe0;
  --display: 'Archivo Black', 'Arial Black', system-ui, sans-serif;
  --body: 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --step--1: 0.875rem;
  --step-0: 1rem;
  --step-1: 1.25rem;
  --step-2: 1.6rem;
  --step-3: 2.1rem;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--body);
  font-size: var(--step-0);
  line-height: 1.5;
  color: var(--ink);
  background-color: var(--desk);
  /* squared exercise-book paper: the maze grid's home */
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  background-position: -1px -1px;
  min-height: 100vh;
}
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

/* ---------- header ---------- */
.top {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.25rem 1.25rem;
  padding: 1.25rem clamp(1rem, 3vw, 2.5rem) 0.75rem;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
  color: var(--ink);
  text-decoration: none;
  align-self: center;
}
.brand-mark { width: 40px; height: 40px; color: var(--ink); flex: none; }
.brand-name { font-family: var(--display); font-size: var(--step-2); letter-spacing: -0.01em; line-height: 1; }
.tagline { margin: 0; color: var(--ink-soft); font-size: var(--step-0); }

/* shown only when index.html is opened as a file */
.file-warning {
  margin: 0.5rem clamp(1rem, 3vw, 2.5rem) 1rem;
  padding: 1rem 1.25rem;
  max-width: 72ch;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 14px;
  box-shadow: 4px 4px 0 var(--ink);
}
.file-warning p { margin: 0 0 0.5rem; }
.file-warning p:last-child { margin: 0; }
.file-warning code { background: var(--desk); padding: 0.05rem 0.35rem; border-radius: 4px; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(300px, 360px) 1fr;
  gap: clamp(1rem, 2.5vw, 2rem);
  padding: 0.5rem clamp(1rem, 3vw, 2.5rem) 3rem;
  align-items: start;
}

/* ---------- settings panel ---------- */
.panel {
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 18px;
  padding: 1.4rem 1.3rem 1.2rem;
  box-shadow: 6px 6px 0 var(--ink);
}
.panel-title { font-family: var(--display); font-weight: 400; font-size: var(--step-1); line-height: 1.2; margin: 0 0 1.1rem; }
.field { border: 0; padding: 0; margin: 0 0 1.4rem; min-width: 0; }
.field legend { font-weight: 700; padding: 0; margin-bottom: 0.55rem; }
.hint { color: #b42318; font-size: var(--step--1); margin: 0.4rem 0 0; }

.shape-tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.tile input, .chip input, .check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.tile-face {
  display: grid;
  justify-items: center;
  gap: 0.25rem;
  padding: 0.65rem 0.3rem 0.5rem;
  border: 2px solid var(--line-strong);
  border-radius: 12px;
  font-size: var(--step--1);
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
}
.tile-face svg { width: 40px; height: 40px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.tile input:checked + .tile-face { border-color: var(--ink); background: var(--ink); color: #fff; }
.tile input:focus-visible + .tile-face, .chip input:focus-visible + .chip-face { outline: 3px solid var(--focus); outline-offset: 2px; }

/* dual range: two native sliders stacked on one track */
.dual-range {
  --from: 0%; --to: 100%;
  position: relative;
  height: 28px;
}
.dual-range::before, .dual-range-fill {
  content: '';
  position: absolute;
  top: 11px; height: 6px;
  border-radius: 3px;
}
.dual-range::before { left: 11px; right: 11px; background: var(--line); }
.dual-range-fill {
  left: calc(11px + (100% - 22px) * var(--from) / 100%);
  width: calc((100% - 22px) * (var(--to) - var(--from)) / 100%);
  background: var(--ink);
}
.dual-range input {
  position: absolute; inset: 0;
  width: 100%; margin: 0;
  background: none;
  pointer-events: none;
  -webkit-appearance: none; appearance: none;
  z-index: 1;
}
.dual-range input::-webkit-slider-runnable-track { background: none; height: 28px; }
.dual-range input::-moz-range-track { background: none; }
.dual-range input::-webkit-slider-thumb {
  -webkit-appearance: none;
  pointer-events: auto;
  width: 22px; height: 22px; margin-top: 3px;
  border-radius: 50%;
  background: var(--crayon);
  border: 3px solid var(--ink);
  cursor: grab;
}
.dual-range input::-moz-range-thumb {
  pointer-events: auto;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--crayon);
  border: 3px solid var(--ink);
  cursor: grab;
}
.dual-range input.on-top { z-index: 2; }
.dual-range input:focus-visible { outline: none; }
.dual-range input:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--focus); }
.dual-range input:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--focus); }

.level-scale {
  display: flex; justify-content: space-between;
  list-style: none; margin: 0.15rem 0 0; padding: 0 5px;
  font-size: 0.75rem; color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}
.level-scale li { width: 12px; text-align: center; }
.level-scale li.in { color: var(--ink); font-weight: 700; }
.readout { margin: 0.4rem 0 0; font-size: var(--step--1); color: var(--ink-soft); }

.count-row { display: flex; align-items: center; gap: 0.75rem; }
.count-row input[type=range] {
  --pct: 0%;
  flex: 1; height: 28px; margin: 0;
  background: none;
  -webkit-appearance: none; appearance: none;
}
.count-row input[type=range]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(90deg, var(--ink) var(--pct), var(--line) var(--pct));
}
.count-row input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
.count-row input[type=range]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--ink); }
.count-row input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px; height: 22px; margin-top: -8px;
  border-radius: 50%; background: var(--crayon); border: 3px solid var(--ink); cursor: grab;
}
.count-row input[type=range]::-moz-range-thumb {
  width: 16px; height: 16px;
  border-radius: 50%; background: var(--crayon); border: 3px solid var(--ink); cursor: grab;
}
.count-row input[type=range]:focus-visible { outline: none; }
.count-row input[type=range]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--focus); }
.count-row input[type=range]:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--focus); }
.count-row input[type=number] {
  width: 4.2rem;
  min-height: 44px;
  padding: 0.35rem 0.5rem;
  border: 2px solid var(--line-strong);
  border-radius: 10px;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.count-row input[type=number]:focus { border-color: var(--ink); }

.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip-face {
  display: inline-flex; align-items: center; gap: 0.35rem;
  min-height: 44px;
  padding: 0.2rem 0.85rem 0.2rem 0.35rem;
  border: 2px solid var(--line-strong);
  border-radius: 999px;
  font-size: var(--step--1); font-weight: 600;
  cursor: pointer;
  background: #fff;
}
.chip-face img { width: 28px; height: 28px; border-radius: 50%; background: #fff; }
.chip input:checked + .chip-face { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.chip input:not(:checked) + .chip-face { color: var(--ink-soft); }
.chip input:not(:checked) + .chip-face img { opacity: 0.35; }

.btn-make {
  width: 100%;
  padding: 0.85rem 1rem;
  border: 2px solid var(--ink);
  border-radius: 14px;
  background: var(--crayon);
  font-family: var(--display);
  font-size: var(--step-1);
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
  transition: transform .08s, box-shadow .08s;
}
.btn-make:hover { background: var(--crayon-deep); }
.btn-make:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn-make:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-make.is-stale { box-shadow: 0 4px 0 var(--ink), 0 0 0 4px #fff, 0 0 0 6px var(--ink); }
.stale { margin: 0 0 0.6rem; font-size: var(--step--1); font-weight: 600; text-align: center; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.privacy { margin: 0.75rem 0 0; font-size: 0.8rem; color: var(--ink-soft); text-align: center; }

/* keep the panel in view only when it fits the screen */
@media (min-width: 861px) and (min-height: 960px) {
  .panel { position: sticky; top: 1rem; }
}

/* ---------- desk with sheets ---------- */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 0.75rem 1.25rem;
  margin-bottom: 1.25rem;
}
.result-title { font-family: var(--display); font-weight: 400; font-size: var(--step-2); margin: 0; line-height: 1.1; }
.toolbar-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.6rem 1rem; }

.btn {
  padding: 0.55rem 1rem;
  border: 2px solid var(--ink);
  border-radius: 12px;
  background: var(--ink); color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.btn:hover { background: #2c3f73; }
.btn-quiet { background: #fff; color: var(--ink); }
.btn-quiet:hover { background: #f1f4fb; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.switch, .check { display: inline-flex; align-items: center; gap: 0.5rem; font-weight: 600; font-size: var(--step--1); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  width: 40px; height: 24px; border-radius: 12px;
  background: var(--desk); position: relative; flex: none;
  box-shadow: inset 0 0 0 2px var(--line-strong);
  transition: background-color .15s;
}
.switch-track::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%; background: #fff;
  transition: transform .15s;
}
.switch input:checked + .switch-track { background: var(--ink); box-shadow: none; }
.switch-track::after { box-shadow: 0 0 0 2px var(--line-strong); }
.switch input:checked + .switch-track::after { box-shadow: none; }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 3px solid var(--focus); outline-offset: 2px; }
.check input { position: static; opacity: 1; width: 18px; height: 18px; accent-color: var(--ink); }

.progress { margin: -0.5rem 0 1.25rem; }
.progress-bar { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; }
.progress-bar span { display: block; height: 100%; width: 0; background: var(--ink); transition: width .2s; }
.progress p { margin: 0.4rem 0 0; font-size: var(--step--1); color: var(--ink-soft); }

/* sheets lie on the desk like loose paper, slightly askew */
.sheets {
  list-style: none; margin: 0; padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: clamp(1rem, 2vw, 1.75rem);
}
.sheet {
  transform: rotate(var(--tilt, 0deg));
  animation: drop .35s ease-out both;
  animation-delay: var(--delay, 0ms);
}
.sheet-btn {
  display: block; width: 100%;
  aspect-ratio: 210 / 297;
  padding: 0; border: 0;
  background: var(--paper);
  border-radius: 3px;
  box-shadow: 0 1px 0 rgba(29, 43, 83, .08), 0 6px 16px -6px rgba(29, 43, 83, .35);
  cursor: zoom-in;
  transition: transform .15s, box-shadow .15s;
}
.sheet-btn svg { display: block; width: 100%; height: 100%; }
.sheet-meta { margin: 0.45rem 0 0; font-size: var(--step--1); font-weight: 600; color: var(--ink-soft); text-align: center; }

/* the "+" sheet at the end of the set */
.sheet-add { transform: none; }
.add-btn {
  display: grid; place-content: center; justify-items: center; gap: 0.4rem;
  width: 100%; aspect-ratio: 210 / 297;
  border: 2px dashed var(--line-strong);
  border-radius: 6px;
  background: rgba(255, 255, 255, .55);
  color: var(--ink);
  font-weight: 700;
  cursor: pointer;
  transition: background-color .15s, border-color .15s;
}
.add-btn:hover { background: #fff; border-color: var(--ink); }
.add-plus {
  display: grid; place-items: center;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--crayon); border: 2px solid var(--ink);
  font-family: var(--display); font-size: 2rem; line-height: 1;
}
.sheet-btn:hover, .sheet-btn:focus-visible {
  transform: rotate(calc(var(--tilt, 0deg) * -1)) translateY(-3px);
  box-shadow: 0 2px 0 rgba(29, 43, 83, .08), 0 14px 26px -10px rgba(29, 43, 83, .45);
}
@keyframes drop {
  from { opacity: 0; transform: rotate(var(--tilt, 0deg)) translateY(-10px); }
}

.load-error { background: #fff; border: 2px solid var(--ink); border-radius: 14px; padding: 1rem 1.25rem; max-width: 60ch; }
.noscript { padding: 1rem 2rem; }

/* ---------- viewer ---------- */
.viewer {
  width: min(96vw, 900px);
  height: min(96vh, 1100px);
  max-width: none; max-height: none;
  padding: 0;
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: var(--desk);
  color: var(--ink);
}
.viewer[open] { display: grid; grid-template-rows: auto 1fr auto; }
.viewer::backdrop { background: rgba(29, 43, 83, .55); }
.viewer-head, .viewer-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 0.9rem;
  background: #fff;
}
.viewer-head { border-bottom: 2px solid var(--ink); }
.viewer-foot { border-top: 2px solid var(--ink); justify-content: center; flex-wrap: wrap; gap: 0.6rem; }
.viewer-title { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.viewer-body {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center;
  gap: 0.5rem; padding: 0.75rem; min-height: 0;
}
.viewer-sheet { height: 100%; min-height: 0; display: flex; justify-content: center; }
.viewer-sheet svg {
  height: 100%; width: auto; max-width: 100%;
  background: #fff; border-radius: 3px;
  box-shadow: 0 10px 30px -12px rgba(29, 43, 83, .5);
}
.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px;
  border: 2px solid var(--ink); border-radius: 50%;
  background: #fff; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* ---------- small screens ---------- */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .panel { position: static; box-shadow: 4px 4px 0 var(--ink); }
  .sheets { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}
@media (max-width: 560px) {
  .viewer-body { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .viewer-body .nav { display: none; }
  .toolbar-actions { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- download dialog ---------- */
.download {
  width: min(92vw, 420px);
  padding: 1.3rem 1.3rem 1.1rem;
  border: 2px solid var(--ink);
  border-radius: 18px;
  color: var(--ink);
  box-shadow: 6px 6px 0 var(--ink);
}
.download::backdrop { background: rgba(29, 43, 83, .45); }
.download-title { font-family: var(--display); font-weight: 400; font-size: var(--step-1); margin: 0 0 1rem; }
.formats { border: 0; padding: 0; margin: 0 0 1rem; display: grid; gap: 0.5rem; }
.formats legend { font-weight: 700; padding: 0; margin-bottom: 0.5rem; }
.format input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.format-face {
  display: grid; gap: 0.1rem;
  padding: 0.65rem 0.9rem 0.65rem 2.6rem;
  border: 2px solid var(--line-strong);
  border-radius: 12px;
  cursor: pointer;
  position: relative;
}
.format-face::before {
  content: ''; position: absolute; left: 0.9rem; top: 50%; translate: 0 -50%;
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--line-strong); background: #fff;
}
.format-face span { font-size: var(--step--1); color: var(--ink-soft); }
.format input:checked + .format-face { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.format input:checked + .format-face::before { border-color: var(--ink); background: radial-gradient(var(--ink) 0 4px, #fff 5px); }
.format input:focus-visible + .format-face { outline: 3px solid var(--focus); outline-offset: 2px; }
.download .check { min-height: 44px; font-size: var(--step-0); }
.download .check input { width: 22px; height: 22px; }
.download-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.75rem; }

/* add dialog */
.add-dialog { width: min(94vw, 480px); }
.add-dialog .field { margin-bottom: 1.1rem; }
/* keep Cancel / Add visible while the dialog scrolls on small screens */
.add-dialog .download-actions {
  position: sticky; bottom: -1.1rem;
  margin: 0.75rem -1.3rem -1.1rem;
  padding: 0.75rem 1.3rem 1.1rem;
  background: #fff;
  border-top: 2px solid var(--desk);
}
.add-pane { margin-bottom: 0.25rem; }
.field-label { display: block; font-weight: 700; margin-bottom: 0.4rem; }
.add-pane input[type=text] {
  width: 100%; min-height: 46px;
  padding: 0.5rem 0.7rem;
  border: 2px solid var(--line-strong);
  border-radius: 12px;
  background: #fff;
}
.add-pane input[type=text] {
  font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  font-variant-numeric: tabular-nums;
}
.add-pane input[type=text]::placeholder { color: #6b78a0; font-weight: 600; }
.add-pane input[type=text][aria-invalid=true] { border-color: #b42318; }
