:root {
  color-scheme: light;
  --paper: #f4ebd5;
  --paper-light: #fffaf0;
  --ink: #20363c;
  --ink-soft: #557078;
  --coral: #db654f;
  --sun: #e8b84d;
  --mint: #63a995;
  --blue: #4f7894;
  --danger: #a33e39;
  --line: #2e4a51;
  --shadow: rgba(35, 42, 37, .2);
}

* { box-sizing: border-box; }

html { background: #314b4e; }

body {
  min-width: 280px;
  min-height: 100vh;
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(255,255,255,.035) 1px, transparent 1px) 0 0 / 34px 34px,
    linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px) 0 0 / 34px 34px,
    #314b4e;
  font-family: "Yu Mincho", "Hiragino Mincho ProN", "Noto Serif JP", serif;
}

button { font: inherit; }
button:focus-visible, [tabindex]:focus-visible { outline: 4px solid #145d7c; outline-offset: 3px; }

.paper-grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .16;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Cfilter id='n'%3E%3CfeTurbulence baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.2'/%3E%3C/svg%3E");
}

.masthead {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 13px;
  width: min(1040px, calc(100% - 24px));
  margin: 14px auto 0;
  color: #fff9e7;
}

.masthead h1 { margin: 1px 0 0; font-size: clamp(1rem, 3.5vw, 1.45rem); letter-spacing: .05em; }
.eyebrow { margin: 0; font-family: ui-monospace, Consolas, monospace; font-size: .72rem; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }

.brand-mark {
  display: grid;
  width: 47px;
  height: 47px;
  place-items: center;
  flex: 0 0 auto;
  border: 2px solid #ffe7ae;
  transform: rotate(-4deg);
  color: #ffe7ae;
  font-size: 1.45rem;
  font-weight: 900;
}

.tool-button {
  min-height: 44px;
  margin-left: auto;
  padding: 8px 12px;
  border: 1px solid #f5e5bf;
  border-radius: 2px;
  color: #fff9e7;
  background: transparent;
  cursor: pointer;
}

main { position: relative; z-index: 1; width: min(1040px, calc(100% - 24px)); margin: 13px auto 38px; }
.is-hidden { display: none !important; }

.start-sheet, .game-shell, .quiz-sheet, .result-sheet {
  position: relative;
  border: 1px solid #d8c9a8;
  box-shadow: 0 18px 45px rgba(17, 28, 28, .32);
  background: var(--paper);
}

.start-sheet {
  display: grid;
  grid-template-columns: minmax(250px, .9fr) minmax(300px, 1.1fr);
  min-height: 590px;
  overflow: hidden;
}

.start-copy { align-self: center; padding: clamp(28px, 5vw, 70px); }
.start-copy h2 { max-width: 12ch; margin: 12px 0 18px; font-size: clamp(2rem, 5vw, 4.1rem); line-height: 1.14; }
.start-copy > p:not(.stamp):not(.small-note) { line-height: 1.95; }
.stamp { display: inline-block; margin: 0; padding: 5px 9px 4px; border: 2px solid var(--coral); transform: rotate(-1deg); color: var(--coral); font-weight: 900; letter-spacing: .08em; }

.torn-shape { position: relative; min-height: 430px; background: #243d42; overflow: hidden; }
.torn-shape::before { content: "切る\A移す\A同じ広さ"; white-space: pre; position: absolute; left: 8%; top: 9%; color: rgba(255,255,255,.16); font-size: clamp(2rem, 5vw, 4.5rem); line-height: 1.35; font-weight: 900; letter-spacing: .12em; }
.torn-shape::after { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(23deg, transparent 0 28px, rgba(255,255,255,.035) 29px 30px); }
.torn-shape span { position: absolute; z-index: 1; filter: drop-shadow(0 12px 8px rgba(0,0,0,.25)); }
.shape-a { width: 47%; aspect-ratio: 1.4; left: 11%; top: 37%; background: var(--sun); clip-path: polygon(0 0, 100% 0, 76% 100%, 18% 100%); transform: rotate(-10deg); }
.shape-b { width: 38%; aspect-ratio: 1.1; left: 47%; top: 28%; background: var(--coral); clip-path: polygon(0 0, 100% 52%, 0 100%); transform: rotate(15deg); }
.shape-c { width: 34%; aspect-ratio: 1.1; left: 42%; top: 62%; background: var(--mint); clip-path: polygon(0 18%, 100% 0, 83% 100%, 8% 81%); transform: rotate(5deg); }

.quick-rules { margin: 24px 0; padding: 0; list-style: none; }
.quick-rules li { display: flex; align-items: center; gap: 10px; margin: 10px 0; line-height: 1.5; }
.quick-rules span { display: grid; width: 28px; height: 28px; place-items: center; flex: 0 0 auto; border: 1px solid var(--ink); border-radius: 50%; font-family: ui-monospace, monospace; font-weight: 900; }

.primary-action, .secondary-action, .tool-tray button {
  min-height: 48px;
  border: 0;
  border-radius: 2px;
  box-shadow: 4px 4px 0 #172b31;
  cursor: pointer;
  font-weight: 900;
}
.primary-action { padding: 12px 24px; color: white; background: var(--coral); }
.primary-action:hover { transform: translate(-1px, -1px); box-shadow: 6px 6px 0 #172b31; }
.small-note { color: var(--ink-soft); font-size: .82rem; }

.game-shell { padding: clamp(13px, 3vw, 28px); }
.status-ribbon { display: grid; grid-template-columns: repeat(3, 1fr); border: 2px solid var(--ink); background: var(--paper-light); }
.status-ribbon > div { display: flex; flex-direction: column; padding: 8px 12px; border-right: 1px dashed var(--ink-soft); }
.status-ribbon > div:last-child { border-right: 0; }
.status-label { color: var(--ink-soft); font-size: .72rem; font-weight: 800; }
.status-ribbon strong { font-family: ui-monospace, Consolas, monospace; font-size: 1.25rem; }

.mission-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin: 22px 3px 10px; }
.mission-heading h2 { margin: 4px 0 0; font-size: clamp(1.35rem, 3vw, 2.15rem); }
.mission-hint { max-width: 38ch; margin: 0; color: var(--ink-soft); line-height: 1.6; text-align: right; }

.danger-banner { display: none; margin: 10px 0; padding: 9px 12px; border: 2px dashed var(--danger); color: var(--danger); background: #fff0e7; }
.danger-banner.is-visible { display: block; animation: stampIn .3s ease-out; }
.danger-banner span { display: inline-grid; width: 24px; height: 24px; place-items: center; margin-right: 5px; border: 2px solid currentColor; border-radius: 50%; }

.workbench { position: relative; border: 3px solid #6b4d35; background: #9a7655; box-shadow: inset 0 0 0 6px #b6936d, inset 0 0 20px rgba(45,22,10,.38); }
.bench-label { position: absolute; z-index: 3; top: 10px; padding: 4px 9px; border: 1px solid var(--line); background: #fff8e8; box-shadow: 2px 2px 0 rgba(0,0,0,.18); font-size: .72rem; font-weight: 900; }
.source-label { left: 4%; transform: rotate(-1deg); }
.target-label { left: 56%; transform: rotate(1deg); }
.puzzle-board { display: block; width: 100%; max-height: 600px; color: var(--blue); touch-action: pan-y; user-select: none; }
.board-paper { fill: #f9f0d9; stroke: #342f2a; stroke-width: 2; }
.grid-layer { color: var(--blue); }
.stitch-line { fill: none; stroke: #967f68; stroke-width: 2; stroke-dasharray: 4 6; }

.target-piece polygon { fill: rgba(60,80,82,.055); stroke: #536d73; stroke-width: 2.5; stroke-dasharray: 7 5; vector-effect: non-scaling-stroke; }
.target-piece.is-ready polygon { stroke: var(--coral); stroke-width: 4; stroke-dasharray: 3 3; }
.puzzle-piece { cursor: grab; filter: url(#paperShadow); transition: filter .16s, opacity .2s; }
.puzzle-piece polygon { stroke: #1d343a; stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.puzzle-piece text { pointer-events: none; fill: #183136; font: 900 15px ui-monospace, Consolas, monospace; text-anchor: middle; dominant-baseline: central; }
.puzzle-piece[data-color="coral"] polygon { fill: var(--coral); }
.puzzle-piece[data-color="sun"] polygon { fill: var(--sun); }
.puzzle-piece[data-color="mint"] polygon { fill: var(--mint); }
.puzzle-piece[data-color="ink"] polygon { fill: #688997; }
.puzzle-piece.is-selected polygon { stroke: #fff; stroke-width: 5; filter: drop-shadow(0 0 0 #112b33); }
.puzzle-piece.is-selected { filter: drop-shadow(0 0 0 #102a31) drop-shadow(0 0 5px #102a31); }
.puzzle-piece.is-placed { cursor: default; }
.puzzle-piece.is-placed polygon { stroke: #fff6dd; }
.puzzle-piece.is-wrong { animation: paperShake .3s ease; }
.workbench.is-danger { box-shadow: inset 0 0 0 6px #b6936d, inset 0 0 34px rgba(163,62,57,.7); }
.workbench.is-success { animation: successRelease .8s ease; }

.selected-readout { min-height: 34px; padding: 7px 12px; border-top: 2px dashed #6b4d35; color: #fffaf0; background: #5e4635; font-weight: 800; text-align: center; }
.tool-tray { display: grid; grid-template-columns: 1fr .72fr 1.25fr; gap: 9px; margin: 12px 0; }
.tool-tray button { padding: 10px 12px; border: 2px solid var(--ink); color: var(--ink); background: #fff9e9; box-shadow: 3px 3px 0 var(--ink); }
.tool-tray button:disabled { cursor: not-allowed; opacity: .43; }
.tool-tray small { display: block; font-size: .7rem; font-weight: 600; }

.formula-strip { display: flex; align-items: baseline; gap: 14px; padding: 12px 15px; border-left: 7px solid var(--sun); background: #fff9e9; }
.formula-strip span { color: var(--ink-soft); font-size: .78rem; }
.formula-strip strong { font-size: clamp(1rem, 2.5vw, 1.35rem); }
.feedback-line { min-height: 1.8em; margin: 12px 2px 0; line-height: 1.7; }

.quiz-sheet, .result-sheet { min-height: 560px; padding: clamp(26px, 6vw, 74px); }
.quiz-sheet h2, .result-sheet h2 { margin: 20px 0 8px; font-size: clamp(1.7rem, 4vw, 3.2rem); }
.quiz-progress { font-family: ui-monospace, monospace; color: var(--ink-soft); }
.quiz-question { max-width: 32ch; font-size: clamp(1.15rem, 3vw, 1.65rem); font-weight: 900; line-height: 1.65; }
.quiz-choices { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; max-width: 760px; }
.quiz-choices button { min-width: 0; min-height: 58px; padding: 10px; overflow-wrap: anywhere; border: 2px solid var(--ink); color: var(--ink); background: #fffaf0; cursor: pointer; font-weight: 800; text-align: left; }
.quiz-choices button.correct { border-color: #276b59; background: #d8f1e6; }
.quiz-choices button.wrong { border-color: var(--danger); background: #f9ddd5; text-decoration: line-through; }
.quiz-explanation { max-width: 740px; min-height: 56px; margin: 14px 0; padding: 12px; border-left: 5px solid var(--mint); background: rgba(255,255,255,.55); line-height: 1.7; }

.result-sheet { overflow: hidden; }
.result-sheet::after { content: ""; position: absolute; right: -8%; bottom: -18%; width: 44%; aspect-ratio: 1; border: 30px double rgba(79,120,148,.11); transform: rotate(17deg); }
.result-seal { position: relative; z-index: 1; display: grid; width: 112px; aspect-ratio: 1; place-items: center; float: right; border: 5px double var(--coral); border-radius: 50%; transform: rotate(7deg); color: var(--coral); font-size: 1.3rem; font-weight: 900; line-height: 1.05; text-align: center; }
.result-score { display: flex; width: min(450px, 100%); align-items: baseline; justify-content: space-between; margin: 28px 0 12px; padding: 16px 20px; border-block: 2px solid var(--ink); }
.result-score strong { font-family: ui-monospace, Consolas, monospace; font-size: clamp(2rem, 8vw, 4.6rem); }
.result-actions { display: flex; flex-wrap: wrap; gap: 13px; margin: 22px 0; }
.secondary-action { padding: 12px 18px; border: 2px solid var(--ink); color: var(--ink); background: #fff9e9; }
.leaderboard-panel { position: relative; z-index: 2; max-width: 780px; margin-top: 20px; padding: 18px; border: 2px solid var(--ink); background: #fff9e9; color: var(--ink); overflow-wrap: anywhere; }
.leaderboard-panel h3 { margin-top: 0; }

.modal-backdrop { position: fixed; z-index: 20; inset: 0; display: grid; place-items: center; padding: 18px; background: rgba(20,37,40,.72); }
.modal-paper { width: min(610px, 100%); max-height: calc(100vh - 36px); overflow-y: auto; padding: clamp(25px, 6vw, 54px); border: 2px solid #ccb993; box-shadow: 12px 15px 0 rgba(0,0,0,.22); background: var(--paper-light); }
.modal-paper h2 { font-size: clamp(1.6rem, 5vw, 2.8rem); }
.modal-paper p { line-height: 1.8; }
.big-formula { margin: 18px 0; padding: 14px; border: 2px dashed var(--blue); color: var(--blue); font-size: clamp(1.1rem, 3vw, 1.55rem); font-weight: 900; }

@keyframes paperShake { 0%,100%{transform:translate(0)} 30%{transform:translate(-4px,2px)} 65%{transform:translate(4px,-1px)} }
@keyframes stampIn { from{opacity:0;transform:scale(.96) rotate(-1deg)} to{opacity:1;transform:scale(1)} }
@keyframes successRelease { 0%{filter:saturate(1)} 45%{filter:saturate(1.7) brightness(1.1)} 100%{filter:saturate(1)} }

@media (max-width: 720px) {
  .start-sheet { grid-template-columns: 1fr; }
  .torn-shape { min-height: 240px; }
  .start-copy { padding: 26px 20px 34px; }
  .mission-heading { display: block; }
  .mission-hint { margin-top: 8px; text-align: left; }
  .tool-tray { grid-template-columns: 1fr 1fr; }
  .tool-tray #repairButton { grid-column: 1 / -1; }
  .quiz-choices { grid-template-columns: 1fr; }
  .result-seal { width: 86px; }
}

@media (max-width: 430px) {
  .masthead { align-items: flex-start; }
  .brand-mark { width: 39px; height: 39px; }
  .masthead .eyebrow { display: none; }
  .tool-button { padding-inline: 8px; font-size: .78rem; }
  main { width: min(100% - 12px, 1040px); margin-top: 8px; }
  .game-shell { padding: 8px; }
  .status-ribbon strong { font-size: 1rem; }
  .status-ribbon > div { padding: 7px 6px; }
  .bench-label { top: 6px; font-size: .62rem; }
  .tool-tray button { font-size: .83rem; }
  .formula-strip { display: block; }
  .formula-strip span { display: block; margin-bottom: 5px; }
  .quiz-sheet, .result-sheet { padding: 23px 17px; }
  .leaderboard-panel { margin-inline: -8px; padding: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
