:root {
  --wm-ink: #102532;
  --wm-muted: #526c7a;
  --wm-line: #b8d2df;
  --wm-blue: #149bd7;
  --wm-pale: #eef8fc;
  --wm-touch: 48px;
  --wm-radius: 10px;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; min-width: 320px; overflow: hidden; font-family: Arial, Helvetica, sans-serif; color: var(--wm-ink); background: #edf5f8; }
button, input, select, summary { font: inherit; }
button, summary, select, input { min-height: var(--wm-touch); }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .48; }

.app { width: 100%; height: 100vh; height: 100dvh; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); overflow: hidden; }
.topbar { display: grid; grid-template-columns: minmax(210px, 1fr) auto; align-items: center; gap: 16px; padding: 12px 18px; color: #fff; background: #071018; }
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-logo { display: inline-flex; border-radius: 6px; }
.brand img { display: block; width: 142px; height: auto; }
.brand h1 { margin: 0; font-size: clamp(1.25rem, 2vw, 2rem); line-height: 1.05; }
.top-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.icon-button, .home-button { min-width: var(--wm-touch); min-height: var(--wm-touch); border: 2px solid #263d51; border-radius: var(--wm-radius); color: #fff; background: #122436; }
.icon-button { display: inline-grid; place-items: center; padding: 0; }
.icon-button:hover, .icon-button:focus-visible, .home-button:hover, .home-button:focus-visible { color: #06111a; border-color: #8fd8f3; background: var(--wm-blue); }
.home-button { display: inline-flex; align-items: center; justify-content: center; padding: 0 14px; text-decoration: none; font-weight: 800; }
.icon { width: 24px; height: 24px; }
.top-view-menu { position: relative; }
.top-view-menu > summary { list-style: none; }
.top-view-menu > summary::-webkit-details-marker { display: none; }
.top-view-panel { position: absolute; z-index: 50; top: calc(100% + 8px); right: 0; display: grid; gap: 6px; width: 190px; padding: 10px; border: 1px solid #36546b; border-radius: var(--wm-radius); background: #102332; box-shadow: 0 12px 30px rgba(0,0,0,.28); }
.top-view-action { min-height: 44px; padding: 0 12px; border: 1px solid #4d687b; border-radius: 8px; color: #fff; background: #173247; text-align: left; }

#writtenMultiplicationModel { min-width: 0; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); overflow: hidden; }
.wm-toolbar { position: relative; z-index: 4; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 18px; border-bottom: 1px solid var(--wm-line); background: #fff; box-shadow: 0 5px 12px rgba(15,33,49,.06); }
.wm-control-group { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.wm-control-label { color: var(--wm-muted); font-weight: 800; }
.wm-alongside-field, .wm-speed-field { display: flex; align-items: center; gap: 8px; color: var(--wm-muted); font-weight: 800; }
.wm-alongside-field select { min-width: 190px; padding: 0 34px 0 11px; border: 2px solid #9bbdcd; border-radius: var(--wm-radius); color: var(--wm-ink); background: #fff; font-weight: 800; }
.wm-speed-field { margin-left: auto; }
.wm-speed-field select { min-width: 104px; min-height: var(--wm-touch); padding: 0 34px 0 11px; border: 2px solid #9bbdcd; border-radius: var(--wm-radius); color: var(--wm-ink); background: #fff; font-weight: 800; }
.wm-button { min-height: var(--wm-touch); display: inline-flex; align-items: center; justify-content: center; padding: 0 14px; border: 2px solid #9bbdcd; border-radius: var(--wm-radius); color: var(--wm-ink); background: #fff; font-weight: 800; }
.wm-button:hover, .wm-button:focus-visible { border-color: #0f80b3; background: #f3fbfe; }
.wm-button.wm-primary { color: #06111a; border-color: #0f80b3; background: #aee7fa; }
.wm-show-menu { position: relative; }
.wm-show-menu > summary { list-style: none; }
.wm-show-menu > summary::-webkit-details-marker { display: none; }
.wm-show-menu > summary::after { content: "▾"; margin-left: 9px; font-size: .86em; }
.wm-show-panel { position: absolute; z-index: 40; top: calc(100% + 7px); right: 0; display: grid; gap: 4px; width: 230px; padding: 9px; border: 1px solid #8fb8ca; border-radius: 10px; background: #fff; box-shadow: 0 12px 28px rgba(15,33,49,.2); }
.wm-show-option { min-height: 44px; display: flex; align-items: center; gap: 10px; padding: 7px 9px; border-radius: 7px; font-weight: 800; cursor: pointer; }
.wm-show-option[hidden] { display: none !important; }
.wm-show-option:hover, .wm-show-option:focus-within { background: var(--wm-pale); }
.wm-show-option input, .wm-worksheet-options input[type="radio"], .wm-worksheet-options input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; flex: 0 0 auto; accent-color: #0f80b3; }

.wm-workspace { position: relative; min-width: 0; min-height: 0; overflow: hidden; padding: 14px; }
.wm-stage { position: absolute; top: 50%; left: 50%; width: min(1480px, calc(100% - 28px)); min-width: 680px; margin: 0; transform-origin: center; transition: transform .18s ease; }
.wm-setup { display: grid; grid-template-columns: minmax(150px, 220px) minmax(150px, 220px) 1fr; gap: 12px; align-items: end; padding: 12px; border: 1px solid var(--wm-line); border-radius: 12px; background: #fff; }
.wm-entry { display: grid; gap: 5px; color: var(--wm-muted); font-weight: 800; }
.wm-entry input { width: 100%; padding: 0 12px; border: 2px solid #8fb8ca; border-radius: 9px; color: var(--wm-ink); background: #fff; font-size: 1.2rem; font-weight: 800; }
.wm-entry input:focus { outline: 3px solid rgba(20,155,215,.2); border-color: #0f80b3; }
.wm-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.wm-estimate { min-height: 54px; display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 10px 16px; margin: 10px 0 0; padding: 10px 14px; border: 2px solid #d6ad36; border-radius: 9px; background: #fff9df; }
.wm-estimate[hidden] { display: none !important; }
.wm-estimate-label { color: #6f5510; font-weight: 900; white-space: nowrap; }
.wm-estimate-calculation { min-width: 0; font-size: 1.2rem; text-align: center; }
.wm-estimate-comparison { color: #4f480f; font-weight: 800; text-align: right; }
.wm-feedback { min-height: 50px; margin: 10px 0; padding: 12px 14px; border-left: 5px solid var(--wm-blue); border-radius: 8px; background: #fff; font-size: 1.02rem; font-weight: 700; }
.wm-feedback[data-tone="complete"] { border-left-color: #1f8f55; background: #effaf4; }
.wm-feedback[data-tone="error"] { border-left-color: #c43f3f; background: #fff2f2; }
.wm-panels { display: grid; grid-template-columns: minmax(430px, 680px); justify-content: center; gap: 14px; align-items: stretch; }
#writtenMultiplicationModel:not(.wm-representation-hidden):not(.wm-expanded-visible) .wm-panels { grid-template-columns: minmax(430px, 680px) minmax(430px, 760px); }
.wm-expanded-visible.wm-representation-hidden .wm-panels { grid-template-columns: repeat(2, minmax(390px, 620px)); }
.wm-expanded-visible:not(.wm-representation-hidden) .wm-panels { grid-template-columns: minmax(360px, 520px) minmax(350px, 520px) minmax(410px, 660px); }
#writtenMultiplicationModel:not(.wm-representation-hidden):not(.wm-expanded-visible) .wm-stage { min-width: 874px; }
.wm-expanded-visible.wm-representation-hidden .wm-stage { min-width: 794px; }
.wm-expanded-visible:not(.wm-representation-hidden) .wm-stage { min-width: 1148px; }
.wm-panel { min-width: 0; min-height: 430px; padding: 14px; border: 1px solid var(--wm-line); border-radius: 12px; background: #fff; box-shadow: 0 5px 14px rgba(15,33,49,.06); }
.wm-panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.wm-panel-heading h2 { margin: 0; font-size: 1.12rem; }
.wm-algorithm-scroll { min-width: 0; overflow: hidden; padding: 6px 2px 12px; }
.wm-algorithm-scroll .question-visual, .wm-expanded-host .question-visual, .wm-representation-host .question-visual { margin: 0; }
.wm-algorithm-scroll figcaption, .wm-expanded-host figcaption, .wm-representation-host figcaption { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; white-space: nowrap; }
.wm-algorithm-scroll svg, .wm-expanded-host svg, .wm-representation-host svg { display: block; width: 100%; height: auto; max-height: 520px; }
.wm-number-sentence { min-height: 32px; margin: 10px 0 0; text-align: center; font-size: 1.25rem; font-weight: 900; }
.wm-expanded-host { min-height: 350px; display: grid; align-items: center; overflow: hidden; }
.wm-expanded-host svg { max-height: 610px; }
.wm-representation-host { min-height: 350px; display: grid; align-items: center; overflow: hidden; }
.wm-representation-composite { min-width: 0; display: grid; gap: 12px; align-items: center; }
.wm-primary-representation { min-width: 0; }
.wm-short-representation-composite { grid-template-rows: auto auto; }
.wm-short-representation-composite .wm-primary-representation svg { max-height: 300px; }
.wm-addition-section, .wm-row-total-card { min-width: 0; padding: 8px; border: 1px solid #c9dce5; border-radius: 9px; background: #f7fbfd; }
.wm-addition-section h3 { margin: 0 0 5px; color: var(--wm-muted); text-align: center; font-size: .92rem; }
.wm-addition-section svg { max-height: 210px; }
.wm-addition-section .column-grid-digit { font-size: 27px!important; }
.wm-long-representation-composite { grid-template-columns: minmax(0, 1.4fr) minmax(160px, .6fr); grid-template-rows: auto auto; align-items: stretch; }
.wm-long-representation-composite .wm-primary-representation { align-self: center; }
.wm-long-representation-composite .wm-primary-representation svg { max-height: 390px; }
.wm-row-total-stack { min-width: 0; display: grid; gap: 8px; align-content: center; }
.wm-row-total-card { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 5px; padding: 6px; white-space: nowrap; font-size: .9rem; }
.wm-row-total-card strong { font-size: 1rem; }
.wm-final-addition { grid-column: 1 / -1; justify-self: center; width: min(430px, 100%); }
.wm-empty-model { margin: auto; padding: 30px; color: var(--wm-muted); text-align: center; font-weight: 700; }

.wm-dialog { width: min(760px, calc(100vw - 28px)); max-height: min(82vh, 780px); padding: 0; border: 0; border-radius: 14px; color: var(--wm-ink); box-shadow: 0 22px 70px rgba(0,0,0,.35); }
.wm-dialog::backdrop { background: rgba(5,15,23,.66); }
.wm-dialog-header { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 15px 18px; color: #fff; background: #071018; }
.wm-dialog-header h2 { margin: 0; font-size: 1.3rem; }
.wm-icon-button { width: 44px; min-height: 44px; border: 1px solid #5b7385; border-radius: 8px; color: #fff; background: #173247; font-size: 1.6rem; }
.wm-dialog-body { padding: 18px 22px 24px; line-height: 1.5; }
.wm-dialog-body h3 { margin: 1.1em 0 .35em; }
.wm-dialog-body li + li { margin-top: .35em; }
.wm-dialog-actions { display: flex; justify-content: flex-end; gap: 9px; flex-wrap: wrap; margin-top: 16px; }
.wm-worksheet-options { display: grid; gap: 14px; }
.wm-worksheet-options fieldset { display: flex; gap: 10px 22px; flex-wrap: wrap; margin: 0; padding: 12px; border: 1px solid var(--wm-line); border-radius: 9px; }
.wm-worksheet-options legend { padding: 0 5px; font-weight: 900; }
.wm-worksheet-options fieldset label, .wm-worksheet-check { min-height: 36px; display: flex; align-items: center; gap: 9px; font-weight: 800; }
.wm-worksheet-field { display: grid; gap: 6px; font-weight: 900; }
.wm-worksheet-field select { width: 100%; min-height: 48px; padding: 0 38px 0 11px; border: 2px solid #9bbdcd; border-radius: 9px; color: var(--wm-ink); background: #fff; font-weight: 800; }
.wm-worksheet-summary { margin: 0; padding: 10px 12px; border-left: 4px solid var(--wm-blue); background: var(--wm-pale); font-weight: 800; }
.wm-worksheet-surface { display: none; }
.wm-worksheet-page { display: none; }

@media (max-width: 980px) {
  .topbar { grid-template-columns: 1fr; }
  .top-actions { justify-content: flex-start; }
  .wm-setup { grid-template-columns: 1fr 1fr; }
  .wm-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .wm-estimate { grid-template-columns: 1fr; justify-items: start; }
  .wm-estimate-calculation, .wm-estimate-comparison { text-align: left; }
  .wm-panel { min-height: 360px; }
}

@media (max-width: 620px) {
  .topbar { padding: 10px 12px; }
  .brand img { width: 116px; }
  .wm-toolbar { padding: 9px 10px; gap: 8px; }
  .wm-speed-field { margin-left: 0; }
  .wm-show-panel { right: 0; left: auto; }
  .wm-workspace { padding: 9px; }
  .wm-setup { grid-template-columns: 1fr; }
  .wm-actions { grid-column: auto; }
  .wm-actions .wm-button { flex: 1 1 140px; }
  .wm-estimate { gap: 5px; }
  .wm-panel { padding: 10px; }
  .wm-panel-heading { align-items: flex-start; flex-direction: column; }
  .wm-long-representation-composite { grid-template-columns: minmax(0, 1.25fr) minmax(150px, .75fr); }
}

@media (prefers-reduced-motion: reduce) {
  .wm-stage { transition: none; }
}

@page { size: A4 landscape; margin: 9mm; }
@media print {
  html, body { width: 100%; height: auto; min-height: 0; overflow: visible; background: #fff; }
  .topbar, .wm-toolbar, .wm-setup, .wm-feedback, .wm-dialog { display: none !important; }
  .app { display: block; width: auto; height: auto; min-height: 0; overflow: visible; }
  #writtenMultiplicationModel { display: block; overflow: visible; }
  .wm-workspace { overflow: visible; padding: 0; }
  .wm-stage { position: static; width: 100% !important; min-width: 0 !important; transform: none !important; }
  .wm-estimate { min-height: 0; margin: 0 0 4mm; padding: 2.5mm 3.5mm; gap: 2mm 4mm; break-inside: avoid; }
  .wm-panels { width: 100%; max-width: 170mm; grid-template-columns: minmax(0, 1fr); justify-content: center; gap: 5mm; margin: 0 auto; }
  #writtenMultiplicationModel:not(.wm-representation-hidden):not(.wm-expanded-visible) .wm-panels { max-width: none; grid-template-columns: minmax(0, 46fr) minmax(0, 54fr); }
  .wm-expanded-visible.wm-representation-hidden .wm-panels { max-width: none; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .wm-expanded-visible:not(.wm-representation-hidden) .wm-panels { max-width: none; grid-template-columns: minmax(0, 31fr) minmax(0, 31fr) minmax(0, 38fr); gap: 3mm; }
  .wm-panel { min-width: 0; min-height: 0; overflow: hidden; padding: 5mm; break-inside: avoid; box-shadow: none; }
  .wm-panel-heading { margin-bottom: 4mm; }
  .wm-algorithm-scroll .question-visual, .wm-expanded-host .question-visual, .wm-representation-host .question-visual { min-width: 0; }
  .wm-expanded-host, .wm-representation-host { min-height: 0; overflow: visible; }
  .wm-algorithm-scroll svg, .wm-expanded-host svg, .wm-representation-host svg { max-height: 112mm; }
  .spire-shared-print-copyright {
    position: fixed;
    right: 0;
    bottom: 0;
    z-index: 2;
    margin: 0 !important;
    padding-left: 2mm;
    background: #fff;
  }
  .wm-worksheet-surface { display: none !important; }
  html[data-spire-print-page="a4-landscape"] body.wm-printing-worksheet { display: block !important; height: auto !important; max-height: none !important; overflow: visible !important; }
  html[data-spire-print-page="a4-landscape"] body.wm-printing-worksheet > .app { display: none !important; }
  html[data-spire-print-page="a4-landscape"] body.wm-printing-worksheet > .wm-worksheet-page { display: flex !important; }
  html[data-spire-print-page="a4-landscape"] body.wm-printing-worksheet > .wm-worksheet-surface { display: block !important; width: 100%; }
  #writtenMultiplicationModel.wm-print-worksheet .wm-workspace { display: none !important; }
  #writtenMultiplicationModel.wm-print-worksheet .wm-worksheet-surface { display: block !important; width: 100%; }
  #writtenMultiplicationModel.wm-print-worksheet .spire-shared-print-copyright { display: none !important; }
  .wm-worksheet-page { box-sizing: border-box; width: 100%; height: 180mm; display: flex; flex-direction: column; gap: 3mm; overflow: hidden; color: #102532; background: #fff; }
  .wm-worksheet-page:not(:last-child) { break-after: page; page-break-after: always; }
  .wm-worksheet-header { min-height: 20mm; display: grid; grid-template-columns: 34mm minmax(0, 1fr) auto; align-items: center; gap: 4mm; padding-bottom: 2mm; border-bottom: .4mm solid #b8d2df; }
  .wm-worksheet-header img { display: block; width: 32mm; height: auto; }
  .wm-worksheet-header h1 { margin: 0; font-size: 18pt; }
  .wm-worksheet-header p { margin: 1mm 0 0; font-size: 9.5pt; }
  .wm-worksheet-pupil { display: grid; gap: 3mm; font-size: 10pt; font-weight: 800; white-space: nowrap; }
  .wm-worksheet-grid { min-height: 0; flex: 1; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4mm; }
  .wm-worksheet-grid[data-question-count="4"] { grid-template-rows: repeat(2, minmax(0, 1fr)); }
  .wm-worksheet-question { min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow: hidden; padding: 2.5mm; border: .45mm solid #9bbdcd; border-radius: 2mm; break-inside: avoid; }
  .wm-worksheet-question h2 { margin: 0 0 1mm; font-size: 13pt; }
  .wm-worksheet-estimate, .wm-worksheet-answer { margin: 0; font-size: 9.5pt; font-weight: 800; }
  .wm-worksheet-models { min-height: 0; flex: 1; display: grid; grid-template-columns: minmax(0, 1fr); align-items: center; gap: 2mm; }
  .wm-worksheet-question-supported .wm-worksheet-models { grid-template-columns: minmax(0, 46fr) minmax(0, 54fr); }
  .wm-worksheet-compact, .wm-worksheet-companion { min-width: 0; min-height: 0; overflow: hidden; }
  .wm-worksheet-question .question-visual { margin: 0; }
  .wm-worksheet-question figcaption { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
  .wm-worksheet-question svg { display: block; width: 100%; height: auto; max-height: 103mm; }
  .wm-worksheet-grid[data-question-count="4"] .wm-worksheet-question svg { max-height: 49mm; }
  .wm-worksheet-answer-page .wm-worksheet-question svg { max-height: 96mm; }
  .wm-worksheet-answer-page .wm-worksheet-grid[data-question-count="4"] .wm-worksheet-question svg { max-height: 51mm; }
  .wm-worksheet-answer { padding-top: 1mm; text-align: right; }
  .wm-worksheet-page footer { min-height: 5mm; display: flex; align-items: flex-end; justify-content: flex-end; font-size: 8.5pt; font-weight: 800; }
}
