:root {
  --ink: #1f1235;
  --muted: #6b5b7b;
  --card: #ffffff;
  --brand: #7e22ce;
  --brand-2: #db2777;
  --sel: #faf5ff;
  --sel-border: #a855f7;
  --play: #16a34a;
  --play-dark: #15803d;
  --good: #16a34a;
  --bad: #dc2626;
  --bg-1: #f0abfc;
  --bg-2: #c4b5fd;
  --bg-3: #93c5fd;
  --radius: 22px;
  --font: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Segoe UI', system-ui, sans-serif;
  --emoji: 'Apple Color Emoji', 'Segoe UI Emoji', 'Noto Color Emoji', sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; min-height: 100%;
  font-family: var(--font); color: var(--ink);
  background: linear-gradient(160deg, var(--bg-1), var(--bg-2) 50%, var(--bg-3)) fixed;
  -webkit-tap-highlight-color: transparent;
}
body { overflow-x: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.screen { display: none; }
.screen.active { display: block; }

/* ---------- Home & results ---------- */
#home, #results {
  min-height: 100dvh;
  padding: calc(14px + var(--safe-t)) calc(16px + var(--safe-r)) calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
}
.inner { max-width: 820px; margin: 0 auto; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.back { color: #fff; font-weight: 600; text-decoration: none; justify-self: start; padding: 6px 2px; text-shadow: 0 1px 2px rgba(76, 29, 149, .35); }
.brand { text-align: center; color: #fff; }
.brand .logo { font-size: clamp(40px, 10vw, 64px); line-height: 1.1; font-family: var(--emoji); }
.brand h1 { margin: 2px 0 0; font-size: clamp(34px, 8vw, 56px); text-shadow: 0 3px 0 rgba(88, 28, 135, .3); }
.brand p { margin: 4px 0 0; font-size: clamp(16px, 4vw, 20px); font-weight: 500; text-shadow: 0 1px 2px rgba(76, 29, 149, .3); }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 10px 30px rgba(88, 28, 135, .18); }
.card h2 { margin: 0 0 12px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 16px; flex: none; }

.years { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (min-width: 640px) { .years { grid-template-columns: repeat(6, 1fr); } }
.year { border: 3px solid #e5e7eb; background: #fafaf9; border-radius: 18px; min-height: 76px; display: grid; place-content: center; padding: 6px; }
.year b { font-size: 28px; line-height: 1; }
.year small { color: var(--muted); font-size: 13px; }
.year[aria-checked="true"] { border-color: var(--sel-border); background: var(--sel); box-shadow: 0 0 0 3px rgba(168, 85, 247, .2); }

.dskp { margin-top: 12px; background: #faf5ff; border-radius: 14px; padding: 10px 12px; }
.dskp summary { cursor: pointer; font-weight: 600; color: var(--brand); }
.dskp ul { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.dskp li { display: grid; grid-template-columns: auto 1fr; gap: 8px; font-size: 14px; line-height: 1.35; }
.dskp .code { font-weight: 700; color: var(--brand); white-space: nowrap; }
.dskp .note { margin: 10px 0 0; font-size: 12.5px; color: var(--muted); line-height: 1.45; }

.option { display: grid; gap: 6px; margin-top: 14px; }
.option-label { font-weight: 600; color: var(--muted); font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 auto; min-height: 44px; padding: 8px 12px; border-radius: 14px; border: 2px solid #e5e7eb; background: #fafaf9; font-weight: 500; font-size: 15px; }
.seg button[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); font-weight: 600; }
.level-seg button { min-height: 56px; font-size: 17px; }
.level-note { margin: 8px 0 0; font-size: 14px; color: var(--muted); line-height: 1.35; }
.tip { margin: 4px 0 8px; font-size: 14px; color: var(--muted); line-height: 1.4; }
.tip.center { text-align: center; }

.frames { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 230px), 1fr)); gap: 10px; }
.frame-card {
  display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-items: center; text-align: left;
  border: 2px solid #ede9fe; background: linear-gradient(160deg, #fff, #faf5ff); border-radius: 18px; padding: 12px; min-height: 92px;
  box-shadow: 0 4px 0 #e9d5ff; transition: transform .1s;
}
.frame-card:active { transform: translateY(2px); box-shadow: 0 2px 0 #e9d5ff; }
.frame-card .fc-emoji { grid-row: span 3; font-size: 40px; font-family: var(--emoji); line-height: 1; }
.frame-card b { font-size: 18px; }
.frame-card small { color: var(--muted); font-size: 13.5px; line-height: 1.3; }
.frame-card .fc-tag { justify-self: start; font-size: 11.5px; font-weight: 600; color: var(--brand); background: #f3e8ff; border-radius: 999px; padding: 1px 8px; }

.draft { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; border: 3px solid #f0abfc; }
.draft p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

.gallery { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.gallery .empty { color: var(--muted); font-size: 14px; text-align: center; padding: 10px; background: #faf5ff; border-radius: 14px; }
.g-item { display: flex; gap: 10px; align-items: center; background: #faf5ff; border-radius: 16px; padding: 8px 8px 8px 12px; }
.g-item .g-emoji { font-size: 30px; font-family: var(--emoji); }
.g-item .g-text { flex: 1; min-width: 0; overflow: hidden; }
.g-item .g-text b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-item .g-text small { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.g-item .btn-small { flex: none; padding: 6px 12px; }

.btn { border: 0; border-radius: 999px; min-height: 52px; padding: 10px 26px; font-weight: 700; font-size: 20px; transition: transform .1s; }
.btn:active { transform: translateY(2px); }
.btn-play { background: var(--play); color: #fff; box-shadow: 0 5px 0 var(--play-dark); }
.btn-ghost { background: #fff; color: var(--brand); box-shadow: 0 5px 0 #e9d5ff; }
.btn-small { border: 2px solid #e9d5ff; background: #fff; border-radius: 999px; padding: 6px 16px; font-weight: 600; font-size: 16px; min-height: 44px; }
.btn-small:disabled { opacity: .6; cursor: progress; }
.install { display: grid; justify-items: center; gap: 6px; color: #fff; font-size: 14px; text-align: center; }
.install p { margin: 0; }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* ---------- Make ---------- */
#make { min-height: 100dvh; }
.mk {
  max-width: 1200px; margin: 0 auto; display: grid; gap: 10px;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) calc(24px + var(--safe-b)) calc(12px + var(--safe-l));
}
.hud { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pill { background: rgba(255,255,255,.95); border: 0; border-radius: 999px; padding: 6px 14px; font-weight: 700; font-size: clamp(15px, 2.4vw, 19px); box-shadow: 0 4px 12px rgba(0,0,0,.12); white-space: nowrap; min-height: 44px; display: inline-flex; align-items: center; }
.hud-frame { flex: 1 1 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; display: block; line-height: 32px; }
.hud-year { background: var(--brand); color: #fff; }
.hud-btn { min-width: 48px; justify-content: center; }
.hud-finish { background: var(--play); color: #fff; }
@media (max-width: 480px) { .hud-finish { display: none; } }

.tabs { display: flex; gap: 6px; background: rgba(255,255,255,.5); padding: 5px; border-radius: 999px; }
.tabs button { flex: 1; border: 0; border-radius: 999px; min-height: 46px; font-weight: 700; font-size: 17px; background: transparent; }
.tabs button[aria-selected="true"] { background: #fff; color: var(--brand); box-shadow: 0 3px 10px rgba(0,0,0,.12); }

.mk-grid { display: grid; gap: 12px; align-items: start; }
.mk-grid[data-tab="write"] .pane-deco, .mk-grid[data-tab="write"] .pane-preview { display: none; }
.mk-grid[data-tab="deco"] .pane-write { display: none; }
@media (min-width: 900px) {
  .tabs { max-width: calc(50% - 6px); }
  .mk-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .mk-grid .pane-preview { display: block !important; grid-column: 2; grid-row: 1 / span 2; position: sticky; top: 10px; }
  .mk-grid .pane-write, .mk-grid .pane-deco { grid-column: 1; grid-row: 1; }
  .only-write { display: none; }
}
.mk-grid[data-tab="deco"] .only-write { display: none; }

.pane-write, .pane-deco { display: grid; gap: 12px; }
.pane h3 { margin: 4px 0 0; font-size: 17px; }
.pane h3 small { font-weight: 400; color: var(--muted); font-size: 13px; }
.field { display: grid; gap: 4px; }
.field > span { font-weight: 600; color: var(--muted); font-size: 14px; }
.field input, .topic-box input {
  width: 100%; min-height: 52px; border: 2px solid #ddd6fe; border-radius: 14px; padding: 8px 12px; font-size: 20px; background: #fff;
}
.field input:focus, .topic-box input:focus, .ln input:focus { outline: 0; border-color: var(--sel-border); box-shadow: 0 0 0 4px rgba(168,85,247,.2); }

.topic-box:empty { display: none; }
.topic-box { display: grid; gap: 6px; }
.topic-box .label { font-weight: 600; color: var(--muted); font-size: 14px; }
.topic-box .seg button { flex: 0 1 auto; }
.topic-box .te { font-family: var(--emoji); }

.example { background: #fdf4ff; border-radius: 14px; padding: 10px 12px; }
.example summary { cursor: pointer; font-weight: 600; color: var(--brand); min-height: 28px; }
.example-text { margin: 8px 0; font-size: 17px; line-height: 1.5; white-space: pre-line; }

.lines { display: grid; gap: 10px; }
.panel-head { margin: 6px 0 -2px; font-weight: 700; color: var(--brand); font-size: 16px; display: flex; align-items: center; gap: 6px; }
.panel-head .pe { font-family: var(--emoji); font-size: 22px; }
.ln { border: 2px solid #ede9fe; border-radius: 16px; padding: 8px 10px 10px; background: #fff; scroll-margin: 90px 0 200px; }
.ln.active { border-color: var(--sel-border); background: #fdfaff; }
.ln.done { border-color: #bbf7d0; }
.ln-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.ln-num { display: inline-grid; place-items: center; min-width: 26px; height: 26px; border-radius: 50%; background: #f3e8ff; color: var(--brand); font-weight: 700; font-size: 14px; }
.ln.done .ln-num { background: #dcfce7; color: var(--good); }
.ln-hint { flex: 1; color: var(--muted); font-size: 14px; line-height: 1.3; }
.ln-who { font-size: 13px; font-weight: 600; background: #fef3c7; border-radius: 999px; padding: 2px 8px; white-space: nowrap; }
.ln-who.narr { background: #e0f2fe; }
.ln .say { border: 0; background: #f5f3ff; border-radius: 50%; width: 44px; height: 44px; flex: none; font-size: 18px; }
.ln-body { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 6px; font-size: 20px; line-height: 1.3; }
.ln-body .fx { font-weight: 600; white-space: pre-wrap; }
.ln-body input {
  flex: 1 1 8em; min-width: 6.5em; min-height: 52px; border: 2px dashed #c4b5fd; border-radius: 12px; padding: 6px 10px;
  font-size: 20px; background: #faf5ff;
}
.ln-body input.open { flex-basis: 100%; border-style: solid; background: #fff; }
.ln-body input:not(:placeholder-shown) { border-style: solid; background: #fff; }
.ln-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.ln-meta:empty { display: none; }
.badge { font-size: 13.5px; font-weight: 600; border-radius: 999px; padding: 3px 10px; background: #f1f5f9; color: var(--muted); }
.badge.ok { background: #dcfce7; color: #166534; }
.badge.no { background: #fef3c7; color: #92400e; }

.bank { border: 2px solid #f0abfc; background: #fdf4ff; border-radius: 16px; padding: 10px; display: grid; gap: 8px; margin-top: -4px; }
.bank h4 { margin: 0; font-size: 14px; color: var(--muted); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 0; border-radius: 14px; background: #fff; min-height: 44px; padding: 6px 12px; font-size: 17px; font-weight: 600;
  box-shadow: 0 3px 0 #e9d5ff, 0 4px 10px rgba(0,0,0,.06); display: inline-flex; align-items: center; gap: 6px;
  touch-action: manipulation;
}
.chip:active { transform: translateY(2px); box-shadow: 0 1px 0 #e9d5ff; }
.chip .ce { font-family: var(--emoji); font-size: 22px; }
.chip.pic { flex-direction: column; min-width: 76px; padding: 6px 10px; gap: 2px; font-size: 15px; }
.chip.pic .ce { font-size: 32px; line-height: 1.1; }
.chip .syl { font-size: 12px; color: var(--brand); background: #f3e8ff; border-radius: 999px; padding: 0 6px; }
.chip.punct { min-width: 52px; justify-content: center; font-size: 24px; }
.chip.rhyme { background: #fef9c3; box-shadow: 0 3px 0 #fde68a; }

.live-checks { background: #faf5ff; border-radius: 16px; padding: 10px 12px; }
.checks { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; text-align: left; }
.checks li { display: grid; grid-template-columns: 30px 1fr; gap: 2px 8px; align-items: start; font-size: 15.5px; line-height: 1.35; }
.checks .ck { grid-row: span 2; display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: #e5e7eb; color: #6b7280; font-weight: 700; }
.checks li.ok .ck { background: #dcfce7; color: var(--good); }
.checks li.no .ck { background: #fef3c7; color: #b45309; }
.checks .cd { color: var(--muted); font-size: 13.5px; }
.finish-big { justify-self: center; width: min(100%, 360px); font-size: 22px; }

.preview-tools { display: flex; gap: 8px; justify-content: center; margin-bottom: 8px; flex-wrap: wrap; }
.stk-sel { display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; margin-top: 8px; background: #fff; border-radius: 22px; padding: 6px 10px; box-shadow: 0 4px 12px rgba(0,0,0,.12); }
#stkSelEmoji { font-family: var(--emoji); font-size: 28px; }

.small-seg button { min-height: 40px; font-size: 14px; padding: 6px 10px; }
.stk-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; }
.stk-grid button { border: 2px solid #f3e8ff; background: #fff; border-radius: 12px; min-height: 48px; font-size: 28px; font-family: var(--emoji); padding: 0; }
.stk-grid button[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 48px; height: 48px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px #e5e7eb; padding: 0; }
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--sel-border); transform: scale(1.08); }
#fontPick button { font-size: 18px; }

/* ---------- The piece (poem or comic) ---------- */
.piece-host { width: 100%; max-width: 560px; margin: 0 auto; }
.piece {
  container-type: inline-size;
  position: relative; overflow: hidden; width: 100%;
  border-radius: 18px; padding: 0;
  box-shadow: 0 10px 30px rgba(88, 28, 135, .2);
  min-height: 20px;
  text-align: center;
}
.pc-in { padding: 7cqw 7cqw 6cqw; }
.pc-title { margin: 0 0 3cqw; font-size: 8cqw; line-height: 1.15; font-weight: 700; overflow-wrap: anywhere; }
.pc-art { font-family: var(--emoji); font-size: 16cqw; line-height: 1.1; margin: -1cqw 0 2cqw; }
.pc-lines { display: grid; gap: 1.6cqw; }
.pc-lines p { margin: 0; line-height: 1.3; overflow-wrap: anywhere; }
.pc-lines.n-s p { font-size: 6.2cqw; }
.pc-lines.n-m p { font-size: 5.3cqw; }
.pc-lines.n-l p { font-size: 4.6cqw; }
.pc-lines.n-xl p { font-size: 4cqw; }
.pc-lines .verse-gap { height: 2cqw; }
.pc-empty { opacity: .35; }
.pc-by { margin-top: 4cqw; font-size: 3.6cqw; opacity: .8; font-style: italic; }
.pc-stk { position: absolute; inset: 0; pointer-events: none; }
.stk {
  position: absolute; transform: translate(-50%, -50%); font-family: var(--emoji); line-height: 1;
  user-select: none; -webkit-user-select: none; touch-action: none; pointer-events: none;
}
.piece.editable .stk { pointer-events: auto; cursor: grab; }
.piece.editable .stk.sel { outline: 3px dashed var(--brand); outline-offset: 3px; border-radius: 8px; }

/* Comic */
.pc-panels { display: grid; grid-template-columns: 1fr 1fr; gap: 3cqw; text-align: left; }
.pc-panels.n3 .pc-panel:last-child { grid-column: 1 / -1; }
.pc-panels.n5 .pc-panel:last-child { grid-column: 1 / -1; }
.pc-panel {
  position: relative; border: 0.8cqw solid currentColor; border-radius: 2.5cqw; background: rgba(255,255,255,.75);
  padding: 2cqw; display: flex; flex-direction: column; gap: 1.5cqw; min-height: 36cqw; color: inherit;
}
.pc-narr { background: #fef9c3; color: #1f1235; border: 0.5cqw solid #1f1235; border-radius: 1.2cqw; padding: 1.2cqw 1.8cqw; font-size: 3.1cqw; line-height: 1.25; overflow-wrap: anywhere; }
.pc-title.long { font-size: 6.4cqw; }
.pc-talk { display: flex; flex-direction: column; justify-content: flex-end; gap: 2.6cqw; flex: 1; padding-bottom: 1cqw; }
.pc-bubble {
  position: relative; background: #fff; color: #1f1235; border: 0.5cqw solid #1f1235; border-radius: 3cqw;
  padding: 1.2cqw 1.8cqw; font-size: 3.2cqw; line-height: 1.25; text-align: center; overflow-wrap: anywhere;
  max-width: 92%; align-self: flex-start;
}
.pc-bubble.w1 { align-self: flex-end; }
.pc-bubble::after {
  content: ''; position: absolute; left: 3.5cqw; bottom: -1.9cqw; width: 2.6cqw; height: 2.6cqw; background: #fff;
  border-right: 0.5cqw solid #1f1235; border-bottom: 0.5cqw solid #1f1235; transform: rotate(45deg);
}
.pc-bubble.w1::after { left: auto; right: 3.5cqw; }
.pc-cast { display: flex; justify-content: space-between; align-items: flex-end; padding: 0 1cqw; }
.pc-char { font-family: var(--emoji); font-size: 9cqw; line-height: 1; }
.pc-scene { font-family: var(--emoji); font-size: 6cqw; opacity: .6; line-height: 1; }

/* ---------- Overlay ---------- */
.overlay { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px; background: rgba(46, 16, 101, .45); overflow-y: auto; }
.panel { background: #fff; border-radius: var(--radius); padding: 20px; width: min(100%, 480px); text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.ov-icon { font-size: 52px; line-height: 1.1; font-family: var(--emoji); }
.panel h2 { margin: 6px 0; font-size: 26px; }
.panel p { margin: 0 0 14px; color: var(--muted); font-size: 17px; line-height: 1.4; }
#ovBody img { display: block; max-width: 100%; max-height: 50vh; margin: 0 auto 14px; border-radius: 12px; box-shadow: 0 4px 16px rgba(0,0,0,.2); }
#ovBody:empty { display: none; }
.spinner { width: 44px; height: 44px; margin: 4px auto; border-radius: 50%; border: 5px solid #f3e8ff; border-top-color: var(--brand); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-b)); transform: translate(-50%, 30px); opacity: 0; z-index: 30;
  background: #1f1235; color: #fff; padding: 10px 18px; border-radius: 999px; font-weight: 600; transition: .3s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* ---------- Results ---------- */
.results { display: grid; gap: 10px; text-align: center; }
.results h2 { justify-content: center; font-size: 30px; margin: 0; }
.results h3 { margin: 10px 0 0; font-size: 18px; }
.stars { font-size: clamp(48px, 13vw, 72px); letter-spacing: 4px; }
.stars .off { filter: grayscale(1); opacity: .3; }
.res-score { margin: 0; font-size: 18px; font-weight: 600; }
.res-piece { margin: 6px auto; }
.saved-note { margin: 0; font-size: 14px; color: var(--muted); }
.table-wrap { overflow-x: auto; }
.std-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 14px; }
.std-table th, .std-table td { padding: 8px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
.std-table th { color: var(--muted); font-weight: 600; }
.std-table td:first-child { font-weight: 700; color: var(--brand); white-space: nowrap; }
.std-table td:last-child { font-weight: 700; white-space: nowrap; }
#copyBtn { justify-self: center; }

#printArea { display: none; }

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

/* ---------- Print: one page, just the piece ---------- */
@media print {
  @page { size: A4 portrait; margin: 12mm; }
  html, body { background: #fff !important; min-height: 0; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block !important; }
  #printArea .piece {
    width: 170mm; max-height: 270mm; margin: 0 auto; box-shadow: none; border: 1px solid #ddd;
    break-inside: avoid; page-break-inside: avoid;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  #printArea .pc-panel { min-height: 0; }
}
