/**
 * Copyright (c) 2014 The xterm.js authors. All rights reserved.
 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
 * https://github.com/chjj/term.js
 * @license MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Originally forked from (with the author's permission):
 *   Fabrice Bellard's javascript vt100 for jslinux:
 *   http://bellard.org/jslinux/
 *   Copyright (c) 2011 Fabrice Bellard
 *   The original design remains. The terminal itself
 *   has been extended to include xterm CSI codes, among
 *   other features.
 */

/**
 *  Default styles for xterm.js
 */

.xterm {
    cursor: text;
    position: relative;
    user-select: none;
    -ms-user-select: none;
    -webkit-user-select: none;
}

.xterm.focus,
.xterm:focus {
    outline: none;
}

.xterm .xterm-helpers {
    position: absolute;
    top: 0;
    /**
     * The z-index of the helpers must be higher than the canvases in order for
     * IMEs to appear on top.
     */
    z-index: 5;
}

.xterm .xterm-helper-textarea {
    padding: 0;
    border: 0;
    margin: 0;
    /* Move textarea out of the screen to the far left, so that the cursor is not visible */
    position: absolute;
    opacity: 0;
    left: -9999em;
    top: 0;
    width: 0;
    height: 0;
    z-index: -5;
    /** Prevent wrapping so the IME appears against the textarea at the correct position */
    white-space: nowrap;
    overflow: hidden;
    resize: none;
}

.xterm .composition-view {
    /* TODO: Composition position got messed up somewhere */
    background: #000;
    color: #FFF;
    display: none;
    position: absolute;
    white-space: nowrap;
    z-index: 1;
}

.xterm .composition-view.active {
    display: block;
}

.xterm .xterm-viewport {
    /* On OS X this is required in order for the scroll bar to appear fully opaque */
    background-color: #000;
    overflow-y: scroll;
    cursor: default;
    position: absolute;
    right: 0;
    left: 0;
    top: 0;
    bottom: 0;
}

.xterm .xterm-screen {
    position: relative;
}

.xterm .xterm-screen canvas {
    position: absolute;
    left: 0;
    top: 0;
}

.xterm .xterm-scroll-area {
    visibility: hidden;
}

.xterm-char-measure-element {
    display: inline-block;
    visibility: hidden;
    position: absolute;
    top: 0;
    left: -9999em;
    line-height: normal;
}

.xterm.enable-mouse-events {
    /* When mouse events are enabled (eg. tmux), revert to the standard pointer cursor */
    cursor: default;
}

.xterm.xterm-cursor-pointer,
.xterm .xterm-cursor-pointer {
    cursor: pointer;
}

.xterm.column-select.focus {
    /* Column selection mode */
    cursor: crosshair;
}

.xterm .xterm-accessibility:not(.debug),
.xterm .xterm-message {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    z-index: 10;
    color: transparent;
    pointer-events: none;
}

.xterm .xterm-accessibility-tree:not(.debug) *::selection {
  color: transparent;
}

.xterm .xterm-accessibility-tree {
  user-select: text;
  white-space: pre;
}

.xterm .live-region {
    position: absolute;
    left: -9999px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

.xterm-dim {
    /* Dim should not apply to background, so the opacity of the foreground color is applied
     * explicitly in the generated class and reset to 1 here */
    opacity: 1 !important;
}

.xterm-underline-1 { text-decoration: underline; }
.xterm-underline-2 { text-decoration: double underline; }
.xterm-underline-3 { text-decoration: wavy underline; }
.xterm-underline-4 { text-decoration: dotted underline; }
.xterm-underline-5 { text-decoration: dashed underline; }

.xterm-overline {
    text-decoration: overline;
}

.xterm-overline.xterm-underline-1 { text-decoration: overline underline; }
.xterm-overline.xterm-underline-2 { text-decoration: overline double underline; }
.xterm-overline.xterm-underline-3 { text-decoration: overline wavy underline; }
.xterm-overline.xterm-underline-4 { text-decoration: overline dotted underline; }
.xterm-overline.xterm-underline-5 { text-decoration: overline dashed underline; }

.xterm-strikethrough {
    text-decoration: line-through;
}

.xterm-screen .xterm-decoration-container .xterm-decoration {
	z-index: 6;
	position: absolute;
}

.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer {
	z-index: 7;
}

.xterm-decoration-overview-ruler {
    z-index: 8;
    position: absolute;
    top: 0;
    right: 0;
    pointer-events: none;
}

.xterm-decoration-top {
    z-index: 2;
    position: relative;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    html, body { height: 100%; background: #1e1e1e; color: #ccc; overflow: hidden; }
    body { display: flex; flex-direction: column; height: 100dvh; font-family: -apple-system, BlinkMacSystemFont, sans-serif; }

    /* ── Splash (blocks all content until auth state is resolved) ── */
    #splash { position: fixed; inset: 0; background: #0d1117; z-index: 120; }

    /* ── Login overlay (Firebase) ────────────────────────────────── */
    #login-overlay {
      display: none; position: fixed; inset: 0;
      background: #0d1117; z-index: 110;
      align-items: center; justify-content: center; flex-direction: column; gap: 20px;
    }
    #login-overlay.visible { display: flex; }
    #login-overlay h2 { color: #e0e0e0; font-size: 22px; font-weight: 600; letter-spacing: 0.3px; }
    #login-overlay > p { color: #666; font-size: 13px; text-align: center; max-width: 300px; }
    .auth-btn {
      display: flex; align-items: center; gap: 10px;
      background: #161b22; color: #e0e0e0;
      border: 1px solid #30363d; border-radius: 8px;
      padding: 12px 24px; font-size: 14px; cursor: pointer;
      width: 260px; justify-content: center;
      -webkit-tap-highlight-color: transparent;
    }
    .auth-btn:hover { background: #21262d; border-color: #555; }
    .auth-btn:active { background: #2d333b; }
    .auth-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
    #auth-error { color: #f85149; font-size: 12px; min-height: 16px; text-align: center; max-width: 300px; }

    /* ── UID overlay ─────────────────────────────────────────────── */
    #uid-overlay {
      display: none; position: fixed; inset: 0;
      background: #0d1117ee; z-index: 100;
      align-items: center; justify-content: center; flex-direction: column; gap: 16px;
    }
    #uid-overlay.visible { display: flex; }
    #uid-overlay h2 { color: #e0e0e0; font-size: 18px; font-weight: 500; }
    #uid-overlay p  { color: #888; font-size: 13px; text-align: center; max-width: 340px; }
    #uid-input {
      width: 320px; background: #161b22; color: #e6edf3;
      border: 1px solid #30363d; border-radius: 6px;
      padding: 10px 14px; font-size: 14px; font-family: 'Courier New', monospace;
      letter-spacing: 0.5px;
    }
    #uid-input:focus { outline: 1px solid #1f6feb; }
    #uid-submit {
      background: #1f6feb; color: #fff; border: none; border-radius: 6px;
      padding: 10px 24px; font-size: 14px; cursor: pointer;
    }
    #uid-submit:hover { background: #388bfd; }
    #uid-error { color: #f85149; font-size: 12px; min-height: 16px; }

    /* Pairing overlay */
    #pair-overlay {
      display: none; position: fixed; inset: 0;
      background: #0d1117ee; z-index: 100;
      align-items: center; justify-content: center; flex-direction: column; gap: 16px;
    }
    #pair-overlay.visible { display: flex; }
    #pair-overlay h2 { color: #e0e0e0; font-size: 18px; font-weight: 500; }
    #pair-overlay p  { color: #888; font-size: 13px; text-align: center; max-width: 340px; }
    #pair-pin {
      color: #58a6ff; font-family: 'Courier New', monospace;
      font-size: 44px; font-weight: 700; letter-spacing: 12px; padding-left: 12px;
    }
    #pair-status { color: #888; font-size: 13px; min-height: 18px; text-align: center; }
    #pair-status.err { color: #f85149; }
    #pair-status.ok  { color: #3fb950; }
    .pair-btn {
      background: #21262d; color: #e6edf3; border: 1px solid #30363d; border-radius: 6px;
      padding: 8px 20px; font-size: 13px; cursor: pointer;
    }
    .pair-btn:hover { border-color: #58a6ff; }
    .pair-btn.hidden { display: none; }

    /* ── Top bar ─────────────────────────────────────────────────── */
    #topbar {
      flex-shrink: 0; display: flex; align-items: center; gap: 8px;
      padding: 6px 10px; background: #252526; border-bottom: 1px solid #3c3c3c;
      min-height: 38px; cursor: pointer; user-select: none;
    }
    #status-dot { width: 9px; height: 9px; border-radius: 50%; background: #555; flex-shrink: 0; transition: background .2s; }
    #status-dot.connected    { background: #4ec94e; }
    #status-dot.disconnected { background: #e05252; }
    #current-session-name {
      flex: 1; min-width: 0; font-size: 13px; color: #ccc;
      overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    }
    #dims { font-size: 11px; color: #555; flex-shrink: 0; white-space: nowrap; }
    #bell-btn {
      background: none; border: none; cursor: pointer; font-size: 16px; padding: 4px 6px;
      flex-shrink: 0; line-height: 1; -webkit-tap-highlight-color: transparent;
    }
    #topbar-chevron { font-size: 10px; color: #666; flex-shrink: 0; }
    #gear-btn {
      background: none; border: none; cursor: pointer; font-size: 16px; padding: 4px 6px;
      flex-shrink: 0; line-height: 1; -webkit-tap-highlight-color: transparent;
    }
    #session-strip {
      display: none; flex-shrink: 0; align-items: center; gap: 5px;
      overflow-x: auto; padding: 5px 8px; background: #161b22;
      border-bottom: 1px solid #30363d; scrollbar-width: none;
      -webkit-overflow-scrolling: touch;
    }
    #session-strip.visible { display: flex; }
    #session-strip::-webkit-scrollbar { display: none; }
    .session-chip {
      min-height: 34px; flex: 0 0 auto; padding: 5px 11px;
      border: 1px solid #30363d; border-radius: 11px;
      color: #c9d1d9; background: #21262d; cursor: pointer;
      font-size: 12px; white-space: nowrap;
    }
    .session-chip.current { color: #58a6ff; border-color: #1f6feb; background: #1f4268; }

    /* ── Settings / Recordings / Player overlays ─────────────────── */
    .sheet {
      display: none; position: fixed; inset: 0; z-index: 60;
      background: #161b22; flex-direction: column;
    }
    .sheet.open { display: flex; }
    .sheet-header {
      display: flex; align-items: center; justify-content: space-between;
      padding: 14px 16px; border-bottom: 1px solid #30363d;
      font-size: 16px; font-weight: 600; color: #e0e0e0; flex-shrink: 0;
    }
    .sheet-header button {
      background: none; border: none; color: #888; cursor: pointer;
      font-size: 20px; padding: 0 4px; line-height: 1;
    }
    .sheet-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 8px 0; }
    .set-row {
      display: flex; align-items: center; gap: 12px; justify-content: space-between;
      padding: 14px 16px; border-bottom: 1px solid #21262d;
    }
    .set-row .set-label { font-size: 14px; color: #e0e0e0; }
    .set-row .set-sub { font-size: 11px; color: #666; margin-top: 3px; }
    .set-section { padding: 14px 16px 4px; font-size: 11px; font-weight: 600; color: #4ec94e; text-transform: uppercase; letter-spacing: .5px; }
    .danger-btn { color: #f85149 !important; border-color: #6e2a2a !important; }
    /* toggle switch */
    .sw { position: relative; width: 42px; height: 24px; flex-shrink: 0; }
    .sw input { opacity: 0; width: 0; height: 0; }
    .sw .slider { position: absolute; inset: 0; background: #333; border-radius: 24px; transition: .2s; cursor: pointer; }
    .sw .slider::before { content: ""; position: absolute; height: 18px; width: 18px; left: 3px; top: 3px; background: #ccc; border-radius: 50%; transition: .2s; }
    .sw input:checked + .slider { background: #2ea043; }
    .sw input:checked + .slider::before { transform: translateX(18px); background: #fff; }
    .sw input:disabled + .slider { opacity: .5; cursor: not-allowed; }
    .rec-item {
      display: flex; align-items: center; gap: 10px;
      padding: 12px 16px; border-bottom: 1px solid #21262d;
    }
    .rec-item .rec-body { flex: 1; min-width: 0; }
    .rec-item .rec-name { font-size: 14px; color: #e0e0e0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .rec-item .rec-meta { font-size: 11px; color: #666; margin-top: 2px; }
    #rec-date { background: #1e1e1e; color: #ccc; border: 1px solid #3c3c3c; border-radius: 4px; padding: 4px 6px; font-size: 12px; }
    .rec-day {
      position: sticky; top: 0; z-index: 1;
      background: #161b22; color: #8b949e;
      font-size: 12px; font-weight: 600; letter-spacing: .02em; text-transform: uppercase;
      padding: 8px 16px; border-bottom: 1px solid #21262d;
    }
    .rec-empty { padding: 24px; color: #666; text-align: center; }
    .consent-box { padding: 16px; color: #bbb; font-size: 13px; line-height: 1.55; }
    .consent-box h3 { color: #e0e0e0; font-size: 15px; margin: 0 0 8px; }
    .consent-box ul { padding-left: 18px; margin: 8px 0; }
    .consent-box li { margin: 4px 0; }
    .consent-actions { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid #30363d; }
    #player-mount { flex: 1; min-height: 0; background: #000; }
    .mcp-box { padding: 18px 16px; color: #bbb; font-size: 13px; line-height: 1.5; }
    .mcp-box h3 { color: #e0e0e0; font-size: 16px; margin: 0 0 8px; }
    .mcp-warning { margin: 12px 0; padding: 12px; border: 1px solid #6a5200; border-radius: 6px; background: #3a2e00; color: #f0d98c; }
    .mcp-host { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid #21262d; }
    .mcp-host input { width: 18px; height: 18px; }
    .mcp-actions { display: flex; gap: 10px; padding: 12px 16px; border-top: 1px solid #30363d; }
    #mcp-status { min-height: 18px; color: #888; margin-top: 12px; }
    #mcp-status.err { color: #f85149; }
    #mcp-status.ok { color: #3fb950; }

    /* ── Session picker overlay ──────────────────────────────────── */
    #picker-overlay {
      display: none; position: fixed; inset: 0; z-index: 50;
      background: #161b22; flex-direction: column;
    }
    #picker-overlay.open { display: flex; }
    #picker-header {
      display: flex; align-items: center; justify-content: space-between;
      padding: 14px 16px; border-bottom: 1px solid #30363d;
      font-size: 16px; font-weight: 600; color: #e0e0e0; flex-shrink: 0;
    }
    #picker-actions { display: flex; align-items: center; gap: 8px; }
    #picker-actions .btn { min-height: 36px; }
    #picker-close {
      background: none; border: none; color: #888; cursor: pointer;
      font-size: 20px; padding: 0 4px; line-height: 1;
    }
    #picker-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
    .picker-item {
      display: flex; align-items: center; gap: 12px;
      padding: 14px 16px; border-bottom: 1px solid #21262d;
      cursor: pointer; -webkit-tap-highlight-color: transparent;
    }
    .picker-item:active { background: #21262d; }
    .picker-item-dot { width: 8px; height: 8px; border-radius: 50%; background: #555; flex-shrink: 0; transition: background .2s; }
    .picker-item-dot.active  { background: #4ec94e; }
    .picker-item-body { flex: 1; min-width: 0; }
    .picker-item-name { font-size: 15px; color: #e0e0e0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .picker-item-meta { font-size: 11px; color: #666; margin-top: 2px; }
    .picker-item-check { color: #4ec94e; font-size: 18px; flex-shrink: 0; }

    /* ── Notice ──────────────────────────────────────────────────── */
    #notice {
      flex-shrink: 0; display: none; align-items: center; justify-content: space-between;
      padding: 5px 10px; font-size: 12px; gap: 8px;
    }
    #notice.warn  { display: flex; background: #3a2e00; border-bottom: 1px solid #6a5200; }
    #notice.info  { display: flex; background: #00294a; border-bottom: 1px solid #0060a0; }
    #notice button { background: none; border: none; color: #888; cursor: pointer; font-size: 13px; flex-shrink: 0; }

    /* ── Info / readme screen ────────────────────────────────────── */
    #info-screen {
      flex: 1; display: none; flex-direction: column;
      align-items: center; justify-content: center;
      padding: 24px 20px; overflow-y: auto; gap: 18px;
      text-align: center;
    }
    #info-screen.visible { display: flex; }
    #info-title { font-size: 22px; color: #e0e0e0; font-weight: 600; letter-spacing: 0.3px; }
    #info-screen > p { font-size: 14px; color: #666; line-height: 1.5; }
    #info-start {
      background: #161b22; border: 1px solid #30363d; border-radius: 8px;
      padding: 12px 16px; text-align: left; width: 100%; max-width: 420px;
    }
    #info-start .info-label { font-size: 11px; color: #555; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 6px; }
    #info-start code {
      display: block; font-family: 'Courier New', monospace;
      font-size: 13px; color: #4ec94e; padding: 2px 0;
    }
    #info-shortcuts {
      display: flex; gap: 12px; flex-wrap: wrap;
      justify-content: center; width: 100%; max-width: 420px;
    }
    .sc-group {
      background: #161b22; border: 1px solid #30363d; border-radius: 8px;
      padding: 12px 14px; text-align: left; flex: 1; min-width: 160px;
    }
    .sc-header {
      font-size: 11px; font-weight: 600; color: #4ec94e;
      text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 8px;
    }
    .sc-row {
      display: flex; justify-content: space-between; align-items: baseline;
      font-size: 12px; padding: 2px 0; gap: 8px;
    }
    .sc-key { font-family: 'Courier New', monospace; color: #e0e0e0; white-space: nowrap; }
    .sc-desc { color: #555; }

    /* ── Terminal ────────────────────────────────────────────────── */
    #terminal-container { flex: 1; overflow: hidden; min-height: 0; }
    #terminal-container .xterm { height: 100%; width: 100%; padding: 0 !important; }
    #terminal-container .xterm-screen { text-align: left; }

    /* ── Action bar ──────────────────────────────────────────────── */
    #actionbar {
      flex-shrink: 0; display: flex; flex-direction: column;
      background: #252526; border-top: 1px solid #3c3c3c;
    }
    #keyhelper {
      display: flex; align-items: center; gap: 5px;
      padding: 6px 8px 4px; flex-wrap: wrap;
      scrollbar-width: none; -webkit-overflow-scrolling: touch;
    }
    #keyhelper::-webkit-scrollbar { display: none; }
    #input-row {
      display: flex; gap: 5px; padding: 4px 8px 6px;
    }
    .btn {
      background: #2d2d2d; color: #ccc; border: 1px solid #3c3c3c; border-radius: 5px;
      padding: 6px 10px; font-size: 13px; cursor: pointer; white-space: nowrap; flex-shrink: 0;
      -webkit-tap-highlight-color: transparent; touch-action: manipulation;
    }
    .btn:active { background: #444; }
    .btn-nav { min-width: 44px; min-height: 44px; font-size: 20px; padding: 0 8px; }
    .btn-mux { font-size: 11px; padding: 6px 8px; color: #aaa; border-color: #333; }
    .mode-badge { font-size: 10px; padding: 1px 5px; border-radius: 3px; font-weight: 600; margin-left: 4px; }
    .mode-readonly  { background: #5a1a1a; color: #f87171; }
    .mode-approved  { background: #1a3a1a; color: #4ade80; }
    #custom-input {
      flex: 1; min-width: 0; background: #1e1e1e; color: #ccc;
      border: 1px solid #3c3c3c; border-radius: 4px; padding: 5px 8px; font-size: 13px;
    }
    #custom-input:focus { outline: 1px solid #007acc; }

    /* ── Portrait: wrap buttons into multiple rows ───────────────── */
    @media (orientation: portrait), (max-width: 600px) {
      #dims { display: none; }
    }

    /* ── Navigation floating island (portrait only) ──────────────── */
    #nav-island {
      display: none;
      position: fixed; right: 10px; top: 50%; transform: translateY(-50%);
      z-index: 30;
      flex-direction: column; gap: 4px;
      background: rgba(37, 37, 38, 0.88);
      backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
      border: 1px solid rgba(100, 100, 100, 0.3);
      border-radius: 18px; padding: 8px 5px;
      box-shadow: 0 4px 24px rgba(0, 0, 0, 0.55), 0 1px 0 rgba(255,255,255,0.05) inset;
    }
    @media (orientation: portrait), (max-width: 600px) {
      #nav-island.active { display: flex; }
    }
    .nav-island-btn {
      background: transparent; border: none; border-radius: 11px;
      width: 48px; height: 48px; font-size: 22px; color: #ccc;
      cursor: pointer; display: flex; align-items: center; justify-content: center;
      -webkit-tap-highlight-color: transparent; touch-action: manipulation; flex-shrink: 0;
    }
    .nav-island-btn:active { background: rgba(255, 255, 255, 0.13); }
    .nav-island-sep {
      margin: 4px 10px; border-top: 1px solid rgba(255, 255, 255, 0.1);
    }
    .nav-island-mux { font-size: 11px; color: #999; letter-spacing: 0.3px; }
