:root {
  color-scheme: light;
  --bg: #fff8ea;
  --ink: #223047;
  --muted: #6b7280;
  --panel: #ffffff;
  --line: #d9e2ef;
  --blue: #2f80ed;
  --green: #31a86b;
  --yellow: #ffcf56;
  --red: #ef6461;
  --purple: #8b5cf6;
  --shadow: 0 18px 44px rgba(34, 48, 71, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(255, 207, 86, 0.28), transparent 30rem),
    linear-gradient(135deg, #fff8ea 0%, #e8f6ff 52%, #f7f2ff 100%);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

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

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

.eyebrow {
  margin: 0 0 6px;
  color: #1d6f91;
  font-size: 0.95rem;
  font-weight: 700;
}

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

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 5vw, 4.25rem);
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 1.35rem;
}

.score-board {
  display: grid;
  gap: 6px;
  min-width: 220px;
  padding: 14px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: var(--shadow);
}

.score-board span,
.history-title span,
.animation-header span {
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}

.score-board strong {
  font-size: 1.2rem;
}

.music-toggle {
  border-color: #b8e0c8;
  background: #eafaf1;
  color: #16784a;
}

.music-toggle.is-off {
  border-color: var(--line);
  background: #f8fafc;
  color: var(--muted);
}

.main-layout {
  display: grid;
  grid-template-columns: 360px minmax(400px, 1fr) 420px;
  gap: 18px;
  align-items: start;
}

.control-panel,
.problem-stage,
.animation-panel,
.history-panel {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
}

.control-panel,
.animation-panel,
.history-panel {
  padding: 18px;
}

.problem-stage {
  position: relative;
  overflow: hidden;
  padding: 22px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.segment,
.primary-button,
.secondary-button,
.ghost-button,
.check-button,
.choice-button {
  min-height: 48px;
  border: 2px solid transparent;
  border-radius: 8px;
  font-weight: 800;
}

.segment {
  background: #edf7ff;
  color: #174d7c;
}

.segment.is-active {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

.setting-grid {
  display: grid;
  gap: 12px;
  margin: 18px 0;
}

label {
  display: grid;
  gap: 6px;
  color: #334155;
  font-weight: 800;
}

select,
input {
  width: 100%;
  min-height: 50px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 800;
  padding: 8px 12px;
}

.action-row,
.animation-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.animation-controls {
  grid-template-columns: 1fr 1fr 1fr;
  margin-top: 14px;
}

.primary-button {
  background: var(--green);
  color: #fff;
}

.secondary-button {
  background: var(--yellow);
  color: #533c00;
}

.ghost-button {
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
}

.check-button {
  width: 100%;
  background: var(--purple);
  color: #fff;
  font-size: 1.1rem;
}

.problem-card {
  width: min(420px, 100%);
  margin: 8px auto 20px;
  padding: 18px;
  border: 3px solid #26364d;
  border-radius: 8px;
  background: #fffdf8;
}

.operator-badge {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  background: #e5f6ff;
  color: #155e75;
  font-weight: 900;
}

.vertical-problem {
  margin-top: 16px;
  font-size: clamp(2.7rem, 7vw, 5.5rem);
  font-weight: 900;
  line-height: 1.08;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.problem-row {
  display: grid;
  grid-template-columns: 0.6em 1fr;
  gap: 0.12em;
}

.problem-row:first-child {
  grid-template-columns: 1fr;
}

.problem-line {
  height: 5px;
  margin: 8px 0;
  background: var(--ink);
}

.answer-area {
  display: grid;
  gap: 14px;
}

.answer-area.mode-number {
  padding: 14px;
  border: 2px solid #dce8f6;
  border-radius: 8px;
  background: #f8fbff;
}

.answer-area.mode-choice {
  padding: 14px;
  border: 2px solid #dce8f6;
  border-radius: 8px;
  background: #fffdf7;
}

.answer-area.mode-choice .check-button {
  margin-top: 2px;
}

.answer-area.mode-number .answer-input {
  color: #174d7c;
}

.answer-area.mode-choice .choices {
  min-height: 126px;
}

.choices {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.choice-button {
  background: #fff;
  border-color: var(--line);
  color: var(--ink);
  font-size: 1.4rem;
  min-height: 58px;
}

.choice-button.is-selected {
  border-color: var(--blue);
  background: #e7f2ff;
  box-shadow: 0 0 0 4px rgba(47, 128, 237, 0.16);
}

.feedback {
  min-height: 54px;
  margin-bottom: 10px;
  text-align: center;
  font-size: 1.45rem;
  font-weight: 900;
}

.feedback.is-correct {
  color: #16784a;
  animation: pop 680ms ease;
}

.feedback.is-wrong {
  color: #c0392b;
  animation: shake 480ms ease;
}

.problem-stage.correct-flash::after,
.problem-stage.wrong-flash::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  animation: fadeFlash 900ms ease forwards;
}

.problem-stage.correct-flash::after {
  background: rgba(49, 168, 107, 0.2);
}

.problem-stage.wrong-flash::after {
  background: rgba(239, 100, 97, 0.18);
}

.animation-header,
.history-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.visual-area {
  min-height: 360px;
  overflow: auto;
  border: 2px dashed #b8c7d9;
  border-radius: 8px;
  background: #fbfdff;
  padding: 14px;
}

.place-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.operand-stack {
  display: grid;
  gap: 12px;
}

.operand-row {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 10px;
  align-items: stretch;
  padding: 10px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.operand-label {
  display: grid;
  place-items: center;
  min-height: 80px;
  border-radius: 8px;
  background: #e7f2ff;
  color: #174d7c;
  font-size: 1.6rem;
  font-weight: 900;
}

.operand-place-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.place-column {
  min-height: 250px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.operand-place-grid .place-column {
  min-height: 110px;
}

.merge-visual {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  align-items: stretch;
}

.merge-sources {
  display: grid;
  gap: 8px;
}

.merge-source,
.merge-target {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.merge-source {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 8px;
  align-items: stretch;
  animation: floatIn 420ms ease both;
}

.mini-place-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.mini-place {
  min-height: 74px;
  border: 2px solid #eef3f8;
  border-radius: 8px;
  padding: 6px;
  background: #fbfdff;
}

.mini-place strong {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.mini-place em {
  color: var(--ink);
  font-style: normal;
  font-weight: 900;
}

.merge-arrow {
  display: grid;
  place-items: center;
  min-height: 72px;
  border-radius: 8px;
  background: #fff7df;
  color: #8a5a00;
  font-size: 2.4rem;
  font-weight: 900;
  animation: arrowPulse 900ms ease-in-out infinite;
}

.merge-target {
  animation: mergeGlow 1100ms ease both;
}

.merge-target .place-grid {
  gap: 6px;
}

.merge-target-title {
  margin-bottom: 8px;
  color: #16784a;
  font-weight: 900;
  text-align: center;
}

.merge-target .place-column {
  min-height: 118px;
}

.merge-target .unit {
  width: 20px;
  height: 20px;
}

.merge-target .ten {
  width: 22px;
  height: 42px;
}

.merge-target .hundred {
  width: 42px;
  height: 42px;
}

.operation-visual .division-groups {
  grid-template-columns: repeat(auto-fit, minmax(82px, 1fr));
}

.taking-away {
  animation: takeAway 1200ms ease both;
}

.subtract-arrow {
  background: #ffe6e4;
  color: #b93833;
}

.divide-split-visual {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: center;
}

.divide-split-visual .merge-arrow {
  min-height: 48px;
  transform: rotate(90deg);
}

.split-pool,
.split-groups {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.split-groups .group-box {
  animation: splitGroupIn 650ms ease both;
}

.split-groups .group-box:nth-child(2) {
  animation-delay: 120ms;
}

.split-groups .group-box:nth-child(3) {
  animation-delay: 240ms;
}

.split-groups .group-box:nth-child(4) {
  animation-delay: 360ms;
}

.subtraction-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.subtraction-stack {
  display: grid;
  gap: 8px;
}

.subtraction-stack-row {
  display: grid;
  grid-template-columns: 84px 1fr;
  gap: 10px;
  align-items: stretch;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.subtraction-stack-label {
  display: grid;
  place-items: center;
  min-height: 74px;
  border-radius: 8px;
  background: #e7f2ff;
  color: #174d7c;
  font-size: 1rem;
  font-weight: 900;
  text-align: center;
  line-height: 1.25;
}

.take-row .subtraction-stack-label {
  background: #fff1f0;
  color: #9b2f2b;
}

.subtraction-minus-line {
  display: grid;
  place-items: center;
  width: 56px;
  height: 34px;
  margin: 0 auto;
  border-radius: 8px;
  background: #ffe6e4;
  color: #9b2f2b;
  font-size: 1.6rem;
  font-weight: 900;
}

.subtraction-mini-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.subtraction-mini-board .mini-place {
  min-height: 76px;
}

.subtraction-column {
  min-height: 170px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.subtraction-column h3 {
  margin: 0 0 10px;
  font-size: 1rem;
  text-align: center;
}

.to-remove {
  position: relative;
  border-color: rgba(185, 56, 51, 0.56);
  background: #ff9c98;
  animation: markRemove 780ms ease both;
}

.to-remove::after {
  content: "×";
  display: grid;
  place-items: center;
  position: absolute;
  inset: -2px;
  color: #8f221e;
  font-size: 1rem;
  font-weight: 900;
}

.to-keep {
  animation: keepGlow 900ms ease both;
}

.subtract-count,
.shortage-note {
  margin-top: 8px;
  padding: 6px;
  border-radius: 8px;
  font-weight: 900;
  text-align: center;
}

.subtract-count {
  background: #fff1f0;
  color: #9b2f2b;
}

.shortage-note {
  background: #fff7df;
  color: #7a4d00;
}

.subtraction-answer {
  display: grid;
  gap: 12px;
}

.subtraction-answer-number {
  display: grid;
  place-items: center;
  min-height: 116px;
  border: 2px solid rgba(49, 168, 107, 0.3);
  border-radius: 8px;
  background: #edfff4;
  color: #16784a;
}

.subtraction-answer-number span {
  font-size: 1.05rem;
  font-weight: 900;
}

.subtraction-answer-number strong {
  font-size: clamp(3rem, 8vw, 5rem);
  line-height: 1;
}

.remaining-side {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.remaining-side .place-column {
  min-height: 150px;
}

.borrow-simple {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  align-items: stretch;
}

.borrow-card {
  min-height: 170px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
  display: grid;
  align-content: start;
  gap: 8px;
}

.borrow-card .place-grid {
  gap: 5px;
}

.borrow-card .place-column {
  min-height: 92px;
  padding: 6px;
}

.borrow-card .unit {
  width: 18px;
  height: 18px;
}

.borrow-card .ten {
  width: 20px;
  height: 38px;
}

.borrow-card .hundred {
  width: 38px;
  height: 38px;
}

.borrow-notes {
  display: grid;
  gap: 8px;
}

.borrow-notes span {
  padding: 8px 10px;
  border-radius: 8px;
  background: #fff7df;
  color: #7a4d00;
  font-size: 1rem;
  font-weight: 900;
  text-align: center;
  line-height: 1.45;
}

.place-column h3,
.group-box h3 {
  margin: 0 0 10px;
  font-size: 1rem;
  text-align: center;
}

.block-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-content: flex-start;
}

.unit,
.ten,
.hundred {
  display: inline-grid;
  place-items: center;
  border: 2px solid rgba(34, 48, 71, 0.2);
  border-radius: 6px;
  color: #18324a;
  font-weight: 900;
  animation: floatIn 420ms ease both;
}

.unit {
  width: 24px;
  height: 24px;
  background: #ffde7a;
}

.ten {
  width: 28px;
  height: 56px;
  background: #86d5ff;
}

.hundred {
  width: 56px;
  height: 56px;
  background: #b6efc9;
}

.moving {
  animation: carryMove 900ms ease both;
}

.step-text {
  min-height: 76px;
  margin-top: 12px;
  padding: 12px;
  border-radius: 8px;
  background: #fff7df;
  color: #473815;
  font-size: 1.1rem;
  font-weight: 800;
  line-height: 1.6;
}

.array-grid {
  display: grid;
  gap: 6px;
  justify-content: center;
  padding: 12px 0;
}

.array-row {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.array-dot {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--yellow);
  border: 2px solid rgba(83, 60, 0, 0.22);
  animation: floatIn 360ms ease both;
}

.division-groups {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 10px;
}

.group-box {
  min-height: 120px;
  border: 2px solid #b8c7d9;
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.history-list {
  display: grid;
  gap: 8px;
  max-height: 265px;
  overflow: auto;
  padding-left: 24px;
  margin: 0;
}

.history-list li {
  padding: 8px;
  border-radius: 8px;
  background: #f7fbff;
  font-weight: 700;
}

@keyframes floatIn {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.88);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes carryMove {
  0% {
    transform: translate(0, 0) scale(1);
  }
  50% {
    transform: translate(-18px, -38px) scale(1.1);
  }
  100% {
    transform: translate(0, 0) scale(1);
  }
}

@keyframes arrowPulse {
  0%,
  100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(8px);
  }
}

@keyframes mergeGlow {
  0% {
    box-shadow: 0 0 0 rgba(49, 168, 107, 0);
    transform: scale(0.98);
  }
  65% {
    box-shadow: 0 0 0 8px rgba(49, 168, 107, 0.16);
    transform: scale(1.01);
  }
  100% {
    box-shadow: 0 0 0 rgba(49, 168, 107, 0);
    transform: scale(1);
  }
}

@keyframes takeAway {
  0% {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
  55% {
    opacity: 0.78;
    transform: translateX(32px) scale(0.96);
  }
  100% {
    opacity: 0.34;
    transform: translateX(62px) scale(0.88);
  }
}

@keyframes splitGroupIn {
  from {
    opacity: 0;
    transform: translateX(-18px) scale(0.92);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes markRemove {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.16);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes keepGlow {
  0%,
  100% {
    box-shadow: none;
  }
  55% {
    box-shadow: 0 0 0 5px rgba(49, 168, 107, 0.14);
  }
}

@keyframes removedFly {
  from {
    opacity: 0;
    transform: translateX(-24px) scale(1.1);
  }
  to {
    opacity: 0.42;
    transform: translateX(0) scale(1);
  }
}

@keyframes pop {
  0%,
  100% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.12);
  }
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-8px);
  }
  75% {
    transform: translateX(8px);
  }
}

@keyframes fadeFlash {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@media (max-width: 1180px) {
  .main-layout {
    grid-template-columns: 340px 1fr;
  }

  .animation-panel,
  .history-panel {
    grid-column: span 2;
  }
}

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

  .app-header,
  .main-layout {
    display: grid;
    grid-template-columns: 1fr;
  }

  .animation-panel,
  .history-panel {
    grid-column: auto;
  }

  .choices,
  .action-row,
  .animation-controls {
    grid-template-columns: 1fr;
  }

  .place-grid {
    grid-template-columns: 1fr;
  }

  .merge-visual,
  .merge-source,
  .divide-split-visual,
  .subtraction-board,
  .subtraction-stack-row,
  .subtraction-mini-board,
  .subtraction-result,
  .borrow-simple {
    grid-template-columns: 1fr;
  }

  .merge-arrow {
    min-height: 48px;
    transform: rotate(90deg);
  }
}

/* LiNest v3: 教材内の意味色は保ち、外枠と操作UIのみ共通化する。 */
@media screen {
  body.linest-themed .app-shell {
    min-width: 0;
    padding-block: 28px 36px;
  }

  body.linest-themed .app-header {
    min-width: 0;
    padding: 20px;
    border: 1px solid var(--linest-border);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: var(--linest-shadow);
  }

  body.linest-themed .app-header > *,
  body.linest-themed .main-layout > * {
    min-width: 0;
  }

  body.linest-themed :where(.control-panel, .problem-stage, .animation-panel, .history-panel) {
    border-color: var(--linest-border);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: var(--linest-shadow);
  }

  body.linest-themed .score-board {
    max-width: 100%;
    border-color: var(--linest-border);
    background: var(--linest-green-soft);
    box-shadow: none;
  }

  body.linest-themed .segment {
    border-color: var(--linest-border);
    background: #fbf8f2;
    color: var(--linest-heading);
  }

  body.linest-themed .segment.is-active {
    border-color: var(--linest-green);
    background: var(--linest-green);
    color: #ffffff;
  }

  body.linest-themed .ghost-button {
    border-color: var(--linest-border);
    background: #ffffff;
    color: var(--linest-heading);
  }

  body.linest-themed .music-toggle:not(.is-off) {
    border-color: #b9d9b8;
    background: var(--linest-green-soft);
    color: var(--linest-green-dark);
  }

  body.linest-themed :where(.setting-grid select, .answer-input input) {
    border-color: var(--linest-border);
    background: #ffffff;
  }
}

@media screen and (max-width: 760px) {
  body.linest-themed .app-shell {
    padding: 16px 12px 28px;
  }

  body.linest-themed .app-header {
    padding: 16px;
  }

  body.linest-themed .score-board {
    width: 100%;
    min-width: 0;
  }

  body.linest-themed :where(button, select, input) {
    min-width: 0;
  }
}
