:root {
  --navy: #063f86;
  --ink: #10294a;
  --muted: #637287;
  --line: #d8e2ec;
  --page: #f6f8fb;
  --blue: #0b67b2;
  --blue-soft: #eaf5ff;
  --teal: #08aaa5;
  --teal-soft: #e8f8f5;
  --orange: #f58220;
  --orange-soft: #fff2e7;
  --violet: #7b42b4;
  --violet-soft: #f5edff;
}

* { box-sizing: border-box; }
html { min-height: 100%; -webkit-text-size-adjust: 100%; }
body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: var(--page);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}
button, input, select { font: inherit; }
.skip-link { position: fixed; left: 12px; top: -80px; z-index: 20; padding: 10px 14px; border-radius: 8px; color: #fff; background: var(--navy); }
.skip-link:focus { top: 12px; }
.page-shell { width: min(1480px, calc(100% - 28px)); margin: 0 auto; }
.site-header { min-height: 96px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 22px; padding: 12px 4px 8px; }
.brand { width: 148px; }
.brand img { display: block; width: 100%; height: auto; }
.title-lockup { text-align: center; }
.title-lockup p { margin: 0 0 2px; color: var(--teal); font-size: .72rem; font-weight: 850; letter-spacing: .12em; text-transform: uppercase; }
.title-lockup h1 { margin: 0; color: var(--navy); font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.55rem, 2.6vw, 2.45rem); line-height: 1.05; }
.header-actions { justify-self: end; display: flex; align-items: center; gap: 8px; }
.button, .icon-button { border: 0; border-radius: 12px; font-weight: 780; cursor: pointer; text-decoration: none; transition: transform .15s ease, box-shadow .15s ease, opacity .15s ease; }
.button { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; padding: 9px 16px; }
.button:hover, .icon-button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .38; cursor: default; transform: none; }
.button-primary { color: #fff; background: var(--navy); box-shadow: 0 7px 16px rgba(6,63,134,.18); }
.button-secondary { color: var(--navy); background: #eef4fb; }
.button-light, .button-quiet { color: var(--navy); background: #fff; border: 1px solid var(--line); }
.icon-button { width: 44px; height: 44px; color: var(--navy); background: #fff; border: 1px solid var(--line); font-size: 1.45rem; }

.controls-card, .board-card, .table-card { border: 1px solid var(--line); border-radius: 22px; background: #fff; box-shadow: 0 9px 24px rgba(19,49,83,.07); }
.controls-card { padding: 15px 18px; }
#division-form { display: grid; grid-template-columns: minmax(160px, 1fr) auto minmax(135px, .75fr) auto auto auto; align-items: end; gap: 12px; }
.number-field, .decimal-field { display: grid; gap: 6px; color: var(--muted); font-size: .73rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.number-field input, .decimal-field select { width: 100%; height: 46px; border: 2px solid var(--role); border-radius: 11px; padding: 6px 12px; color: var(--ink); background: #fff; font-size: 1.2rem; font-weight: 800; outline: none; }
.number-field input:focus, .decimal-field select:focus { box-shadow: 0 0 0 4px color-mix(in srgb, var(--role) 18%, transparent); }
.role-dividend { --role: var(--blue); }
.role-divisor { --role: var(--teal); }
.division-symbol { align-self: center; padding-top: 16px; color: var(--ink); font-size: 1.5rem; font-weight: 900; }
.mode-control { min-width: 205px; margin: 0; padding: 0; border: 0; }
.mode-control legend { margin-bottom: 6px; color: var(--muted); font-size: .73rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.segmented { display: flex; height: 46px; padding: 4px; border-radius: 12px; background: #eef2f7; }
.segmented button { flex: 1; padding: 6px 11px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; font-weight: 780; cursor: pointer; }
.segmented button.is-active { color: var(--navy); background: #fff; box-shadow: 0 2px 7px rgba(26,49,77,.12); }
.decimal-field { --role: var(--orange); width: 94px; }
.form-error { margin: 9px 0 0; color: #a22c38; font-weight: 700; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr) 250px; gap: 15px; margin-top: 15px; }
.board-card, .table-card { min-width: 0; }
.board-card { padding: 15px; }
.instruction { min-height: 74px; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 13px 17px; border-radius: 15px; background: #f3f7fc; }
.instruction p, .table-heading p { margin: 0 0 3px; color: var(--teal); font-size: .75rem; font-weight: 900; letter-spacing: .1em; text-transform: uppercase; }
.instruction h2 { margin: 0; color: var(--ink); font-size: clamp(1rem, 1.7vw, 1.28rem); line-height: 1.28; }
.instruction > span { flex: none; color: var(--muted); font-size: .82rem; font-weight: 800; }
.division-stage { min-height: 470px; display: grid; grid-template-rows: 1fr auto; padding: 30px 28px 16px; }
.long-division { align-self: start; width: min(100%, 820px); margin: 0 auto; display: grid; grid-template-columns: minmax(330px, 1fr) minmax(150px, .42fr); }
.work-column { min-width: 0; padding: 16px 12px 30px 0; }
.digit-row { min-height: 50px; display: grid; grid-template-columns: repeat(var(--columns), minmax(30px, 48px)); justify-content: end; align-items: center; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: clamp(1.45rem, 3.2vw, 2.25rem); font-weight: 800; }
.digit { position: relative; min-width: 0; text-align: center; }
.digit.comma-after::after { content: ","; position: absolute; right: -0.15em; bottom: -0.03em; }
.digit.pending { opacity: .12; }
.digit.active { color: var(--blue); background: var(--blue-soft); border-radius: 8px; }
.digit.product-active { color: var(--ink); }
.subtraction-row { position: relative; color: var(--ink); border-bottom: 2px solid var(--ink); }
.subtraction-row::before { content: "−"; position: absolute; left: 1px; color: var(--ink); }
.partial-row { color: var(--blue); }
.falling-digit { z-index: 1; color: var(--teal); animation: descend .8s ease-out both; }
.falling-digit::before { content: "↓"; position: absolute; left: 50%; bottom: 76%; color: var(--teal); font-family: system-ui, sans-serif; font-size: .9em; transform: translateX(-50%); animation: fade-arrow 1.5s ease both; }
@keyframes descend { from { transform: translateY(-22px); opacity: .15; } to { transform: translateY(0); opacity: 1; } }
@keyframes fade-arrow { 0%, 55% { opacity: .85; } 100% { opacity: 0; } }
.potence { border-left: 4px solid var(--navy); }
.potence-box { min-height: 82px; display: grid; place-items: center; position: relative; padding: 14px 10px 24px; font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: clamp(1.5rem, 3vw, 2.2rem); font-weight: 850; }
.potence-box:first-child { border-bottom: 4px solid var(--navy); color: var(--teal); }
.potence-box:last-child { color: var(--orange); }
.role-label { position: absolute; left: 50%; bottom: 5px; transform: translateX(-50%); color: var(--muted); font-family: "Segoe UI", system-ui, sans-serif; font-size: .65rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.role-strip { display: grid; grid-template-columns: repeat(7, auto); justify-content: center; align-items: center; gap: 8px; margin-top: 16px; }
.role-card { min-width: 105px; display: grid; gap: 3px; padding: 8px 12px; border: 2px solid var(--role-color); border-radius: 10px; text-align: center; background: var(--role-soft); }
.role-card strong { min-height: 1.3em; color: var(--role-color); font-family: "DejaVu Sans Mono", Consolas, monospace; font-size: 1.2rem; }
.role-card small { color: var(--muted); font-size: .65rem; font-weight: 850; letter-spacing: .07em; text-transform: uppercase; }
.role-card.dividend { --role-color: var(--blue); --role-soft: var(--blue-soft); }
.role-card.divisor { --role-color: var(--teal); --role-soft: var(--teal-soft); }
.role-card.quotient { --role-color: var(--orange); --role-soft: var(--orange-soft); }
.role-card.remainder { --role-color: var(--violet); --role-soft: var(--violet-soft); }
.relation-sign { color: var(--ink); font-size: 1.25rem; font-weight: 900; }
.step-controls { display: grid; grid-template-columns: auto minmax(80px, 1fr) auto auto; align-items: center; gap: 10px; padding-top: 12px; border-top: 1px solid #e7edf4; }
.progress { height: 6px; overflow: hidden; border-radius: 999px; background: #e9eef5; }
.progress span { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--teal), var(--blue)); transition: width .2s ease; }

.table-card { align-self: start; padding: 16px; }
.table-heading { padding: 3px 4px 12px; }
.table-heading h2 { margin: 0; color: var(--navy); font-size: 1.3rem; }
.multiples { display: grid; gap: 4px; }
.multiple { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 7px; min-height: 35px; padding: 5px 9px; border-radius: 9px; color: var(--muted); font-variant-numeric: tabular-nums; }
.multiple:nth-child(odd) { background: #f6f8fb; }
.multiple strong { color: var(--ink); text-align: right; }
.multiple.is-active { color: var(--orange); background: var(--orange-soft); box-shadow: inset 3px 0 var(--orange); }
.multiple.is-active strong { color: var(--orange); }
.site-footer { display: flex; flex-wrap: wrap; justify-content: center; gap: 7px; padding: 19px 10px 24px; color: #718096; font-size: .8rem; text-align: center; }
.site-footer a, .site-footer button { padding: 0; border: 0; color: #526d8d; background: transparent; font: inherit; text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }

@media (max-width: 1020px) {
  #division-form { grid-template-columns: 1fr auto 1fr auto; }
  .mode-control { min-width: 0; }
  .decimal-field { width: auto; }
  #division-form > .button-primary { grid-column: -2 / -1; }
}
@media (max-width: 780px) {
  .page-shell { width: min(100% - 18px, 1480px); }
  .site-header { grid-template-columns: auto 1fr auto; gap: 10px; }
  .brand { width: 112px; }
  .title-lockup p { display: none; }
  .title-lockup h1 { font-size: 1.35rem; }
  .button-light { display: none; }
  #division-form { grid-template-columns: 1fr auto 1fr; }
  .mode-control { grid-column: 1 / 3; }
  .decimal-field { grid-column: 3; }
  #division-form > .button-primary { grid-column: 1 / -1; }
  .workspace { grid-template-columns: 1fr; }
  .table-card { width: 100%; }
  .multiples { grid-template-columns: repeat(2, 1fr); }
  .long-division { grid-template-columns: minmax(230px, 1fr) minmax(110px, .45fr); }
  .division-stage { min-height: 410px; padding: 22px 4px 13px; overflow-x: auto; }
  .digit-row { grid-template-columns: repeat(var(--columns), minmax(25px, 38px)); }
  .role-strip { grid-template-columns: repeat(4, minmax(72px, 1fr)); width: min(100%, 500px); margin-inline: auto; }
  .relation-sign { display: none; }
  .role-card { min-width: 0; padding: 7px 4px; }
  .step-controls { grid-template-columns: 1fr 1fr; }
  .progress { grid-row: 2; grid-column: 1 / -1; }
}
@media (max-width: 480px) {
  .header-actions { display: none; }
  .site-header { grid-template-columns: auto 1fr; }
  .title-lockup { text-align: right; }
  .controls-card { padding: 12px; }
  #division-form { grid-template-columns: 1fr auto 1fr; gap: 8px; }
  .instruction { align-items: flex-start; }
  .instruction h2 { font-size: .96rem; }
  .step-controls .button { padding-inline: 9px; font-size: .83rem; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
