:root {
  color-scheme: light;
  --ink: #49372a;
  --muted: #6e6259;
  --paper: #fffdf8;
  --card: #ffffff;
  --line: #eadfce;
  --green: #257836;
  --green-dark: #185b27;
  --green-pale: #eaf5e5;
  --orange: #d86d3b;
  --orange-pale: #fff0e6;
  --blue: #2e7da0;
  --blue-pale: #e8f4f8;
  --purple: #7259a8;
  --shadow: 0 12px 34px rgba(73, 55, 42, 0.09);
  --heading: "Zen Maru Gothic", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", sans-serif;
}

* { box-sizing: border-box; }

html,
body { max-width: 100%; overflow-x: hidden; }

body {
  margin: 0;
  background:
    radial-gradient(circle at 8% 4%, rgba(228, 241, 220, 0.8), transparent 28rem),
    linear-gradient(180deg, #fffdf8, #fbf8f0);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic", sans-serif;
}

button,
input,
select { font: inherit; }

button { touch-action: manipulation; }

button:focus-visible,
input:focus-visible,
select:focus-visible,
summary:focus-visible {
  outline: 4px solid rgba(46, 125, 160, 0.28);
  outline-offset: 3px;
}

.app-shell {
  width: min(1120px, calc(100% - 24px));
  margin: 0 auto;
  padding: clamp(18px, 4vw, 42px) 0 56px;
}

.hero { margin-bottom: 20px; }

.eyebrow,
.step-label,
.regroup-panel__eyebrow {
  margin: 0;
  color: var(--green);
  font-family: var(--heading);
  font-size: 0.9rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.hero h1 {
  margin: 4px 0 8px;
  font-family: var(--heading);
  font-size: clamp(2.15rem, 7vw, 4rem);
  line-height: 1.15;
}

.hero__lead { margin: 0; color: var(--muted); font-size: clamp(1rem, 2.4vw, 1.18rem); }

.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  max-width: 680px;
  margin-bottom: 14px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.88);
}

.mode-tab {
  min-height: 52px;
  border: 0;
  border-radius: 13px;
  background: transparent;
  color: var(--muted);
  font-family: var(--heading);
  font-size: clamp(0.98rem, 2.4vw, 1.08rem);
  font-weight: 800;
  cursor: pointer;
}

.mode-tab.is-active { background: var(--green); color: #fff; box-shadow: 0 7px 18px rgba(37, 120, 54, 0.22); }

.control-card,
.visual-card,
.howto {
  border: 1px solid var(--line);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.control-card { margin-bottom: 16px; padding: clamp(16px, 3vw, 26px); }

.control-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.control-heading h2,
.ruler-card h2,
.regroup-panel h2 { margin: 4px 0 0; font-family: var(--heading); font-size: clamp(1.2rem, 3vw, 1.55rem); }

.step-picker,
.example-picker,
.lesson-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.step-button,
.example-picker button,
.lesson-actions button,
.supporter-tools button,
.reset-button {
  min-height: 48px;
  padding: 9px 15px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-weight: 800;
  cursor: pointer;
}

.step-button.is-active,
.example-picker button.is-active,
.lesson-actions .primary-button {
  border-color: var(--green);
  background: var(--green-pale);
  color: var(--green-dark);
}

.move-buttons {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr);
  align-items: center;
  gap: 12px;
  max-width: 760px;
  margin: 20px auto 0;
}

.move-button {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  justify-content: center;
  column-gap: 8px;
  min-height: 76px;
  padding: 8px 18px;
  border: 0;
  border-radius: 20px;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(73, 55, 42, 0.14);
}

.move-button > span { grid-row: 1 / 3; font-size: 2.1rem; font-weight: 500; }
.move-button b { font-size: 1.55rem; line-height: 1; }
.move-button small { font-weight: 800; }
.move-button--minus { background: var(--blue); }
.move-button--plus { background: var(--orange); }
.move-button:active { transform: translateY(1px); }

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

.reset-button { color: var(--muted); white-space: nowrap; }

.supporter-tools { margin-top: 18px; border-top: 1px dashed var(--line); padding-top: 14px; }
.supporter-tools summary { width: fit-content; color: var(--muted); font-weight: 800; cursor: pointer; }
.supporter-tools__body { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; margin-top: 14px; }
.supporter-tools label { display: grid; gap: 5px; color: var(--muted); font-size: 0.9rem; font-weight: 800; }
.supporter-tools input,
.supporter-tools select { min-height: 48px; padding: 7px 10px; border: 2px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); }
.supporter-tools button { border-color: var(--green); color: var(--green-dark); }

.control-heading--lesson { align-items: flex-start; }
.example-picker { max-width: 560px; justify-content: flex-end; }
.lesson-controller { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
.lesson-equation { margin: 0; font-family: var(--heading); font-size: clamp(1.8rem, 5vw, 2.8rem); font-weight: 900; }
.lesson-progress { margin: 4px 0 0; color: var(--muted); font-weight: 700; }

.visual-card { padding: clamp(16px, 3.6vw, 32px); }

.number-summary {
  display: flex;
  align-items: center;
  gap: clamp(16px, 4vw, 36px);
  margin-bottom: 20px;
  padding: 16px clamp(16px, 3vw, 28px);
  border-radius: 20px;
  background: linear-gradient(120deg, var(--green-pale), #f8fbf5);
}

.number-summary p { margin: 0; }
#currentLabel { color: var(--green-dark); font-family: var(--heading); font-weight: 800; }
#currentNumber { display: block; color: var(--green-dark); font-family: var(--heading); font-size: clamp(3.2rem, 10vw, 6.8rem); font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.place-sentence { color: var(--ink); font-size: clamp(1rem, 2.8vw, 1.35rem); font-weight: 800; }

.ruler-card { margin-bottom: 22px; padding: 16px; border: 1px solid var(--line); border-radius: 18px; background: var(--paper); }
.ruler-card__heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.ruler-card__heading span { color: var(--muted); font-weight: 800; }
.ruler { padding: 34px 8px 0; }
.ruler__track { position: relative; height: 18px; border: 2px solid #d5c8b5; border-radius: 999px; background: #fff; }
.ruler__fill { height: 100%; border-radius: inherit; background: linear-gradient(90deg, #83be75, var(--green)); transition: width 0.36s ease; }
.ruler__marker { position: absolute; top: 50%; width: 4px; height: 34px; border-radius: 4px; background: var(--orange); transform: translate(-50%, -50%); transition: left 0.36s ease; }
.ruler__marker span { position: absolute; bottom: calc(100% + 7px); left: 50%; padding: 3px 8px; border-radius: 8px; background: var(--orange); color: #fff; font-weight: 900; transform: translateX(-50%); }
.ruler__ticks { display: flex; justify-content: space-between; margin-top: 8px; color: var(--muted); font-size: clamp(0.62rem, 1.8vw, 0.82rem); font-variant-numeric: tabular-nums; }

.place-board { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.place-column { min-width: 0; overflow: hidden; border: 2px solid var(--place-color); border-radius: 18px; background: var(--place-pale); }
.place-column header { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 0 8px; padding: 11px 12px; border-bottom: 2px solid var(--place-color); background: rgba(255, 255, 255, 0.8); }
.place-column header span { color: var(--place-color); font-family: var(--heading); font-size: clamp(1.25rem, 3vw, 1.65rem); font-weight: 900; }
.place-column header small { color: var(--muted); font-weight: 800; }
.place-digit { grid-column: 2; grid-row: 1 / 3; display: grid; width: 46px; height: 46px; place-items: center; border-radius: 50%; background: var(--place-color); color: #fff; font-size: 1.45rem; }
.place-column--thousands { --place-color: #7259a8; --place-pale: #f2eefb; }
.place-column--hundreds { --place-color: #2f7798; --place-pale: #eaf5fa; }
.place-column--tens { --place-color: #d17b28; --place-pale: #fff4df; }
.place-column--ones { --place-color: #3c8a4d; --place-pale: #edf7ea; }

.piece-stack { display: flex; flex-wrap: wrap; align-content: center; align-items: center; justify-content: center; gap: 7px; min-height: 176px; padding: 12px 8px; }
.zero-piece { color: var(--muted); font-weight: 800; opacity: 0.6; }
.piece { display: inline-block; flex: 0 0 auto; animation: piece-in 0.38s calc(var(--piece-index, 0) * 35ms) both; }
.piece--ones { width: 24px; height: 24px; border: 2px solid #28713a; border-radius: 6px; background: linear-gradient(135deg, #a9d69c, #58a766); box-shadow: inset -3px -3px 0 rgba(31, 102, 47, 0.17); }
.piece--tens { width: 19px; height: 78px; border: 2px solid #b9651c; border-radius: 5px; background: repeating-linear-gradient(to bottom, #f9c06e 0 6px, #d98130 6px 8px); }
.piece--hundreds { width: 52px; height: 52px; border: 2px solid #236581; border-radius: 6px; background-color: #8fc9df; background-image: linear-gradient(rgba(35, 101, 129, 0.28) 1px, transparent 1px), linear-gradient(90deg, rgba(35, 101, 129, 0.28) 1px, transparent 1px); background-size: 5px 5px; }
.piece--thousands { position: relative; width: 70px; height: 70px; border: 3px solid #5c438f; border-radius: 9px; background-color: #a893d1; background-image: linear-gradient(rgba(92, 67, 143, 0.22) 1px, transparent 1px), linear-gradient(90deg, rgba(92, 67, 143, 0.22) 1px, transparent 1px); background-size: 7px 7px; box-shadow: 8px -8px 0 #d4cae8; }

.regroup-panel { margin-top: 18px; padding: clamp(16px, 3vw, 24px); border: 3px solid #efb33f; border-radius: 20px; background: #fff9df; }
.regroup-panel__eyebrow { color: #8d5a00; }
.regroup-events { display: grid; gap: 10px; margin-top: 14px; }
.regroup-event { display: grid; grid-template-columns: minmax(90px, 1fr) auto minmax(90px, 1fr); align-items: center; gap: 10px; padding: 12px; border-radius: 15px; background: #fff; }
.regroup-event > p { grid-column: 1 / -1; margin: 2px 0 0; text-align: center; font-weight: 900; }
.transform-side { min-width: 0; text-align: center; }
.transform-side > span { display: block; margin-bottom: 7px; color: var(--muted); font-size: 0.78rem; font-weight: 800; }
.transform-arrow { color: var(--orange); font-size: 2rem; font-weight: 900; }
.mini-set { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 3px; min-height: 38px; }
.mini-set--10 { display: grid; grid-template-columns: repeat(5, auto); justify-content: center; }
.piece--mini.piece--ones { width: 13px; height: 13px; border-width: 1px; border-radius: 3px; }
.piece--mini.piece--tens { width: 7px; height: 34px; border-width: 1px; background-size: auto; }
.piece--mini.piece--hundreds { width: 27px; height: 27px; border-width: 1px; background-size: 3px 3px; }
.piece--mini.piece--thousands { width: 36px; height: 36px; border-width: 2px; background-size: 4px 4px; box-shadow: 4px -4px 0 #d4cae8; }
.regroup-panel.is-playing .transform-source .piece { animation: gather 0.64s calc(var(--piece-index, 0) * 18ms) both; }
.regroup-panel.is-playing .transform-target .piece { animation: reveal 0.42s 0.45s both; }

.feedback { min-height: 52px; margin: 16px 0 0; padding: 13px 16px; border-left: 5px solid var(--blue); border-radius: 8px; background: var(--blue-pale); font-weight: 800; }
.feedback--event { border-color: #d99819; background: #fff8da; }
.howto { margin-top: 16px; padding: 16px 20px; color: var(--muted); box-shadow: none; }
.howto p { margin: 0; }
.howto p + p { margin-top: 8px; }

[hidden] { display: none !important; }

@keyframes piece-in {
  from { opacity: 0; transform: translateY(8px) scale(0.84); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes gather {
  0% { opacity: 1; transform: translateX(0) scale(1); }
  70% { opacity: 0.9; transform: translateX(18px) scale(0.72); }
  100% { opacity: 0.16; transform: translateX(28px) scale(0.42); }
}

@keyframes reveal {
  from { opacity: 0; transform: translateX(-24px) scale(0.45); }
  70% { transform: translateX(0) scale(1.14); }
  to { opacity: 1; transform: translateX(0) scale(1); }
}

@media (max-width: 820px) {
  .control-heading,
  .control-heading--lesson,
  .lesson-controller { align-items: stretch; flex-direction: column; }
  .example-picker { max-width: none; justify-content: flex-start; }
  .place-board { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 520px) {
  .app-shell { width: min(100% - 16px, 1120px); padding-top: 14px; }
  .mode-tabs { width: 100%; }
  .mode-tab { padding: 7px; }
  .control-card,
  .visual-card { border-radius: 18px; }
  .step-picker { display: grid; grid-template-columns: repeat(3, 1fr); }
  .step-button { min-width: 0; padding-inline: 7px; }
  .move-buttons { grid-template-columns: 1fr 1fr; }
  .move-buttons .reset-button { grid-column: 1 / -1; grid-row: 2; }
  .move-button { min-width: 0; }
  .supporter-tools__body { display: grid; grid-template-columns: 1fr 1fr; }
  .supporter-tools__body button { grid-column: 1 / -1; }
  .number-summary { align-items: flex-start; flex-direction: column; gap: 8px; }
  .ruler { padding-inline: 2px; }
  .ruler__ticks span:nth-child(even) { visibility: hidden; }
  .place-column header { padding: 9px; }
  .place-digit { width: 40px; height: 40px; }
  .piece-stack { min-height: 148px; padding-inline: 5px; }
  .regroup-event { grid-template-columns: minmax(74px, 1fr) auto minmax(74px, 1fr); padding-inline: 6px; }
  .regroup-event > p { font-size: 0.88rem; }
}

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

.custom-step-control {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 16px;
  padding: 12px 14px;
  border: 1px dashed #c8bca9;
  border-radius: 14px;
  background: #fffcf6;
}

.custom-step-control label {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-weight: 800;
}

.custom-step-control input {
  width: 72px;
  min-height: 42px;
  padding: 5px 8px;
  border: 2px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  font-weight: 800;
}

.custom-step-control button {
  min-height: 42px;
  padding: 7px 14px;
  border: 2px solid var(--green);
  border-radius: 999px;
  background: var(--green-pale);
  color: var(--green-dark);
  font-weight: 900;
  cursor: pointer;
}

.expression-form {
  display: grid;
  gap: 10px;
  margin-bottom: 20px;
  padding: clamp(14px, 3vw, 22px);
  border: 2px solid #bedab5;
  border-radius: 18px;
  background: linear-gradient(125deg, #f3faef, #fff);
}

.expression-form h2 { margin: 4px 0 0; font-family: var(--heading); font-size: clamp(1.25rem, 3vw, 1.55rem); }
.expression-form label { display: grid; gap: 6px; color: var(--muted); font-weight: 800; }
.expression-form__row { display: flex; flex-wrap: wrap; gap: 8px; }
.expression-form input {
  min-width: min(100%, 240px);
  min-height: 50px;
  flex: 1 1 190px;
  padding: 8px 12px;
  border: 2px solid #8fbe83;
  border-radius: 11px;
  background: #fff;
  color: var(--ink);
  font-size: 1.15rem;
  font-weight: 800;
}

.expression-form button {
  min-height: 50px;
  padding: 9px 15px;
  border: 0;
  border-radius: 11px;
  background: var(--green);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}

.expression-hint,
.expression-error { margin: 0; font-size: 0.9rem; font-weight: 700; }
.expression-hint { color: var(--muted); }
.expression-error { min-height: 1.35em; color: #b63b32; }

.operation-model {
  margin-top: 18px;
  padding: 16px;
  border: 2px solid #d5c7eb;
  border-radius: 18px;
  background: #faf8ff;
}

.operation-model__heading { display: grid; gap: 2px; }
.operation-model__heading p,
.operation-summary,
.operation-overflow { margin: 0; color: var(--muted); font-size: 0.88rem; font-weight: 800; }
.operation-model__heading b { font-family: var(--heading); font-size: 1.12rem; }
.operation-groups { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.operation-group__item {
  display: flex;
  align-items: center;
  min-height: 42px;
  gap: 7px;
  padding: 6px 9px;
  border: 2px solid #c9bce2;
  border-radius: 12px;
  background: #fff;
  opacity: 0.55;
  transition: opacity 0.24s ease, background 0.24s ease, transform 0.24s ease;
}

.operation-group__item.is-complete {
  border-color: var(--purple);
  background: #eee8fb;
  opacity: 1;
  transform: translateY(-1px);
}

.operation-group__item > span:first-child {
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 50%;
  background: var(--purple);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 900;
}

.operation-dots { display: flex; flex-wrap: wrap; gap: 3px; max-width: 88px; }
.operation-dot { display: block; width: 9px; height: 9px; border-radius: 50%; background: var(--orange); }
.operation-quantity { color: var(--orange); font-weight: 900; }
.operation-overflow { margin-top: 9px; }

.visual-picker-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fffcf7;
}

.visual-picker-card h2 { margin: 4px 0 0; font-family: var(--heading); font-size: 1.15rem; }
.visual-mode-picker { display: flex; flex-wrap: wrap; gap: 7px; }

.visual-mode-button {
  min-height: 42px;
  padding: 7px 12px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-weight: 900;
  cursor: pointer;
}

.visual-mode-button.is-active {
  border-color: var(--blue);
  background: var(--blue-pale);
  color: #1e617d;
}

.quantity-card {
  margin-bottom: 22px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--paper);
}

.balloon-scene,
.water-scene {
  display: grid;
  justify-items: center;
  align-content: center;
  min-height: 270px;
  padding: 12px;
  overflow: hidden;
  border-radius: 14px;
}

.balloon-scene {
  background:
    radial-gradient(circle at 15% 20%, rgba(255, 255, 255, 0.95) 0 8px, transparent 9px),
    radial-gradient(circle at 75% 32%, rgba(255, 255, 255, 0.82) 0 14px, transparent 15px),
    linear-gradient(#dff3fb, #f7fcff);
}

.balloon {
  display: grid;
  width: 170px;
  height: 190px;
  place-items: center;
  border: 4px solid #b34829;
  border-radius: 52% 48% 50% 50%;
  background: radial-gradient(circle at 32% 28%, #fff2d0 0 8%, #f5a14a 9% 24%, var(--orange) 55%);
  color: #fff;
  font-family: var(--heading);
  font-size: 2.8rem;
  font-weight: 900;
  text-shadow: 0 2px 0 rgba(116, 47, 23, 0.35);
  transform: scale(var(--balloon-size, 0.5));
  transform-origin: center bottom;
  transition: transform 0.36s ease, filter 0.36s ease, background 0.36s ease;
}

.balloon::after {
  position: absolute;
  bottom: -19px;
  width: 0;
  height: 0;
  border: 11px solid transparent;
  border-top-color: #b34829;
  content: "";
}

.balloon.is-empty { filter: saturate(0.45); background: #d6c9bf; }
.balloon-string { width: 2px; height: 38px; margin-top: -22px; background: #8c735f; }
.balloon-scene p,
.water-scene p { margin: 4px 0 0; color: var(--ink); font-size: 1.1rem; font-weight: 800; }

.water-scene { background: linear-gradient(150deg, #f4fbfd, #e6f4f8); }

.water-cup {
  position: relative;
  width: min(240px, 78vw);
  height: 210px;
  overflow: hidden;
  border: 5px solid #2e7da0;
  border-top: 0;
  border-radius: 0 0 28px 28px;
  background: rgba(255, 255, 255, 0.72);
  box-shadow: inset 0 0 0 6px rgba(255, 255, 255, 0.45);
}

.water-cup::before {
  position: absolute;
  inset: 0;
  z-index: 2;
  background: repeating-linear-gradient(to top, transparent 0 40px, rgba(46, 125, 160, 0.25) 41px 43px);
  content: "";
  pointer-events: none;
}

.water-fill {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  min-height: 0;
  place-items: center;
  overflow: hidden;
  background: linear-gradient(90deg, #66c3dd, #2e8ab1);
  color: #fff;
  font-family: var(--heading);
  font-size: 2.5rem;
  font-weight: 900;
  text-shadow: 0 2px 0 rgba(18, 81, 107, 0.35);
  transition: height 0.36s ease;
}

.water-fill::before {
  position: absolute;
  top: -8px;
  right: -12px;
  left: -12px;
  height: 17px;
  border-radius: 50%;
  background: #82d2e8;
  content: "";
}

.water-fill span { position: relative; z-index: 1; }

@media (max-width: 820px) {
  .visual-picker-card { align-items: flex-start; flex-direction: column; }
}

@media (max-width: 520px) {
  .custom-step-control { align-items: stretch; }
  .custom-step-control label { justify-content: space-between; }
  .custom-step-control button { width: 100%; }
  .expression-form__row { display: grid; }
  .expression-form input { min-width: 0; width: 100%; }
  .visual-mode-picker { width: 100%; }
  .visual-mode-button { flex: 1 1 auto; padding-inline: 7px; }
  .balloon-scene,
  .water-scene { min-height: 236px; }
  .balloon { width: 145px; height: 165px; }
  .water-cup { height: 184px; }
}
