:root {
  color-scheme: light;
  --paper: #eef2f1;
  --surface: #ffffff;
  --ink: #142c32;
  --muted: #5b6d71;
  --line: #c9d3d1;
  --teal: #247f76;
  --amber: #d79a31;
  --rose: #c95f7d;
  --blue: #4d79ad;
  --deep: #173138;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  letter-spacing: 0;
}
button, input { font: inherit; }

.topbar {
  min-height: 78px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}
.title-group { min-width: 260px; }
.title-group p { margin: 0 0 3px; color: var(--teal); font-size: 11px; font-weight: 750; }
h1 { margin: 0; font-size: 25px; line-height: 1.2; }
.controls { display: flex; align-items: center; justify-content: flex-end; gap: 14px; }
.segmented {
  display: grid;
  grid-template-columns: repeat(2, 82px);
  overflow: hidden;
  border: 1px solid #9baba9;
  border-radius: 6px;
  background: #edf1f0;
}
.segmented button {
  min-height: 36px;
  padding: 7px 9px;
  border: 0;
  border-right: 1px solid #9baba9;
  background: transparent;
  color: #405255;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-pressed="true"] { background: var(--deep); color: #fff; }
.count-control { width: 142px; display: grid; gap: 3px; color: var(--muted); font-size: 11px; font-weight: 700; }
.count-control span { display: flex; justify-content: space-between; }
.count-control input { width: 100%; accent-color: var(--teal); }
.rotate-control { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 11px; white-space: nowrap; }
.rotate-control input { width: 16px; height: 16px; accent-color: var(--teal); }
.answer-toggle {
  min-height: 36px;
  padding: 7px 12px;
  border: 1px solid var(--teal);
  border-radius: 6px;
  background: #fff;
  color: var(--teal);
  cursor: pointer;
  font-size: 12px;
  font-weight: 750;
}
.answer-toggle[aria-pressed="true"] { background: var(--teal); color: #fff; }
button:focus-visible, input:focus-visible { outline: 3px solid #79b8d0; outline-offset: 2px; }

.workspace {
  height: calc(100vh - 184px);
  min-height: 590px;
  display: grid;
  grid-template-columns: 226px minmax(460px, 1fr) minmax(330px, 390px);
}
.problem-rail { overflow: auto; background: #f7f9f8; border-right: 1px solid var(--line); }
.rail-heading { height: 56px; padding: 11px 14px; border-bottom: 1px solid var(--line); display: flex; align-items: baseline; justify-content: space-between; }
.rail-heading strong { font-size: 16px; }
.rail-heading span { color: var(--muted); font-size: 10px; }
.problem-list { display: grid; }
.problem-row {
  width: 100%;
  min-height: 58px;
  padding: 9px 12px;
  border: 0;
  border-bottom: 1px solid #dce3e1;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 8px;
}
.problem-row:hover { background: #eaf0ee; }
.problem-row[aria-current="true"] { background: #dcebe7; box-shadow: inset 4px 0 0 var(--teal); }
.problem-row b { color: var(--teal); font-size: 12px; }
.problem-row span { display: block; font-family: Consolas, monospace; font-size: 13px; font-weight: 700; }
.problem-row small { display: block; margin-top: 3px; color: var(--muted); font-size: 10px; }

.stage { position: relative; min-width: 0; overflow: hidden; background: #dfe8e6; border-right: 1px solid var(--line); }
#three-canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.live-measure {
  position: absolute;
  top: 16px;
  left: 16px;
  min-width: 210px;
  padding: 10px 12px;
  display: grid;
  gap: 4px;
  background: rgba(255,255,255,.93);
  border-left: 4px solid var(--teal);
  box-shadow: 0 8px 22px rgba(25,49,54,.12);
}
.live-measure span { color: var(--muted); font-size: 10px; }
.live-measure strong { font-family: Georgia, "Yu Mincho", serif; font-size: 18px; }
.stage-key {
  position: absolute;
  left: 14px;
  bottom: 14px;
  max-width: calc(100% - 28px);
  padding: 8px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  border: 1px solid rgba(104,128,125,.55);
  border-radius: 5px;
  background: rgba(255,255,255,.9);
  color: #42575a;
  font-size: 10px;
}
.stage-key span { display: flex; align-items: center; gap: 6px; }
.stage-key .dot { width: 9px; height: 9px; border-radius: 50%; background: #fff; border: 2px solid var(--deep); }
.stage-key .line { width: 20px; border-top: 3px solid var(--amber); }
.stage-key .line.total { border-top-color: var(--deep); border-top-style: dashed; }

.problem-pane { overflow: auto; padding: 20px 22px 28px; background: var(--surface); }
.problem-pane section { margin: 0 0 19px; padding: 0 0 19px; border-bottom: 1px solid #d9e1df; }
.problem-pane section:last-child { border-bottom: 0; }
.problem-number { margin-bottom: 8px; color: var(--teal); font-size: 11px; font-weight: 800; }
.statement { margin: 0; font-family: "Yu Mincho", "Hiragino Mincho ProN", serif; font-size: 15px; line-height: 1.9; }
.problem-pane h2 { margin: 0 0 9px; font-size: 14px; }
.problem-pane p { margin: 0; color: #405257; font-size: 12px; line-height: 1.75; }
.problem-pane .note { margin-top: 9px; color: var(--muted); font-size: 11px; }
.motif { display: flex; align-items: center; gap: 5px; overflow-x: auto; padding-bottom: 3px; }
.motif span { flex: 0 0 28px; height: 28px; display: grid; place-items: center; border: 1px solid #aebdba; background: #e8efed; font-family: Georgia, serif; font-size: 11px; }
.motif b { flex: 0 0 16px; color: var(--rose); text-align: center; font-size: 15px; }
.formula { padding: 9px 11px; border-left: 3px solid var(--amber); background: #f5f0e4; font-family: Georgia, "Yu Mincho", serif; font-size: 18px; font-weight: 700; }
.answer-detail p { margin-top: 10px; }

.answer-band {
  min-height: 106px;
  padding: 13px 20px;
  display: grid;
  grid-template-columns: 190px 180px 1fr;
  align-items: center;
  gap: 20px;
  background: var(--deep);
  color: #fff;
}
.answer-band span { color: #83c8bd; font-size: 11px; font-weight: 800; }
.answer-band strong { color: #f1c367; font-family: Georgia, serif; font-size: 25px; }
.answer-band p { margin: 0; color: #ccdadb; font-size: 11px; line-height: 1.65; }
#error-message { position: fixed; inset: 20px; z-index: 10; padding: 20px; background: #fff; border: 2px solid #b7355d; color: #7b1634; }

@media (max-width: 1080px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .controls { width: 100%; justify-content: space-between; }
  .workspace { height: auto; grid-template-columns: 190px minmax(0, 1fr); }
  .stage { height: 600px; border-right: 0; }
  .problem-pane { grid-column: 1 / -1; border-top: 1px solid var(--line); }
}

@media (max-width: 700px) {
  .topbar { padding: 12px 14px; }
  h1 { font-size: 21px; }
  .controls { display: grid; grid-template-columns: 1fr 1fr; }
  .segmented { grid-column: 1 / -1; grid-template-columns: repeat(2, 1fr); }
  .count-control { width: 100%; }
  .workspace { display: block; }
  .problem-rail { border-right: 0; border-bottom: 1px solid var(--line); }
  .rail-heading { display: none; }
  .problem-list { display: flex; overflow-x: auto; }
  .problem-row { min-width: 150px; border-right: 1px solid #dce3e1; }
  .stage { height: 54vh; min-height: 380px; }
  .problem-pane { padding: 18px 16px 24px; }
  .answer-band { grid-template-columns: 1fr; gap: 7px; padding: 17px 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

