/* ==========================================================================
   IBM MDA / Hercules style monochrome terminal: 80x25, 9x14 cells (720x350)
   shown on a 4:3 tube.
   ========================================================================== */

/* "Web437 IBM MDA" from The Ultimate Oldschool PC Font Pack v2.2 by VileR
   (int10h.org), licensed CC BY-SA 4.0 - see fonts/LICENSE.TXT. */
@font-face {
    font-family: "IBM MDA";
    src: url("../fonts/ibm-mda-9x14.woff") format("woff");
    font-display: block;
}

/* ---- Phosphors ----------------------------------------------------------- */
:root {
    --cell-h: 14px;                 /* character cell height (font size) */
    --lines: 350;                   /* scanlines in the picture */
}
[data-theme="green"] {
    --fg: #2fd65c;                  /* normal intensity */
    --hi: #8dffa6;                  /* high intensity */
    --bg: #020a04;                  /* unlit tube */
    --glow: rgba(60, 255, 110, 0.55);
    --tube: #0a2212;                /* faint glow of the glass */
}
[data-theme="amber"] {
    --fg: #f0a000;
    --hi: #ffd070;
    --bg: #0a0600;
    --glow: rgba(255, 170, 30, 0.55);
    --tube: #241500;
}
[data-theme="white"] {
    --fg: #b9c4c9;
    --hi: #ffffff;
    --bg: #05070a;
    --glow: rgba(200, 225, 255, 0.5);
    --tube: #141a1f;
}

/* ---- Page and display ---------------------------------------------------- */
html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: #1c1c1c;            /* only visible around a forced-aspect display */
}
body {
    display: grid;
    place-items: center;
}

/* The physical panel. Fills the page; for testing it can be forced to a shape. */
#display {
    width: 100vw;
    height: 100vh;
    background: #000;
    container-type: size;
    display: grid;
    place-items: center;
    overflow: hidden;
}
#display[data-aspect="4:3"] {
    width: min(100vw, 100vh * 4 / 3);
    height: min(100vh, 100vw * 3 / 4);
}
#display[data-aspect="16:9"] {
    width: min(100vw, 100vh * 16 / 9);
    height: min(100vh, 100vw * 9 / 16);
}

/* ---- The 4:3 tube -------------------------------------------------------- */
.screen {
    position: relative;
    width: min(100cqw, 100cqh * 4 / 3);
    height: min(100cqh, 100cqw * 3 / 4);
    background: var(--bg);
    overflow: hidden;
    /* Picture position and size, adjusted with SETUP SCREEN */
    transform: translate(var(--ox, 0px), var(--oy, 0px)) scale(var(--size, 1));
}

/* SETUP SCREEN: show the edge of the 4:3 screen while it is being positioned */
#display.setup .screen::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 2;
    pointer-events: none;
    border: 2px dashed var(--hi);
    border-radius: inherit;
}

/* The lit picture: a little smaller than the glass, as on a real monitor. */
.viewport {
    position: absolute;
    inset: 4.5% 4%;
}

/* ---- Character grid ------------------------------------------------------
   Laid out at its natural 720x350 size, then stretched (non-square pixels)
   to fill the viewport by Terminal.fit().                                   */
.terminal {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    transform: scale(var(--sx, 1), var(--sy, 1));

    font-family: "IBM MDA", Menlo, Consolas, "DejaVu Sans Mono", monospace;
    font-size: var(--cell-h);
    line-height: var(--cell-h);
    font-variant-ligatures: none;

    color: var(--fg);
    cursor: default;
    user-select: none;
}

.terminal .row {
    width: 80ch;
    height: var(--cell-h);
    overflow: hidden;
    white-space: pre;
}

/* ---- MDA attributes ------------------------------------------------------ */
.terminal .hi  { color: var(--hi); }
.terminal .rev { background: var(--fg); color: var(--bg); text-shadow: none; }
.terminal .rev.hi { background: var(--hi); }
.terminal .ul  { background: linear-gradient(currentColor, currentColor) 0 12px / 100% 1px no-repeat; }
.terminal .hid { visibility: hidden; }
.terminal .blink { animation: blink 1s steps(1) infinite; }

/* ---- Cursor: blinking underline on scanlines 12-13 ----------------------- */
.terminal .cursor {
    position: absolute;
    left: calc(var(--cx, 0) * 1ch);
    top: calc(var(--cy, 0) * var(--cell-h) + 11px);
    width: 1ch;
    height: 2px;
    background: var(--hi);
    animation: blink 0.5s steps(1) infinite;
}
.terminal .cursor.hidden { display: none; }

@keyframes blink {
    50% { opacity: 0; }
}

/* ---- CRT feel (toggle with the "crt" class on <body>) -------------------- */

/* Phosphor bloom */
.crt .terminal {
    text-shadow: 0 0 0.5px currentColor, 0 0 3px var(--glow);
}
.crt .terminal .rev {
    box-shadow: 0 0 3px var(--glow);
}
.crt .terminal .cursor {
    box-shadow: 0 0 3px var(--glow);
}

/* Curved glass: rounded corners and a faint glow in the middle of the tube */
.crt .screen {
    border-radius: 5% / 6.5%;
    background: radial-gradient(ellipse at center, var(--tube) 0%, var(--bg) 75%);
}

/* Scanlines, one per picture line */
.crt .viewport::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(to bottom, transparent 50%, rgba(0, 0, 0, 0.3) 50%);
    background-size: 100% calc(100% / var(--lines));
}

/* Vignette and glass reflection */
.crt .screen::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background:
        radial-gradient(ellipse at 30% 12%, rgba(255, 255, 255, 0.05) 0%, transparent 45%),
        radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.55) 100%);
    box-shadow: inset 0 0 6cqh 1cqh rgba(0, 0, 0, 0.8);
}
