/* Internal terminals should now take up all space inside the workspace */
#terminals {
    flex: 1;
    display: flex;
    position: relative;
    flex-direction: column;
    width: 100%;
    overflow: hidden !important;
    background: #fff;
}

/* Force the xterm-viewport (the actual scrollable area) to stay inside */
.xterm-viewport {
    border-radius: 0 0 12px 0;
    overflow-y: auto !important;
}

/*
 * The native scrollbar belongs to .xterm-viewport, which is only as tall as
 * the xterm element -- i.e. the panel minus the bottom reserve -- so it always
 * stopped short and looked broken. Hide it and draw our own in
 * #guestTerminalScrollbar, which is anchored to #guestTerminal and therefore
 * spans the reserve too. Driven entirely by the viewport's standard
 * scrollTop/scrollHeight, so it does not depend on xterm internals.
 */
#guestTerminal .xterm-viewport {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#guestTerminal .xterm-viewport::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

/*
 * Single source of truth for the terminal surface. guest_terminal.js reads
 * --terminal-bg for the xterm theme, so the rows xterm paints and the panel
 * behind them cannot drift apart and band.
 */
:root {
    --terminal-bg: #1e1e1e;
    --terminal-fg: #e5e7eb;

    /*
     * How far the live prompt sits above the bottom edge, so viewers are not
     * tracking typing down in the corner of the screen.
     *
     * dvh, not vh: on iOS Safari vh is measured against the *largest* viewport
     * (toolbar collapsed), so a vh reserve is wrong by the height of the URL
     * bar and jumps as it hides. dvh tracks the viewport that is actually
     * visible. The vh line above is the fallback for anything without dvh.
     *
     * clamp bounds are rem so they follow the user's root font size rather
     * than a fixed pixel count -- on a phone the floor keeps a readable gap
     * without eating the screen, on a large monitor the ceiling stops the
     * reserve growing into dead space.
     */
    --terminal-bottom-reserve: 10vh;
    --terminal-bottom-reserve: clamp(2rem, 10dvh, 7rem);
}

/*
 * Fill whatever height is left under the header instead of a fixed 80%. The
 * old 80% left a dead band that read as a stray grey bar, because xterm paints
 * its own background over only its own rows.
 *
 * The reserve is padding on this element -- inside the terminal's own
 * background -- so it blends instead of banding, and fitAddon sizes rows to
 * the content box. safe-area-inset keeps it clear of the iPhone home
 * indicator; it resolves to 0px everywhere else.
 */
#guestTerminal {
    flex: 1;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: 0;
    /* One-finger gestures scroll the grid and scrollback together; leave
       browser pinch zoom available for accessibility. */
    touch-action: pinch-zoom;
    position: relative;
    overflow: hidden;
    background-color: var(--terminal-bg);
    padding-bottom: calc(
        var(--terminal-bottom-reserve) + env(safe-area-inset-bottom, 0px)
    );
    box-sizing: border-box;
}

/*
 * Landscape phones have so little height that any percentage reserve costs
 * more rows than the lift is worth. Drop to a token gap there.
 */
@media (max-height: 480px) {
    :root {
        --terminal-bottom-reserve: 1rem;
    }
}

/* This is the div where xterm is injected */
#guestTerminal > div {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: 100%;
    min-height: 0;
    overflow: hidden;
    display: flex;
}

#guestTerminal .xterm {
    width: 100%;
    height: 100%;
    flex: 1;
}

.terminal.xterm {
    height: 100%;
    padding: 10px;
}

/* xterm owns .xterm-screen's pixel height, matching the presenter's grid. */

/*
 * max-width/min-width pin the panel to the pane it sits in. Without them a
 * child that cannot shrink (the header, below) grows the pane's scrollWidth,
 * and because the panel's background only paints its own box you can pan
 * sideways into bare body background. html/body are overflow:hidden, so the
 * element that actually scrolls in that case is #leftPane.
 */
#guestTerminalContainer {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    background-color: var(--terminal-bg);
}

/* Header Container */
#guestTerminalContainer .panel-header {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    height: 40px !important;
    padding: 0 10px !important;
    box-sizing: border-box !important;
    flex: 0 0 40px;
    /* A flex item will not shrink below its min-content width unless told to.
       The title + tab strip + five buttons exceed a phone's width, so without
       this the header is what pushes the panel sideways. */
    min-width: 0;
    max-width: 100%;
    gap: 8px;
    background-color: var(--primary-color, #1e293b);
}

/* The title is the one part worth sacrificing first on a narrow screen. */
#guestTerminalContainer .panel-header-title {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Buttons keep their size; the header as a whole is what must not overflow. */
#guestTerminalContainer .panel-header-actions {
    flex: 0 0 auto;
}

#guestTerminalContainer { container-type: inline-size; }
.terminal-heading {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
}

/*
 * Controls Wrapper. This is the flexible part of the header: when there are
 * more tabs than fit, the strip scrolls within itself rather than widening
 * the panel. min-width:0 is what actually allows it to shrink.
 */
#terminalControls {
    display: flex !important;
    align-items: center !important;
    align-self: center !important;
    height: 26px !important;
    margin-left: 10px;
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
}

#terminalControls::-webkit-scrollbar {
    display: none;
}

/*
 * getTabContainer() in guest_terminal.js sets flex-wrap:wrap inline. Combined
 * with the fixed 26px height that made a second tab wrap out of the strip and
 * overlap the terminal, so override it and scroll instead.
 */
#guestTerminalTabs {
    display: flex !important;
    align-items: center !important;
    justify-content: flex-start !important;
    flex-wrap: nowrap !important;
    gap: 6px;
    height: 26px !important;
    margin: 0 !important;
    padding: 0 !important;
}

#guestTerminalTabs button {
    flex: 0 0 auto;
}

/* --- Guest Terminal Tab Buttons --- */
#guestTerminalTabs button {
    height: 26px !important;
    min-height: 26px !important;
    max-height: 26px !important;
    padding: 0 12px !important;
    margin: 0 !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    line-height: 1 !important;
    vertical-align: middle !important;
    box-sizing: border-box !important;
    background: rgba(255, 255, 255, 0.15) !important;
    color: rgba(255, 255, 255, 0.8) !important;
    border: 1px solid rgba(255, 255, 255, 0.25) !important;
    border-radius: 4px !important;
    font-size: 12px !important;
    font-weight: 500 !important;
    cursor: pointer;
    transition: all 0.15s ease-in-out;
}

#guestTerminalTabs button:hover {
    background: rgba(255, 255, 255, 0.28) !important;
    color: #ffffff !important;
}

/* Active Tab: High-contrast solid white pill with accent underline */
#guestTerminalTabs button.active-tab-style,
#guestTerminalTabs button.active {
    background: #ffffff !important;
    color: #0f172a !important; /* High contrast dark text */
    font-weight: 700 !important;
    border: 1px solid #ffffff !important;
    border-bottom: 3px solid var(--tertiary-color, #38bdf8) !important;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3) !important;
}

/* --- Panel Header Action Buttons --- */
.panel-header-actions {
    display: flex !important;
    align-items: center !important;
    gap: 6px !important;
    height: 26px !important;
}

.panel-header .terminal-action-btn {
    height: 26px !important;
    min-height: 26px !important;
    max-height: 26px !important;
    padding: 0 8px !important;
    box-sizing: border-box !important;
    background: rgba(255, 255, 255, 0.12) !important;
    color: #ffffff !important;
    border: 1px solid rgba(255, 255, 255, 0.25) !important;
    border-radius: 4px !important;
    font-size: 12px !important;
    font-weight: 500 !important;
    cursor: pointer;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 5px !important;
    line-height: 1 !important;
    transition: background-color 0.15s ease-in-out, color 0.15s ease-in-out,
        border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.panel-header .terminal-action-btn:hover {
    filter: brightness(1.2);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25) !important;
}

.panel-header .terminal-action-btn:active {
    transform: translateY(1px);
}

.panel-header .terminal-action-btn i {
    font-size: 12px !important;
    line-height: 1 !important;
}

/* --- Specific Color Accents for Zoom Controls --- */

/* Zoom Out (Amber / Orange) */
.panel-header .btn-zoom-out {
    background: rgba(245, 158, 11, 0.25) !important;
    border-color: rgba(245, 158, 11, 0.5) !important;
    color: #fbbf24 !important;
}
.panel-header .btn-zoom-out:hover {
    background: rgba(245, 158, 11, 0.4) !important;
    color: #ffffff !important;
}

/* Zoom In (Emerald Green) */
.panel-header .btn-zoom-in {
    background: rgba(16, 185, 129, 0.25) !important;
    border-color: rgba(16, 185, 129, 0.5) !important;
    color: #34d399 !important;
}
.panel-header .btn-zoom-in:hover {
    background: rgba(16, 185, 129, 0.4) !important;
    color: #ffffff !important;
}

/* Reset Zoom (Cyan / Slate) */
.panel-header .btn-zoom-reset {
    background: rgba(14, 165, 233, 0.22) !important;
    border-color: rgba(14, 165, 233, 0.45) !important;
    color: #38bdf8 !important;
}
.panel-header .btn-zoom-reset:hover {
    background: rgba(14, 165, 233, 0.38) !important;
    color: #ffffff !important;
}

/*
 * Narrow screens: drop the button text labels and keep the icons. Five
 * labelled buttons plus the title and tab strip do not fit a phone, and the
 * overflow is what produced the pan-into-white-space. font-size:0 on the
 * button hides its text node while the icon keeps its own size -- this also
 * covers the "Copied" label copyTerminalContent() swaps in at runtime.
 */
@container (max-width: 700px) {
    #guestTerminalContainer .panel-header {
        padding: 0 6px !important;
        gap: 6px;
    }

    #guestTerminalContainer .panel-header-title span,
    #guestTerminalContainer .panel-header-title {
        font-size: 13px;
    }

    #guestTerminalContainer .panel-header .terminal-action-btn {
        font-size: 0 !important;
        gap: 0 !important;
        padding: 0 7px !important;
    }

    #guestTerminalContainer .panel-header .terminal-action-btn i {
        font-size: 13px !important;
    }
}

/*
 * Very narrow: the title is redundant next to the terminal icon and the tabs,
 * so give its space to the tab strip.
 */
@container (max-width: 420px) {
    #guestTerminalContainer .panel-header-title {
        display: none !important;
    }
}

/* The split view can be only 200px wide even on a desktop. Give its session
   picker a separate row instead of shrinking it behind the action buttons. */
@container (max-width: 260px) {
    #guestTerminalContainer .panel-header {
        height: auto !important;
        flex-basis: auto;
        flex-wrap: wrap;
        padding: 4px 6px !important;
    }
    #guestTerminalContainer .terminal-heading { flex-basis: 100%; }
    #guestTerminalContainer .panel-header-actions { margin-left: auto; }
    #guestTerminalTabSelect { width: 100%; }
}

/* ---------------------------------------------------------------
   Full-height custom scrollbar
   --------------------------------------------------------------- */

/*
 * Anchored to #guestTerminal, so it runs the full height of the terminal
 * area including the bottom reserve -- the whole point of replacing the
 * native one. Sits above the rows but is only interactive on the thumb
 * region, so it never eats a text selection.
 */
#guestTerminalScrollbar {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 10px;
    z-index: 5;
    opacity: 0;
    transition: opacity 0.2s ease;
    background: transparent;
    touch-action: none;
}

/* Visible while scrolling, hovering the terminal, or dragging. */
#guestTerminal:hover #guestTerminalScrollbar,
#guestTerminalScrollbar.is-active,
#guestTerminalScrollbar.is-dragging {
    opacity: 1;
}

#guestTerminalScrollbar.is-empty {
    opacity: 0 !important;
    pointer-events: none;
}

#guestTerminalScrollThumb {
    position: absolute;
    top: 0;
    right: 2px;
    width: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.28);
    transition: background 0.15s ease, width 0.15s ease;
    will-change: transform;
}

#guestTerminalScrollbar:hover #guestTerminalScrollThumb,
#guestTerminalScrollbar.is-dragging #guestTerminalScrollThumb {
    width: 8px;
    right: 1px;
    background: rgba(255, 255, 255, 0.5);
}

/* Coarse pointers get a wider hit area; there is no hover to reveal it. */
@media (pointer: coarse) {
    #guestTerminalScrollbar {
        width: 16px;
    }

    #guestTerminalScrollThumb {
        width: 5px;
        right: 3px;
    }
}

/* ---------------------------------------------------------------
   Session picker: tab strip on desktop, dropdown on mobile
   --------------------------------------------------------------- */

/*
 * Both controls are always populated by guest_terminal.js; which one shows is
 * purely this media query, so there is no JS branch on viewport width to get
 * out of step with the CSS.
 */
#guestTerminalTabSelect {
    display: none;
    height: 26px;
    max-width: 100%;
    min-width: 0;
    padding: 0 22px 0 8px;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 4px;
    background-color: rgba(255, 255, 255, 0.15);
    color: #ffffff;
    font-size: 12px;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;

    /* Native arrow varies wildly across mobile browsers; draw our own so the
       control matches the other header buttons. */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath fill='%23ffffff' d='M1 1.5 6 6.5l5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 7px center;
    background-size: 9px 6px;
}

/* The option list is rendered by the OS, which ignores the dark header. */
#guestTerminalTabSelect option {
    background-color: #1f2937;
    color: #ffffff;
}

@container (max-width: 700px) {
    /* Dropdown replaces the strip outright, rather than both competing for
       the header's width. */
    #guestTerminalTabs {
        display: none !important;
    }

    #guestTerminalTabSelect {
        display: block;
    }

    /* A single session is not a choice worth a control. */
    #guestTerminalTabSelect.is-single {
        display: none;
    }

    /* The strip was the flexible item; the dropdown takes that role. */
    #terminalControls {
        flex: 1 1 auto;
        margin-left: 0;
    }
}
