/* Minimal ANSI classes styling for console */
.ansi {
    white-space: pre-wrap;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

.ansi.bold {
    font-weight: 700;
}

.ansi.underline {
    text-decoration: underline;
}

.ansi.fg-black {
    color: #000;
}

.ansi.fg-red {
    color: #e11d48;
}

.ansi.fg-green {
    color: #16a34a;
}

.ansi.fg-yellow {
    color: #f59e0b;
}

.ansi.fg-blue {
    color: #2563eb;
}

.ansi.fg-magenta {
    color: #c026d3;
}

.ansi.fg-cyan {
    color: #0891b2;
}

.ansi.fg-white {
    color: #e5e7eb;
}

.ansi.fg-bright-black {
    color: #6b7280;
}

.ansi.fg-bright-red {
    color: #f43f5e;
}

.ansi.fg-bright-green {
    color: #22c55e;
}

.ansi.fg-bright-yellow {
    color: #fbbf24;
}

.ansi.fg-bright-blue {
    color: #3b82f6;
}

.ansi.fg-bright-magenta {
    color: #d946ef;
}

.ansi.fg-bright-cyan {
    color: #06b6d4;
}

.ansi.fg-bright-white {
    color: #ffffff;
}

.console-page {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.console-controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

.console-panels {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 12px;
}

.console-output {
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 8px;
    height: 260px;
    overflow: auto;
    background: #0b1020;
    color: #cbd5e1;
}

.panel-title {
    margin-bottom: 6px;
    font-weight: 600;
}

.sys {
    color: #94a3b8;
    font-style: italic;
}

    /* ===== CRT console theme ===== */
    :root {
        --crt-bg: #0f172a;
        --crt-fg: #e2e8f0; /* overridden by theme-*/
        --crt-dim: #64748b; /* dimmed text */
        --crt-glow: rgba(0, 0, 0, 0);
        --crt-scanline: rgba(255, 255, 255, 0.03);
        --crt-vignette: rgba(0, 0, 0, 0.45);
        --crt-border: #1e293b;
    }

    /* Themes
       Note: theme-default/amber/green/cyan classes are toggled dynamically via JS (classList.add/remove)
       so static analyzers may not see direct usage. */
    .crt.theme-default {
        --crt-fg: #e2e8f0;
        --crt-dim: #64748b;
        --crt-glow: rgba(0, 0, 0, 0);
    }

    .crt.theme-green {
        --crt-fg: #9cff9c;
        --crt-dim: #3f6b3f;
        --crt-glow: rgba(156, 255, 156, 0.38);
    }

    .crt.theme-amber {
        --crt-fg: #ffc66d;
        --crt-dim: #6b553f;
        --crt-glow: rgba(255, 198, 109, 0.38);
    }

    .crt.theme-cyan {
        --crt-fg: #8feaff;
        --crt-dim: #3f5f6b;
        --crt-glow: rgba(143, 234, 255, 0.38);
    }

    /* Main screen area; legacy themes add vignette and scanlines below */
    .console-page.crt {
        position: relative;
        z-index: 0;
        display: flex;
        flex-direction: column;
        height: calc(100dvh - 180px);
        max-height: calc(100dvh - 180px);
        min-height: 0;
        background: var(--crt-bg);
        color: var(--crt-fg);
        border: 1px solid var(--crt-border);
        border-radius: 14px;
        padding: 16px;
        overflow: hidden;
        font-family: var(--pg-mono, ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace);
        text-shadow: 0 0 6px var(--crt-glow), 0 0 2px var(--crt-glow);
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    }

    /* Legacy CRT themes keep the retro curvature/gloss and background glow */
    .console-page.crt:not(.theme-default) {
        background: radial-gradient(120% 90% at 50% 50%, rgba(255, 255, 255, 0.03) 0%, transparent 40%), var(--crt-bg);
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55), inset 0 0 40px rgba(0, 0, 0, 0.5);
    }

    /* subtle curvature/gloss (legacy CRT themes only) */
    .console-page.crt:not(.theme-default)::before {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(120% 90% at 50% 10%, rgba(255, 255, 255, 0.06), transparent 60%),
        radial-gradient(120% 90% at 50% 100%, rgba(255, 255, 255, 0.02), transparent 60%);
        mix-blend-mode: screen;
        pointer-events: none;
    }

    /* scanlines + slight flicker (legacy CRT themes only) */
    .console-page.crt:not(.theme-default)::after {
        content: "";
        position: absolute;
        inset: -2px;
        background: repeating-linear-gradient(to bottom, transparent 0 2px, var(--crt-scanline) 2px 3px),
        radial-gradient(120% 90% at 50% 50%, transparent 40%, var(--crt-vignette) 100%);
        opacity: 0.85;
        pointer-events: none;
        animation: crt-flicker 6.8s infinite steps(60, end);
    }

    @keyframes crt-flicker {
        0%, 100% {
            opacity: 0.90;
        }
        50% {
            opacity: 0.80;
        }
        53% {
            opacity: 0.86;
        }
    }

    /* Toolbar: heading, description and action controls */
    .console-toolbar {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        flex-shrink: 0;
        padding-bottom: 12px;
        border-bottom: 1px solid var(--crt-border);
    }

    .console-toolbar-title h2 {
        color: var(--crt-fg);
        opacity: 0.95;
        letter-spacing: 0.02em;
        font-size: 1.05rem;
        margin: 0;
    }

    .console-toolbar-desc {
        margin: 2px 0 0;
        font-size: 0.8rem;
        color: var(--crt-dim);
        opacity: 0.9;
    }

    .console-toolbar-actions {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 8px;
        font-size: 0.85rem;
    }

    .console-state {
        color: var(--crt-fg);
        opacity: 0.85;
    }

    .console-follow-label {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        color: var(--crt-fg);
        opacity: 0.85;
    }

    .console-action-btn, .console-theme-select {
        background: transparent;
        border: 1px solid var(--crt-dim);
        color: var(--crt-fg);
        padding: 2px 8px;
        border-radius: 4px;
        font-family: inherit;
        font-size: 0.85em;
        cursor: pointer;
        opacity: 0.85;
        transition: opacity 0.2s, background-color 0.2s;
    }

    .console-action-btn:hover:not(:disabled), .console-theme-select:hover {
        background: rgba(255, 255, 255, 0.05);
        opacity: 1;
    }

    .console-action-btn:focus-visible, .console-theme-select:focus-visible, .crt-copy-btn:focus-visible {
        outline: 2px solid var(--crt-fg);
        outline-offset: 1px;
    }

    /* Severity/search strip */
    .console-filter-strip {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        flex-shrink: 0;
        margin-top: 10px;
    }

    .console-filter-tabs {
        display: flex;
        flex-wrap: wrap;
        gap: 4px;
    }

    .console-filter-tab {
        background: transparent;
        border: 1px solid transparent;
        border-radius: 4px;
        color: var(--crt-dim);
        padding: 4px 10px;
        font-family: inherit;
        font-size: 0.85em;
        cursor: pointer;
    }

    .console-filter-tab:hover {
        color: var(--crt-fg);
    }

    .console-filter-tab:focus-visible {
        outline: 2px solid var(--crt-fg);
        outline-offset: 1px;
    }

    .console-filter-tab.is-active {
        color: var(--crt-fg);
        border-color: var(--crt-dim);
    }

    .console-filter-tab .filter-count {
        opacity: 0.75;
    }

    .console-search-input {
        background: transparent;
        border: 1px solid var(--crt-dim);
        color: var(--crt-fg);
        padding: 4px 8px;
        border-radius: 4px;
        font-family: inherit;
        font-size: 0.85em;
        min-width: 180px;
    }

    .console-search-input:focus-visible {
        outline: 2px solid var(--crt-fg);
        outline-offset: 1px;
    }

    /* Zero-match empty state, centered over the log viewport */
    .console-empty-state {
        position: absolute;
        inset: 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 8px;
        pointer-events: none;
        color: var(--crt-dim);
        text-align: center;
    }

    .console-empty-state[hidden] {
        display: none;
    }

    .console-empty-icon {
        font-size: 2rem;
        opacity: 0.7;
    }

    /* Panels grid */
    .console-panels {
        display: flex;
        flex-direction: column;
        flex: 1;
        gap: 12px;
        min-height: 0;
        width: 100%;
        min-width: 0; /* prevent flex children from shrinking content area to 50% */
        box-sizing: border-box;
    }

    .panel {
        position: relative;
        display: flex;
        flex-direction: column;
        flex: 1;
        border: 1px solid rgba(255, 255, 255, 0.06);
        border-radius: 10px;
        background: linear-gradient(180deg, rgba(255, 255, 255, 0.03), rgba(0, 0, 0, 0.06));
        box-shadow: inset 0 0 25px rgba(0, 0, 0, 0.45);
        overflow: hidden; /* let inner .console-output be the scroller */
        min-height: 0;
        width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    /* Console output */
    .console-output {
        flex: 1 1 0;
        padding: 10px;
        padding-bottom: 20px;
        overflow-y: auto;
        overflow-x: hidden;
        color: var(--crt-fg);
        white-space: normal;
        word-break: break-word;
        min-height: 0;
        height: auto !important;
        max-height: none !important;
        overscroll-behavior: contain;
        width: 100%;
        min-width: 0;
        box-sizing: border-box;
        -webkit-overflow-scrolling: touch;
    }

    /* Preserve whitespace on log lines */
    .console-output > div {
        white-space: pre-wrap;
    }

    .console-output > div:first-of-type {
        padding-top: 0;
    }

    .console-output > div:last-of-type {
        padding-bottom: 20px;
    }

    /* stderr lines in red
       Note: .stderr-line is applied dynamically when rendering stderr frames. */
    .stderr-line, .stderr-line * {
        color: #ff4444 !important;
        text-shadow: 0 0 6px rgba(255, 68, 68, 0.35) !important;
    }

    /* Backend wraps stderr content in <span class="stderr"> */
    .stderr, .stderr * {
        color: #ff4444 !important;
        text-shadow: 0 0 6px rgba(255, 68, 68, 0.35) !important;
    }

    /* echoed commands and system messages
       Note: .cmd.echo is set from JS when echoing user-entered commands. */
    .cmd.echo {
        opacity: 0.95;
    }

    /* System messages
       Note: .sys is assigned by JS when rendering system lines. */
    .sys {
        color: var(--crt-dim);
        opacity: 0.9;
        font-style: italic;
    }

    /* timestamp for echoed commands */
    /* Timestamp for echoed commands
       Note: .ts spans are created dynamically when echoing commands. */
    .ts {
        color: var(--crt-dim);
        opacity: 0.9;
        margin-right: 6px;
    }

    /* Prompt label before input
       Note: .prompt-label is static in markup but can be toggled dynamically. */
    .prompt-label {
        color: var(--crt-fg);
        opacity: 0.95;
    }

    /* Dedicated command composer, separate from the log viewport */
    .console-composer {
        position: relative;
        display: flex;
        align-items: center;
        gap: 8px;
        flex-shrink: 0;
        margin-top: 10px;
        padding-top: 10px;
        border-top: 1px solid var(--crt-border, #1e293b);
    }

    .console-composer-input {
        flex: 1 1 auto;
        min-width: 0;
        background: transparent;
        border: 1px solid var(--crt-dim);
        color: var(--crt-fg);
        padding: 6px 10px;
        border-radius: 4px;
        font-family: inherit;
        font-size: 1em;
    }

    .console-composer-input:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    .console-composer-input:focus-visible {
        outline: 2px solid var(--crt-fg);
        outline-offset: 1px;
    }

    .console-action-btn:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    /* Searchable command history popover, anchored above the composer */
    .console-history-panel {
        position: absolute;
        bottom: calc(100% + 6px);
        left: 0;
        right: 0;
        max-height: 260px;
        display: flex;
        flex-direction: column;
        background: var(--crt-bg, #0f172a);
        border: 1px solid var(--crt-dim);
        border-radius: 6px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
        z-index: 5;
    }

    .console-history-search-row {
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 8px;
        border-bottom: 1px solid var(--crt-border, #1e293b);
    }

    .console-history-search-row .console-search-input {
        flex: 1 1 auto;
        min-width: 0;
    }

    .console-history-list {
        list-style: none;
        margin: 0;
        padding: 4px;
        overflow-y: auto;
        min-height: 0;
    }

    .console-history-item {
        padding: 6px 8px;
        border-radius: 4px;
        color: var(--crt-fg);
        white-space: pre-wrap;
        word-break: break-word;
        cursor: pointer;
        font-family: inherit;
    }

    .console-history-item:hover,
    .console-history-item.is-active {
        background: rgba(255, 255, 255, 0.08);
    }

    /* Reduced motion */
    @media (prefers-reduced-motion: reduce) {
        .console-page.crt::after {
            animation: none;
        }
    }

    /* Scrollbars (WebKit/Blink) */
    .console-output::-webkit-scrollbar {
        height: 10px;
        width: 10px;
    }

    .console-output::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.04);
    }

    .console-output::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.12);
        border-radius: 6px;
    }

    .crt-copy-btn {
        background: transparent;
        border: 1px solid var(--crt-dim);
        color: var(--crt-fg);
        padding: 2px 8px;
        border-radius: 4px;
        font-family: inherit;
        font-size: 1em;
        cursor: pointer;
        opacity: 0.8;
        transition: opacity 0.2s, background-color 0.2s, color 0.2s;
    }

    .crt-copy-btn:hover:not(:disabled) {
        background: rgba(255, 255, 255, 0.05);
        opacity: 1;
    }

    .crt-copy-btn:disabled {
        cursor: default;
        opacity: 0.6;
    }

/* Narrow viewports: reclaim vertical space taken by the mobile nav chrome */
@media (max-width: 640px) {
    .console-page.crt {
        height: calc(100dvh - 120px);
        max-height: calc(100dvh - 120px);
        padding: 12px;
    }
}

/* Fill the screen when expanded via the native Fullscreen API */
.console-page.crt:fullscreen {
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
}

