:root {
  color-scheme: light;
  --background: #dbe8e3; --paper: #f4f9f7; --card: #fff;
  --ink: #0f2f2b; --muted: #4a6762; --hairline: #cbded7;
  --accent: #0a7266; --button-text: #fff; --light-accent: #c3ead9;
  --field: #0c2e35; --field-edge: #1a5457; --grid: #326269;
  --field-text: #ebfaf6; --signal: #abf6d1; --danger: #f5969e;
  --screen-haze: #1e575a;
  --pieces: #6bd0d2, #f6d88e, #b9a4eb, #eaa6aa, #a2e5aa, #9bb4fa;
  --glow: #0a726638; --lift: 0 1px 2px #0f2f2b14, 0 8px 24px #0f2f2b12;
  --radius: 20px; --shell-width: 520px;
  --text: 'Hiragino Sans', 'Yu Gothic UI', 'Noto Sans JP', system-ui, sans-serif;
  --display: var(--text);
  --mono: ui-monospace, 'SF Mono', 'Cascadia Mono', 'Roboto Mono', monospace;
  font: 15px/1.6 var(--text);
}

:root[data-theme="paper"] {
  --background: #ecd9c5; --paper: #fbf3e7; --card: #fffaf2;
  --ink: #46241f; --muted: #7a5347; --hairline: #e4c9b5;
  --accent: #a53f43; --button-text: #fff8ee; --light-accent: #f5c8b2;
  --field: #3b2629; --field-edge: #664044; --grid: #6e4d4c;
  --field-text: #fff2df; --signal: #f9d291; --danger: #ff9cae;
  --screen-haze: #7b4a43;
  --pieces: #f5ae8c, #f9d291, #dfb0cf, #d3978d, #b5d3a7, #96c9e5;
  --glow: #a53f4333; --lift: 0 1px 0 #46241f1a, 0 7px 20px #46241f14;
  --radius: 10px;
  --display: 'Hiragino Mincho ProN', 'Yu Mincho', 'Noto Serif JP', serif;
}

:root[data-theme="ribbon"] {
  color-scheme: dark;
  --background: #0f0a10; --paper: #150e16; --card: #221523;
  --ink: #ffe6f3; --muted: #d9a9c6; --hairline: #5f3852;
  --accent: #ffa1d1; --button-text: #2a0f22; --light-accent: #4f2140;
  --field: #1a1021; --field-edge: #6d345b; --grid: #55324e;
  --field-text: #ffe7f8; --signal: #ffb5e2; --danger: #ff79a3;
  --screen-haze: #552348;
  --pieces: #f5a9ce, #f4c58d, #c5a1f6, #f58ca9, #aadde5, #e7b1ee;
  --glow: #ff6bb44d; --lift: 0 0 0 1px #0f0a10, 0 0 26px #fa57b426;
  --radius: 22px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; color: var(--ink); background: var(--background); -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }
button:focus-visible, a:focus-visible, canvas:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
button:disabled { cursor: default; opacity: .5; }
[hidden] { display: none !important; }
h1, h2, p, figure { margin: 0; }

/* A tiny pre-blurred copy of the illustration tints the page, so every game picks up its own colours. */
.ambient {
  position: fixed; inset: -4%; z-index: -1; opacity: .62;
  background: var(--background) url('./ambient.webp') center / cover no-repeat;
}
.shell {
  display: flex; flex-direction: column;
  width: min(100%, var(--shell-width)); min-height: 100svh; margin-inline: auto;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  box-shadow: 0 0 60px #0000001f;
}
.boot { padding: 32px 14px; }

.masthead {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-bottom: 10px; border-bottom: 1px solid var(--hairline);
}
.mark { display: flex; align-items: center; gap: 10px; min-width: 0; }
.mark-icon {
  display: grid; place-items: center; width: 36px; height: 36px; flex: 0 0 36px;
  border-radius: 12px; background: var(--light-accent); color: var(--ink); font-size: 20px; line-height: 1;
}
.mark-text { min-width: 0; }
.game-label, .eyebrow, .micro, .board-title, .controls-label {
  font: 700 10px/1.3 var(--mono); letter-spacing: .14em;
}
.game-label { color: var(--accent); }
.mark-title { font-size: 13px; font-weight: 750; line-height: 1.35; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.masthead-tools { display: flex; gap: 6px; }
.top-action {
  position: relative; width: 44px; height: 44px; border: 1px solid var(--hairline); border-radius: 14px;
  background: var(--card); font-size: 17px; line-height: 1;
}
.top-action.off { color: var(--muted); }
.top-action.off::after {
  content: ''; position: absolute; left: 11px; right: 11px; top: 50%; height: 2px;
  border-radius: 2px; background: currentColor; transform: rotate(-42deg);
}

.stage { display: grid; gap: 12px; margin-top: 12px; align-content: start; }
.stage[data-screen="play"] { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 10px; }
.stage:not([data-screen="play"]) > * { animation: rise .42s cubic-bezier(.2, .7, .2, 1) both; }
.stage:not([data-screen="play"]) > :nth-child(2) { animation-delay: .05s; }
.stage:not([data-screen="play"]) > :nth-child(n + 3) { animation-delay: .1s; }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }

.intro, .result-head { display: grid; gap: 6px; padding-top: 6px; }
.intro h1, .result-head h1 {
  font-family: var(--display); font-size: clamp(27px, 8vw, 40px); font-weight: 850; line-height: 1.16;
  letter-spacing: -.035em; overflow-wrap: anywhere; text-wrap: balance;
}
.intro p, .result-head p { color: var(--muted); font-size: 13.5px; }
.eyebrow { color: var(--accent); }
.result-head h1 { font-size: clamp(34px, 11vw, 52px); }

.hero { position: relative; display: grid; place-items: center; }
.illustration { position: relative; line-height: 0; }
.hero-frame {
  display: inline-block; max-width: 100%; overflow: hidden; border-radius: var(--radius);
  background: var(--field); box-shadow: var(--lift), 0 0 0 1px var(--hairline);
}
.hero-frame > .base { display: block; width: auto; height: auto; max-width: 100%; max-height: min(54svh, 540px); }
.reward-layer {
  position: absolute; inset: 0; width: 100%; height: 100%;
  animation: unveil 1.1s cubic-bezier(.2, .7, .2, 1); transition: opacity .16s;
}
.reward-layer.away { opacity: 0; }
@keyframes unveil { from { opacity: 0; filter: brightness(1.9) saturate(.3) blur(8px); transform: scale(1.04); } }
.hero-frame.revealed::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(105deg, transparent 38%, #ffffffb8 50%, transparent 62%) 130% 0 / 260% 100% no-repeat;
  animation: shine 1.25s .25s ease-out both;
}
@keyframes shine { to { background-position: -70% 0; opacity: 0; } }
.badge {
  position: absolute; left: 10px; bottom: 10px; padding: 7px 12px; border-radius: 99px;
  background: color-mix(in srgb, var(--field) 80%, transparent); color: var(--field-text);
  font-size: 11.5px; font-weight: 750; line-height: 1.2; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.badge::before { content: '✦ '; color: var(--signal); }
.zoom {
  position: absolute; top: 8px; right: 8px; width: 44px; height: 44px; border: 0; border-radius: 50%;
  background: color-mix(in srgb, var(--field) 62%, transparent); color: var(--field-text); font-size: 18px; line-height: 1;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.burst { position: absolute; left: 50%; top: 42%; pointer-events: none; }
.burst i {
  position: absolute; left: -5px; top: -5px; width: 10px; height: 10px; border-radius: 2px; background: var(--accent);
  opacity: 0; animation: burst 1s .2s cubic-bezier(.1, .7, .3, 1);
}
.burst i:nth-child(odd) { border-radius: 50%; background: var(--signal); width: 7px; height: 7px; }
.burst i:nth-child(3n) { background: var(--danger); }
@keyframes burst {
  from { opacity: 1; transform: rotate(var(--turn)) translateY(0) scale(.3); }
  to { opacity: 0; transform: rotate(var(--turn)) translateY(calc(-1 * var(--reach))) scale(1.2) rotate(90deg); }
}

.primary, .secondary, .touch, .choice, .segment { font-weight: 750; border-radius: 14px; }
.primary {
  width: 100%; min-height: 54px; border: 0; background: var(--accent); color: var(--button-text);
  font-size: 16px; letter-spacing: .04em; box-shadow: inset 0 1px 0 #ffffff40, 0 8px 20px var(--glow);
}
.secondary, .touch, .choice { min-height: 48px; border: 1px solid var(--hairline); background: var(--card); }
.secondary { padding-inline: 14px; }
.primary:active, .secondary:active, .touch:active, .touch.held, .choice:active, .segment:active { transform: translateY(1px) scale(.985); }
.action-row { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; }
.compare { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border: 1px solid var(--hairline); border-radius: 16px; background: var(--card); }
.segment { min-height: 44px; border: 0; border-radius: 12px; background: transparent; color: var(--muted); }
.segment[aria-pressed="true"] { background: var(--light-accent); color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--accent); }
.segment[aria-pressed="true"]::before { content: '● '; font-size: 9px; vertical-align: 2px; color: var(--accent); }
.hint { color: var(--muted); font-size: 12px; text-align: center; }
.hint:empty { display: none; }

.consent { display: grid; gap: 14px; padding: 26px 20px 20px; margin-top: 8svh; border: 1px solid var(--hairline); border-radius: var(--radius); background: var(--card); box-shadow: var(--lift); }
.consent h1 { font-family: var(--display); font-size: clamp(22px, 6.4vw, 30px); line-height: 1.25; overflow-wrap: anywhere; }
.consent-text { padding: 14px 0; border-block: 1px solid var(--hairline); font-size: 15px; line-height: 1.8; white-space: pre-wrap; overflow-wrap: anywhere; }
.stage-picker { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.stage-chip {
  display: grid; place-items: center; min-width: 52px; min-height: 52px; padding: 4px 6px; border: 1px solid var(--hairline);
  border-radius: 14px; background: var(--card); line-height: 1.1;
}
.stage-chip small { font: 700 8px/1.2 var(--mono); letter-spacing: .1em; color: var(--muted); }
.stage-chip b { font: 800 18px/1.1 var(--mono); }
.stage-chip[aria-pressed="true"] { border-color: var(--accent); background: var(--light-accent); box-shadow: inset 0 0 0 1.5px var(--accent); }
.stage-chip:disabled { border-style: dashed; }
.save { display: grid; place-items: center; text-decoration: none; }
.no-save img { -webkit-touch-callout: none; -webkit-user-drag: none; user-select: none; -webkit-user-select: none; }
.credits { display: grid; gap: 8px; justify-items: center; padding-top: 4px; text-align: center; }
.credit-author { display: grid; gap: 1px; font-size: 14px; }
.credit-author .micro { color: var(--muted); }
.credit-links { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.link-chip {
  display: inline-flex; align-items: center; gap: 5px; min-height: 44px; padding: 0 16px; border: 1px solid var(--hairline); border-radius: 99px;
  background: var(--card); font-size: 13px; font-weight: 750; text-decoration: none;
}
.link-chip::after { content: '↗'; color: var(--accent); }

.hud { flex: none; }
.stats { display: flex; border: 1px solid var(--hairline); border-radius: 14px; background: var(--card); }
.stat { flex: 1; min-width: 0; padding: 5px 11px 4px; }
.stat + .stat { border-left: 1px solid var(--hairline); }
.stat span { display: block; color: var(--muted); font-size: 10px; font-weight: 700; line-height: 1.35; white-space: nowrap; }
.stat strong { display: block; font: 800 16px/1.3 var(--mono); white-space: nowrap; }

.play-area { display: flex; flex-direction: column; flex: 1; min-width: 0; min-height: 0; gap: 10px; }
.board-card, .gallery {
  min-width: 0; border: 1px solid var(--hairline); border-radius: var(--radius); background: var(--card);
  padding: 5px; box-shadow: var(--lift);
}
.board-card { width: fit-content; max-width: 100%; margin-inline: auto; }
.board {
  display: block; width: auto; max-width: 100%; height: auto; max-height: calc(100svh - var(--chrome, 280px));
  border-radius: calc(var(--radius) - 5px); background: var(--field); touch-action: none;
}
.board-title { display: flex; gap: 6px; justify-content: space-between; padding: 5px 6px 1px; color: var(--muted); }
.gallery { display: flex; flex-direction: column; overflow: hidden; max-height: 400px; }
.art {
  display: grid; flex: 1 1 110px; min-height: 96px; contain: size; place-items: center;
  overflow: hidden; border-radius: calc(var(--radius) - 5px); background: var(--field);
}
.art .illustration { width: var(--picture-width, 100%); max-width: 100%; max-height: 100%; aspect-ratio: var(--picture-ratio); }
.art .base { width: 100%; height: 100%; object-fit: contain; }
.piece { position: absolute; display: block; animation: flip .5s cubic-bezier(.2, .7, .2, 1); }
@keyframes flip { from { opacity: 0; filter: brightness(2.2); transform: scale(.82); } }
.gallery-caption { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 7px 1px; color: var(--muted); }
.gallery-caption span:first-child { font-size: 12px; font-weight: 750; }
.play-controls { display: grid; flex: none; gap: 6px; }
.control-row { display: flex; justify-content: center; gap: 6px; }
.touch { flex: 1; width: 100%; min-width: 44px; min-height: 54px; padding: 0 3px; line-height: 1.05; touch-action: none; user-select: none; -webkit-user-select: none; }
.touch b { display: block; font-size: 20px; }
.touch small { font-size: 10px; color: var(--muted); }
.touch.paint-on { background: var(--accent); color: var(--button-text); border-color: transparent; }
.touch.paint-on small { color: inherit; }

.blocks-layout { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(104px, 1fr); gap: 10px; }
.blocks-layout + .play-controls .touch { min-height: 62px; }
.blocks-layout > .board-card { margin: 0 0 auto; }
.play-blocks { --chrome: 290px; }
.blocks-aside { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.blocks-aside .gallery { flex: 1; }
.blocks-aside .board-card { width: auto; margin: 0; }
.next-piece { width: 100%; }
.progress-card { display: grid; gap: 6px; padding: 9px; border: 1px solid var(--hairline); border-radius: 14px; background: var(--card); color: var(--muted); }
.progress-track { height: 6px; overflow: hidden; border-radius: 99px; background: var(--hairline); }
.progress-fill { height: 100%; border-radius: 99px; background: var(--accent); transition: width .3s; }

.shooter-layout, .quiz-layout { display: flex; flex-direction: column; flex: 1; gap: 10px; min-height: 0; }
.shooter-layout .gallery, .quiz-layout .gallery { flex: 1 1 auto; }
.play-shooter { --chrome: 376px; aspect-ratio: 350 / 372; }
.territory-board { --chrome: 292px; }
.breaker-board { --chrome: 226px; }

.quiz-card { display: grid; gap: 12px; padding: 16px; border: 1px solid var(--hairline); border-radius: var(--radius); background: var(--card); box-shadow: var(--lift); }
.quiz-card h2 { font-family: var(--display); font-size: clamp(19px, 5.4vw, 25px); line-height: 1.4; text-wrap: pretty; }
.quiz-card .control-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); }
.choices { display: grid; gap: 7px; }
.choice { display: flex; align-items: center; gap: 10px; padding: 7px 11px; font-size: 14.5px; text-align: start; }
.choice[aria-pressed="true"] { border-color: var(--accent); background: var(--light-accent); box-shadow: inset 0 0 0 1px var(--accent); }
.choice.wrong { border-style: dashed; }
.choice-letter { display: grid; place-items: center; flex: 0 0 28px; height: 28px; border: 1px solid var(--hairline); border-radius: 50%; background: var(--card); font: 700 12px var(--mono); }
.choice.right .choice-letter, .choice.wrong .choice-letter { border-color: var(--accent); font-size: 15px; }
.verdict { min-height: 1.6em; font-weight: 800; text-align: center; }

dialog { color: var(--ink); }
dialog::backdrop { background: #0d0812c7; }
.pause { width: min(92vw, 360px); padding: 22px; border: 1px solid var(--hairline); border-radius: var(--radius); background: var(--paper); }
.pause form { display: grid; gap: 10px; }
.pause h2 { font-family: var(--display); font-size: 24px; }
.pause .hint { text-align: start; margin-bottom: 4px; }
.viewer { width: 100vw; height: 100dvh; max-width: none; max-height: none; padding: 0; border: 0; background: #08060be6; }
.viewer img { width: 100%; height: 100%; object-fit: contain; }
.viewer-close { position: fixed; top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right)); border-radius: 50%; font-size: 22px; }

/* Theme details */
.masthead { position: relative; }
:root:not([data-theme]) .masthead::after, [data-theme="tide"] .masthead::after {
  content: ''; position: absolute; left: 0; bottom: -1px; width: 46px; height: 3px; border-radius: 3px; background: var(--accent);
}
[data-theme="paper"] .masthead { border-bottom: 3px double var(--hairline); }
[data-theme="paper"] .mark-icon { border-radius: 4px 4px 14px 4px; }
[data-theme="paper"] .primary, [data-theme="paper"] .secondary, [data-theme="paper"] .touch, [data-theme="paper"] .choice,
[data-theme="paper"] .top-action, [data-theme="paper"] .stats, [data-theme="paper"] .progress-card { border-radius: 8px; }
[data-theme="paper"] .primary { box-shadow: inset 0 -3px 0 #00000026, 0 6px 16px var(--glow); letter-spacing: .1em; }
[data-theme="paper"] .hero-frame { box-shadow: var(--lift), 0 0 0 1px var(--hairline), 0 0 0 6px var(--card), 0 0 0 7px var(--hairline); margin: 7px; max-width: calc(100% - 14px); }
[data-theme="paper"] .intro h1, [data-theme="paper"] .result-head h1 { letter-spacing: 0; font-weight: 700; }
[data-theme="ribbon"] .shell {
  background:
    radial-gradient(ellipse at 8% 0, #5a2542b8 0, transparent 44%),
    radial-gradient(ellipse at 100% 92%, #48233bb8 0, transparent 52%),
    color-mix(in srgb, var(--paper) 88%, transparent);
}
[data-theme="ribbon"] .masthead { border-bottom-style: dashed; }
[data-theme="ribbon"] .mark-icon { border-radius: 50%; background: #3a1c35; color: #ffa9db; border: 1px solid #ff9ed493; box-shadow: 0 0 17px #ff78ca37; }
[data-theme="ribbon"] .board-card, [data-theme="ribbon"] .gallery {
  background-image: radial-gradient(circle at center, #e38bbb59 0 1px, transparent 1.6px); background-size: 7px 7px;
}
[data-theme="ribbon"] .intro h1, [data-theme="ribbon"] .result-head h1 { text-shadow: 0 0 28px #ffa5d480; }
[data-theme="ribbon"] .touch, [data-theme="ribbon"] .top-action { border-radius: 14px 14px 20px 20px; }
[data-theme="ribbon"] .primary { background: linear-gradient(100deg, #ffc2e3, #ff7fbd); border-radius: 99px; }
[data-theme="ribbon"] .hero-frame { box-shadow: 0 0 0 1px #ff9ed466, 0 0 0 5px #150e16, 0 0 0 6px #ff9ed433, 0 0 44px #fa57b440; margin: 6px; max-width: calc(100% - 12px); }

@media (max-height: 570px) and (orientation: landscape) {
  :root { --shell-width: 780px; }
  .shell { padding-block: 8px; }
  .stage { gap: 7px; margin-top: 7px; }
  .stage[data-screen="play"] { gap: 6px; }
  .masthead { padding-bottom: 5px; }
  .hero-frame > .base { max-height: 46svh; }
  .play-area { gap: 6px; }
  .shooter-layout, .quiz-layout { flex-direction: row; align-items: flex-start; }
  .shooter-layout > *, .quiz-layout > * { flex: 1; }
  .shooter-layout .gallery, .quiz-layout .gallery { align-self: stretch; }
  .shooter-layout .gallery, .quiz-layout .gallery { max-height: calc(100svh - 132px); }
  .play-shooter, .breaker-board { --chrome: 184px; }
  .territory-board, .play-blocks { --chrome: 152px; }
  .play-area:has(.blocks-layout), .play-area:has(.territory-board) { flex-direction: row; align-items: flex-start; }
  .play-area:has(.blocks-layout) .play-controls, .play-area:has(.territory-board) .play-controls { flex: 0 0 250px; }
  .blocks-layout { flex: 1; grid-template-columns: auto minmax(110px, 1fr); }
  .next-piece { width: auto; max-height: 52px; margin-inline: auto; }
  .progress-card { display: none; }
  .touch, .blocks-layout + .play-controls .touch { min-height: 46px; }
}

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