@font-face {
    font-family: "Grundschrift";
    src: url("Grundschrift-Regular.otf") format("opentype");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

:root {
    --bg: rgb(232, 238, 243);
    --panel: #ffffff;
    --ink: #1f252b;
    --muted: #6f7880;
    --hairline: rgba(21, 31, 40, 0.14);
    --nav: rgb(86, 110, 141);
    --z: rgb(63, 139, 129);
    --h: rgb(26, 71, 102);
    --t: rgb(153, 39, 39);
    --zt: rgb(242, 128, 91);
    --zero: #333333;
    --shadow: 0 10px 28px rgba(31, 37, 43, 0.08);
    --radius: 8px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --sw-einer: var(--zt);
    --sw-zehner: var(--t);
    --sw-hunderter: var(--h);
    --sw-tausender: var(--z);
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    overflow-x: hidden;
}

button,
select,
input {
    font: inherit;
}

button {
    -webkit-tap-highlight-color: transparent;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
}

.app-shell {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.topbar {
    height: 54px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 6px max(14px, env(safe-area-inset-left)) 6px max(14px, env(safe-area-inset-right));
    background: var(--panel);
    border-bottom: 1px solid var(--hairline);
}

.topbar h1 {
    margin: 0;
    font-size: clamp(18px, 2.2vw, 23px);
    font-weight: 650;
    text-align: left;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

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

.back-button,
.icon-button {
    width: 38px;
    height: 38px;
    display: inline-grid;
    place-items: center;
    border: 0;
    background: transparent;
    color: var(--nav);
    border-radius: 8px;
    cursor: pointer;
}

.back-button svg,
.icon-button svg,
.precision-menu svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.3;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon-button:hover,
.back-button:hover {
    background: rgba(86, 110, 141, 0.09);
}

.precision-menu {
    height: 36px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0 8px 0 10px;
    color: var(--ink);
    background: rgba(255, 255, 255, 0.9);
    border-radius: 8px;
    position: relative;
}

.precision-menu select {
    appearance: none;
    border: 0;
    background: transparent;
    color: inherit;
    min-width: 132px;
    outline: 0;
    cursor: pointer;
}

.precision-menu svg {
    width: 18px;
    height: 18px;
}

.workspace {
    --stage-height: clamp(220px, 45dvh, 560px);
    flex: 1;
    display: grid;
    grid-template-rows: var(--stage-height) auto auto auto;
    gap: clamp(8px, 1.4vw, 14px);
    padding: clamp(10px, 2vw, 20px);
    padding-bottom: calc(clamp(12px, 2vw, 22px) + var(--safe-bottom));
    align-content: start;
}

.dezimat-stage,
.number-line-panel,
.place-value-panel,
.decimal-panel,
.fraction-panel {
    background: var(--panel);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.dezimat-stage {
    position: relative;
    height: var(--stage-height);
    min-height: 0;
    overflow: hidden;
}

#dezimatCanvas,
#numberLineCanvas {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

.stage-hint {
    position: absolute;
    left: 50%;
    bottom: 10px;
    transform: translateX(-50%);
    max-width: min(90%, 640px);
    padding: 7px 12px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.88);
    color: var(--muted);
    border: 1px solid var(--hairline);
    font-size: clamp(12px, 1.9vw, 14px);
    text-align: center;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.stage-hint.is-dismissed {
    opacity: 0;
    pointer-events: none;
}

.number-line-panel {
    height: clamp(74px, 12dvh, 104px);
    overflow: hidden;
}

.lower-area {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(280px, 0.85fr);
    gap: clamp(8px, 1.4vw, 14px);
}

.place-value-panel,
.decimal-panel,
.fraction-panel {
    min-width: 0;
    padding: clamp(10px, 1.8vw, 16px);
    overflow-x: auto;
}

.decimal-panel {
    display: grid;
    align-content: start;
}

.section-title {
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 14px;
}

.place-value-table {
    --place-cell-size: clamp(72px, 8.8vw, 92px);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    width: max-content;
    max-width: none;
    margin: 0 auto;
    overflow: visible;
}

.pv-header,
.pv-row,
.pv-buttons-row {
    display: flex;
    justify-content: center;
    gap: 0;
}

.pv-header {
    margin-bottom: 4px;
}

.pv-label {
    width: var(--place-cell-size);
    text-align: center;
    color: var(--muted);
    font-family: "Grundschrift", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: calc(var(--place-cell-size) * 0.43);
    font-style: italic;
    font-weight: 600;
    line-height: 1.1;
}

.pv-label[data-label="E"] {
    color: var(--zero);
    font-style: normal;
}

.pv-label[data-label="z"] {
    color: var(--z);
}

.pv-label[data-label="h"] {
    color: var(--h);
}

.pv-label[data-label="t"] {
    color: var(--t);
}

.pv-label[data-label="zt"] {
    color: var(--zt);
}

.pv-label.hidden,
.pv-cell.hidden,
.pv-button-cell.hidden {
    display: none;
}

.pv-cell {
    width: var(--place-cell-size);
    height: var(--place-cell-size);
    position: relative;
    overflow: hidden;
    border: 2px solid #111;
    margin-left: -2px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
}

.pv-cell:first-child {
    margin-left: 0;
}

.pv-scroll {
    position: relative;
    width: 100%;
    height: 100%;
    will-change: transform;
}

.pv-scroll.animating {
    transition: transform 0.18s ease-out;
}

.pv-digit {
    width: 100%;
    height: var(--place-cell-size);
    display: flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    font-family: "Grundschrift", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: calc(var(--place-cell-size) * 0.72);
    line-height: 1;
}

/* Grundschrift sitzt in der Line-Box etwas zu weit oben — visuell mittig korrigieren */
.pv-digit > .pv-digit-text,
.pv-digit-text {
    display: inline-block;
    transform: translateY(0.08em);
}

.pv-digit.over-nine {
    color: #c00;
}

.pv-digit.is-long {
    font-size: calc(var(--place-cell-size) * 0.42);
}

.pv-cell.has-comma::after {
    content: ",";
    position: absolute;
    right: calc(var(--place-cell-size) * 0.08);
    bottom: calc(var(--place-cell-size) * 0.22);
    z-index: 3;
    color: #111;
    font-size: calc(var(--place-cell-size) * 0.62);
    line-height: 0.65;
    pointer-events: none;
}

.pv-buttons-row {
    margin-top: 6px;
}

.pv-button-cell {
    width: var(--place-cell-size);
    display: flex;
    justify-content: space-between;
    gap: 4px;
    padding: 0 5px;
}

.pv-arrow {
    width: calc(var(--place-cell-size) * 0.42);
    height: calc(var(--place-cell-size) * 0.36);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 0;
    padding: 0;
    background: transparent;
    color: transparent;
    cursor: pointer;
}

.pv-arrow.active.left {
    color: rgb(184, 53, 49);
}

.pv-arrow.active.right {
    color: #31404d;
}

.pv-arrow:disabled,
.pv-arrow:not(.active) {
    pointer-events: none;
}

.pv-arrow svg {
    width: 92%;
    height: 92%;
}

.place-table-row {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0;
}

.place-slot.hidden {
    display: none;
}

.place-label,
.decimal-label {
    width: min(100%, 78px);
    aspect-ratio: 1.45 / 1;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 0;
    background: transparent;
    font-size: clamp(18px, 3vw, 32px);
    font-weight: 600;
}

.place-value-table .place-label {
    width: var(--place-cell-size);
    height: 40px;
    aspect-ratio: auto;
    font-style: italic;
}

.place-value-table .place-label[data-place="1"] {
    font-style: normal;
}

.place-label[data-place="1"],
.decimal-label[data-place="1"] {
    color: var(--zero);
}

.place-label[data-place="2"],
.decimal-label[data-place="2"] {
    color: var(--z);
}

.place-label[data-place="3"],
.decimal-label[data-place="3"] {
    color: var(--h);
}

.place-label[data-place="4"],
.decimal-label[data-place="4"] {
    color: var(--t);
}

.place-label[data-place="5"],
.decimal-label[data-place="5"] {
    color: var(--zt);
}

.transfer-row {
    width: var(--place-cell-size);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 4px;
    padding: 6px 8px 0;
}

.transfer-row button {
    width: calc(var(--place-cell-size) * 0.38);
    height: calc(var(--place-cell-size) * 0.34);
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 0;
    color: rgb(184, 53, 49);
    background: transparent;
    cursor: pointer;
    font-size: clamp(26px, 4vw, 34px);
    line-height: 1;
}

.transfer-row button:last-child {
    background: transparent;
    color: #31404d;
}

.transfer-row button:disabled {
    opacity: 0.16;
    cursor: default;
}

.place-value-wheel-host {
    width: var(--place-cell-size);
    height: var(--place-cell-size);
    display: grid;
    justify-items: center;
    position: relative;
    border: 2px solid #111;
    margin-left: -2px;
    padding: 0;
    background: #fff;
    overflow: hidden;
}

.place-value-wheel-host:first-child {
    margin-left: 0;
}

.place-value-wheel-host.has-comma::after {
    content: ",";
    position: absolute;
    right: 8px;
    bottom: 5px;
    z-index: 5;
    color: #111;
    font-size: calc(var(--place-cell-size) * 0.62);
    line-height: 0.7;
    pointer-events: none;
}

.count-wheel {
    position: relative;
    width: min(100%, 90px);
    height: clamp(58px, 8vw, 82px);
    overflow: hidden;
    border: 2px solid #111;
    border-radius: 6px;
    background: #fff;
    touch-action: none;
    user-select: none;
}

.count-wheel-table {
    width: 100%;
    height: 100%;
    border: 0;
    border-radius: 0;
    background: transparent;
}

.count-wheel-table::before,
.count-wheel-table::after {
    display: none;
}

.count-wheel::before,
.count-wheel::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: 33%;
    pointer-events: none;
    z-index: 2;
}

.count-wheel::before {
    top: 0;
    background: linear-gradient(#fff, rgba(255, 255, 255, 0));
}

.count-wheel::after {
    bottom: 0;
    background: linear-gradient(rgba(255, 255, 255, 0), #fff);
}

.wheel-track {
    transition: transform 0.16s ease-out;
}

.wheel-value {
    height: clamp(42px, 6vw, 58px);
    display: grid;
    place-items: center;
    color: currentColor;
    font-size: clamp(25px, 4.2vw, 44px);
    line-height: 1;
}

.count-wheel-table .wheel-value {
    height: 52px;
    font-size: clamp(28px, 4.3vw, 42px);
}

.wheel-value.muted {
    opacity: 0.25;
}

.count-wheel-table .wheel-value.muted {
    opacity: 0;
}

.count-wheel-table .wheel-value.is-long {
    font-size: clamp(18px, 2.9vw, 28px);
}

.place-column[data-index="1"],
.decimal-digit[data-index="1"] {
    color: var(--z);
}

.place-column[data-index="2"],
.decimal-digit[data-index="2"] {
    color: var(--h);
}

.place-column[data-index="3"],
.decimal-digit[data-index="3"] {
    color: var(--t);
}

.place-column[data-index="4"],
.decimal-digit[data-index="4"] {
    color: var(--zt);
}

.decimal-display {
    display: flex;
    justify-content: center;
    align-items: end;
    gap: 0;
    min-height: clamp(88px, 15vw, 128px);
}

.decimal-digit {
    display: grid;
    justify-items: center;
    gap: 4px;
    flex: 0 0 clamp(42px, 6.5vw, 62px);
}

.decimal-box-row {
    display: flex;
    align-items: stretch;
    justify-content: center;
    gap: 0;
}

.decimal-comma {
    align-self: center;
    font-size: clamp(36px, 6vw, 56px);
    line-height: 1;
    margin: 0 -2px 8px -1px;
}

.picker-row {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 0;
    margin-top: clamp(14px, 4vw, 30px);
    color: #111;
}

.decimal-zero-picker .sw-label {
    width: 50px;
}

.decimal-zero-picker .sw-column {
    width: 50px;
    height: 60px;
}

.decimal-zero-picker .sw-digit {
    height: 60px;
    font-size: 50px;
    color: #111;
    cursor: default;
}

.decimal-comma-inline {
    width: 16px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    align-self: flex-start;
    margin-top: calc(0.9rem + 4px);
    color: #111;
    font-family: "GrundschriftRegular", "Grundschrift", -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 50px;
    line-height: 1;
}

.picker-row .stellenwert-picker {
    transform: none;
    transform-origin: center;
}

.fraction-panel {
    min-height: clamp(84px, 13vw, 120px);
}

.fraction-sequence {
    min-height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: clamp(10px, 3vw, 24px);
    overflow-x: auto;
    padding-bottom: 4px;
}

.fraction {
    min-width: clamp(48px, 8vw, 74px);
    display: grid;
    grid-template-rows: 1fr 2px 1fr;
    justify-items: center;
    align-items: center;
    font-size: clamp(20px, 4vw, 34px);
}

.fraction.zero {
    opacity: 0.3;
}

.fraction-line {
    width: 100%;
    height: 2px;
    background: #111;
}

.plus {
    font-size: clamp(20px, 4vw, 32px);
}

.settings-dialog {
    width: min(92vw, 360px);
    border: 0;
    border-radius: 12px;
    padding: 0;
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.25);
}

.settings-dialog::backdrop {
    background: rgba(20, 28, 34, 0.24);
}

.settings-dialog form {
    padding: 12px 16px 18px;
}

.dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--hairline);
    padding-bottom: 8px;
}

.dialog-header h2 {
    margin: 0;
    font-size: 19px;
}

.settings-group {
    display: grid;
    gap: 10px;
    margin-top: 24px;
}

.settings-group:first-of-type {
    margin-top: 14px;
}

.settings-group-label {
    color: var(--muted);
    font-size: 13px;
    margin-bottom: -2px;
}

.settings-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 16px;
}

.settings-group input {
    width: 18px;
    height: 18px;
    accent-color: var(--nav);
}

.is-hidden {
    display: none !important;
}

@media (max-width: 900px) {
    .topbar {
        gap: 6px;
        padding-left: 8px;
        padding-right: 8px;
    }

    .topbar h1 {
        font-size: 16px;
    }

    .back-button,
    .icon-button {
        width: 32px;
        height: 32px;
    }

    .topbar-actions {
        gap: 3px;
        min-width: 0;
    }

    .precision-menu {
        height: 32px;
        max-width: 118px;
        padding-left: 6px;
        padding-right: 4px;
        gap: 2px;
    }

    .precision-menu select {
        min-width: 0;
        width: 94px;
        font-size: 13px;
    }

    .precision-menu svg {
        width: 15px;
        height: 15px;
        flex: 0 0 auto;
    }

    .precision-menu::before {
        content: attr(data-short-label);
        min-width: 24px;
        color: var(--ink);
        text-align: center;
        pointer-events: none;
    }

    .precision-menu select {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        opacity: 0;
    }

    .workspace {
        grid-template-rows: var(--stage-height) auto auto auto;
        padding: 8px;
    }

    .lower-area {
        grid-template-columns: 1fr;
    }

    .place-value-table {
        --place-cell-size: 68px;
        min-width: max-content;
        justify-content: center;
        gap: 0;
    }

    .place-label,
    .decimal-label {
        width: min(100%, 72px);
    }

    .transfer-row {
        width: var(--place-cell-size);
    }

    .transfer-row button {
        width: calc(var(--place-cell-size) * 0.38);
    }

    .count-wheel {
        width: 100%;
    }

    .count-wheel-table {
        width: 100%;
    }

    .decimal-digit {
        flex-basis: 50px;
    }

    .section-title {
        margin-bottom: 5px;
    }

    .stage-hint {
        display: none;
    }
}

@media (orientation: landscape) and (max-height: 620px) {
    .workspace {
        grid-template-columns: minmax(390px, 1.25fr) minmax(340px, 0.75fr);
        grid-template-rows: 1fr auto auto;
        align-items: stretch;
    }

    .dezimat-stage {
        grid-row: 1 / span 3;
        min-height: 0;
    }

    .number-line-panel {
        height: 76px;
    }

    .lower-area {
        grid-template-columns: 1fr;
    }

    .fraction-panel {
        min-height: 78px;
    }
}
