:root {
  --blue: #149bd7;
  --ink: #102131;
  --muted: #5c6b78;
  --line: #d8e2ea;
  --panel: #ffffff;
  --paper: #f5f9fc;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.12);
  --radius: 8px;
  --touch: 48px;
}

* { box-sizing: border-box; }

html,
body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: #eef4f8;
  font-family: Arial, Helvetica, sans-serif;
}

body {
  overflow: hidden;
  touch-action: manipulation;
}

button,
input,
select { font: inherit; }

button,
select,
input[type="text"] {
  min-height: var(--touch);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
}

button {
  padding: 0 14px;
  cursor: pointer;
  font-weight: 800;
}

button:hover,
select:hover { border-color: var(--blue); background: #e5f6fc; }

button:focus-visible,
select:focus-visible,
input:focus-visible,
.representation-control input:focus-visible + span,
.home-button:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 2px;
}

button:disabled { cursor: wait; opacity: 0.55; }

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

.topbar {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) auto;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  background: #071018;
  color: #fff;
}

.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; object-fit: contain; }
.title-block { min-width: 0; }
h1 { margin: 0; font-size: clamp(1.35rem, 2vw, 2.05rem); line-height: 1.05; }

.top-actions { display: flex; align-items: center; justify-content: end; gap: 8px; flex-wrap: wrap; }

.icon-button {
  width: var(--touch);
  padding: 0;
  display: inline-grid;
  place-items: center;
  border-color: #263d51;
  background: #122436;
  color: #fff;
}

.icon-button:hover,
.icon-button[aria-pressed="true"] { border-color: #8fd8f3; background: var(--blue); color: #06111a; }
.icon { display: block; width: 24px; height: 24px; pointer-events: none; }

.home-button {
  min-height: var(--touch);
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #263d51;
  border-radius: var(--radius);
  background: #122436;
  color: #fff;
  font-weight: 800;
  text-decoration: none;
}

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

.toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  box-shadow: 0 6px 14px rgba(15, 33, 49, 0.06);
  z-index: 3;
}

.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.controls > label { color: var(--muted); font-size: 0.9rem; font-weight: 800; }
select { padding: 0 34px 0 12px; background: #f8fbfd; font-weight: 800; }
button.primary { border-color: var(--blue); background: var(--blue); color: #06111a; }

.representation-control {
  min-width: 0;
  margin: 0 6px 0 0;
  padding: 0;
  display: inline-flex;
  border: 0;
}

.representation-control legend {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.representation-control label { position: relative; cursor: pointer; }
.representation-control input { position: absolute; opacity: 0; pointer-events: none; }
.representation-control span {
  min-height: var(--touch);
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  border: 2px solid var(--line);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 800;
}
.representation-control label:first-of-type span { border-radius: var(--radius) 0 0 var(--radius); }
.representation-control label:last-of-type span { margin-left: -2px; border-radius: 0 var(--radius) var(--radius) 0; }
.representation-control input:checked + span { position: relative; z-index: 1; border-color: var(--blue); background: #dff4fc; }

.sentence,
.status {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  min-height: 0 !important;
  margin: -1px !important;
  padding: 0 !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.workspace {
  min-height: 0;
  padding: 18px;
  overflow: auto;
  overscroll-behavior: contain;
  background:
    linear-gradient(#dce8f0 1px, transparent 1px),
    linear-gradient(90deg, #dce8f0 1px, transparent 1px);
  background-size: 34px 34px;
}

.stage {
  --view-zoom: 1;
  min-width: calc(980px * var(--view-zoom));
  width: max-content;
  max-width: none;
  margin: 0 auto;
}

.workspace.is-fit .stage { width: 100%; min-width: 0; }

.chart-shell {
  position: relative;
  width: calc(980px * var(--view-zoom));
  min-width: 720px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fff;
  box-shadow: var(--shadow);
}

.workspace.is-fit .chart-shell { width: 100%; min-width: 0; }
.chart-host { width: 100%; }
.chart-host .place-value-model { width: 100%; max-width: none; margin: 0; }
.chart-host .diagram-visual {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: calc(100dvh - 230px);
  margin-inline: auto;
}
.chart-host figcaption { display: none; }

.input-layer { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.input-layer[hidden] { display: none; }

.place-entry {
  position: absolute;
  min-height: 0 !important;
  padding: 0;
  pointer-events: auto;
  text-align: center;
  font-weight: 850;
  box-shadow: 0 6px 15px rgba(15, 33, 49, 0.14);
}

.digit-stepper {
  position: absolute;
  display: grid;
  grid-template-rows: var(--digit-arrow-height, 20px) var(--digit-entry-height, 78px) var(--digit-arrow-height, 20px);
  row-gap: var(--digit-gap, 10px);
  border-radius: 8px;
  background: transparent;
  box-shadow: inset 0 0 0 1px rgba(91, 111, 126, 0.4);
  pointer-events: none;
}
.digit-stepper:focus-within {
  box-shadow: inset 0 0 0 2px var(--blue), 0 0 0 3px rgba(20, 155, 215, 0.2);
}
.digit-stepper > * { pointer-events: auto; }
.digit-stepper-button {
  width: 34px;
  min-height: var(--digit-arrow-height, 20px) !important;
  height: var(--digit-arrow-height, 20px);
  justify-self: center;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: #536a79;
  font-size: 0.72rem;
  line-height: 1;
  opacity: 0.72;
}
.digit-stepper-button:hover,
.digit-stepper-button:focus-visible { border-color: var(--blue); background: #d8f1fb; color: #17354a; opacity: 1; }
.place-entry--digit {
  position: static;
  width: 100% !important;
  height: var(--digit-entry-height, 78px) !important;
  border-color: transparent;
  border-radius: 0;
  background: transparent !important;
  color: transparent !important;
  -webkit-text-fill-color: transparent !important;
  caret-color: transparent !important;
  font-size: clamp(2.7rem, 4.6vw, 4.65rem);
  box-shadow: none;
  cursor: default;
}
.place-entry--digit:focus,
.place-entry--digit:focus-visible { border-color: transparent; outline: none; }
.place-entry--counter { border-color: var(--blue); background: rgba(255, 255, 255, 0.96); font-size: 1rem; }

.print-copyright { display: none; }

.instructions-dialog {
  width: min(820px, calc(100vw - 32px));
  max-height: min(780px, calc(100vh - 32px));
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  color: var(--ink);
  background: #fff;
  box-shadow: 0 24px 70px rgba(6, 17, 26, 0.28);
}
.instructions-dialog::backdrop { background: rgba(6, 17, 26, 0.5); }
.instructions-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; background: #071018; color: #fff; }
.instructions-header h2 { margin: 0; font-size: 1.25rem; }
.instructions-body { max-height: calc(min(780px, 100vh - 32px) - 76px); padding: 16px 18px 20px; overflow: auto; line-height: 1.45; }
.instructions-body h3 { margin: 16px 0 8px; font-size: 1.05rem; }
.instructions-body h3:first-child { margin-top: 0; }
.instructions-body p,
.instructions-body ul,
.instructions-body ol { margin: 0 0 10px; }
.instructions-body li { margin-bottom: 6px; }

@media (max-width: 1040px) {
  body { overflow: auto; }
  .app { min-height: 100vh; height: auto; }
  .topbar,
  .toolbar { grid-template-columns: 1fr; }
  .top-actions { justify-content: start; }
  .workspace { min-height: 620px; }
}

@media (max-width: 680px) {
  .topbar,
  .toolbar { padding-inline: 10px; }
  .controls { align-items: stretch; }
  .representation-control { width: 100%; }
  .representation-control label { flex: 1; }
  .representation-control span { width: 100%; justify-content: center; }
  .controls > label { align-self: center; }
  .workspace { min-height: 560px; padding: 10px; }
  .chart-shell { min-width: 700px; }
}

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

@media print {
  @page { size: A4 landscape; margin: 10mm; }

  html,
  body { width: auto; height: auto; overflow: visible; background: #fff; }
  .app { display: block; height: auto; min-width: 0; }
  .topbar { display: block; padding: 0 0 6mm; background: #fff; color: var(--ink); }
  .brand { align-items: flex-start; }
  .brand img { width: 42mm; }
  .top-actions,
  .toolbar .controls,
  .input-layer,
  .status,
  .instructions-dialog { display: none !important; }
  .toolbar { display: block; padding: 0 0 4mm; border: 0; box-shadow: none; }
  .sentence { display: none !important; }
  .workspace { width: 100% !important; min-width: 0 !important; padding: 0; overflow: visible; background: #fff; }
  .stage,
  .workspace.is-fit .stage {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    margin: 0 !important;
  }
  .chart-shell,
  .workspace.is-fit .chart-shell {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    border-color: #7f8d98;
    box-shadow: none;
    overflow: visible;
    break-inside: avoid-page;
  }
  .chart-host,
  .chart-host .place-value-model {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    max-height: 142mm;
    margin: 0 !important;
  }
  .chart-host .diagram-visual {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: 132mm !important;
    margin: 0 auto !important;
  }
  .print-copyright { display: block; margin-top: 3mm; font-size: 9pt; font-weight: 700; text-align: right; }
}
