:root {
    --terminal-font-size: 16px;
    --terminal-line-height: 1.08;
    --terminal-bg: #000;
    --terminal-fg: #c8c8c8;
    --terminal-bright: #fff;
    --terminal-dim: #777;
    --softkey-height: 48px;
    --ansi-seam-overlap: .06em;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    background: #000;
}

body {
    min-height: 100dvh;
    overflow: hidden;
    color: var(--terminal-fg);
    font-family: "Courier New", Courier, monospace;
}

.bbs-shell {
    position: fixed;
    inset: 0 0 var(--softkey-height) 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    overflow: auto;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 8px max(8px, env(safe-area-inset-left));
    outline: none;
}

.bbs-shell.is-busy { cursor: progress; }

.terminal {
    /* One physical pixel of safety avoids clipping the 80th monospace glyph
       when 80ch rounds fractionally below the rendered line width. */
    width: calc(80ch + 1px);
    min-height: 25em;
    /* 1.1.2: never wrap an 80-column ANSI line because sub-pixel rounding can
       push the final border glyph onto the next line. Portrait mode overrides this. */
    white-space: pre;
    overflow-wrap: normal;
    word-break: normal;
    font-size: var(--terminal-font-size);
    line-height: var(--terminal-line-height);
    letter-spacing: 0;
    font-variant-ligatures: none;
    font-synthesis: none;
    tab-size: 8;
    color: var(--terminal-fg);
    background: var(--terminal-bg);
    text-shadow: 0 0 4px rgba(255,255,255,.12);
}

.softkeys {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    min-height: var(--softkey-height);
    display: flex;
    gap: 1px;
    background: #333;
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 20;
}

.softkeys[hidden] { display: none; }

.softkeys button,
.keyboard-trigger,
.rotate-box button {
    border: 0;
    border-radius: 0;
    background: #111;
    color: #ddd;
    font: inherit;
}

.softkeys button {
    flex: 1;
    min-width: 0;
    padding: 10px 6px;
    cursor: pointer;
}

.softkeys button:active { background: #333; color: #fff; }
.softkeys button:disabled { color: #666; cursor: not-allowed; }
.softkeys button.is-keyboard-selected {
    background: #ddd;
    color: #000;
    outline: 2px solid #fff;
    outline-offset: -2px;
}

.keyboard-trigger {
    position: fixed;
    right: 10px;
    bottom: calc(var(--softkey-height) + 10px + env(safe-area-inset-bottom));
    width: 42px;
    height: 42px;
    border: 1px solid #555;
    z-index: 25;
    cursor: pointer;
}

.mobile-input {
    position: fixed;
    left: -10000px;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: .01;
}

.rotate-hint {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.96);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.rotate-hint:not([hidden]) { display: flex; }

.rotate-box {
    max-width: 360px;
    border: 1px solid #888;
    padding: 24px;
    text-align: center;
    display: grid;
    gap: 16px;
    font-size: 15px;
}

.rotate-icon { font-size: 48px; }
.rotate-box span { color: #aaa; line-height: 1.4; }
.rotate-box button { border: 1px solid #777; padding: 10px; cursor: pointer; }

@media (pointer: fine) and (min-width: 900px) {
    .softkeys,
    .keyboard-trigger { display: none; }
    .bbs-shell { bottom: 0; }

    /* 2.3: desktop keeps the classic clean terminal until cursor navigation is
       actually used. The existing server-provided command bar then becomes the
       visible selection target without changing any BBS command semantics. */
    body.keyboard-nav-active .softkeys { display: flex; }
    body.keyboard-nav-active .bbs-shell { bottom: var(--softkey-height); }
}

@media (max-width: 599px) and (orientation: portrait) {
    :root { --softkey-height: 96px; }
    .softkeys { flex-wrap: wrap; }
    .softkeys button { flex: 1 1 30%; font-size: 12px; }
    .terminal { width: 100%; white-space: pre-wrap; }
}

.file-picker {
    position: fixed;
    left: -10000px;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: .01;
}

/* 0.7: more main-menu softkeys on touch tablets; keep labels readable. */
@media (pointer: coarse) and (min-width: 600px) {
    :root { --softkey-height: 96px; }
    .softkeys { flex-wrap: wrap; }
    .softkeys button { flex: 1 1 16%; font-size: 12px; }
}

/* 1.1 ANSI renderer: classic 16-color terminal palette. */
.ansi-fg-0  { color: #000000; }
.ansi-fg-1  { color: #aa0000; }
.ansi-fg-2  { color: #00aa00; }
.ansi-fg-3  { color: #aa5500; }
.ansi-fg-4  { color: #0000aa; }
.ansi-fg-5  { color: #aa00aa; }
.ansi-fg-6  { color: #00aaaa; }
.ansi-fg-7  { color: #aaaaaa; }
.ansi-fg-8  { color: #555555; }
.ansi-fg-9  { color: #ff5555; }
.ansi-fg-10 { color: #55ff55; }
.ansi-fg-11 { color: #ffff55; }
.ansi-fg-12 { color: #5555ff; }
.ansi-fg-13 { color: #ff55ff; }
.ansi-fg-14 { color: #55ffff; }
.ansi-fg-15 { color: #ffffff; }

.ansi-bg-0  { background-color: #000000; }
.ansi-bg-1  { background-color: #aa0000; }
.ansi-bg-2  { background-color: #00aa00; }
.ansi-bg-3  { background-color: #aa5500; }
.ansi-bg-4  { background-color: #0000aa; }
.ansi-bg-5  { background-color: #aa00aa; }
.ansi-bg-6  { background-color: #00aaaa; }
.ansi-bg-7  { background-color: #aaaaaa; }
.ansi-bg-8  { background-color: #555555; }
.ansi-bg-9  { background-color: #ff5555; }
.ansi-bg-10 { background-color: #55ff55; }
.ansi-bg-11 { background-color: #ffff55; }
.ansi-bg-12 { background-color: #5555ff; }
.ansi-bg-13 { background-color: #ff55ff; }
.ansi-bg-14 { background-color: #55ffff; }
.ansi-bg-15 { background-color: #ffffff; }

.ansi-dim { opacity: .65; }
.ansi-underline { text-decoration: underline; }

/* 1.8.1: close the hairline side-bearing that Courier-style web monospace
   fonts can leave between adjacent CP437 block/shading cells. The shadow
   overlaps only the painted right edge; character advance/80-column layout
   remains untouched. Using em keeps the correction proportional when users
   zoom the page. */
.ansi-seam-fill {
    text-shadow: var(--ansi-seam-overlap) 0 0 currentColor, 0 0 4px rgba(255,255,255,.12);
}

/* 1.9: trusted static ANSI artwork is rendered as one 80xN pixel-cell canvas.
   The canvas owns the complete 8x16 grid, so neighbouring CP437 cells cannot
   expose browser-font side bearings or line-height seams. It scales as one
   image to the same visual width as the 80-column terminal. Dynamic menus and
   input remain normal DOM text. */
.ansi-bitmap-art {
    display: block;
    width: calc(80ch + 1px);
    height: auto;
    max-width: none;
    margin: 0;
    padding: 0;
    background: #000;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

@media (max-width: 599px) and (orientation: portrait) {
    .ansi-bitmap-art {
        width: 100%;
        height: auto;
    }
}


/* 2.1 protected File Base media preview. */
.preview-overlay {
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(0, 0, 0, .96);
    padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    align-items: center;
    justify-content: center;
}
.preview-overlay:not([hidden]) { display: flex; }
.preview-window {
    width: min(96vw, 1100px);
    max-height: 94dvh;
    border: 1px solid #777;
    background: #000;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    overflow: hidden;
}
.preview-header, .preview-meta {
    min-height: 38px;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 10px;
    background: #111;
    color: #ddd;
}
.preview-header { justify-content: space-between; border-bottom: 1px solid #555; }
.preview-meta { border-top: 1px solid #555; color: #aaa; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.preview-header button {
    border: 1px solid #666;
    border-radius: 0;
    background: #000;
    color: #ddd;
    padding: 7px 10px;
    font: inherit;
    cursor: pointer;
}
.preview-content {
    min-height: 180px;
    max-height: calc(94dvh - 84px);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    background: #000;
}
.preview-content img, .preview-content video {
    display: block;
    max-width: 100%;
    max-height: calc(94dvh - 100px);
    width: auto;
    height: auto;
}
.preview-content video { width: 100%; }
.preview-error { padding: 24px; color: #ddd; text-align: center; white-space: pre-wrap; }

/* 2.2 Scene audio provider UI. Decoder engines remain browser-local. */
.scene-audio-host {
    width: min(92vw, 760px);
    box-sizing: border-box;
    padding: 18px;
    color: #ddd;
    font: inherit;
}
.scene-audio-info,
.scene-audio-runtime-meta {
    display: grid;
    gap: 4px;
    margin-bottom: 16px;
}
.scene-audio-runtime-meta {
    margin-top: 16px;
    margin-bottom: 0;
    padding-top: 12px;
    border-top: 1px solid #333;
}
.scene-audio-line {
    display: grid;
    grid-template-columns: 110px minmax(0, 1fr);
    gap: 10px;
    min-width: 0;
}
.scene-audio-key { color: #888; }
.scene-audio-value {
    overflow-wrap: anywhere;
    color: #ddd;
}
.scene-audio-status {
    margin: 14px 0;
    padding: 10px;
    border: 1px solid #555;
    background: #080808;
    color: #ddd;
    text-align: center;
}
.scene-audio-pending { color: #aaa; }
.scene-audio-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin: 14px 0;
}
.scene-audio-controls button {
    border: 1px solid #666;
    border-radius: 0;
    background: #000;
    color: #ddd;
    padding: 8px 12px;
    font: inherit;
    cursor: pointer;
}
.scene-audio-controls button:disabled {
    opacity: .45;
    cursor: default;
}
.scene-audio-controls button.is-keyboard-selected {
    background: #ddd;
    color: #000;
    outline: 2px solid #fff;
    outline-offset: -2px;
}
.scene-audio-seek-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
}
.scene-audio-seek-row input[type="range"] { width: 100%; }
.scene-audio-clock { min-width: 105px; text-align: right; white-space: nowrap; }
.scene-audio-native { width: 100%; margin-top: 14px; }

@media (max-width: 599px) and (orientation: portrait) {
    .scene-audio-host { width: 100%; padding: 12px; }
    .scene-audio-line { grid-template-columns: 82px minmax(0, 1fr); gap: 8px; }
    .scene-audio-seek-row { grid-template-columns: 1fr; }
    .scene-audio-clock { text-align: center; }
}

.scene-audio-sid-clock {
    margin: 10px 0;
    text-align: center;
    font-weight: 700;
}

/* 2.4 RC2: file lists can expose the current page directly as selectable
   softkeys. When a screen has many enabled actions, use a two-row command bar
   so filenames remain readable while cursor/SPACE navigation stays unchanged. */
body.file-list-softkeys-many .softkeys {
    min-height: 96px;
    flex-wrap: wrap;
}
body.file-list-softkeys-many .softkeys button {
    flex: 1 1 13%;
    font-size: 12px;
}
@media (pointer: fine) and (min-width: 900px) {
    body.keyboard-nav-active.file-list-softkeys-many .bbs-shell { bottom: 96px; }
}

/* 2.6 C64 Interactive Preview (VC64Web / VirtualC64 provider). */
.c64-preview-host {
    width: min(94vw, 900px);
    box-sizing: border-box;
    color: #ddd;
    font: inherit;
}
.c64-preview-shell {
    padding: 18px;
    box-sizing: border-box;
}
.c64-preview-info {
    display: grid;
    gap: 4px;
    margin-bottom: 14px;
}
.c64-preview-line {
    display: grid;
    grid-template-columns: 120px minmax(0, 1fr);
    gap: 10px;
}
.c64-preview-key { color: #888; }
.c64-preview-value { color: #ddd; overflow-wrap: anywhere; }
.c64-preview-media-list {
    display: grid;
    gap: 6px;
    max-height: min(40dvh, 360px);
    overflow: auto;
    padding: 2px;
}
.c64-preview-media {
    display: grid;
    grid-template-columns: 64px minmax(0, 1fr);
    gap: 10px;
    width: 100%;
    border: 1px solid #444;
    border-radius: 0;
    background: #050505;
    color: #bbb;
    padding: 8px 10px;
    font: inherit;
    text-align: left;
    cursor: pointer;
}
.c64-preview-media.is-selected,
.c64-preview-media:focus-visible {
    outline: none;
    border-color: #ddd;
    background: #ddd;
    color: #000;
}
.c64-preview-media-format { white-space: nowrap; }
.c64-preview-media-name { overflow-wrap: anywhere; }
.c64-preview-selected-meta {
    min-height: 22px;
    margin: 10px 0 0;
    color: #999;
    text-align: center;
}
.c64-preview-controls,
.c64-preview-local-controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin: 12px 0;
}
.c64-preview-controls button,
.c64-preview-local-controls button {
    border: 1px solid #666;
    border-radius: 0;
    background: #000;
    color: #ddd;
    padding: 8px 12px;
    font: inherit;
    cursor: pointer;
}
.c64-preview-controls button:disabled {
    color: #666;
    border-color: #333;
    cursor: default;
}
.c64-preview-status {
    margin-top: 12px;
    padding: 10px;
    border: 1px solid #444;
    background: #080808;
    color: #aaa;
    text-align: center;
    white-space: pre-wrap;
}
.c64-preview-status.is-ready { color: #ddd; }
.c64-preview-status.is-warning { color: #ccc; border-style: dashed; }
.c64-preview-status.is-error { color: #fff; border-color: #888; }
.c64-preview-status.is-pending { color: #aaa; }
.c64-preview-player-section {
    width: 100%;
    box-sizing: border-box;
    padding: 10px;
}
.c64-preview-player-slot {
    width: min(100%, 820px);
    margin: 0 auto;
    min-height: 280px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: #000;
}
.c64-preview-player-target {
    width: 100%;
    min-height: 280px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #333;
    color: #888;
}
.c64-preview-player-slot #player_container {
    width: 100% !important;
    max-width: 820px;
    margin: 0 auto;
}
.c64-preview-player-slot #vc64web {
    border: 0;
    display: block;
    width: 100%;
}
@media (max-width: 599px) {
    .c64-preview-shell,
    .c64-preview-player-section { padding: 8px; }
    .c64-preview-line { grid-template-columns: 92px minmax(0, 1fr); gap: 6px; }
    .c64-preview-player-slot,
    .c64-preview-player-target { min-height: 190px; }
}
