@font-face { font-family: 'Charis SIL'; src: url(fonts/CharisSILR.ttf) format('truetype'); font-display: swap; }
@font-face { font-family: 'lovecraft'; src: url(fonts/lovecraft.otf) format('opentype'); font-display: swap; }
@font-face { font-family: 'Madina'; src: url(fonts/Madina.otf) format('opentype'); font-display: swap; }

:root {
  --bg: #faf6f0;
  --paper: #fffdf9;
  --ink: #2b211a;
  --muted: #7d6e62;
  --line: #e9dfd2;
  --accent: #b4532a;
  --accent-ink: #fff;
  --sel: #2b211a;
  --burn: #5a3519;
  --cut: #9a6f45;
  --radius: 14px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 Inter, system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
h2 { font: 600 17px/1.3 Fraunces, Georgia, serif; margin: 0 0 12px; display: flex; align-items: center; gap: 10px; }
.num { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--ink); color: var(--bg); font: 600 12px Inter, sans-serif; flex: none; }
.count { margin-left: auto; font: 500 13px Inter, sans-serif; color: var(--muted); }

.top { padding: 18px 16px 6px; text-align: center; }
.brand { font: 600 24px/1.1 Fraunces, Georgia, serif; letter-spacing: .2px; }
.brand em { font-weight: 400; }
.lede { margin: 6px auto 0; color: var(--muted); max-width: 520px; font-size: 14px; }
.lede em { color: var(--accent); font-style: normal; font-weight: 600; }

.layout { max-width: 1180px; margin: 0 auto; padding: 0 16px; }

/* preview */
.preview {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg); padding: 8px 0 6px;
  border-bottom: 1px solid var(--line);
}
.view-tabs { display: flex; gap: 4px; justify-content: center; margin-bottom: 6px; }
.vt { border: 0; background: transparent; padding: 6px 14px; border-radius: 999px; color: var(--muted); font-weight: 500; font-size: 13px; }
.vt.on { background: var(--ink); color: var(--bg); }
.stage { height: 30vh; min-height: 200px; max-height: 340px; position: relative; }
@media (max-width: 899px) {
  .view-tabs { margin-bottom: 2px; }
  .vt { padding: 4px 12px; font-size: 12px; }
  .hint { font-size: 11px; margin-top: 2px; min-height: 15px; }
}
.view { position: absolute; inset: 0; display: none; }
.view.on { display: block; }
.view svg { width: 100%; height: 100%; display: block; }
.hint { text-align: center; color: var(--muted); font-size: 12px; margin: 4px 0 0; min-height: 17px; }

.eng .fil1, .eng .fil2, .eng .fil3 { fill: var(--burn); }
.eng .fil0 { fill: none; }
.eng .str0 { stroke: var(--burn); stroke-width: 35; }
.piece { fill: url(#wood); stroke: none; transition: filter .2s; }
.piece-hit { fill: transparent; cursor: pointer; }
.piece-empty { fill: none; stroke: #b99872; stroke-width: 60; stroke-dasharray: 160 160; opacity: .6; }
.cutlines path, .cutlines line, .cutlines polygon { fill: none; stroke: var(--cut); stroke-width: 70; stroke-linejoin: round; }
.slotnum { font: 600 1300px Inter, sans-serif; fill: #b99872; text-anchor: middle; dominant-baseline: central; opacity: .75; }
.placed { animation: pop .28s ease-out; transform-box: fill-box; transform-origin: center; }
@keyframes pop { from { opacity: 0; transform: scale(.6); } to { opacity: 1; transform: scale(1); } }

/* controls */
.step { padding: 18px 0 8px; border-bottom: 1px solid var(--line); }
.step:last-of-type { border-bottom: 0; }
.note { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button {
  background: var(--paper); border: 1.5px solid var(--line); border-radius: var(--radius);
  padding: 10px 4px; display: grid; gap: 1px; justify-items: center;
}
.seg b { font: 600 22px/1 Fraunces, serif; }
.seg small { color: var(--muted); font-size: 12px; }
.seg button.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }

.names { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: end; }
.names label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.names input {
  font: 500 16px Inter, sans-serif; color: var(--ink); width: 100%;
  padding: 11px 12px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--paper);
}
.names input:focus { outline: none; border-color: var(--ink); }
.amp { font: 400 22px Fraunces, serif; padding-bottom: 9px; color: var(--muted); }

.lids { display: grid; grid-auto-flow: column; grid-auto-columns: 104px; gap: 10px; overflow-x: auto; padding: 2px 2px 10px; scroll-snap-type: x mandatory; margin: 0 -16px; padding-left: 16px; padding-right: 16px; scrollbar-width: none; }
.lids::-webkit-scrollbar { display: none; }
.lid {
  scroll-snap-align: start; border: 1.5px solid var(--line); border-radius: 12px; background: var(--paper);
  padding: 6px; display: grid; gap: 4px; justify-items: center; font-size: 12px; color: var(--muted);
}
.lid svg { width: 90px; height: 90px; display: block; border-radius: 6px; }
.lid.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); color: var(--ink); font-weight: 600; }

.chips { display: flex; gap: 6px; overflow-x: auto; margin: 0 -16px 6px; padding: 0 16px 6px; scrollbar-width: none; position: sticky; z-index: 3; background: var(--bg); }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1.5px solid var(--line); background: var(--paper); border-radius: 999px; padding: 6px 12px; font-size: 13px; font-weight: 500; white-space: nowrap; }
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip i { font-style: normal; opacity: .6; margin-left: 4px; font-size: 12px; }

.cat h3 { font: 600 15px Fraunces, serif; margin: 14px 0 8px; color: var(--ink); scroll-margin-top: 50px; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile {
  position: relative; border: 1.5px solid var(--line); background: var(--paper); border-radius: 12px;
  aspect-ratio: 1 / 1; padding: 8px 6px 6px; display: block; overflow: hidden;
  transition: border-color .15s, background .15s, transform .1s;
}
.tile:active { transform: scale(.97); }
.tile svg { width: 100%; height: 100%; display: block; }
.tile .eng .fil1, .tile .eng .fil2 { fill: #3b2a1f; }
.tile .no { position: absolute; top: 5px; left: 7px; font: 600 12px Inter, sans-serif; color: var(--muted); }
.tile .ck {
  position: absolute; top: 5px; right: 5px; width: 22px; height: 22px; border-radius: 50%;
  border: 1.5px solid var(--line); background: var(--paper); display: grid; place-items: center;
  font: 600 11px Inter, sans-serif; color: transparent;
}
.tile.on { border-color: var(--sel); background: #f6eadb; box-shadow: inset 0 0 0 1px var(--sel); }
.tile.on .ck { background: var(--sel); border-color: var(--sel); color: #fff; }
.tile.on .no { color: var(--ink); }
.tile.full:not(.on) { opacity: .45; }

.spacer { height: 110px; }

/* bottom bar */
.bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: rgba(255, 253, 249, .96); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--line); padding: 0 16px calc(10px + env(safe-area-inset-bottom));
}
.progress { height: 3px; background: var(--line); margin: 0 -16px 10px; }
.fill { height: 100%; width: 0; background: var(--accent); transition: width .25s; }
.bar-row { display: flex; align-items: center; gap: 8px; max-width: 1180px; margin: 0 auto; }
.status { font-size: 14px; color: var(--muted); margin-right: auto; white-space: nowrap; }
.status b { font: 600 20px Fraunces, serif; color: var(--ink); }
.primary {
  border: 0; border-radius: 999px; padding: 12px 18px; background: var(--ink); color: var(--bg);
  font-weight: 600; font-size: 15px; white-space: nowrap;
}
.primary:disabled { background: #cfc4b8; color: #fff; cursor: not-allowed; }
.primary.ready { background: var(--accent); }
.primary.wide { width: 100%; margin-top: 14px; }
.ghost { border: 1.5px solid var(--line); background: var(--paper); border-radius: 999px; padding: 10px 12px; font-size: 13px; font-weight: 500; white-space: nowrap; }

/* modal */
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(30, 20, 12, .45); display: grid; align-items: end; justify-items: center; }
.modal[hidden] { display: none; }
.sheet {
  position: relative; width: 100%; max-width: 480px; background: var(--paper); border-radius: 20px 20px 0 0;
  padding: 22px 18px calc(18px + env(safe-area-inset-bottom)); animation: up .25s ease-out;
}
@keyframes up { from { transform: translateY(40px); } }
.sheet h3 { font: 600 22px Fraunces, serif; margin: 0 0 4px; }
.sheet p { margin: 0 0 10px; color: var(--muted); }
.sheet textarea { width: 100%; font: 13px/1.4 ui-monospace, Menlo, monospace; border: 1.5px solid var(--line); border-radius: 10px; padding: 10px; resize: none; background: var(--bg); color: var(--ink); }
.x { position: absolute; top: 10px; right: 12px; border: 0; background: transparent; font-size: 26px; line-height: 1; color: var(--muted); }
.parts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.parts button { border: 1.5px solid var(--line); background: var(--bg); border-radius: 999px; padding: 6px 10px; font-size: 12.5px; }
.how { margin: 14px 0 0; padding-left: 20px; color: var(--ink); font-size: 14px; display: grid; gap: 4px; }

.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg); padding: 9px 14px; border-radius: 999px; font-size: 13px; opacity: 0; pointer-events: none; transition: .2s; z-index: 50; white-space: nowrap; }
.toast.show { opacity: 1; transform: translateX(-50%); }

@media (min-width: 560px) {
  .tiles { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 900px) {
  .top { padding-top: 28px; }
  .brand { font-size: 30px; }
  .layout { display: grid; grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr); gap: 40px; align-items: start; }
  .preview { top: 16px; border: 1px solid var(--line); border-radius: 20px; background: var(--paper); padding: 14px; margin-top: 14px; }
  .stage { height: min(68vh, 560px); max-height: none; }
  .tiles { grid-template-columns: repeat(4, 1fr); }
  .lids { margin: 0; padding: 2px 2px 10px; grid-auto-flow: row; grid-template-columns: repeat(5, 1fr); grid-auto-columns: auto; overflow: visible; }
  .lid svg { width: 100%; height: auto; aspect-ratio: 1; }
  .chips { margin: 0 0 6px; padding: 6px 0; }
  .sheet { border-radius: 20px; margin-bottom: 10vh; }
  .modal { align-items: center; }
}

/* lid = center explainer, clear, undo */
.note.top { margin: -4px 0 12px; text-align: left; }
.note b { color: var(--ink); font-weight: 600; }
.match {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px;
  margin-top: 6px; padding: 12px; border-radius: var(--radius); background: var(--paper); border: 1px dashed #d8c6b0;
  max-width: 360px;
}
.match figure { margin: 0; display: grid; gap: 4px; justify-items: center; }
.match svg { width: 100%; max-width: 120px; aspect-ratio: 1; display: block; }
.match figcaption { font-size: 12px; color: var(--muted); font-weight: 500; }
.match .eq { font: 600 22px Fraunces, serif; color: var(--accent); }
.match p { grid-column: 1 / -1; margin: 0; text-align: center; font-size: 12.5px; color: var(--muted); }
.center-piece path, .center-piece polygon { fill: url(#wood); stroke: var(--cut); stroke-width: 60; }
.clear { margin-left: 8px; border: 1.5px solid var(--line); background: var(--paper); border-radius: 999px; padding: 4px 10px; font: 500 12.5px Inter, sans-serif; color: var(--ink); }
.clear[hidden] { display: none; }
.count { margin-left: auto; }
.toast { display: flex; align-items: center; gap: 12px; }
.toast.show { pointer-events: auto; }
.toast button { border: 0; background: transparent; color: #f2b48f; font-weight: 600; font-size: 13px; padding: 0; }
.toast button[hidden] { display: none; }

/* segmented progress meter */
.meter { max-width: 460px; margin: 6px auto 0; padding: 0 4px; }
.segs { display: grid; grid-template-columns: repeat(var(--n, 24), 1fr); gap: 3px; height: 10px; }
.segs i { display: block; border-radius: 3px; background: #ebe0d2; transition: background .2s; }
.segs i.on { background: linear-gradient(180deg, #c8683c, var(--accent)); }
.segs i.pop { animation: seg .35s ease-out; }
@keyframes seg { 0% { transform: scaleY(1.9); filter: brightness(1.25); } 100% { transform: scaleY(1); } }
.mtext { display: flex; justify-content: space-between; align-items: baseline; margin-top: 5px; font-size: 12.5px; color: var(--muted); }
.mtext b { font: 600 16px Fraunces, serif; color: var(--ink); }
.mtext .left { font-weight: 600; color: var(--accent); }
.meter.done .segs i.on { background: linear-gradient(180deg, #4f9a63, #2f7a45); }
.meter.done .mtext .left { color: #2f7a45; }
@media (max-width: 899px) {
  .meter { margin-top: 4px; }
  .segs { height: 8px; gap: 2px; }
  .mtext { margin-top: 3px; font-size: 12px; }
  .mtext b { font-size: 14px; }
}

/* typing names on a phone: compact preview, no bottom bar */
@media (max-width: 899px) {
  body.typing .stage { height: 18vh; min-height: 110px; }
  body.typing .view-tabs, body.typing .meter, body.typing .hint { display: none; }
  body.typing .bar { display: none; }
  body.typing .preview { padding: 4px 0; }
}

/* copy buttons in the "Copied" sheet */
.parts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.parts .cp {
  display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px;
  border-radius: 14px; padding: 10px 12px; font: 600 15px Inter, sans-serif;
  border: 1.5px solid var(--accent); background: #fff; color: var(--accent);
  transition: background .15s, color .15s, border-color .15s;
}
.parts .cp-all { grid-column: 1 / -1; background: var(--accent); color: #fff; font-size: 16px; min-height: 52px; }
.parts .cp:active { transform: scale(.98); }
.parts .cp .ic { width: 16px; height: 16px; flex: none; background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='12' height='12' rx='2'/%3E%3Cpath d='M5 15H4a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1'/%3E%3C/svg%3E") center / contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='9' y='9' width='12' height='12' rx='2'/%3E%3Cpath d='M5 15H4a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1'/%3E%3C/svg%3E") center / contain no-repeat; }
.parts .cp.done { background: #2f7a45; border-color: #2f7a45; color: #fff; }
.parts .cp.done .ic { display: none; }

/* big, obvious Puzzle / Box lid switch */
.view-tabs {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px; max-width: 420px; margin: 0 auto 8px;
  padding: 4px; border-radius: 16px; background: #efe6da; border: 1px solid var(--line);
}
.vt {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 8px 10px; border-radius: 12px; border: 0;
  background: transparent; color: var(--ink); font: 600 16px Inter, sans-serif;
}
.vt svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; }
.vt.on { background: var(--ink); color: #fff; box-shadow: 0 2px 6px rgba(43, 33, 26, .25); }
.vt.on svg { fill: none; }
.vt:not(.on) { color: var(--accent); }
@media (max-width: 899px) {
  .view-tabs { margin-bottom: 6px; }
  .vt { min-height: 42px; font-size: 15px; padding: 6px 8px; }
}
/* "Choose for me" button */
.ghost.pick { border-color: var(--accent); color: var(--accent); font-weight: 600; font-size: 14px; padding: 10px 14px; }
@media (max-width: 430px) {
  .bar-row { gap: 6px; }
  .status .lbl { display: none; }
  .status b { font-size: 18px; }
  .ghost.pick { padding: 10px 10px; font-size: 13.5px; }
  .bar .primary { padding: 12px 14px; font-size: 14.5px; }
}
