/* Semantic color scheme */
:root {
    /* Primary base colors */
    --surface-bgc: black;
    --surface-text-c: white;
    --surface-muted-text-c: gray;
    --editable-text-bgc: white;
    --editable-text-fgc: black;

    /* Button */
    --button-bgc: white;
    --button-text-c: black;
    --button-border-c: darkgray;
    --button-hover-bgc: whitesmoke;
    --button-active-bgc: lightgray;
    --button-shadow-c: gray;
    --important-button-bgc: dimgray;

    --button-elevation: 2px;
    --button-elevation-pressed: 1px;
    
    /* UI elements */
    --border-c: silver;
    --attention-c: gold;

    /* Status colors */
    --sensors-c: lime;
    --error-c: red;

    /* Scrollbar colors */
    --scroll-bar-track-c: silver;
    --scrollbar-thumb-c: gray;

    /* Derived values */
    --_button-bgc-inverted-grayscale: oklch(from var(--button-bgc) calc(1 - l) 0 0);
    /* darker yellow for text if button bg is white */
    --attention-button-text-c: color-mix(in lch, var(--attention-c), var(--_button-bgc-inverted-grayscale) 30%);
    
    /* Typography */
    --font-mono: 'Courier New', Consolas, 'Liberation Mono', monospace;

    /* Button shadows */
    --button-shadow: 0 var(--button-elevation) 0 var(--button-shadow-c);
    --button-shadow-pressed: 0 var(--button-elevation-pressed) 0 var(--button-shadow-c);
}


/* WebKit scrollbar styling for Safari, iOS, and Chrome */
body::-webkit-scrollbar-corner,
body::-webkit-scrollbar {
    background: var(--scroll-bar-track-c);
}
body::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb-c);
}
body {
    scrollbar-color: var(--scrollbar-thumb-c) var(--scroll-bar-track-c);
}

/* Modern CSS Reset */
*,
*::before,
*::after {
    box-sizing: border-box;
}

* {
    margin: 0;
}

body {
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

img,
picture,
video,
canvas,
svg {
    display: block;
    max-width: 100%;
}

input,
button,
textarea,
select {
    font: inherit;
}

p,
h1,
h2,
h3,
h4,
h5,
h6 {
    overflow-wrap: break-word;
}
/* Layout scaffolding */
body {
    background: var(--surface-bgc);
    color: var(--surface-text-c);
    display: flex;
    flex-direction: column;
    font-family: var(--font-mono);
    font-size: 12px;
    min-height: 100vh;
    padding: 4px;
    position: relative;
}

/* Buttons */
button,
.load-select {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    background: var(--button-bgc);
    border: 2px solid var(--button-border-c);
    border-radius: 12px;
    box-shadow: var(--button-shadow);
    color: var(--button-text-c);
    cursor: pointer;
    flex: 1;
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: bold;
    margin: 2px;
    min-width: 60px;
    padding: 8px 2px;
    text-align: center;
    touch-action: manipulation;
    transition: .1s ease;
    user-select: none;
}

/* Assemble and Run button special styling - darker monochrome. Same specificity as
   the :hover/:active rules below and placed before them, so those states apply to
   it without being repeated. */
button.important-button {
    background: var(--important-button-bgc);
    color: var(--surface-text-c);
}

button:hover {
    background: var(--button-hover-bgc);
    color: var(--button-text-c);
}

button:active {
    background: var(--button-active-bgc);
    transform: translateY(1px);
    box-shadow: var(--button-shadow-pressed);
}

button:focus-visible {
    outline: 3px solid var(--button-text-c);
    outline-offset: 2px;
}

/* Menu */
.header-item {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: .3s;
    background: var(--button-bgc);
    border: none;
    border-radius: 4px;
    box-shadow: none;
    color: var(--button-text-c);
    font-size: 20px;
    height: 24px;
    /* Icon-sized: these buttons show a single glyph */
    min-width: 36px;
    width: 36px;
}

.menu-dropdown {
    background: var(--surface-bgc);
    border: 2px solid var(--border-c);
    border-radius: 8px;
    box-shadow: 0 4px 8px var(--button-shadow-c);
    display: none;
    min-width: 120px;
    position: absolute;
    right: 0;
    top: 45px;
    z-index: var(--z-menu-dropdown);
}

.menu-item {
    border-bottom: 1px solid var(--border-c);
    color: var(--surface-text-c);
    display: block;
    padding: 10px 15px;
    text-decoration: none;
    transition: background .3s;
}

.menu-item:last-child {
    border-bottom: none;
}

.menu-item:hover {
    background: var(--button-hover-bgc);
    color: var(--surface-bgc);
}

.header-item:hover {
    background: var(--button-hover-bgc);
    border-color: var(--button-bgc);
    transform: scale(1.1);
}

.section-main-flex {
    display: flex;
    align-items: stretch;
    gap: 10px;
    height: 100%;
}

/* Assembly Editor - 3 Column Layout */
.assembly-editor {
    background: var(--editable-text-bgc);
    border: 1px solid var(--border-c);
    display: flex;
    flex: 1;
    flex-direction: column;
    font-family: var(--font-mono);
    font-size: 12px;
    min-height: 200px;
    position: relative;
    touch-action: pan-y;
}

.assembly-columns {
    display: flex;
    flex: 1;
    height: 100%;
    overflow: hidden;
    position: relative;
}

.address-column,
.opcodes-column,
.assembly-column {
    color: var(--editable-text-fgc);
    font-family: var(--font-mono);
    font-size: 12px;
    max-height: 100%;
    line-height: 12px;
    overflow-y: auto;
    padding: 10px;
    white-space: pre-wrap;
    word-break: break-all;
}

/* The two read-only gutter columns share everything but width and alignment */
.address-column,
.opcodes-column {
    border-right: 1px solid var(--border-c);
    color: var(--surface-muted-text-c);
    font-size: 9px;
    line-height: 12px;
    overflow: hidden;
    padding: 10px 2px;
    -webkit-user-select: none;
    user-select: none;
}

.address-column {
    flex: 0 0 27px;
    text-align: center;
}

.opcodes-column {
    flex: 0 0 53px;
}

.assembly-column {
    flex: 1;
    outline: none;
    overflow-wrap: break-word;
}

.assembly-column:empty::before {
    color: var(--surface-muted-text-c);
    content: attr(data-placeholder);
    font-style: italic;
    pointer-events: none;
}

/* Assembly editor highlighting */
.highlighted-line {
    background: var(--attention-c);
    color: var(--button-text-c) ;
    font-weight: bold;
    display: inline;
    border-radius: 2px;
    padding: 1px 2px;
}

/* Hide address and opcodes columns by default */
.assembly-editor .address-column,
.assembly-editor .opcodes-column {
    display: none;
}

/* Show address and opcodes columns when explicitly enabled */
.assembly-editor.show-address-opcodes .address-column,
.assembly-editor.show-address-opcodes .opcodes-column {
    display: block;
}

/* When showing address/opcodes, enable no-wrap for assembly column */
.assembly-editor.show-address-opcodes .assembly-column {
    white-space: pre;
    overflow-x: auto;
    word-wrap: normal;
    word-break: normal;
}

/* Hide address and opcodes columns when assembly editor is expanded */
.expanded-element .address-column,
.expanded-element .opcodes-column {
    display: none;
}

/* Adjust assembly column when expanded to take full width */
.expanded-element .assembly-column {
    flex: 1;
}

.expanded-element {
    z-index: var(--z-expanded-element);
}

/* Note shown when an element is expanded telling user how to restore size */
.restore-size-note {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 5vh;
    background: var(--surface-bgc);
    color: var(--surface-text-c);
    text-align: center;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-restore-message);
}

/* Screen */
.screen-container {
    position: relative;
    flex: 0 0 auto;
}

.audio-start-prompt {
    position: absolute;
    inset: 0;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: clamp(12px, 4cqw, 24px);
    border: 2px solid var(--border-c);
    background: var(--editable-text-bgc);
    color: var(--editable-text-fgc);
    text-align: center;
}

.audio-start-prompt[hidden] {
    display: none;
}

.audio-start-prompt p {
    margin: 0 0 8px;
    font-size: clamp(16px, 3cqw, 22px);
}

.audio-start-prompt button {
    flex: 0 0 auto;
    width: min(100%, 260px);
    min-height: 44px;
    padding: 10px 18px;
    font-size: 14px;
}

.execution-section:has(.audio-start-prompt:not([hidden])) .not-ready-message,
.execution-section:has(.audio-start-prompt:not([hidden])) .keyboard-status {
    visibility: hidden;
}

#screen {
    position: relative;
    background: var(--editable-text-bgc);
    border: 2px solid var(--border-c);
    color: var(--editable-text-fgc);
    contain: layout style;
    display: grid;
    flex: 0 0 auto;
    grid-template-columns: repeat(32, 1fr);
    grid-template-rows: repeat(24, calc(100% / 24));
    /* Responsive sizing: 50% container width, height = 3/4 width */
    width: 480px;
    height: 360px;
    overflow: hidden;
    padding: 5px;
    will-change: contents;
}

/* Centered cells and edit-mode button share centering */
.screen-char,
.game-buttons button.edit-mode {
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    overflow: hidden;
}

.screen-char.inverted {
    background: var(--editable-text-fgc);
    color: var(--editable-text-bgc);
}

.screen-char.sinclair-font {
    font-family: 'VT323';
    font-size: calc(360px / 18);
}
.screen-char:not(.sinclair-font) {
    font-family: var(--font-mono);
    font-size: calc(360px / 22);
}

.screen-char.sinclair-font.inverted {
    transform: scaleX(1.2);
}

.screen-char.sinclair-font:not(.inverted) {
    transform: scaleX(2);
}

/* PLOT cells must meet at their edges; stretched font glyphs leave seams and
   produce colored text-antialiasing fringes, especially in stippled faces. */
.screen-char.plot-graphics {
    min-width: 0;
    min-height: 0;
    font-size: 0;
    transform: none;
    background: transparent;
}

.plot-screen {
    position: absolute;
    inset: 5px;
    width: calc(100% - 10px);
    height: calc(100% - 10px);
    overflow: visible;
    pointer-events: none;
    z-index: 1;
}

.plot-cell {
    --plot-ink: var(--editable-text-fgc);
    --plot-paper: var(--editable-text-bgc);
}

.plot-cell.inverted {
    --plot-ink: var(--editable-text-bgc);
    --plot-paper: var(--editable-text-fgc);
}



/* Layout sections */
.top-row {
    display: flex;
    gap: 10px;
    margin-bottom: 0;
}

.top-row .assembly-section {
    flex: 2;
}

.top-row .listing-section {
    flex: 0.67;
}

.section {
    border: 1px solid var(--border-c);
    border-radius: 5px;
    display: flex;
    flex-direction: column;
    margin-bottom: 4px;
    overflow-y: auto;
    padding: 4px;
}

.section h3 {
    color: var(--surface-text-c);
    flex-shrink: 0;
    margin: 0 0 4px;
}

.section-content,
.section-main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.section-main {
    flex-direction: column;
}

.section-controls {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    margin-left: 4px;
    min-width: 160px;
    overflow-y: auto;
}

.execution-section .section-content {
    container-type: inline-size;
    display: flex;
    flex-direction: row;
    gap: 4px;
    align-items: flex-start;
}


.execution-controls {
    flex: 1;
    min-width: 300px;
}

/* Text outputs */
#machineCode,
#console {
    background: var(--editable-text-bgc);
    border: 1px solid var(--border-c);
    color: var(--editable-text-fgc);
    flex: 1;
    font-family: var(--font-mono);
    font-size: 11px;
    overflow-y: auto;
    padding: 10px;
    white-space: pre-wrap;
}

#machineCode.error {
    /* Increase contrast vs light background by darkening error red slightly */
    color: color-mix(in srgb, var(--error-c), black 25%);
}

/* Heights */
.assembly-section,
.listing-section,
.console-section {
    flex: 0 0 auto;
}

.execution-section {
    flex: 1;
    min-height: max(100px, 33.33vh);
}

.assembly-section,
.listing-section {
    height: 33vh;
    min-height: max(100px, 33vh);
}

.listing-section {
    max-height: 50vh;
}

/* Collapsed sections */
.listing-section.collapsed,
.console-section.collapsed {
    height: auto;
    min-height: 0;
    max-height: 50px;
    overflow: hidden;
}

.listing-section.collapsed .section-content,
.console-section.collapsed .section-content {
    display: none;
}

/* Clickable headers for collapsible sections */
.listing-section h3,
.console-section h3 {
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    position: relative;
}

.listing-section h3:hover:not(:has(.header-item:hover)),
.console-section h3:hover:not(:has(.header-item:hover)) {
    background: var(--button-hover-bgc);
    color: var(--button-text-c);
    border-radius: 3px;
}

/* Keep the disclosure cue in the header's layout, away from its action buttons.
   CSS borders avoid depending on a font or encoding for the arrow glyph. */
.listing-section h3::before,
.console-section h3::before {
    content: "";
    flex: 0 0 auto;
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 6px solid currentColor;
    color: var(--surface-muted-text-c);
}

.listing-section.collapsed h3::before,
.console-section.collapsed h3::before {
    transform: rotate(-90deg);
}

.console-section {
    height: 12vh;
    min-height: max(100px, 12vh);
}

/* Controls */
.controls {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    gap: 2px;
    margin-top: 0;
    text-align: center;
}

.assembly-section .section-controls .controls {
    flex-direction: column;
}

.assembly-section .section-controls .controls button,
.assembly-section .section-controls .controls .load-select {
    flex: none;
}

/* Program loaders: one button each on wide screens; the select replaces them on
   narrow screens (see the narrow media query) where four stacked buttons cost a
   quarter of the viewport */
.load-select {
    display: none;
}

/* Keyboard status */
.keyboard-status {
    border: 2px solid transparent;
    border-radius: 4px;
    color: var(--surface-muted-text-c);
    font-size: 11px;
    font-style: italic;
    margin: 10px 0 0;
    padding: 2px 5px;
    text-align: center;
    transition: .3s;
}

.keyboard-status.get-attention {
    background: var(--attention-c);
    border: 2px solid var(--border-c);
    font-weight: bold;
    font-style: normal;
    color: var(--button-text-c)
}

/* Animations */
@keyframes colorAwayFromBgToAttention {

    50% {
        color: var(--attention-button-text-c);
    }
}

/* Hide expand button on wide screens where there's enough space */
@media (min-width: 769px) {
    .header-item[title="Expand Editor"],
    .header-item[title="Expand Magazine Listing"] {
        display: none;
    }

    /* The page menu (.header-items) overlays the top-right corner, which is where
       this heading's own buttons end when the sections sit side by side */
    .listing-section h3 {
        padding-right: 48px;
    }
}

/* Responsive */
/* not working @media (max-width: var(--narrow-max-width, 768px)) { */
@media (max-width: 768px) {
    body {
        padding: 0;
    }

    .controls {
        gap: 0;
    }

    /* Loaders take one row: the select instead of the four buttons */
    .load-btn {
        display: none;
    }

    .load-select {
        display: inline-block;
    }

    /* Editor lifecycle (narrow screens only, see updateButtonVisibility): loaders while
       the editor is empty, Clear and Assemble once it has content */
    .assembly-empty-state #assemble-run-btn,
    .assembly-empty-state .clear-btn,
    .assembly-content-state .load-select {
        display: none;
    }

    .assembly-content-state .clear-btn,
    .assembly-content-state #assemble-run-btn {
        display: inline-block;
    }

    body .header-items {
        right: 3px;
        top: 0px;
    }

    .top-row {
        flex-direction: column;
    }

    .section {
        margin-bottom: 0;
    }

    .section-content {
        flex-direction: column;
    }

    .execution-section .section-content {
        flex-direction: column;
        gap: 0;
    }

    .section-controls {
        margin-left: 0;
        min-width: auto;
    }

    .execution-controls {
        margin-left: 0;
        min-width: auto;
        max-width: none;
        padding-left: 0;
        width: 100%;
    }

    #screen {
        width: 100cqw;
        height: calc(100cqw * 3 / 4);
    }
    
    .screen-char:not(.sinclair-font) {
        font-size: calc(100cqw * 3 / 4 / 22);
    }
    
    .screen-char.sinclair-font {
        font-size: calc(100cqw * .75* 3 / 4 / 18);
    }

    .assembly-section {
        min-height: 60vh;
        max-height: 60vh;
    }

     .assembly-columns {
       overflow-y: auto;
    }

    .console-section {
        height: 15vh;
        min-height: max(100px, 15vh);
    }

    .listing-section {
        display: none;
    }

    .listing-section:has(#machineCode:not(:empty)) {
        display: flex;
        height: 33.33vh;
    }

    /* Hide the program loader when stepping */
    body.stepping .load-select {
        display: none;
    }
}

/* Touch-only devices */
@media (pointer: coarse) and (hover: none) {
    .keyboard-status {
        display: none;
    }
}

/* Hover effects only on devices that support true hovering */
@media (hover: hover) {
    .not-ready-message:hover {
        background: var(--attention-c);
    }
}


.not-ready-message {
    color: var(--button-text-c);
    cursor: pointer;
    font-size: 11px;
    font-weight: bold;
    margin: 10px 0 0;
    text-align: center;
    transition: .3s;
}

.not-ready-message {
    background: var(--attention-c);
    border: 2px solid var(--attention-c);
}

/* Header menu */
.header-items {
    position: absolute;
    right: 6px;
    top: 4px;
    z-index: 1000;
    display: flex;
    gap: 2px;
}

/* Buttons inside h3 headings should stay inline */
h3 .header-item {
    display: inline-flex;
    margin-left: 0;
    vertical-align: middle;
    padding: 2px 6px;
    position: relative;
    z-index: 10;
    flex: 0 0 auto;
}

/* Right-aligned, larger header buttons for console & listing sections
   Consolidated to avoid duplication while preserving per-section behavior. */
.console-section h3,
.listing-section h3 {
    display: flex;
    align-items: center;
    gap: 8px;
}

.console-section h3 .header-item:first-of-type,
.listing-section h3 .header-item:first-of-type {
    /* push the first header-item to the right so the group stays together */
    margin-left: auto;
}

/* Debug */
.debug {
    background: var(--surface-bgc);
    border: 1px solid var(--border-c);
    border-radius: 6px;
    color: var(--surface-muted-text-c);
    display: grid;
    flex-shrink: 0;
    font-family: 'Courier New', monospace;
    font-size: 10px;
    gap: 4px 10px;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    margin-top: 4px;
    padding: 12px;
}

.debug>div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2px 0;
}

.debug span {
    color: var(--sensors-c);
    font-weight: bold;
    letter-spacing: .5px;
    text-align: right;
}

.sample-buttons { display: contents; }
.game-buttons {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    margin: 4px 0;
}
.game-buttons button {
    min-width: 0;
    height: 36px;
    padding: 0 2px;
    margin: 0;
    font-size: 12px;
}
.startup-setting select {
    max-width: 100%;
    padding: 4px;
    background: var(--editable-text-bgc);
    color: var(--editable-text-fgc);
    border: 1px solid var(--border-c);
}
.startup-setting select { display: block; width: 100%; margin-top: 6px; }
.touch-keyboard {
    display: none;
    position: absolute;
    inset: 0;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    resize: none;
    font: 16px sans-serif;
    border: 0;
    /* A real input over the screen opens the phone keyboard on the tap itself. */
    opacity: 0;
}
@media (any-pointer: coarse) {
    .touch-keyboard { display: block; }
}

/* Update Dialog Overlay */
.update-dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: color-mix(in srgb, var(--surface-bgc) 70%, transparent);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: var(--z-maximum);
}

.update-dialog {
    max-width: 400px;
    margin: 0;
    flex: none;
}
