/* Calculator (/calculator): what is its own - the keypad, the display and the history tape. The stage, the mode
   switch, the buttons, the theme and the shared colours come from the app kit (css/app-kit, docs/app-kit.md).
   Loaded only on this app's page (StylePaths). */

/* The device's own colours: the keys, the function keys and the shadow. The body, the display, the text, the lines and
   the accent (the "=" key, the operators) are the kit's tokens. Declared in the kit's theme scopes (tokens.css), so the
   stage follows the app's Light / Dark select and the history in the panel follows the site theme. */
.calculator-landing-app {
    --calc-body: var(--ak-bg);
    --calc-display: var(--ak-panel);
    --calc-key: #f3f5f9;
    --calc-key-hover: #e6eaf2;
    --calc-fn: #e9edf5;
    --calc-op: color-mix(in srgb, var(--ak-accent) 15%, var(--ak-bg));
    --calc-op-text: color-mix(in srgb, var(--ak-accent) 80%, var(--ak-text));
    --calc-shadow: 0 10px 30px rgba(25, 42, 69, 0.12);
}

.dark .calculator-landing-app > .filter-landing-controls,
.calculator-landing-app[data-theme="dark"] > .filter-landing-stage {
    --calc-body: var(--ak-panel);
    --calc-display: var(--ak-bg);
    --calc-key: #1e293d;
    --calc-key-hover: #28364f;
    --calc-fn: #19233a;
    --calc-op: color-mix(in srgb, var(--ak-accent) 24%, var(--ak-panel));
    --calc-op-text: color-mix(in srgb, var(--ak-accent) 70%, var(--ak-text));
    --calc-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

/* The stage fits the viewport: the keypad rows share the height the stage has left, so the whole calculator is usable
   without scrolling the page, on a large screen and on a phone. */
.calculator-landing-app > .filter-landing-stage.ak-stage {
    height: min(calc(100dvh - var(--seo-chrome-height, 4.75rem) - 24px), 860px);
    min-height: 360px;
    aspect-ratio: auto;
    padding: clamp(12px, 3cqi, 32px);
    overflow: hidden;
    box-shadow: none;
    touch-action: manipulation;
}

body.filter-app-mode .calculator-landing-app > .filter-landing-stage.ak-stage {
    height: 100%;
    min-height: 0;
}

.calculator-landing-app .calc {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    max-width: 440px;
    height: 100%;
    max-height: 780px;
    min-height: 0;
    margin-block: auto;
    padding: 14px;
    border: 1px solid var(--ak-border);
    border-radius: 22px;
    background: var(--calc-body);
    box-shadow: var(--calc-shadow);
    color: var(--ak-text);
    font-variant-numeric: tabular-nums;
}

.calculator-landing-app .calc[data-mode="scientific"] {
    max-width: 920px;
    max-height: 700px;
}

/* Basic / Scientific: the kit's segmented control, the full width of the device. */
.calculator-landing-app .calc-modes {
    flex: 0 0 auto;
}

.calculator-landing-app .calc-modes .ak-seg-btn {
    min-height: 2.25rem;
}

/* --- Display ------------------------------------------------------------------------------------------------------- */

.calculator-landing-app .calc-display {
    position: relative;
    display: flex;
    flex: 0 0 auto;
    flex-direction: column;
    align-items: flex-end;
    justify-content: flex-end;
    gap: 4px;
    min-height: clamp(76px, 15vh, 118px);
    padding: clamp(20px, 3vh, 26px) 16px 12px;
    border-radius: 16px;
    background: var(--calc-display);
    overflow: hidden;
}

.calculator-landing-app .calc-indicators {
    position: absolute;
    top: 8px;
    left: 14px;
    display: flex;
    gap: 10px;
    color: var(--ak-muted);
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
}

.calculator-landing-app .calc[data-mode="basic"] .calc-ind-angle {
    visibility: hidden;
}

.calculator-landing-app .calc-ind-mem {
    padding: 0 6px;
    border-radius: var(--ak-radius-pill);
    background: var(--calc-op);
    color: var(--calc-op-text);
}

.calculator-landing-app .calc-expr {
    width: 100%;
    min-height: 1.4em;
    overflow-x: auto;
    color: var(--ak-muted);
    font-size: 1.05rem;
    text-align: right;
    white-space: nowrap;
    scrollbar-width: none;
}

.calculator-landing-app .calc-result {
    width: 100%;
    overflow: hidden;
    color: var(--ak-text);
    font-size: clamp(2.2rem, 8cqi, 3rem);
    font-weight: 600;
    line-height: 1.1;
    text-align: right;
    white-space: nowrap;
    opacity: 0.7;
}

.calculator-landing-app .calc-result.is-final {
    opacity: 1;
}

.calculator-landing-app .calc-result.is-error {
    color: var(--ak-danger);
    opacity: 1;
}

/* --- Keypad -------------------------------------------------------------------------------------------------------- */

.calculator-landing-app .calc-pads {
    display: grid;
    flex: 1 1 auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 10px;
    min-height: 0;
}

.calculator-landing-app .calc[data-mode="scientific"] .calc-pads {
    grid-template-rows: minmax(0, 4fr) minmax(0, 6fr);
}

.calculator-landing-app .calc-pad {
    display: grid;
    grid-auto-rows: minmax(0, 1fr);
    gap: 8px;
    min-height: 0;
}

.calculator-landing-app .calc-pad--basic {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.calculator-landing-app .calc-pad--sci {
    grid-template-columns: repeat(5, minmax(0, 1fr));
}

.calculator-landing-app .calc[data-mode="basic"] .calc-pad--sci {
    display: none;
}

@container (min-width: 760px) {
    .calculator-landing-app .calc[data-mode="scientific"] .calc-pads {
        grid-template-columns: minmax(0, 5fr) minmax(0, 4fr);
        grid-template-rows: minmax(0, 1fr);
        align-items: stretch;
    }

    .calculator-landing-app .calc[data-mode="scientific"] .calc-pad--sci {
        grid-auto-rows: 1fr;
    }
}

.calculator-landing-app .calc-key {
    min-width: 0;
    min-height: 30px;
    height: 100%;
    padding: 0 4px;
    border: 0;
    border-radius: 14px;
    background: var(--calc-key);
    color: var(--ak-text);
    font: inherit;
    font-size: clamp(1rem, 2.6vh, 1.3rem);
    line-height: 1;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 120ms ease, transform 80ms ease;
}

.calculator-landing-app .calc-key sup {
    font-size: 0.6em;
}

.calculator-landing-app .calc-key:hover {
    background: var(--calc-key-hover);
}

.calculator-landing-app .calc-key:active,
.calculator-landing-app .calc-key.is-pressed {
    transform: scale(0.96);
    background: var(--calc-key-hover);
}

.calculator-landing-app .calc-key:focus-visible {
    outline: 2px solid var(--ak-accent);
    outline-offset: 2px;
}

.calculator-landing-app .calc-key:disabled {
    opacity: 0.45;
    cursor: default;
}

.calculator-landing-app .calc-key--num {
    font-size: clamp(1.1rem, 3vh, 1.45rem);
    font-weight: 600;
}

.calculator-landing-app .calc-key--fn,
.calculator-landing-app .calc-key--mem {
    background: var(--calc-fn);
}

.calculator-landing-app .calc-key--fn,
.calculator-landing-app .calc-key--mem,
.calculator-landing-app .calc-pad--sci .calc-key {
    font-size: clamp(0.85rem, 2.2vh, 1.1rem);
}

.calculator-landing-app .calc-key--mem {
    font-weight: 600;
}

.calculator-landing-app .calc-key[aria-pressed="true"] {
    background: var(--calc-op);
    color: var(--calc-op-text);
}

.calculator-landing-app .calc-key--op {
    background: var(--calc-op);
    color: var(--calc-op-text);
    font-size: clamp(1.15rem, 3.1vh, 1.5rem);
}

.calculator-landing-app .calc-key--clear {
    background: var(--calc-fn);
    color: var(--ak-danger);
    font-weight: 700;
}

.calculator-landing-app .calc-key--eq,
.calculator-landing-app .calc-key--eq:hover {
    background: var(--ak-accent);
    color: var(--ak-on-accent);
    font-size: clamp(1.2rem, 3.2vh, 1.6rem);
}

.calculator-landing-app .calc-key--eq:hover {
    background: color-mix(in srgb, var(--ak-accent) 85%, var(--ak-text));
}

/* The operator that waits for its second number stays marked (inverted with an accent ring),
   as on a hand-held calculator, and stays distinct from the solid "=" key. */
.calculator-landing-app .calc-key.is-active,
.calculator-landing-app .calc-key.is-active:hover {
    background: var(--calc-body);
    color: var(--ak-accent);
    box-shadow: inset 0 0 0 2px var(--ak-accent);
    font-weight: 700;
}

/* --- History tape (control panel) ---------------------------------------------------------------------------------- */

.calculator-landing-app .calc-history {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.calculator-landing-app .calc-history-list {
    gap: 4px;
    max-height: 340px;
    overflow-y: auto;
}

.calculator-landing-app .calc-history-item {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    width: 100%;
    padding: 8px 10px;
    border: 1px solid transparent;
    border-radius: var(--ak-radius-sm);
    background: transparent;
    color: var(--ak-text);
    font: inherit;
    text-align: right;
    cursor: pointer;
}

.calculator-landing-app .calc-history-item:hover,
.calculator-landing-app .calc-history-item:focus-visible {
    border-color: color-mix(in srgb, var(--ak-accent) 35%, transparent);
    background: color-mix(in srgb, var(--ak-accent) 8%, transparent);
}

.calculator-landing-app .calc-history-item:focus-visible {
    outline: 2px solid var(--ak-accent);
    outline-offset: 1px;
}

.calculator-landing-app .calc-history-expr {
    max-width: 100%;
    overflow: hidden;
    opacity: 0.7;
    font-size: 0.85rem;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.calculator-landing-app .calc-history-result {
    color: var(--calc-op-text);
    font-size: 1.15rem;
    font-weight: 600;
    word-break: break-all;
}

/* --- Narrow screens ------------------------------------------------------------------------------------------------ */

@media (max-width: 960px) {
    /* !important: the shared mobile rule caps the stage at a square with a long :not() chain. */
    body.filter-app-mode .filter-landing-app.calculator-landing-app:not(.is-panel-collapsed) {
        grid-template-rows: minmax(0, 1fr) auto !important;
    }

    body.filter-app-mode .filter-landing-app.calculator-landing-app > .filter-landing-stage.ak-stage {
        height: 100% !important;
        max-height: none !important;
        place-items: stretch;
    }

    body.filter-app-mode .filter-landing-app.calculator-landing-app > .filter-landing-controls {
        max-height: min(18vh, 150px) !important;
    }
}

@media (max-width: 640px) {
    .calculator-landing-app > .filter-landing-stage.ak-stage {
        height: calc(100dvh - var(--seo-chrome-height, 4.75rem));
        min-height: 320px;
        padding: 0;
    }

    body.filter-app-mode .calculator-landing-app > .filter-landing-stage.ak-stage {
        height: 100%;
        min-height: 0;
    }

    .calculator-landing-app .calc,
    .calculator-landing-app .calc[data-mode="scientific"] {
        width: 100%;
        max-width: none;
        max-height: none;
        padding: 8px;
        gap: 8px;
        border: 0;
        border-radius: 0;
        box-shadow: none;
    }

    .calculator-landing-app .calc-pad,
    .calculator-landing-app .calc-pads {
        gap: 6px;
    }

    .calculator-landing-app .calc-key {
        min-height: 28px;
    }
}
