/* Mates: cuaderno de cuadrícula y regletas de Cuisenaire. Todo cuelga del margen rojo. */
@font-face {
    font-family: 'Shantell Sans';
    src: url('fonts/shantell-sans.woff2') format('woff2');
    font-weight: 300 800;
    font-display: swap;
}
@font-face {
    font-family: 'Atkinson Next';
    src: url('fonts/atkinson-400.woff2') format('woff2');
    font-weight: 400;
    font-display: swap;
}
@font-face {
    font-family: 'Atkinson Next';
    src: url('fonts/atkinson-700.woff2') format('woff2');
    font-weight: 700;
    font-display: swap;
}

:root {
    /* Color de cada regleta, del 1 al 10 */
    --r1: #ffffff;
    --r2: #d6333b;
    --r3: #84d16c;
    --r4: #a93fb9;
    --r5: #f8d13a;
    --r6: #1c7c43;
    --r7: #20212b;
    --r8: #8a5527;
    --r9: #2360c8;
    --r10: #f28c26;

    --paper: #edf4fb;
    --line: #d3e2f1;
    --line-strong: #a9c3dc;
    --ink: #16233f;
    --ink-soft: #4b5a78;
    --margin-red: #e8636a;
    --white: #ffffff;

    --square: 28px;
    --margin: clamp(16px, 8vw, 96px);
    --display: 'Shantell Sans', 'Comic Sans MS', sans-serif;
    --text: 'Atkinson Next', Verdana, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    color: var(--ink);
    font: 400 1.125rem/1.5 var(--text);
    background-color: var(--paper);
    background-image:
        linear-gradient(var(--line) 1px, transparent 1px),
        linear-gradient(90deg, var(--line) 1px, transparent 1px);
    background-size: var(--square) var(--square);
}

/* El margen del cuaderno */
body::before {
    content: '';
    position: fixed;
    inset: 0 auto 0 calc(var(--margin) - 2px);
    width: 2px;
    background: var(--margin-red);
    pointer-events: none;
}

a { color: inherit; }
button { font: inherit; color: inherit; cursor: pointer; background: none; border: 0; }
:focus-visible { outline: 3px solid var(--r9); outline-offset: 2px; }

.sr {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
}

/* Cada clase rN pinta con el color de la regleta y un texto legible encima */
.r1 { background-color: var(--r1); color: var(--ink); }
.r2 { background-color: var(--r2); color: #fff; }
.r3 { background-color: var(--r3); color: var(--ink); }
.r4 { background-color: var(--r4); color: #fff; }
.r5 { background-color: var(--r5); color: var(--ink); }
.r6 { background-color: var(--r6); color: #fff; }
.r7 { background-color: var(--r7); color: #fff; }
.r8 { background-color: var(--r8); color: #fff; }
.r9 { background-color: var(--r9); color: #fff; }
.r10 { background-color: var(--r10); color: var(--ink); }

/* Cabecera y pie */
.topbar {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px 14px calc(var(--margin) + 16px);
}

.brand {
    display: inline-flex;
    align-items: flex-end;
    gap: 8px;
    font: 800 1.5rem/1 var(--display);
    text-decoration: none;
}

.brand-rods { display: inline-flex; align-items: flex-end; gap: 2px; }
.brand-rods i { display: block; width: 7px; border-radius: 2px; }
.brand-rods i:nth-child(1) { height: 10px; }
.brand-rods i:nth-child(2) { height: 16px; }
.brand-rods i:nth-child(3) { height: 22px; }

.topbar-actions { display: flex; gap: 8px; }

.chip-button {
    padding: 6px 12px;
    font-size: 0.95rem;
    font-weight: 700;
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 8px;
}

.foot {
    margin-top: auto;
    padding: 24px 16px 20px calc(var(--margin) + 16px);
    color: var(--ink-soft);
    font-size: 0.9rem;
}

.page {
    width: 100%;
    max-width: calc(var(--margin) + 780px);
    padding: 8px 16px 32px var(--margin);
}

.page > :not(.stairs):not(.practice):not(.result) { margin-left: 16px; }

/* Títulos: rotulados a mano */
.title {
    font: 800 clamp(2.4rem, 8vw, 4rem)/1.05 var(--display);
    letter-spacing: -0.01em;
}

.subtitle {
    margin-top: 32px;
    margin-bottom: 12px;
    font: 700 1.5rem/1.2 var(--display);
}

.lead { max-width: 34em; margin-top: 8px; color: var(--ink-soft); font-size: 1.2rem; }

.back {
    display: inline-block;
    margin-bottom: 12px;
    color: var(--ink-soft);
    font-weight: 700;
    text-decoration: none;
}

.back:hover { color: var(--ink); text-decoration: underline; }

/* Regletas */
.rod, .step, .rod-button {
    box-shadow: inset 0 2px 0 rgb(255 255 255 / 0.28), inset 0 -3px 0 rgb(0 0 0 / 0.16), 0 1px 2px rgb(22 35 63 / 0.35);
}

.rod {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: calc(var(--n) * var(--u));
    height: var(--u);
    border-radius: 4px;
    font: 700 0.85rem/1 var(--text);
    background-image: repeating-linear-gradient(90deg, transparent 0 calc(var(--u) - 1px), rgb(0 0 0 / 0.16) calc(var(--u) - 1px) var(--u));
}

.rod.r1 { outline: 1px solid var(--line-strong); }

.rod-ghost {
    color: var(--ink-soft);
    background: rgb(255 255 255 / 0.6);
    border: 2px dashed var(--line-strong);
    box-shadow: none;
}

.heading-rod { --u: 24px; display: flex; margin-bottom: 12px; }

/* Escalera de la bienvenida */
.stair-mini { --u: min(30px, calc((100vw - var(--margin) - 48px) / 10)); display: flex; flex-direction: column; gap: 3px; margin-top: 20px; }

/* La escalera */
.stairs {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 100%;
    margin-top: 24px;
    container-type: inline-size;
}

.step {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: calc(var(--n) * 10cqw);
    min-height: 58px;
    padding: 0 14px;
    border-radius: 0 7px 7px 0;
    text-decoration: none;
    background-image: repeating-linear-gradient(90deg, transparent 0 calc(10cqw - 1px), rgb(0 0 0 / 0.12) calc(10cqw - 1px) 10cqw);
    transition: translate 160ms ease;
    animation: slide-in 420ms cubic-bezier(0.2, 0.8, 0.2, 1) backwards;
}

.step:nth-child(2) { animation-delay: 45ms; }
.step:nth-child(3) { animation-delay: 90ms; }
.step:nth-child(4) { animation-delay: 135ms; }
.step:nth-child(5) { animation-delay: 180ms; }
.step:nth-child(6) { animation-delay: 225ms; }
.step:nth-child(7) { animation-delay: 270ms; }
.step:nth-child(8) { animation-delay: 315ms; }
.step:nth-child(9) { animation-delay: 360ms; }
.step:nth-child(10) { animation-delay: 405ms; }

@media (hover: hover) {
    .step:hover { translate: 10px 0; }
}

@keyframes slide-in {
    from { transform: translateX(-100%); opacity: 0; }
}

.step-name { font: 800 clamp(1.1rem, 3.6vw, 1.55rem)/1 var(--display); }
.step-count { font-size: 0.9rem; font-weight: 700; opacity: 0.9; white-space: nowrap; }

.stairs-tables .step { min-height: 48px; }

.extras {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
    gap: 12px;
    margin-top: 28px;
}

.extra, .exam, .level {
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 10px;
    text-decoration: none;
}

.extra { display: flex; flex-direction: column; padding: 12px 14px; }
.extra b, .exam b { font: 700 1.3rem/1.2 var(--display); }
.extra span, .exam span { color: var(--ink-soft); font-size: 1rem; }
.extra:hover, .exam:hover, .level:hover { box-shadow: 4px 4px 0 var(--ink); }

.exam { display: flex; flex-direction: column; margin-top: 20px; padding: 14px 16px; }

/* Perfiles */
.profiles { display: flex; flex-direction: column; gap: 8px; container-type: inline-size; }
.profile { display: flex; align-items: center; gap: 12px; }

.rod-button {
    width: min(100%, 320px);
    min-height: 56px;
    padding: 0 16px;
    border-radius: 7px;
    font: 800 1.4rem/1 var(--display);
    text-align: left;
}

.link { color: var(--ink-soft); font-size: 0.95rem; text-decoration: underline; }

.new-profile input[name='name'] {
    display: block;
    width: min(100%, 320px);
    padding: 12px 14px;
    font: 700 1.3rem var(--text);
    color: var(--ink);
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 8px;
}

.swatches { margin: 16px 0; border: 0; }
.swatches legend { margin-bottom: 6px; font-weight: 700; }
.swatch { position: relative; display: inline-block; margin: 0 6px 6px 0; cursor: pointer; }
.swatch input { position: absolute; opacity: 0; }
.swatch span:first-of-type { display: block; width: 44px; height: 44px; border-radius: 7px; border: 3px solid transparent; }
.swatch input:checked + span { border-color: var(--ink); box-shadow: 0 0 0 2px var(--white) inset; }
.swatch input:focus-visible + span { outline: 3px solid var(--r9); outline-offset: 2px; }

/* Botones */
.btn-main, .btn-soft {
    display: inline-block;
    min-height: 54px;
    padding: 12px 26px;
    font: 800 1.25rem/1.2 var(--display);
    text-align: center;
    text-decoration: none;
    border-radius: 10px;
    border: 2px solid var(--ink);
}

.btn-main { color: #fff; background: var(--ink); }
.btn-main:hover { background: var(--r9); border-color: var(--r9); }
.btn-soft { background: var(--white); }
.btn-soft:hover { box-shadow: 4px 4px 0 var(--ink); }

.actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 24px; }

/* Niveles */
.levels { display: flex; flex-direction: column; gap: 8px; }

.level {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 54px;
    padding: 10px 14px;
    font-weight: 700;
}

.mark { flex: none; padding: 2px 8px; color: #fff; font-size: 0.85rem; background: var(--r6); border-radius: 6px; }
.mark-soft { color: var(--ink-soft); background: var(--paper); }

/* La mesa */
.lab { padding: 16px; background: var(--white); border: 2px solid var(--ink); border-radius: 12px; }
.lab-controls { display: flex; flex-wrap: wrap; gap: 12px 20px; }

.stepper { display: grid; grid-template-columns: 44px 3.2ch 44px; align-items: center; gap: 4px; }
.stepper-label { grid-column: 1 / -1; color: var(--ink-soft); font-size: 0.95rem; font-weight: 700; }
.stepper output { font-size: 1.4rem; font-weight: 700; text-align: center; }

.stepper button {
    width: 44px;
    height: 44px;
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1;
    background: var(--paper);
    border: 2px solid var(--ink);
    border-radius: 8px;
}

.lab-text { margin: 16px 0 8px; font: 800 clamp(1.5rem, 5vw, 2rem)/1.2 var(--display); }

/* Dibujos */
.vis { container-type: inline-size; width: 100%; margin-top: 8px; }
.vis-caption { margin: 6px 0; font-size: 1.05rem; }
.vis-caption b { color: var(--r2); }
.vis-svg { display: block; width: min(100%, 280px); height: auto; }

.vis-rods { --u: min(calc(100cqw / var(--units)), 36px); display: flex; flex-direction: column; gap: 4px; }
.rod-row { display: flex; }

.groups { display: flex; flex-wrap: wrap; gap: 10px; }

.group {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 4px;
    max-width: 92px;
    min-width: 34px;
    min-height: 34px;
    padding: 8px;
    background: var(--paper);
    border-radius: 10px;
}

.group-off { opacity: 0.35; }
.group-rest { background: none; border: 2px dashed var(--line-strong); }

.dot {
    display: block;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    animation: pop 240ms ease backwards;
}

@keyframes pop {
    from { transform: scale(0); opacity: 0; }
}

.vis-pair { display: flex; flex-wrap: wrap; gap: 16px; }
.vis-pair figure { flex: 1 1 130px; max-width: 240px; }
.vis-pair figcaption { margin-bottom: 6px; }

/* Bloques de centenas, decenas y unidades */
.block-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 4px; min-height: 44px; }
.block-row-out { opacity: 0.8; }

.b-hundred, .b-ten, .b-one { display: inline-block; flex: none; }

.b-hundred {
    width: 60px;
    height: 60px;
    background-color: var(--r9);
    background-image:
        linear-gradient(rgb(255 255 255 / 0.4) 1px, transparent 1px),
        linear-gradient(90deg, rgb(255 255 255 / 0.4) 1px, transparent 1px);
    background-size: 6px 6px;
    border-radius: 2px;
}

.b-ten {
    width: 6px;
    height: 60px;
    background-color: var(--r10);
    background-image: linear-gradient(rgb(0 0 0 / 0.25) 1px, transparent 1px);
    background-size: 6px 6px;
    border-radius: 1px;
}

.b-ones { display: inline-grid; grid-template-columns: repeat(5, 8px); gap: 2px; align-self: flex-end; }
.b-one { width: 8px; height: 8px; background: var(--r2); border-radius: 1px; }

.vis-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; color: var(--ink-soft); font-size: 0.9rem; }
.vis-legend .b-hundred { width: 16px; height: 16px; background-size: 1.6px 1.6px; }
.vis-legend .b-ten { width: 3px; height: 16px; background-size: 3px 3px; margin-left: 8px; }
.vis-legend .b-one { margin-left: 8px; }

/* Rectángulo de multiplicar */
.area { display: grid; gap: 3px; width: min(100%, 460px); min-height: 150px; }
.area-head { display: grid; place-items: center; padding: 2px 8px; font-weight: 700; color: var(--ink-soft); }

.area-cell {
    display: grid;
    place-items: center;
    min-width: 54px;
    padding: 6px;
    font: 800 1.25rem/1 var(--display);
    border-radius: 4px;
}

/* Barras de quebrados */
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: flex; align-items: center; gap: 12px; }
.bar-stack { flex: 1; display: flex; flex-direction: column; gap: 4px; min-width: 0; }

.bar {
    display: flex;
    gap: 2px;
    height: 34px;
    padding: 2px;
    background: var(--ink);
    border-radius: 5px;
}

.bar i { flex: 1; min-width: 0; border-radius: 2px; }
.bar .bar-off { background: var(--white); }

/* Potencias con la base repetida */
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.chip-group { display: inline-flex; flex-wrap: wrap; gap: 3px; padding: 5px; background: var(--paper); border-radius: 8px; }
.chip { display: grid; place-items: center; min-width: 32px; height: 32px; font-weight: 700; border-radius: 5px; }
.chip.r1 { border: 1px solid var(--line-strong); }

/* Práctica */
.practice, .result { padding-left: 16px; }

.practice-top { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.practice-top .back { margin: 0; }
.practice-title { font: 700 1.2rem/1.2 var(--display); text-align: right; }

.cells { display: flex; gap: 3px; margin: 12px 0; }

.cells i {
    flex: 1;
    max-width: 44px;
    height: 16px;
    background: var(--white);
    border: 2px solid var(--line-strong);
    border-radius: 3px;
    transition: background-color 200ms ease;
}

.cells .now { border-color: var(--ink); }
.cells .first { background: var(--accent); border-color: var(--ink); }
.cells .second { background: var(--accent); border-color: var(--ink); opacity: 0.55; }
.cells .shown { background: var(--line); border-color: var(--line-strong); }
.cells-big i { height: 24px; }

.sheet {
    padding: 22px 18px;
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 12px;
}

.prompt { font-size: 1.2rem; font-weight: 700; }
.prompt:empty, .figure:empty, .note:empty, .help:empty { display: none; }
.figure { margin-bottom: 8px; }

.expr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.28em;
    margin: 10px 0;
    font-size: clamp(2.4rem, 11vw, 4.2rem);
    font-weight: 700;
    line-height: 1.1;
    font-variant-numeric: lining-nums;
}

.x-op { color: var(--ink-soft); font-weight: 400; }
.x-word { font-size: 0.55em; color: var(--ink-soft); }

.x-frac {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0.06em;
    font-size: 0.72em;
    line-height: 1;
}

.x-frac > span:first-child { padding: 0 0.15em 0.08em; border-bottom: 0.06em solid currentcolor; align-self: stretch; text-align: center; }
.x-frac > span:last-child { padding-top: 0.04em; }
.x-small { font-size: 1.05rem; font-weight: 700; }
.x-pow sup { font-size: 0.55em; vertical-align: 0.75em; line-height: 0; }

.x-sqrt { display: inline-flex; align-items: stretch; }
.x-radical { padding-right: 0.02em; font-weight: 400; }
.x-under { padding: 0 0.1em; border-top: 0.06em solid currentcolor; margin-top: 0.1em; }

.slot {
    display: inline-grid;
    place-items: center;
    min-width: 1.5em;
    min-height: 1.15em;
    padding: 0 0.15em;
    font-weight: 700;
    background: var(--paper);
    border: 3px dashed var(--line-strong);
    border-radius: 8px;
}

.slot-active { background: #fff8d9; border: 3px solid var(--ink); }
.slot-static { min-width: 1.3em; min-height: 1.3em; color: var(--ink-soft); border-width: 2px; }
.slot-choice { min-width: 1.2em; color: var(--ink-soft); }
.x-pow sup .slot { min-width: 1.4em; border-width: 2px; }

.note { margin-top: 10px; font: 700 1.35rem/1.25 var(--display); text-align: center; }
.note-good { color: var(--r6); animation: pop 240ms ease; }
.note-try { color: var(--r9); }

.help { margin-top: 12px; padding-top: 12px; border-top: 2px dashed var(--line-strong); }
.steps { margin-top: 10px; list-style: none; font-size: 1.15rem; }
.steps li { padding: 2px 0; }

.controls { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 14px; }
.controls .btn-main { width: min(100%, 318px); }

.btn-drawing {
    padding: 8px 16px;
    font: 700 1.1rem/1.2 var(--display);
    color: var(--r9);
    background: var(--white);
    border: 2px solid var(--r9);
    border-radius: 8px;
}

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; width: min(100%, 318px); }

.key {
    min-height: 60px;
    font-size: 1.7rem;
    font-weight: 700;
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 10px;
    touch-action: manipulation;
}

.key:active { background: var(--r5); }
.key-soft { color: var(--ink-soft); background: var(--paper); }
.choices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: min(100%, 318px); }

/* Tablas */
.table-visual {
    min-height: 150px;
    padding: 6px 14px 14px;
    background: var(--white);
    border: 2px solid var(--ink);
    border-radius: 12px;
    margin-top: 16px;
}

.table-rows { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin-top: 12px; }

.table-row {
    min-height: 50px;
    padding: 8px;
    font-size: 1.25rem;
    background: var(--white);
    border: 2px solid var(--line-strong);
    border-radius: 8px;
}

.table-row b { color: var(--r2); }
.table-row:hover { border-color: var(--ink); }
.table-row.lit { background: var(--r5); border-color: var(--ink); }
.table-row.lit b { color: var(--ink); }

/* Resultado */
.review-list { display: flex; flex-wrap: wrap; gap: 8px; }

.review-item .x-frac { font-size: 0.85em; }

.review-item {
    display: inline-flex;
    align-items: center;
    gap: 0.3em;
    padding: 4px 10px;
    font-size: 1.15rem;
    font-weight: 700;
    background: var(--white);
    border: 2px solid var(--line-strong);
    border-radius: 8px;
}

.falling {
    position: fixed;
    top: -24px;
    z-index: 10;
    width: 16px;
    height: 16px;
    border-radius: 3px;
    pointer-events: none;
    animation: fall linear forwards;
}

@keyframes fall {
    to { transform: translateY(calc(100dvh + 40px)) rotate(420deg); }
}

@media (min-width: 1000px) {
    .page { max-width: calc(var(--margin) + 1060px); }
    .page > :not(.practice):not(.result) { max-width: 780px; }
    .practice { display: grid; grid-template-columns: minmax(0, 1fr) 318px; gap: 0 24px; align-items: start; }
    .practice-top, .cells { grid-column: 1 / -1; }
    .controls { margin-top: 0; }
}

@media (max-width: 480px) {
    .step { padding: 0 8px; }
    .step-name { font-size: 1.05rem; }
    .step-count { font-size: 0.78rem; }
    .table-rows { grid-template-columns: 1fr; }
    .btn-main, .btn-soft { flex: 1 1 auto; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
