:root {
  color-scheme: light;
  --paper: #fffefa;
  --ink: #2c423c;
  --muted: #546d64;
  --line: #d0e0d9;
  --green: #356e60;
  --mint: #e8f4ee;
  --cut: #a9543d;
  --blue: #27638b;
  --gold: #9a641a;
  --serif: "Yu Mincho", "Hiragino Mincho ProN", "Noto Serif CJK JP", serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: radial-gradient(ellipse at 85% 0%, #deefe8, transparent 60%), #f5f3eb;
  font: 16px/1.65 "Yu Gothic", "Hiragino Kaku Gothic ProN", "Noto Sans CJK JP", sans-serif;
}

a, button, input { -webkit-tap-highlight-color: transparent; }
button { font: inherit; cursor: pointer; }
:focus-visible { outline: 3px solid #36749a; outline-offset: 3px; }

.lab-shell {
  max-width: 1100px;
  padding: 18px 24px 44px;
  margin: auto;
}

.lab-nav, .shape-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.lab-nav a, .shape-picker a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 6px 15px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--green);
  background: var(--paper);
  font-weight: 700;
  text-decoration: none;
}

.lab-nav a { font-size: .875rem; }
.lab-header { margin: 16px 0 14px; }
.lab-header h1 {
  margin: 0;
  font: 500 clamp(1.65rem, 5.5vw, 2.6rem)/1.28 var(--serif);
}
.lab-header p {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: .94rem;
}

.shape-picker { margin-bottom: 14px; }
.shape-picker a { gap: 6px; }
.shape-picker a[aria-current] {
  background: var(--green);
  border-color: var(--green);
  color: white;
}
.shape-picker small { font-size: .75rem; }

.lesson-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.85fr) minmax(255px, 1fr);
  grid-template-areas: "toolbar question" "diagram quiz" "controls lesson";
  align-items: start;
  gap: 9px 22px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 0 16px 40px #3864570b;
}

.diagram-toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  min-height: 44px;
  gap: 6px 12px;
  margin-inline: 1px;
}

.diagram-mode {
  color: var(--green);
  background: var(--mint);
  border-radius: 999px;
  padding: 4px 11px;
  font-size: .875rem;
  font-weight: 800;
}

.diagram-guide {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .875rem;
  color: var(--muted);
}
.diagram-guide::before {
  width: 22px;
  border-top: 2px dashed var(--cut);
  content: "";
}
.diagram-guide[data-line="fold"]::before { border-color: var(--green); }
.diagram-guide[data-line="joined"]::before { border: 2px solid var(--gold); border-radius: 3px; }

.diagram-area { grid-area: diagram; position: relative; min-width: 0; }

.diagram {
  display: block;
  width: 100%;
  height: clamp(300px, 42vw, 382px);
  border: 1px solid #dfeae4;
  border-radius: 16px;
  background:
    radial-gradient(circle at 48% 40%, #ffffffb3, transparent 78%),
    radial-gradient(circle, #b6d4c9 1px, transparent 1px) 4px 4px / 16px 16px,
    #edf6f1;
  cursor: grab;
  touch-action: pan-y pinch-zoom;
  user-select: none;
}
.diagram:active { cursor: grabbing; }
.diagram[data-flat] { cursor: default; }
.rotate-hint {
  position: absolute;
  right: 8px;
  bottom: 7px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #fffefad9;
  color: #476259;
  font-size: .75rem;
  pointer-events: none;
}
.surface-light { fill: url(#face-light); }
.base-label { fill: #31584d; stroke: #fffdfa; stroke-width: 3; paint-order: stroke; font-size: 16px; font-weight: 800; pointer-events: none; }
.model-edge { stroke: #416e66; stroke-width: 1.45; stroke-linecap: round; }
.model-edge.cut { stroke: var(--cut); stroke-width: 2.25; stroke-dasharray: 6 4; }
.model-edge.fold { stroke: #497a6f; stroke-width: 1.7; stroke-dasharray: 5 3; }
.model-edge.boundary { stroke: #456b65; }
.edge-outline { stroke: #fffefa; stroke-width: 7; stroke-linecap: round; }
.edge-mark { --edge-color: #587b71; stroke: var(--edge-color); stroke-width: 3; stroke-linecap: round; }
.edge-mark.target { --edge-color: var(--blue); stroke-width: 4; }
.edge-mark.wrong { --edge-color: var(--cut); stroke-dasharray: 4 3; stroke-width: 4; }
.edge-mark.correct { --edge-color: var(--gold); stroke-width: 4.5; }
.edge-coin { fill: #fffefa; stroke: #4a7165; stroke-width: 1.45; }
.edge-coin.target { fill: var(--blue); stroke: #fffefa; }
.edge-coin.wrong { fill: #fff3ec; stroke: var(--cut); }
.edge-coin.correct { fill: #ffe7a9; stroke: var(--gold); }
.edge-letter { fill: #315a50; font-size: 15px; font-weight: 800; pointer-events: none; }
.edge-letter.target { fill: #fff; }
.edge-letter.wrong { fill: #8d3925; }
.edge-letter.correct { fill: #6b4716; }
.edge-hit { stroke: transparent; stroke-width: 16px; pointer-events: stroke; }
.corner-hint { fill: #6c488b; stroke: #fffdf9; stroke-width: 2.2; pointer-events: none; }
.diagram [data-edge-choice] { cursor: pointer; }

.model-controls {
  grid-area: controls;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px 16px;
}
.action-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.fold-control {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  font-size: .875rem;
  color: var(--muted);
  white-space: nowrap;
}
.fold-control input {
  width: 100%;
  min-height: 36px;
  margin: 0;
  accent-color: var(--green);
  cursor: pointer;
}

.primary-button, .quiet-button, .edge-options button {
  min-height: 44px;
  padding: 6px 14px;
  border: 1px solid;
  border-radius: 12px;
  font-weight: 700;
}
.primary-button { min-width: 146px; background: var(--green); color: white; border-color: var(--green); }
.primary-button:hover { background: #2c5c50; }
.primary-button[data-tone="secondary"] { background: var(--paper); color: var(--green); border-color: var(--line); }
.primary-button[data-tone="secondary"]:hover { background: var(--mint); }
.quiet-button { background: #fffefa; color: var(--green); border-color: var(--line); font-size: .875rem; }
.quiet-button:hover { background: var(--mint); }
#next-base { margin-left: auto; }

#match-heading {
  grid-area: question;
  align-self: center;
  margin: 0;
  font: 600 clamp(1.10rem, 2vw, 1.45rem)/1.45 var(--serif);
}
.quiz-tools { grid-area: quiz; min-width: 0; }
.edge-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr)) minmax(64px, 1.2fr);
  gap: 6px;
}
.edge-options button { padding-inline: 6px; color: #285747; background: #f5fbf7; border-color: #a9ccbd; font-size: 1.15rem; }
.edge-options button:hover { background: var(--mint); }
.edge-options button[data-state="wrong"] { color: #8d3925; background: #fff1e9; border-color: #c57961; }
.edge-options #hint { color: #64417e; background: #f8f2fa; border-color: #decfe8; font-size: .875rem; }
.edge-options #hint[aria-pressed="true"] { background: #eee1f3; border-color: #9e7ab7; }
.match-feedback { margin: 0; font-size: .94rem; color: var(--muted); }
.edge-options:not([hidden]) + .match-feedback:not(:empty) { margin-top: 8px; }
.match-feedback[data-state="wrong"] { color: #843924; }
.match-feedback[data-state="correct"] { color: #6b4716; font-weight: 700; }
.hint-text { margin: 8px 0 0; padding: 6px 9px; border-radius: 8px; background: #f3eaf7; color: #58376d; font-size: .875rem; }
.model-explanation {
  grid-area: lesson;
  align-self: center;
  margin: 0;
  color: var(--muted);
  font-size: .875rem;
}

@media (max-width: 750px) {
  .lab-shell { padding: 9px 12px 30px; }
  .lab-header { margin: 9px 0; }
  .shape-picker { gap: 6px; margin-bottom: 11px; }
  .shape-picker a { padding-inline: 11px; font-size: .875rem; }
  .lesson-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "toolbar" "question" "diagram" "quiz" "controls" "lesson";
    gap: 6px;
    padding: 8px;
    border-radius: 18px;
  }
  #match-heading { font-family: inherit; font-size: 1rem; font-weight: 700; line-height: 1.42; padding-inline: 2px; }
  .diagram { height: clamp(192px, 55vw, 310px); border-radius: 13px; }
  .model-controls { gap: 5px 10px; }
  .model-explanation { padding: 7px 3px 1px; border-top: 1px solid var(--line); }
}

@media (max-width: 425px) {
  .model-controls { grid-template-columns: minmax(0, 1fr); }
  .action-buttons > button { flex: 1 1 auto; min-width: 0; }
  .diagram-toolbar { gap: 4px 8px; }
  .diagram-mode { padding-inline: 8px; }
  .diagram-guide::before { width: 16px; }
  #next-base { padding-inline: 9px; font-size: .8125rem; }
  .fold-control { gap: 6px; padding-inline: 3px; }
}
