/* ============================================================
   可視化サンドボックス
   ============================================================ */

.sandbox {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0.9rem 1.5rem 3rem;
}

.sb-head {
  align-items: end;
  display: grid;
  gap: 1rem;
  grid-template-columns: minmax(0, 1fr) auto;
  margin: 0.65rem 0 0.85rem;
}
.sb-head h1 {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(1.7rem, 3.8vw, 2.45rem);
  line-height: 1.22;
  margin: 0.45rem 0 0.45rem;
}
.sb-lede { color: var(--ink-soft); max-width: 60ch; margin: 0; }
.sb-lede b { color: var(--green); font-weight: 700; }
.sb-copy { min-width: 0; }
.sb-head .page-actions {
  justify-content: flex-end;
  margin-top: 0;
}
.sandbox .route-note {
  margin-bottom: 0.75rem;
  padding: 0.68rem 0.85rem;
}

/* ---------- presets ---------- */
.presets {
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 0.8rem;
  padding: 0.55rem;
}
.preset {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--ink-soft);
  background: var(--card);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  min-height: 38px;
  padding: 0.4rem 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background-color 0.15s ease;
}
.preset:hover { border-color: var(--green); color: var(--green); }
.preset[aria-pressed="true"] {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}

/* ---------- layout ---------- */
.sb-grid-area {
  display: grid;
  grid-template-columns: minmax(540px, 1.35fr) minmax(280px, 0.65fr);
  gap: 1rem;
  align-items: start;
}
.sb-grid-area > * { min-width: 0; }
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.grid-panel { padding: 0.95rem; }

/* ---------- toolbar / formula bar ---------- */
.toolbar { display: flex; gap: 0.7rem; align-items: center; margin-bottom: 0.9rem; min-width: 0; }
.formula-bar {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 0.35rem 0.5rem;
  min-width: 0;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.formula-bar:focus-within {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(29, 122, 81, 0.14);
}
.fx-label {
  font-family: var(--font-mono);
  font-style: italic;
  color: var(--ink-faint);
  padding: 0 0.3rem;
  border-right: 1px solid var(--line);
}
.fx-cell {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--green);
  min-width: 2.2em;
  text-align: center;
}
#formulaInput {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  color: var(--ink);
  outline: none;
  padding: 0.2rem;
  min-width: 0;
}

.mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--ink);
  background: var(--paper);
  border: 1.5px solid var(--line-strong);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s, background-color 0.15s, color 0.15s;
}
.mode-toggle:hover { border-color: var(--green); }
.mode-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--green);
  transition: background 0.2s;
}
.mode-toggle.formula { background: var(--green); color: #fff; border-color: var(--green); }
.mode-toggle.formula .mode-dot { background: var(--live); }

.preset-lesson {
  background: linear-gradient(90deg, #fff, var(--green-tint));
  border: 1px solid rgba(22, 131, 76, 0.22);
  border-radius: var(--radius-sm);
  display: grid;
  gap: 0.62rem;
  margin: -0.18rem 0 0.65rem;
  padding: 0.72rem 0.78rem;
}
.preset-lesson[hidden] {
  display: none;
}
.lesson-head {
  display: grid;
  gap: 0.16rem;
}
.lesson-kicker {
  color: var(--live);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.lesson-head strong {
  color: var(--green-deep);
  font-family: var(--font-display);
  font-size: 1.05rem;
  line-height: 1.3;
}
.lesson-head p {
  color: var(--ink-soft);
  font-size: 0.9rem;
  line-height: 1.55;
  margin: 0;
}
.reference-compare {
  display: grid;
  gap: 0.5rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.reference-card {
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  display: grid;
  gap: 0.22rem;
  min-width: 0;
  padding: 0.52rem 0.6rem;
}
.reference-card code {
  color: var(--green-deep);
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 800;
}
.reference-card b {
  color: var(--ink);
  font-size: 0.84rem;
}
.reference-card span {
  color: var(--ink-soft);
  font-size: 0.82rem;
  line-height: 1.45;
}
.lesson-rule {
  background: rgba(255, 255, 255, 0.7);
  border: 1px dashed rgba(22, 131, 76, 0.35);
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.5;
  margin: 0;
  padding: 0.48rem 0.58rem;
}
.lesson-rule code {
  color: var(--green-deep);
  font-family: var(--font-mono);
  font-weight: 800;
}

.flow-legend {
  align-items: center;
  background: linear-gradient(90deg, #fff, var(--paper));
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin: -0.2rem 0 0.65rem;
  min-height: 40px;
  padding: 0.42rem 0.55rem;
}
.flow-legend:empty {
  display: none;
}
.flow-chip {
  align-items: center;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink-soft);
  display: inline-flex;
  font-size: 0.82rem;
  font-weight: 800;
  gap: 0.35rem;
  min-height: 28px;
  padding: 0.2rem 0.58rem;
}
.flow-chip::before {
  border-radius: 999px;
  content: "";
  height: 0.55rem;
  width: 0.55rem;
}
.flow-chip.result {
  border-color: rgba(22, 131, 76, 0.28);
  color: var(--green-deep);
}
.flow-chip.result::before {
  background: var(--green);
}
.flow-chip.refs {
  border-color: rgba(223, 91, 63, 0.24);
  color: var(--live);
}
.flow-chip.refs::before {
  background: var(--live);
}

/* ---------- the sheet ---------- */
.grid-wrap {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  position: relative;
}
.sheet {
  display: grid;
  position: relative;
  width: max-content;
  min-width: 100%;
  font-family: var(--font-mono);
  user-select: none;
}
.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
  overflow: hidden;
}
@keyframes dash { to { stroke-dashoffset: 0; } }
.flow-line-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.78);
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 5;
}
.flow-line {
  fill: none;
  opacity: 0.72;
  stroke: var(--live);
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}
.flow-dot {
  fill: var(--card);
  stroke: var(--live);
  stroke-width: 2.4;
}
.flow-dot.target {
  fill: var(--live);
  stroke: #fff;
}

.cell, .head {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  min-height: 42px;
  display: flex;
  align-items: center;
  padding: 0 0.6rem;
  font-size: 0.92rem;
  white-space: nowrap;
}
.head {
  background: var(--paper-2);
  color: var(--ink-faint);
  font-weight: 600;
  justify-content: center;
  font-size: 0.8rem;
  position: sticky;
}
.corner { background: var(--paper-2); border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }

.cell { background: var(--card); cursor: pointer; transition: background 0.15s, box-shadow 0.15s; position: relative; }
.cell.text { color: var(--ink-soft); }
.cell.num { justify-content: flex-end; }
.cell.formula-cell { color: var(--green-deep); }
.cell.empty { background: var(--card); }
.cell.error { color: var(--live); }
.cell:hover { background: var(--green-tint); }
.cell:focus-visible,
.cell:focus-within {
  outline: 3px solid var(--live-soft);
  outline-offset: -3px;
  z-index: 6;
}
.cell-input {
  width: 100%;
  border: none;
  outline: none;
  background: #fff;
  font-family: var(--font-mono);
  font-size: 0.92rem;
  color: var(--ink);
}

/* selected */
.cell.selected {
  background: #fff;
  box-shadow: inset 0 0 0 2.5px var(--green);
  z-index: 4;
}
/* precedent (read-from) */
.cell.precedent {
  background: var(--live-tint);
  box-shadow: inset 3px 0 0 var(--live), inset 0 0 0 1px rgba(223, 91, 63, 0.28);
  z-index: 3;
}
/* recalculation flash */
@keyframes flash {
  0% { background: var(--live); color: #fff; }
  100% { background: var(--card); }
}
.cell.flash { animation: flash 0.6s ease-out; z-index: 2; }

/* show formula mode */
.sheet.show-formula .cell.formula-cell {
  background: var(--green-tint);
  color: var(--green-deep);
  justify-content: flex-start;
}

.hint { color: var(--ink-faint); font-size: 0.82rem; margin: 0.8rem 0 0; }
.hint kbd {
  font-family: var(--font-mono);
  background: var(--paper-2);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  padding: 0.05rem 0.35rem;
  font-size: 0.78rem;
}

/* ---------- inspector ---------- */
.inspector {
  max-height: calc(100vh - 6rem);
  overflow: auto;
  padding: 1.1rem 1.1rem 1.35rem;
  position: sticky;
  scrollbar-gutter: stable;
  top: 5rem;
}
.insp-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
  margin: 0 0 1rem;
  padding-bottom: 0.7rem;
  border-bottom: 1px solid var(--line);
}
.insp-empty { color: var(--ink-faint); font-size: 0.92rem; }

.insp-section { margin-bottom: 1.3rem; }
.insp-section:last-child { margin-bottom: 0; }
.insp-label {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 0.5rem;
}

.value-chip {
  font-family: var(--font-mono);
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--green-deep);
  background: var(--green-tint);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.9rem;
  display: inline-block;
}
.raw-formula {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.7rem;
  word-break: break-all;
}

/* machine diagram */
.machine {
  display: flex;
  align-items: stretch;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.machine-inputs { display: flex; flex-direction: column; gap: 0.4rem; }
.input-chip {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  background: var(--live-tint);
  border: 1px solid var(--live-soft);
  border-radius: 6px;
  padding: 0.3rem 0.55rem;
  white-space: nowrap;
}
.input-chip .iv { color: var(--live); font-weight: 600; }
.machine-box {
  display: grid;
  place-items: center;
  background: var(--green);
  color: #fff;
  border-radius: 10px;
  padding: 0.6rem 0.9rem;
  font-family: var(--font-mono);
  font-weight: 600;
  min-width: 64px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  align-self: center;
}
.machine-box .fn-name { font-size: 1rem; }
.machine-box .fn-sub { font-size: 0.66rem; opacity: 0.85; font-weight: 400; }
.machine-arrow { align-self: center; color: var(--ink-faint); font-size: 1.3rem; }
.machine-out {
  align-self: center;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--green-deep);
  background: #fff;
  border: 2px solid var(--green);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
}

/* IF gate */
.gate { display: grid; gap: 0.55rem; }
.gate-cond {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  text-align: center;
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 0.5rem;
}
.gate-cond .res { font-weight: 700; }
.gate-cond .res.t { color: var(--green); }
.gate-cond .res.f { color: var(--live); }
.gate-branches { display: grid; grid-template-columns: 1fr 1fr; gap: 0.55rem; }
.branch {
  border-radius: var(--radius-sm);
  border: 1.5px dashed var(--line-strong);
  padding: 0.6rem;
  text-align: center;
  opacity: 0.45;
  transition: border-color 0.2s, background-color 0.2s, opacity 0.2s;
}
.branch .btag { font-size: 0.72rem; font-family: var(--font-mono); color: var(--ink-faint); }
.branch .bval { font-family: var(--font-mono); font-weight: 600; margin-top: 0.2rem; }
.branch.active {
  opacity: 1;
  border-style: solid;
  border-color: var(--green);
  background: var(--green-tint);
}
.branch.active.is-false { border-color: var(--live); background: var(--live-tint); }

/* step-by-step */
.steps { display: grid; gap: 0.35rem; }
.step {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  background: var(--paper);
  border-radius: 6px;
  padding: 0.35rem 0.55rem;
}
.step .sn {
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  font-size: 0.68rem;
  display: grid; place-items: center;
  flex-shrink: 0;
}
.step .sexpr { color: var(--ink-soft); }
.step .sto { color: var(--ink-faint); }
.step .sval { color: var(--green-deep); font-weight: 600; }
.step.final .sn { background: var(--live); }

@media (max-width: 900px) {
  .sb-head {
    align-items: start;
    grid-template-columns: 1fr;
  }
  .sb-head .page-actions {
    justify-content: flex-start;
  }
  .sb-grid-area { grid-template-columns: 1fr; }
  .inspector {
    max-height: none;
    overflow: visible;
    position: static;
  }
  .reference-compare {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  .sandbox { padding-left: 1rem; padding-right: 1rem; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .mode-toggle { justify-content: center; width: 100%; }
  .grid-panel { padding: 0.85rem; }
}
