:root {
    --calc-colour: light-dark(#eee, #111);
    --button-colour: light-dark(#eee, #111);
    --button-border-colour: light-dark(#666, #666);
    --screen-colour: light-dark(#f4f4f4, #333);
    --screen-border-colour: light-dark(#000, #fff);
    --calc-border-colour: light-dark(#000, #ccc);
    --bg-colour: light-dark(#ddd, #000);
    --status-bar-boxed-colour: light-dark(#fff, #000);
    --status-bar-colour: light-dark(#000, #fff);
    --calc-shadow: light-dark(rgba(0, 0, 0, 0.25), rgba(255, 255, 255, 0.25));

    --text-colour: light-dark(#000, #fff);
    --shift-colour: goldenrod;
    --alpha-colour: red;
    --main-colour: cyan;
    --store-colour: lightpink;
    --recall-colour: lightgreen;

    --show-help: none;

    --scale: 97.5;

    --h: min(calc(var(--scale) * 1vh), calc(var(--scale) * 2vw));
    --h: min(calc(var(--scale) * 1svh), calc(var(--scale) * 2vw));
}

@font-face { font-family: 'Inter'; src: url('./assets/fonts/Inter18pt-Regular.woff2') format('woff2'); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: 'Inter'; src: url('./assets/fonts/Inter18pt-Bold.woff2') format('woff2'); font-weight: 700; font-style: normal; font-display: swap; }
@font-face { font-family: 'Inter'; src: url('./assets/fonts/Inter18pt-Italic.woff2') format('woff2'); font-weight: 400; font-style: italic; font-display: swap; }
@font-face { font-family: 'Inter'; src: url('./assets/fonts/Inter18pt-BoldItalic.woff2') format('woff2'); font-weight: 700; font-style: italic; font-display: swap; }

*, *::before, *::after {
    font: inherit;
    color: inherit;
    -webkit-tap-highlight-color: transparent;
}

*:focus:not(:focus-visible)

html[data-theme="light"] {
  color-scheme: light;
}

html[data-theme="dark"] {
  color-scheme: dark;
}

html:not([data-theme]) {
    color-scheme: light dark;
}

body {
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100svh;

    touch-action: pan-x pan-y;

    background-color: var(--bg-colour);

    font-family: "Inter", sans-serif;
    color: var(--text-colour);
    overflow: hidden;
}

.calculator {
    box-sizing: border-box;

    border: 1px solid var(--calc-border-colour);

    aspect-ratio: 1 / 2;

    height: var(--h);
    width: auto;

    padding: calc(var(--h) / 40);
    row-gap: calc(var(--h) / 40);
    border-radius: calc(var(--h) / 80);
    box-shadow: 0 0 calc(var(--h) / 80) var(--calc-shadow);

    background-color: var(--calc-colour);

    container-type: size;
    scrollbar-gutter: stable;

    user-select: none;

    display: grid;
    grid-template-rows: 0.2fr 2fr 7fr;
}

:fullscreen .calculator {
    height: min(
        calc(100svh),
        calc(100vw * 2)
    );
    padding: calc(var(--h) / 160);
    border: none;
    box-shadow: none;
}

:fullscreen body {
    background-color: var(--calc-colour);
}

button {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;

    background-color: var(--button-colour);
    border: calc(var(--h) * 0.001) solid var(--button-border-colour);
    border-radius: calc(var(--h) * 0.005);
}

button:hover {
    cursor: pointer;
}

#top-bezel {
    font-size: 2.5cqb;
    min-height: 0;
    font-style: italic;
    text-align: right;
}

#screen {
    display: flex;
    flex-direction: column;
    container-type: size;
    min-height: 0;
    width: 95%;
    padding: 1% 2.5% 2.5% 2.5%;
    border: 1px solid var(--screen-border-colour);
}

#display {
    display: flex;
    flex-direction: column;
    height: 100%;
    justify-content: space-between;
    align-items: flex-end;
    container-type: size;
    min-height: 0;
}

#overlay-display {
    position: absolute;
    width: 100cqw;
    height: 100cqb;
    font-size: 15cqb;
    color: red;
    
    text-align: center;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none; 
}

#input-display {
    align-self: flex-start;

    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;

    width: 100%;
    padding: 4px 0;
    box-sizing: border-box;

    font-size: 25cqb;

    white-space: nowrap;
    overflow-x: auto;
    overflow-y: auto;

    user-select: text; 
}

#output-display {
    display: grid;
    align-content: end;
    font-size: 25cqb;
    padding: 4px 0;
    
    overflow-x: auto;
    width: 100cqw;
    flex: 1 0 auto;
    overflow-y: hidden;
    white-space: nowrap;
    text-align: right;

    user-select: text; 
}

#output-display .katex:has(.katex-html:empty) {
    display: none;
}

.katex-mathml {
    user-select: none;
}

#buttons-area {
    position: relative;
    width: 100%;
    height: 100%;
}

#buttons-container {
    min-height: 0;
    display: grid;
    grid-template-rows: repeat(var(--rows), 1fr);
    height: 100%;
    box-sizing: border-box;
    container-type: size;
}

#buttons-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.button-row {
    display: grid;
    grid-auto-columns: minmax(0, 1fr);
    min-width: 0;
    min-height: 0;
}

.button-wrapper {
    display: grid;
    grid-template-rows: 3cqb minmax(0, 1fr);
    min-width: 0;
    min-height: 0;
    padding: 0.5cqb;
}

.button-label {
    font-size: 2.5cqb;
    min-width: 0;
    padding: 0 0 0.5cqb 0;
    font-weight: 700;

    white-space: nowrap;
    display: flex;
    align-items: center;
    justify-content: center;

    /*justify-content: space-between;*/
}

.input-button {
    font-weight: 500;
    width: 100%;
    height: 100%;
    min-width: 0;
    
    box-sizing: border-box;
    justify-content: center;
    padding: 0px;

    position: relative;

    transition: background-color 0.2s ease-out;
}

.calc-key-selector .input-button:hover,
.calc-key-selector .nav-button:hover {
    outline: 1cqb solid var(--main-colour);
    z-index: 10000;
}

.calc-key-selector .label-shift:hover {
    outline: 1cqb solid var(--shift-colour);
    z-index: 10000;
    border-radius: 0.5cqb;
    cursor: pointer;
}

.calc-key-selector .label-alpha:hover {
    outline: 1cqb solid var(--alpha-colour);
    z-index: 10000;
    border-radius: 0.5cqb;
    cursor: pointer;
}

.nav-button {
    font-weight: 500;
    font-size: 3.5cqb;
    width: 5cqb;
    height: 6cqb;
    box-sizing: border-box;
    justify-content: center;
    padding: 0.5cqb;
    pointer-events: all;
    transition: background-color 0.2s ease-out;
}

.label-main {
    color: var(--text-colour);
    padding: 0 0.5cqb 0 0.5cqb;
    position: relative;
}

.label-shift {
    color: var(--shift-colour);
    padding: 0 0.5cqb 0 0.5cqb;
    position: relative;
}

.label-alpha {
    color: var(--alpha-colour);
    padding: 0 0.5cqb 0 0.5cqb;
    position: relative;
}

.help-overlay-container {
    position: absolute;
    inset: 0;

    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.help-overlay {
    content: "A";
    text-align: center;
    width: fit-content;

    font-weight: bold;
    color: var(--calc-colour);
    padding: 0.5cqb 1cqb;
    max-width: 100%;

    z-index: 1;
    pointer-events: none;

    display: var(--show-help);
}

.pressed-Main,
.pressed-Shift,
.pressed-Alpha,
.pressed-Store,
.pressed-Recall {
    transition: background-color 0s; 
}

.pressed-Main {
    background-color: var(--main-colour);
}

.pressed-Shift {
    background-color: var(--shift-colour);
}

.pressed-Alpha {
    background-color: var(--alpha-colour);
}

.pressed-Store {
    background-color: var(--store-colour);
}

.pressed-Recall {
    background-color: var(--recall-colour);
}

#message-display {
    position: absolute;
    align-self: flex-start;
    pointer-events: none;
    font-size: 20cqb;
    width: 100cqw;
}

#status-bar {
    align-self: flex-start;
    font-size: 10cqb;
    height: 15cqb;
    min-height: 0;
    width: 100cqw;
    display: flex;
    justify-content: space-between;
    
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}

.status-boxed {
    color: var(--status-bar-boxed-colour);
    background-color: var(--status-bar-colour);
    width: 10cqb;
    height: 12cqb;
    min-height: 0;
    text-align: center;
    border-radius: 2cqb;
}

.status {
    opacity: 0.2;
}

.status-visible {
    opacity: 1 !important;
}

.message-display-line {
    height: 25cqb;
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
}
.math-cursor-placeholder {
    display: inline-block;
    position: relative;
    width: 0;
    vertical-align: middle;

    pointer-events: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none; 
}

.math-cursor-placeholder::after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 0.8cqw;
    top: 10%;
    height: 60%;
    background: currentColor;
}

.math-cursor-placeholder::after {
    animation: cursor-blink 1.5s steps(1) infinite;
}

@keyframes cursor-blink {
    0%, 50% {
        opacity: 1;
    }

    50.01%, 100% {
        opacity: 0;
    }
}

.math-square-placeholder {
    position: relative;
    display: inline-block;
    vertical-align: middle;

    pointer-events: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none; 
}

.math-square-placeholder::after {
    content: "";
    position: absolute;
    inset: 0;
    border: 0.4cqw solid currentColor;
    top: 10%;
    height: 60%;
    box-sizing: border-box;
}


.menubar {
    box-sizing: border-box;

    display: flex;
    align-items: center;

    flex-shrink: 0;
    justify-content: center;
}

.menuitem {
    max-height: min(5rem, 15svh);
    text-align: center;
    color: var(--text-colour);
    background: none;
    border: none;
    aspect-ratio: 1;
    box-sizing: border-box;
}

.menuicon {
    width: 100%;
    height: 100%;
    aspect-ratio: 1;

    padding: 15%;
    box-sizing: border-box;
}

body {
    flex-direction: row;
}

.menubar {
    width: min(5rem, 15svh);
    height: 100svh;
    flex-direction: column;
}

/* Narrow screen */
@media (max-aspect-ratio: 3 / 4) {
    body {
        flex-direction: column;
    }

    .calculator {
        height: min(
            calc(100svh - 7.5svh),
            calc(100vw * 2)
        );
        width: auto;
    }

    .menubar {
        width: 100%;
        height: auto;
        flex-direction: row;
    }
    
    :fullscreen .menubar {
        display: none;
    }

    .menuitem {
        max-height: 7.5svh;
    }

    :fullscreen #fullscreen-exit {
        display: block;
    }
}

.popup-wrapper {
    border: 1px solid var(--calc-border-colour);
    max-height: 80svh;
    height: 100%;
    aspect-ratio: 1/2;
    box-sizing: border-box;

    background-color: var(--screen-colour);
    border-radius: calc(var(--h) / 160);
    box-shadow: 0 0 calc(var(--h) / 80) var(--calc-shadow);

    position: relative;

    container-type: size;
    font-size: calc(var(--h) / 50);
}

.popup {
    overflow: scroll;
    height: 100%;
    padding: 0 2svh;
}

@media (max-aspect-ratio: 1) {
    .popup-wrapper {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);

        width: calc(var(--h) / 2);
        margin: 0;
        z-index: 10001;
    }

    .calc-key-selector .popup-wrapper {
        height: 15%;
        top: 15%;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .calc-key-selector .popup * {
        display: none !important;
    }

    .calc-key-selector #function-browser {
        border: none;
    }

    .calc-key-selector .popup #settings-key-mapping,
    .calc-key-selector .popup .settings-remap-list-container,
    .calc-key-selector .popup #settings-key-remap-status,
    .calc-key-selector .popup #function-browser,
    .calc-key-selector .popup #help-button-lookup-status,
    .calc-key-selector .popup #help-button-lookup-cancel {
        display: flex !important;
        justify-content: center;
        flex-direction: column-reverse;
    }
}

.popup-close {
    position: absolute;
    top: 0;
    right: 0;

    font-size: 10cqw;
    user-select: none;

    background: none;
    border: none;
}

.popup button {
    padding: 1cqw;
    margin: 2cqw 0;
}

#function-browser {
    border: 1px solid var(--screen-border-colour);
    padding: 2cqw;
    display: flex;
    flex-direction: column;
}

#function-browser-results {
    border: 1px solid var(--screen-border-colour);
    padding: 2cqw;
    overflow-y: scroll;
}

#function-browser-result {
    border: 1px solid var(--screen-border-colour);
    padding: 2cqw;
    height: 50%;
}

.function-browser-result-title {
    font-size: 8cqw;
    font-weight: bold;
    font-style: italic;
}

.function-browser-result-subtitle {
    font-weight: bold;
    font-style: italic;
}

.function-browser-result-warning {
    color: red;
    font-style: italic;
}

h1 {
    font-size: 12cqw;
    font-weight: bold;
}

h2 {
    font-size: 8cqw;
    font-weight: bold;
    font-style: italic;
    margin-bottom: 0;
}

h3 {
    font-size: 6cqw;
    font-weight: bold;
    font-style: italic;
    margin-bottom: 0;
}

code {
    font-family: 'Courier New', Courier, monospace;
    font-size: 4cqw;
}

.settings-remap-list-container {
    text-align: center;
    justify-content: center;
    display: flex;
    flex-direction: column;
    padding: 5cqw 10cqw;
}

.settings-remove-user-keybind {
    margin: 0 !important;
}

#fullscreen-exit {
    position: absolute;
    top: 0;
    left: 0;
    width: max(8svh, 8svw);
    aspect-ratio: 1;
    border: none;
    background: none;
    display: none;
}

#fullscreen-exit img {
    width: 100%;
}