:root {
  --blue: #149bd7;
  --blue-dark: #087daf;
  --ink: #102131;
  --muted: #5c6b78;
  --paper: #eef4f8;
  --panel: #ffffff;
  --line: #d8e2ea;
  --dark: #071018;
  --green: #1d8f5a;
  --red: #f26d6d;
  --amber: #f3b23a;
  --violet: #6d5fd0;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.14);
  --radius: 8px;
  --touch: 48px;
  font-family: Arial, Helvetica, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(#dce8f0 1px, transparent 1px),
    linear-gradient(90deg, #dce8f0 1px, transparent 1px),
    var(--paper);
  background-size: 34px 34px;
  letter-spacing: 0;
  touch-action: manipulation;
}

button,
select {
  font: inherit;
  min-height: var(--touch);
  border-radius: var(--radius);
}

button {
  border: 2px solid transparent;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-weight: 900;
}

select {
  border: 2px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  padding: 0 12px;
  font-weight: 800;
}

select.compact-select {
  max-width: 170px;
}

button:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 2px;
}

.app {
  min-width: 320px;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.spire-add-sub-model {
  display: contents;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 6px 14px;
  background: var(--dark);
  color: #ffffff;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  display: inline-flex;
  border-radius: 6px;
}

.brand img {
  width: 112px;
  height: auto;
  display: block;
  object-fit: contain;
  flex: 0 0 auto;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.1;
}

h1 {
  font-size: clamp(1.25rem, 2vw, 1.95rem);
  font-weight: 800;
}

.top-actions,
.toolbar,
.toolbar-left,
.toolbar-right,
.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.top-actions {
  justify-content: end;
}

.icon-button,
.home-button {
  min-height: var(--touch);
  background: #122436;
  color: #ffffff;
  border: 2px solid #263d51;
  border-radius: var(--radius);
}

.icon-button {
  width: var(--touch);
  padding: 0;
  display: inline-grid;
  place-items: center;
}

.home-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  text-decoration: none;
  font-weight: 800;
  white-space: nowrap;
}

.icon-button:hover,
.home-button:hover,
.home-button:focus-visible {
  background: var(--blue);
  border-color: #8fd8f3;
  color: #06111a;
}

.icon {
  width: 24px;
  height: 24px;
  display: block;
  pointer-events: none;
}

.toolbar {
  justify-content: space-between;
  padding: 8px 14px;
  background: #ffffff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 14px rgba(15, 33, 49, 0.06);
  z-index: 3;
}

.toolbar-right {
  margin-left: auto;
  justify-content: end;
}

.control-label {
  color: var(--muted);
  font-weight: 800;
  font-size: 0.82rem;
}

.wide-button {
  min-height: var(--touch);
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-color: var(--line);
  background: #f8fbfd;
  color: var(--ink);
  white-space: nowrap;
}

.wide-button.primary,
.toggle-button.is-on {
  background: var(--violet);
  border-color: #4d409f;
  color: #ffffff;
}

.wide-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.segmented-control {
  min-height: var(--touch);
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
}

.segmented-control button {
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 0;
  background: #f8fbfd;
  color: var(--ink);
}

.segmented-control button + button {
  border-left: 2px solid var(--line);
}

.segmented-control button.is-on,
.segmented-control button[aria-pressed="true"] {
  background: var(--violet);
  color: #ffffff;
}

.history-button {
  width: var(--touch);
  padding: 0;
}

.history-icon {
  width: 22px;
  height: 22px;
  display: block;
}

.stepper {
  display: grid;
  grid-template-columns: 42px 58px 42px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #ffffff;
}

.stepper.wide-stepper {
  grid-template-columns: 42px minmax(104px, auto) 42px;
}

.stepper button {
  min-height: 44px;
  border: 0;
  border-radius: 0;
  background: #f8fbfd;
  color: var(--ink);
  font-size: 1.25rem;
  padding: 0;
}

.stepper button:hover { background: #e7f6fc; }

.stepper-output {
  min-height: 44px;
  display: grid;
  place-items: center;
  border-inline: 2px solid var(--line);
  font-size: 1.18rem;
  font-weight: 900;
}

.tool-menu {
  position: relative;
}

.tool-menu summary {
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #f8fbfd;
  font-weight: 900;
  cursor: pointer;
  list-style: none;
}

.tool-menu summary::-webkit-details-marker { display: none; }

.menu-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 12;
  width: min(330px, calc(100vw - 28px));
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: var(--shadow);
}

.menu-panel .control-group {
  align-items: start;
  justify-content: space-between;
}

.menu-panel .control-group select {
  width: 100%;
}

.menu-panel .wide-button {
  width: 100%;
}

.check-row {
  min-height: 38px;
  display: grid;
  grid-template-columns: 24px 1fr;
  gap: 8px;
  align-items: center;
  font-weight: 800;
}

.check-row input {
  width: 20px;
  height: 20px;
  accent-color: var(--blue);
}

.model-root {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  padding: 8px 12px 12px;
  overflow: hidden;
}

.spire-add-sub-model.number-sentence-hidden .model-root {
  grid-template-rows: minmax(0, 1fr);
}

.equation-strip {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.equation {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: clamp(8px, 2vw, 18px);
  font-size: clamp(2rem, 5vw, 4rem);
  font-weight: 900;
  line-height: 1;
  white-space: nowrap;
}

.equation.is-multi {
  flex-wrap: wrap;
  align-items: center;
  row-gap: 7px;
  font-size: clamp(1.35rem, 3vw, 2.7rem);
  white-space: normal;
}

.equation.is-fact-family {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, auto));
  justify-content: center;
  gap: 8px 18px;
  font-size: clamp(1.08rem, 2.1vw, 1.9rem);
}

.equation-extra,
.fact-family-line {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.equation-extra {
  flex-basis: 100%;
  color: var(--muted);
  font-size: 0.7em;
}

.equation-value {
  min-width: 1.4em;
  text-align: center;
}

.equation-value.is-covered {
  color: #ffffff;
  background: var(--ink);
  border-radius: var(--radius);
}

.equation-note {
  color: var(--muted);
  font-weight: 800;
  text-align: right;
  line-height: 1.3;
}

.model-stage {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 8px;
  overflow: hidden;
}

.visual-board {
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--shadow);
}

.visual-zoom {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  transform-origin: center center;
}

.spire-add-sub-model:not(.flexible-part-whole) #visualFrame {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
}

.flexible-part-whole .model-root {
  grid-template-rows: auto minmax(0, 1fr);
  gap: 4px;
  padding: 4px 8px 8px;
}

.flexible-part-whole .model-stage {
  grid-template-rows: minmax(0, 1fr);
  gap: 6px;
}

.flexible-part-whole .equation-strip {
  grid-template-columns: 1fr;
  padding: 4px 8px;
}

.flexible-part-whole .equation-note,
.flexible-part-whole .teaching-panel {
  display: none;
}

.flexible-part-whole .visual-board {
  padding: 0;
  height: 100%;
  position: relative;
}

.flexible-part-whole .visual-zoom,
.flexible-part-whole #visualFrame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.flexible-part-whole #visualFrame {
  display: grid;
  place-items: center;
}

.flexible-part-whole .model-svg {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

.editable-number {
  cursor: pointer;
}

.editable-number circle {
  transition: stroke 0.14s ease, stroke-width 0.14s ease, filter 0.14s ease;
}

.editable-number:hover circle,
.editable-number.is-editing circle {
  stroke: var(--violet);
  stroke-width: 11px;
  filter: drop-shadow(0 8px 12px rgba(82, 73, 168, 0.18));
}

.number-pad {
  position: absolute;
  right: 14px;
  bottom: 14px;
  z-index: 6;
  width: min(280px, calc(100% - 28px));
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: 0 18px 36px rgba(15, 33, 49, 0.18);
}

.number-pad[hidden] {
  display: none;
}

.number-pad-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}

.number-pad-header strong {
  font-size: 0.94rem;
}

.number-pad-header output {
  min-width: 96px;
  padding: 6px 8px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #f8fbfd;
  text-align: right;
  font-weight: 900;
}

.number-pad-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.number-pad-grid button {
  min-height: 42px;
  padding: 0 8px;
  border-color: var(--line);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 900;
}

.number-pad-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.model-svg,
.number-line-rep {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.number-line-representation #visualFrame {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
}

#visualFrame .number-line-model {
  width: 100%;
  height: 100%;
  margin: 0;
  display: grid;
  place-items: center;
}

#visualFrame .ten-frame-model,
#visualFrame .double-sided-counters-model,
#visualFrame .counter-set-model {
  width: 100%;
  height: 100%;
  margin: 0;
  display: grid;
  place-items: center;
  align-content: center;
}

#visualFrame .ten-frame-model .diagram-visual,
#visualFrame .double-sided-counters-model .diagram-visual,
#visualFrame .counter-set-model .diagram-visual {
  width: min(100%, 920px);
  max-height: 100%;
}

#visualFrame .ten-frame-model {
  position: absolute;
  inset: 0;
  min-height: 0;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#visualFrame .ten-frame-model .diagram-visual {
  flex: 0 1 auto;
  min-height: 0;
  width: min(94%, 1040px);
  height: calc(100% - 36px);
  max-height: calc(100% - 36px);
}

#visualFrame .ten-frame-model .model-note {
  margin: 4px 12px 0;
}

#visualFrame .number-line-model .diagram-visual {
  width: min(100%, 920px);
  max-height: 100%;
}

#visualFrame .number-line-model figcaption,
#visualFrame .ten-frame-model figcaption,
#visualFrame .double-sided-counters-model figcaption,
#visualFrame .counter-set-model figcaption,
#visualFrame .number-line-model .model-number-sentence {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.model-svg text,
.number-line-rep text {
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 800;
  fill: var(--ink);
}

.teaching-panel {
  min-height: 52px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: 0 8px 18px rgba(15, 33, 49, 0.07);
}

.teaching-panel strong {
  display: block;
  margin-bottom: 2px;
}

.teaching-panel span {
  color: var(--muted);
  font-weight: 800;
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(7, 16, 24, 0.62);
}

.dialog {
  width: min(780px, 100%);
  max-height: min(84vh, 760px);
  overflow: auto;
  padding: 18px;
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
}

.dialog h2 { margin-top: 0; }
.dialog li { margin: 0.4rem 0; }

.print-only {
  display: none;
}

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

  .top-actions {
    justify-content: start;
  }
}

@media (max-width: 920px) {
  html,
  body {
    overflow: auto;
  }

  .app {
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  .toolbar,
  .equation-strip,
  .teaching-panel {
    grid-template-columns: 1fr;
  }

  .model-root {
    min-height: 720px;
    overflow: visible;
  }

  .equation-note {
    text-align: left;
  }
}

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

  .top-actions,
  .toolbar-right {
    justify-content: start;
  }

  .toolbar-right .tool-menu .menu-panel {
    width: min(330px, calc(100vw - 24px));
    right: auto;
    left: 0;
  }

  .equation {
    font-size: clamp(1.8rem, 10vw, 2.8rem);
    gap: 7px;
  }
}

@media print {
  @page { size: A4 landscape; margin: 8mm; }
  html,
  body {
    height: 194mm;
    min-height: 0;
    max-height: 194mm;
    overflow: hidden;
    background: #ffffff;
  }
  .topbar,
  .toolbar,
  .dialog-backdrop,
  .teaching-panel {
    display: none !important;
  }
  .app {
    height: 100% !important;
    min-height: 0;
    max-height: 100%;
    display: block;
    overflow: hidden;
  }
  .spire-add-sub-model:not(.flexible-part-whole) .model-root {
    box-sizing: border-box;
    height: 100% !important;
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 3mm;
    padding: 0;
    overflow: hidden;
  }
  .equation-strip,
  .visual-board {
    box-shadow: none;
    break-inside: avoid;
  }
  .spire-add-sub-model:not(.flexible-part-whole) .model-stage {
    height: 100%;
    min-height: 0;
    grid-template-rows: minmax(0, 1fr);
    gap: 0;
    overflow: hidden;
  }
  .spire-add-sub-model:not(.flexible-part-whole) .visual-board,
  .spire-add-sub-model:not(.flexible-part-whole) .visual-zoom,
  .spire-add-sub-model:not(.flexible-part-whole) #visualFrame,
  .spire-add-sub-model:not(.flexible-part-whole) #visualFrame .question-visual {
    width: 100%;
    height: 100% !important;
    min-height: 0;
    max-height: 100%;
  }
  .spire-add-sub-model:not(.flexible-part-whole) .visual-zoom {
    transform: none !important;
  }
  .spire-add-sub-model:not(.flexible-part-whole) #visualFrame .question-visual {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    overflow: hidden;
  }
  .spire-add-sub-model:not(.flexible-part-whole) #visualFrame .diagram-visual {
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
  }
  .spire-add-sub-model:not(.flexible-part-whole) #visualFrame .model-note {
    margin: 2mm 0 0;
  }
  .flexible-part-whole .model-root {
    box-sizing: border-box;
    height: 154mm;
    min-height: 154mm;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 3mm;
    padding: 0;
    overflow: hidden;
  }
  .flexible-part-whole .model-stage,
  .flexible-part-whole .visual-board {
    height: 100%;
    min-height: 0;
  }
  .flexible-part-whole .model-stage {
    display: grid;
    grid-template-rows: minmax(0, 1fr);
    gap: 0;
    overflow: hidden;
  }
  .flexible-part-whole .visual-board {
    position: relative;
  }
  .flexible-part-whole .visual-zoom,
  .flexible-part-whole #visualFrame {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    transform: none !important;
  }
  .flexible-part-whole #visualFrame {
    display: grid;
    place-items: center;
    overflow: hidden;
  }
  .flexible-part-whole .model-svg {
    display: block;
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
  }
  .print-only {
    display: block;
    margin-top: 8px;
    color: #5c6b78;
    font-weight: 700;
  }
}
