/* Layout: 左に立体の舞台（杭と玉）、右に段ごとの断面と操作。狭い画面では縦積み */
:root {
  --bg: #e7ebf0;
  --panel: #f6f8fa;
  --stage: #dfe5ec;
  --ink: #17202b;
  --muted: #5a6778;
  --line: #c6cfd9;
  --p1: #e8801a;
  --p1-deep: #9c4b00;
  --p2: #2b57d0;
  --p2-deep: #142c78;
  --plate: #c3ccd7;
  --plate-edge: #9eaaba;
  --rod: #7f8b9c;
  --hover: #17202b;
  --win: #f2c230;
  --focus: #2b57d0;
  --danger: #c4291c;
  --good: #1f8a4c;
  --f-display: "Dela Gothic One", "Hiragino Sans", "Yu Gothic", system-ui, sans-serif;
  --f-body: "Zen Kaku Gothic New", "Hiragino Sans", "Yu Gothic", "Noto Sans JP", system-ui, sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1319; --panel: #161c24; --stage: #121820; --ink: #e5eaf0; --muted: #8b97a7; --line: #283240;
    --p1: #f28e2b; --p1-deep: #8a3f00; --p2: #5b84f0; --p2-deep: #1b3480;
    --plate: #262f3b; --plate-edge: #1b222c; --rod: #56627a; --hover: #e5eaf0; --win: #f5cc3d; --focus: #8eabf7; --danger: #ff6b5e; --good: #4cc983;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1319; --panel: #161c24; --stage: #121820; --ink: #e5eaf0; --muted: #8b97a7; --line: #283240;
  --p1: #f28e2b; --p1-deep: #8a3f00; --p2: #5b84f0; --p2-deep: #1b3480;
  --plate: #262f3b; --plate-edge: #1b222c; --rod: #56627a; --hover: #e5eaf0; --win: #f5cc3d; --focus: #8eabf7; --danger: #ff6b5e; --good: #4cc983;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--f-body);
  font-size: 15px;
  line-height: 1.6;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

.app {
  max-width: 1180px;
  margin: 0 auto;
  padding-block: 14px 24px;
  display: grid;
  gap: 14px;
}

header.top {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 16px;
  justify-content: space-between;
}
h1 {
  font-family: var(--f-display);
  font-weight: 400;
  font-size: clamp(26px, 4vw, 38px);
  letter-spacing: 0.02em;
  margin: 0;
  line-height: 1.1;
}
h1 small {
  font-family: var(--f-mono);
  font-size: 12px; letter-spacing: 0.18em; color: var(--muted);
  margin-left: 10px; vertical-align: middle;
}
.tagline { color: var(--muted); font-size: 13px; margin: 0; }

.main {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(300px, 1fr);
  gap: 14px;
  align-items: start;
}
@media (max-width: 820px) { .main { grid-template-columns: minmax(0, 1fr); } }

.stage {
  position: relative;
  background: var(--stage);
  border-radius: 14px;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  max-width: 100%;
  max-height: 78vh;
  width: 100%;
}
.stage canvas { width: 100%; height: 100%; display: block; touch-action: none; cursor: pointer; }
.stage .hint {
  position: absolute; left: 12px; bottom: 10px; font-size: 12px; color: var(--muted);
  pointer-events: none;
}
.stage .viewbtns { position: absolute; right: 10px; bottom: 8px; display: flex; gap: 6px; }

.side { display: grid; gap: 14px; min-width: 0; }

.status {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; background: var(--panel); border-radius: 12px;
  border: 1px solid var(--line);
}
.status .who { font-weight: 700; font-size: 17px; }
.status .sub { color: var(--muted); font-size: 13px; }
.status .sub.warn { color: var(--danger); font-weight: 700; }
.status .sub.good { color: var(--good); font-weight: 700; }
.stage.previewing::after, .layers.previewing::after {
  content: attr(data-label); position: absolute; top: 10px; left: 12px;
  font-size: 12px; font-weight: 700; color: var(--panel); background: var(--ink);
  padding: 2px 8px; border-radius: 6px; pointer-events: none;
}
.layers { position: relative; }
.layers.previewing { box-shadow: 0 0 0 2px var(--ink) inset; }
.layers.previewing::after { top: auto; bottom: -11px; left: 50%; transform: translateX(-50%); white-space: nowrap; }

.clock {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 12px; align-items: center;
  padding: 10px 14px; background: var(--panel); border-radius: 12px; border: 1px solid var(--line);
}
.clock-arrow {
  width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center;
  font-size: 26px; font-weight: 700; background: var(--bg); border: 2px solid var(--line); line-height: 1;
}
.clock.imminent .clock-arrow { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.clock.imminent { border-color: var(--ink); }
.clock-title { font-size: 15px; }
.clock-title b { font-family: var(--f-mono); font-size: 17px; }
.clock-sub { font-size: 12px; color: var(--muted); }
.cycle span { font-family: var(--f-mono); margin: 0 1px; opacity: 0.45; }
.cycle span.cur { opacity: 1; font-weight: 700; color: var(--ink); }
.pips { display: flex; gap: 4px; margin: 4px 0 2px; }
.pip {
  width: 14px; height: 14px; border-radius: 50%; border: 1.5px solid var(--line); background: transparent;
}
.pip.p1 { border-color: color-mix(in srgb, var(--p1) 55%, var(--line)); }
.pip.p2 { border-color: color-mix(in srgb, var(--p2) 55%, var(--line)); }
.pip.done.p1 { background: var(--p1); border-color: var(--p1); }
.pip.done.p2 { background: var(--p2); border-color: var(--p2); }
.pip.now { box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--ink); }
.pip.trigger { border-radius: 3px; }
.after { grid-column: 1 / -1; display: grid; gap: 4px; border-top: 1px dashed var(--line); padding-top: 8px; }
.after-title { font-size: 12px; color: var(--muted); font-weight: 700; }
.after-title b { color: var(--ink); }
.minis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.mini { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; aspect-ratio: 1 / 1; max-width: 76px; width: 100%; justify-self: center; }
.mini i { border-radius: 50%; background: var(--stage); position: relative; }
.mini i.s1 { background: var(--p1); } .mini i.s2 { background: var(--p2); }
.mini i.t1 { box-shadow: inset 0 0 0 2px var(--p1); } .mini i.t2 { box-shadow: inset 0 0 0 2px var(--p2); }
.mini i.t1.t2 { box-shadow: inset 0 0 0 2px var(--p1), 0 0 0 1px var(--p2); }
.mini i.imm { animation: blink 1s steps(2, start) infinite; }
.mini i.win { box-shadow: 0 0 0 2px var(--win); }
.mini i.mark::after { content: ""; position: absolute; inset: 32%; border-radius: 50%; background: rgba(255,255,255,0.95); }
@keyframes blink { 50% { opacity: 0.35; } }
.moves i { font-style: normal; font-weight: 700; color: var(--ink); margin-left: 2px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; vertical-align: -2px; margin-right: 4px; }
.legend i.imm { border: 3px solid var(--ink); }
.legend i.pend { border: 2px dashed var(--ink); }
.legend i.nextc { background: color-mix(in srgb, var(--rod) 30%, var(--stage)); }
.legend b { color: var(--danger); margin-right: 4px; }
.chip {
  width: 22px; height: 22px; border-radius: 50%; flex: none;
  box-shadow: inset -3px -4px 6px rgba(0,0,0,0.25), inset 2px 2px 4px rgba(255,255,255,0.45);
}
.chip.p1 { background: var(--p1); }
.chip.p2 { background: var(--p2); }
.status.win { border-color: var(--win); box-shadow: 0 0 0 2px var(--win) inset; }

.layers {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;
  padding: 12px; background: var(--panel); border-radius: 12px; border: 1px solid var(--line);
}
.axis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; margin-top: 2px; }
.axis span { font-family: var(--f-mono); font-size: 10px; color: var(--muted); text-align: center; line-height: 1.2; }
.grid-wrap { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 3px; }
.yaxis { display: grid; grid-template-rows: repeat(4, 1fr); }
.yaxis span { font-family: var(--f-mono); font-size: 10px; color: var(--muted); display: grid; align-items: center; }
.mini-wrap { display: grid; gap: 2px; justify-items: center; }
.mini-wrap small { font-size: 10px; color: var(--muted); font-family: var(--f-mono); }
.pip-key { font-size: 11px; color: var(--muted); margin-left: 6px; }
.layer h3 {
  margin: 0 0 4px; font-size: 11px; font-weight: 700; color: var(--muted);
  letter-spacing: 0.08em; font-family: var(--f-mono); text-align: center;
}
.grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px;
  aspect-ratio: 1 / 1;
}
.cell {
  appearance: none; border: 0; padding: 0; margin: 0;
  background: var(--stage); border-radius: 50%;
  aspect-ratio: 1 / 1; cursor: pointer; position: relative;
}
.cell.locked { background: color-mix(in srgb, var(--stage) 55%, var(--panel)); cursor: default; }
.cell.next { background: color-mix(in srgb, var(--rod) 30%, var(--stage)); }
.cell.hov { box-shadow: inset 0 0 0 2px var(--hover); }
.cell.s1 { background: radial-gradient(circle at 35% 30%, rgba(255,255,255,.7), var(--p1) 30%, var(--p1-deep)); }
.cell.s2 { background: radial-gradient(circle at 35% 30%, rgba(255,255,255,.7), var(--p2) 30%, var(--p2-deep)); }
.cell.last::after {
  content: ""; position: absolute; inset: 36%; border-radius: 50%; background: rgba(255,255,255,0.92);
}
.cell.win { box-shadow: 0 0 0 2.5px var(--win); }
.cell.t1::before, .cell.t2::before {
  content: ""; position: absolute; inset: 14%; border-radius: 50%;
  border: 2px dashed var(--p1);
}
.cell.t2::before { border-color: var(--p2); }
.cell.t1.imm::before, .cell.t2.imm::before { border-style: solid; border-width: 3px; }
.cell.t1.t2::before { border-color: var(--p1) var(--p2) var(--p1) var(--p2); }
.cell.poison::after {
  content: "×"; position: absolute; inset: 0; display: grid; place-items: center;
  font-weight: 700; font-size: 15px; color: var(--danger); line-height: 1;
}
.cell.fork::after {
  content: "△"; position: absolute; inset: 0; display: grid; place-items: center;
  font-weight: 700; font-size: 12px; color: var(--danger); line-height: 1; opacity: 0.85;
}
.cell.must::after {
  content: "!"; position: absolute; inset: 8%; display: grid; place-items: center; border-radius: 50%;
  font-weight: 700; font-size: 14px; color: var(--panel); background: var(--danger); line-height: 1;
}
.cell.winning::after {
  content: "★"; position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 13px; color: var(--good); line-height: 1;
}
.cell.pv { background: color-mix(in srgb, var(--pvc) 26%, var(--stage)); }
.cell.s1.pv, .cell.s2.pv { outline: 2px solid var(--pvc); outline-offset: -1px; }
.cell:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

.controls {
  display: grid; gap: 10px; padding: 12px 14px; background: var(--panel);
  border-radius: 12px; border: 1px solid var(--line);
}
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
label.field { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
select, button.btn {
  font: inherit; font-size: 14px; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px;
}
button.btn { cursor: pointer; font-weight: 700; }
button.btn:hover { border-color: var(--ink); }
button.btn.primary { background: var(--ink); color: var(--panel); border-color: var(--ink); }
button.btn:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, select:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.small { font-size: 12px; padding: 4px 9px !important; }

.moves {
  font-family: var(--f-mono); font-size: 12px; color: var(--muted);
  max-height: 84px; overflow: auto; line-height: 1.7; word-break: break-word;
}
.moves b.p1 { color: var(--p1); } .moves b.p2 { color: var(--p2); }

details.rules {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
}
details.rules summary { cursor: pointer; font-weight: 700; }
details.rules h4 { margin: 12px 0 0; font-size: 13px; letter-spacing: 0.06em; color: var(--muted); }
details.rules ul { padding-left: 1.2em; margin: 6px 0; }
.howto {
  list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px;
  counter-reset: step;
}
.howto li {
  counter-increment: step; font-size: 13px; color: var(--muted); line-height: 1.5;
  display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: start; min-width: 0;
}
.howto li::before {
  content: counter(step); font-family: var(--f-mono); font-weight: 600; font-size: 12px;
  width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center;
  background: var(--ink); color: var(--panel);
}
.howto b { color: var(--ink); }
@media (max-width: 820px) { .howto { grid-template-columns: minmax(0, 1fr); gap: 4px; } }
.howto-box > summary { cursor: pointer; font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
@media (min-width: 821px) { .howto-box > summary { display: none; } }
@media (max-width: 820px) {
  .stage { aspect-ratio: 5 / 4; }
  .clock { grid-template-columns: auto minmax(0, 1fr); }
  .clock #peek { grid-column: 1 / -1; justify-self: start; }
  .mini { max-width: 92px; }
}
.mini i.imm::before { content: ""; position: absolute; inset: 30%; border-radius: 50%; background: currentColor; }
.mini i.t1 { color: var(--p1); } .mini i.t2 { color: var(--p2); }
.clock.over .pips, .clock.over .after, .clock.over #peek { display: none; }
details.rules p, details.rules li { font-size: 14px; max-width: 65ch; }

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

.puzzlebar {
  display: grid; gap: 6px; padding: 12px 14px; border-radius: 12px;
  background: var(--panel); border: 2px solid var(--ink);
}
.pz-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.pz-head b { font-family: var(--f-display); font-weight: 400; font-size: 17px; letter-spacing: 0.02em; }
#pzSolved { font-family: var(--f-mono); font-size: 12px; color: var(--muted); }
#pzText { margin: 0; font-size: 14px; }
#pzText.ok { color: var(--good); font-weight: 700; }
#pzText.ng { color: var(--danger); font-weight: 700; }
