:root {
  color-scheme: light;
  --ink: #14213d;
  --muted: #60708b;
  --paper: #f4f7fb;
  --card: #ffffff;
  --line: #dce4ef;
  --navy: #173b67;
  --navy-deep: #0b2545;
  --blue: #2878c8;
  --blue-pale: #e9f4ff;
  --orange: #e47927;
  --orange-pale: #fff1e5;
  --green: #11795b;
  --green-pale: #e7f7f1;
  --red: #b64040;
  --shadow: 0 18px 48px rgba(23, 59, 103, 0.12);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 300px;
  background: var(--paper);
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 0%, rgba(40, 120, 200, 0.1), transparent 28rem),
    var(--paper);
  font-family:
    Inter, "Noto Sans JP", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 3px solid rgba(40, 120, 200, 0.35);
  outline-offset: 2px;
}

.lab-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 24px;
}

.lab-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.14em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 7px;
  font-size: clamp(1.55rem, 3vw, 2.25rem);
  line-height: 1.2;
  letter-spacing: -0.035em;
}

.lab-header p:last-child {
  max-width: 42rem;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.94rem;
  line-height: 1.7;
}

.lab-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(310px, 0.9fr);
  gap: 18px;
}

.stage-card,
.control-card {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.stage-heading,
.section-title {
  display: flex;
  align-items: center;
  gap: 11px;
}

.stage-heading {
  justify-content: space-between;
  min-height: 78px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.stage-heading > div {
  display: flex;
  align-items: center;
  gap: 11px;
}

.step-number {
  display: inline-grid;
  flex: 0 0 30px;
  width: 30px;
  height: 30px;
  place-items: center;
  border-radius: 50%;
  color: white;
  background: var(--navy);
  font-size: 0.83rem;
  font-weight: 800;
}

h2 {
  margin-bottom: 2px;
  font-size: 1rem;
  line-height: 1.35;
}

.stage-heading p,
.section-title p {
  margin: 0;
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1.45;
}

.quiet-button {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--navy);
  background: var(--card);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 750;
  transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

.quiet-button:hover:not(:disabled) {
  border-color: #9ab2ce;
  background: #f7faff;
  transform: translateY(-1px);
}

.quiet-button.danger:not(:disabled) {
  color: var(--red);
}

.quiet-button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.scene {
  position: relative;
  height: 558px;
  overflow: hidden;
  background:
    linear-gradient(rgba(255, 255, 255, 0.022) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.022) 1px, transparent 1px),
    #071c33;
  background-size: 34px 34px;
  touch-action: none;
}

.scene::after {
  position: absolute;
  right: 14px;
  bottom: 12px;
  content: "DRAG TO EXPLORE";
  color: rgba(255, 255, 255, 0.38);
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  pointer-events: none;
}

.scene canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

.scene canvas.dragging {
  cursor: grabbing;
}

.scene-message {
  position: absolute;
  z-index: 2;
  top: 50%;
  left: 50%;
  width: min(86%, 27rem);
  margin: 0;
  padding: 14px 18px;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  color: rgba(255, 255, 255, 0.86);
  background: rgba(7, 28, 51, 0.86);
  text-align: center;
  font-size: 0.85rem;
  line-height: 1.6;
}

.scene-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  min-height: 44px;
  align-items: center;
  padding: 8px 16px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 650;
}

.scene-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}

.cation-dot {
  background: var(--orange);
}

.anion-dot {
  background: var(--blue);
}

.legend-line {
  width: 18px;
  height: 2px;
  background: #41d9b2;
}

.control-card {
  display: flex;
  flex-direction: column;
}

.control-section {
  padding: 17px;
}

.control-section + .control-section {
  border-top: 1px solid var(--line);
}

fieldset {
  min-width: 0;
  margin: 14px 0 0;
  padding: 0;
  border: 0;
}

legend {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
}

.ion-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.ion-option {
  min-height: 52px;
  padding: 6px 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--ink);
  background: white;
  cursor: pointer;
  line-height: 1.1;
  transition: border-color 150ms ease, background 150ms ease, transform 150ms ease;
}

.ion-option:hover {
  border-color: #9ab2ce;
  transform: translateY(-1px);
}

.ion-option[aria-pressed="true"] {
  border-color: var(--blue);
  background: var(--blue-pale);
  box-shadow: inset 0 0 0 1px var(--blue);
}

.ion-option .symbol {
  display: block;
  font-size: 1.05rem;
  font-weight: 850;
}

.ion-option .name {
  display: block;
  margin-top: 4px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.58rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.combination-section {
  flex: 1;
  background: #fbfcfe;
}

.counter-list {
  display: grid;
  gap: 8px;
  margin: 14px 0;
}

.counter-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  min-height: 58px;
  padding: 8px 9px 8px 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: white;
}

.counter-ion {
  display: flex;
  align-items: center;
  gap: 9px;
}

.counter-sphere {
  display: grid;
  flex: 0 0 35px;
  width: 35px;
  height: 35px;
  place-items: center;
  border-radius: 50%;
  color: white;
  background: var(--blue);
  box-shadow: inset -5px -5px 10px rgba(0, 0, 0, 0.2);
  font-size: 0.67rem;
  font-weight: 850;
}

.counter-sphere.cation {
  background: var(--orange);
}

.counter-copy strong,
.counter-copy small {
  display: block;
}

.counter-copy strong {
  font-size: 0.8rem;
}

.counter-copy small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.65rem;
}

.counter-actions {
  display: flex;
  align-items: center;
  gap: 5px;
}

.counter-actions button {
  display: grid;
  width: 32px;
  height: 32px;
  padding: 0;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--navy);
  background: white;
  cursor: pointer;
  font-size: 1.05rem;
  font-weight: 800;
}

.counter-actions button:hover:not(:disabled) {
  border-color: var(--blue);
  background: var(--blue-pale);
}

.counter-actions button:disabled {
  cursor: not-allowed;
  opacity: 0.35;
}

.counter-actions output {
  min-width: 24px;
  text-align: center;
  font-size: 0.95rem;
  font-weight: 850;
}

.charge-panel {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: white;
}

.charge-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 0.73rem;
  font-weight: 750;
}

.charge-heading strong {
  color: var(--orange);
  font-size: 1.25rem;
}

.charge-heading strong.neutral {
  color: var(--green);
}

.charge-track {
  display: grid;
  grid-template-columns: 18px 1fr 18px;
  gap: 5px;
  align-items: center;
  margin: 8px 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.track-line {
  position: relative;
  height: 4px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--blue), #cbd5e1 50%, var(--orange));
}

.track-line::after {
  position: absolute;
  top: -3px;
  left: 50%;
  width: 1px;
  height: 10px;
  content: "";
  background: var(--ink);
  opacity: 0.35;
}

#charge-marker {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  width: 12px;
  height: 12px;
  transform: translate(-50%, -50%);
  border: 2px solid white;
  border-radius: 50%;
  background: var(--orange);
  box-shadow: 0 1px 5px rgba(20, 33, 61, 0.3);
  transition: left 260ms ease, background 260ms ease;
}

.charge-calculation {
  min-height: 1.2em;
  margin: 0;
  color: var(--muted);
  text-align: center;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 0.7rem;
}

.result-card {
  margin-top: 10px;
  padding: 13px 14px;
  border: 1px solid #f1c9a8;
  border-radius: 12px;
  background: var(--orange-pale);
  text-align: center;
  transition: border-color 240ms ease, background 240ms ease;
}

.result-card.solved {
  border-color: #94d5c1;
  background: var(--green-pale);
}

.result-card.neutral-not-simple {
  border-color: #e3ca75;
  background: #fff9df;
}

.result-card span,
.result-card strong {
  display: block;
}

.result-card span {
  margin-bottom: 3px;
  color: var(--orange);
  font-size: 0.64rem;
  font-weight: 850;
  letter-spacing: 0.09em;
}

.result-card.solved span {
  color: var(--green);
}

.result-card strong {
  font-size: 1.35rem;
}

.result-card p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.68rem;
  line-height: 1.45;
}

@media (max-width: 880px) {
  .lab-grid {
    grid-template-columns: 1fr;
  }

  /* 縦に積むときは手順どおり「①②選ぶ・数える → ③3Dで確かめる」の順に並べる。
     DOM順は 3→1→2 なので、そのままだと操作前に3D画面が出てしまう。 */
  .control-card {
    order: 1;
  }

  .stage-card {
    order: 2;
  }

  .scene {
    height: 470px;
  }
}

@media (max-width: 560px) {
  .lab-shell {
    padding: 14px;
  }

  .lab-header {
    gap: 12px;
  }

  .lab-header .quiet-button {
    margin-top: 22px;
  }

  .stage-heading {
    align-items: flex-start;
    gap: 8px;
  }

  .stage-heading .danger {
    max-width: 88px;
  }

  .scene {
    height: 385px;
  }

  .ion-options {
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
  }

  .ion-option {
    min-height: 48px;
    padding: 5px 2px;
  }

  .ion-option .name {
    font-size: 0.54rem;
  }
}

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

/* 教材ページに埋め込まれたときは、記事側に同じ見出し・説明があるため重ねて見せない。
   スマホでは、この重複だけで操作パネルが1画面ぶん下へ押し下げられていた。 */
:root[data-embedded] .lab-shell {
  padding: 12px;
}

:root[data-embedded] .lab-header {
  margin-bottom: 10px;
}

:root[data-embedded] .lab-header .eyebrow,
:root[data-embedded] .lab-header h1,
:root[data-embedded] .lab-header p:last-child {
  display: none;
}

:root[data-embedded] .lab-header {
  justify-content: flex-end;
}
