.app-layout {
    flex: 1 1 auto;
    display: flex;
    min-height: 0;
}
#editor-column {
    flex: 0 0 380px;
    min-width: 280px;
    max-width: 55%;
    overflow-y: auto;
    padding: 20px 12px 40px 24px;
}
#display-column {
    flex: 1 1 auto;
    /* min-width:0 (not just min-width:300px, which only sets a floor, not
       an override of flex's default min-width:auto) is what actually lets
       this column shrink below its content's intrinsic width — without it
       a wide calc board pushes the whole page wider instead of scrolling
       inside #calc-stage. */
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 20px 24px;
    overflow-y: auto;
    overflow-x: hidden;
}
@media (max-width: 900px) {
    .app-layout { flex-direction: column; }
    #editor-column, #display-column { flex: 1 1 auto; max-width: 100%; padding: 16px; }
}

.segmented { display: flex; flex-wrap: wrap; gap: 6px; }
.segmented button {
    flex: 1 1 auto;
    min-width: 54px;
    padding: 9px 10px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--surface);
    font: inherit;
    font-size: 17px;
    white-space: nowrap;
    cursor: pointer;
    color: var(--text);
}
.segmented button.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.segmented button:focus-visible, .base-picker button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--primary);
}

#step-nav { text-align: center; max-width: 720px; }
#step-position { font-weight: 700; font-size: 14px; }
#step-description { margin: 4px 0 0 0; min-height: 20px; }

#step-controls { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; }
#empty-hint { text-align: center; }
#error-msg { font-size: 12.5px; }

/* ---------- Calculation board (shared cell language for +, −, ×) ---------- */

#calc-stage { width: 100%; max-width: 860px; min-width: 0; display: flex; align-items: center; justify-content: center; overflow-x: auto; padding: 8px 4px; }
#calc-board { display: inline-flex; flex-direction: column; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

/* ---------- Base pickers ---------- */

.base-group { margin-top: 8px; }
.base-group label { display: block; font-size: 12.5px; margin-bottom: 6px; }
.base-picker { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.base-picker button {
    padding: 6px 12px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--surface);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
    color: var(--text);
}
.base-picker button.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.base-picker input[type="number"] {
    width: 56px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    padding: 5px 8px;
    font: inherit;
    font-size: 13px;
    background: var(--surface);
    color: var(--text);
}

/* ---------- Multi-stage layout (base conversions + the operation) ---------- */

#calc-board-wrap { display: flex; flex-direction: column; gap: 18px; align-items: center; width: 100%; }
.stage-summary {
    font-size: 13.5px;
    color: var(--success);
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    display: flex;
    align-items: center;
    gap: 6px;
}
.stage-summary::before { content: "✓"; }
.stage-title { font-size: 12.5px; color: var(--muted); margin-bottom: 6px; text-align: center; }
.conv-lines { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 16px; display: flex; flex-direction: column; gap: 4px; align-items: center; }
.conv-line { padding: 2px 8px; border-radius: 6px; }
.conv-line.active { background: color-mix(in srgb, var(--primary) 14%, transparent); font-weight: 700; }
.conv-final { font-weight: 700; color: var(--success); margin-top: 4px; }

#calc-board { --cell-w: 38px; }

.calc-row { display: flex; align-items: center; }
.calc-row.sum-rule { border-top: 2px solid var(--text); margin-top: 2px; padding-top: 2px; }

.cell {
    width: 38px;
    height: 44px;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 23px;
    color: var(--text);
    border-radius: 6px;
}
.cell.blank { visibility: hidden; }
.cell.sign {
    width: 26px;
    font-size: 23px;
    color: var(--muted);
    justify-content: flex-start;
}
.cell.muted-ghost { color: var(--border-strong); }
.cell.active { background: color-mix(in srgb, var(--primary) 14%, transparent); font-weight: 700; }
.cell.result { font-weight: 700; }
.cell.result.active { color: var(--primary); }
.cell.negative-sign { color: var(--danger); font-weight: 700; }

.carry-row .cell, .borrow-row .cell {
    height: 22px;
    font-size: 13px;
    color: var(--primary-dark);
    font-weight: 600;
}
.borrow-row .cell { color: var(--danger); }
.carry-row .cell.active, .borrow-row .cell.active { background: color-mix(in srgb, var(--primary) 18%, transparent); border-radius: 6px 6px 0 0; }

.op-label-row .cell {
    height: 30px;
    font-size: 12.5px;
    color: var(--muted);
    justify-content: flex-start;
}

/* ---------- Division board ---------- */

#calc-board.is-division { align-items: flex-start; }
.div-top { display: flex; align-items: flex-start; }
.div-dividend { display: flex; }
.div-divider { align-self: stretch; width: 0; border-left: 2px solid var(--text); margin: 0 10px; }
.div-right { display: flex; flex-direction: column; min-width: 90px; }
.div-divisor-row { display: flex; }
.div-quotient-rule { border-top: 2px solid var(--text); margin-top: 6px; }
.div-quotient-row { display: flex; margin-top: 6px; min-height: 44px; }
.div-cascade { display: flex; flex-direction: column; }
.div-block { display: flex; flex-direction: column; transition: margin-left 0.2s; }
.div-block-rule { border-top: 2px solid var(--text); margin-top: 2px; }
.div-block.active .cell { background: color-mix(in srgb, var(--primary) 10%, transparent); }
/* Must match .cell.sign's width exactly (here and in the mobile media query
   below) — this sits in the same leading column as the sign cells on the
   window/remainder rows of the same block, and as the placeholder cell on
   the dividend row above, so every row's digit columns line up. */
.div-block .minus-cell { width: 26px; color: var(--muted); font-size: 20px; display: flex; align-items: center; justify-content: center; }
.div-remainder-final { font-weight: 700; color: var(--success); }

@media (max-width: 480px) {
    #calc-board { --cell-w: 28px; }
    .cell { width: 28px; height: 36px; font-size: 18px; }
    .cell.sign, .div-block .minus-cell { width: 20px; font-size: 18px; }
    .carry-row .cell, .borrow-row .cell { height: 18px; font-size: 11px; }
}
