/**
 * 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-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 {
  font-family: monospace;
  user-select: text;
  white-space: pre;
}

.xterm .xterm-accessibility-tree > div {
  transform-origin: left;
  width: fit-content;
}

.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;
}



/* Derived from vs/base/browser/ui/scrollbar/media/scrollbar.css */

/* xterm.js customization: Override xterm's cursor style */
.xterm .xterm-scrollable-element > .scrollbar {
    cursor: default;
}

/* Arrows */
.xterm .xterm-scrollable-element > .scrollbar > .scra {
	cursor: pointer;
	font-size: 11px !important;
}

.xterm .xterm-scrollable-element > .visible {
	opacity: 1;

	/* Background rule added for IE9 - to allow clicks on dom node */
	background:rgba(0,0,0,0);

	transition: opacity 100ms linear;
	/* In front of peek view */
	z-index: 11;
}
.xterm .xterm-scrollable-element > .invisible {
	opacity: 0;
	pointer-events: none;
}
.xterm .xterm-scrollable-element > .invisible.fade {
	transition: opacity 800ms linear;
}

/* Scrollable Content Inset Shadow */
.xterm .xterm-scrollable-element > .shadow {
	position: absolute;
	display: none;
}
.xterm .xterm-scrollable-element > .shadow.top {
	display: block;
	top: 0;
	left: 3px;
	height: 3px;
	width: 100%;
	box-shadow: var(--vscode-scrollbar-shadow, #000) 0 6px 6px -6px inset;
}
.xterm .xterm-scrollable-element > .shadow.left {
	display: block;
	top: 3px;
	left: 0;
	height: 100%;
	width: 3px;
	box-shadow: var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset;
}
.xterm .xterm-scrollable-element > .shadow.top-left-corner {
	display: block;
	top: 0;
	left: 0;
	height: 3px;
	width: 3px;
}
.xterm .xterm-scrollable-element > .shadow.top.left {
	box-shadow: var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset;
}

:root, [data-theme="dark"] {
  --bg: #101418;
  --panel: #171c22;
  --panel-2: #1e242c;
  --line: #2a323c;
  --text: #e6e6e6;
  --muted: #9aa4b0;
  --accent: #5fb0ff;
  --danger: #ff6b6b;
  --idle: #8b96a3;
  --working: #5fb0ff;
  --blocked: #ffab4a;
  --done: #58d68d;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f2f4f7;
  --panel: #ffffff;
  --panel-2: #eceff3;
  --line: #ccd4de;
  --text: #1a2029;
  --muted: #5b6675;
  --accent: #1668d8;
  --danger: #c92a2a;
  --idle: #5b6675;
  --working: #1668d8;
  --blocked: #9a6100;
  --done: #1d7a4c;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, sans-serif;
  overscroll-behavior: none;
}

#app { display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 16px; margin: 0 auto 0 0; }
.term-title { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; justify-content: flex-end; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 36px; padding: 6px 12px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  font-size: 13px; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); color: #08131f; border-color: transparent; font-weight: 600; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn:active { filter: brightness(1.2); }

.conn { min-height: 0; max-height: 0; overflow: hidden; font-size: 12px; color: var(--blocked); background: var(--panel); transition: max-height 0.2s; }
.conn.bad { min-height: 22px; max-height: 22px; padding: 2px 12px; }

.list { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }

.card {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px;
  text-decoration: none; color: var(--text);
}
.card-main { min-width: 0; margin-right: auto; }
.card-name { font-weight: 650; font-size: 15px; }
.card-where { font-size: 12px; color: var(--muted); }
.card-title { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 60vw; }

.badge { font-size: 11px; padding: 3px 8px; border-radius: 999px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; flex: none; }
:root, [data-theme="dark"] .badge-idle { background: #263040; color: var(--idle); }
:root, [data-theme="dark"] .badge-working { background: #14304a; color: var(--working); }
:root, [data-theme="dark"] .badge-blocked { background: #3d2c14; color: var(--blocked); }
:root, [data-theme="dark"] .badge-done { background: #133a28; color: var(--done); }
:root, [data-theme="dark"] .badge-unknown { background: #262a30; color: var(--idle); }
:root, [data-theme="dark"] .badge-shell { background: #1c222a; color: var(--muted); }

[data-theme="light"] .badge-idle { background: #e4e8ee; color: var(--idle); }
[data-theme="light"] .badge-working { background: #dbeafe; color: var(--working); }
[data-theme="light"] .badge-blocked { background: #fdf0d5; color: var(--blocked); }
[data-theme="light"] .badge-done { background: #d9f2e3; color: var(--done); }
[data-theme="light"] .badge-unknown { background: #eceff3; color: var(--idle); }
[data-theme="light"] .badge-shell { background: #e9edf2; color: var(--muted); }

.card.shell { opacity: 0.72; }
.card.shell .card-name { font-weight: 500; }

.empty { padding: 20px; text-align: center; color: var(--muted); }

.manage { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 12px; }
.ws-block, .tab-block { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.ws-block { display: flex; flex-direction: column; gap: 8px; }
.ws-head, .tab-head { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.pane-actions { display: flex; gap: 6px; margin-left: auto; }
.pane-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.pane-row { display: flex; align-items: center; gap: 8px; font-size: 12px; background: var(--panel-2); border-radius: 8px; padding: 6px 8px; }
.pane-id { color: var(--accent); font-family: ui-monospace, monospace; flex: none; }
.pane-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }

.term-holder { flex: 1; min-height: 0; padding: 4px; overflow: auto; }
.term-holder .xterm { height: fit-content; }
.xterm-viewport { width: auto !important; }

.term-status { font-size: 12px; color: var(--blocked); padding: 2px 10px; min-height: 16px; }

.promptbar { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; background: var(--panel); border-top: 1px solid var(--line); }
.promptbar input, .promptbar textarea {
  flex: 1 1 120px; min-height: 38px; max-height: 140px; padding: 4px 10px;
  border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font-size: 14px; font-family: inherit;
  resize: none;
}
.promptbar textarea { line-height: 1.3; }
.btn.mini { min-height: 34px; padding: 4px 8px; font-size: 12px; }
#prompt-btn { min-width: 74px; flex: none; }
#prompt-btn[data-mode="stop"] { background: var(--danger); color: #fff; border-color: transparent; }
#prompt-btn[data-mode="queue"] { background: var(--blocked); color: #241a05; border-color: transparent; }

.sheet-box { align-items: stretch; width: min(360px, 92vw); max-height: 70vh; text-align: left; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-weight: 650; }
.sheet-body { display: flex; flex-direction: column; gap: 6px; overflow-y: auto; }
.sheet-row { justify-content: flex-start; text-align: left; font-size: 13px; }
.sheet-body input {
  min-height: 38px; padding: 4px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font-size: 14px;
}
.model-list { display: flex; flex-direction: column; gap: 6px; overflow-y: auto; max-height: 46vh; }

.overlay { position: fixed; inset: 0; background: rgba(6,9,12,0.8); display: flex; align-items: center; justify-content: center; z-index: 10; }
.overlay.hidden { display: none; }
.overlay-box { background: var(--panel); border: 1px solid var(--line); padding: 20px; border-radius: 12px; text-align: center; display: flex; flex-direction: column; gap: 12px; }

.login { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); z-index: 20; }
.login.hidden { display: none; }
.login-box { background: var(--panel); border: 1px solid var(--line); padding: 24px; border-radius: 14px; display: flex; flex-direction: column; gap: 12px; width: min(340px, 88vw); }
.login-box h1 { margin: 0; font-size: 18px; }
.login-box p { margin: 0; color: var(--muted); font-size: 13px; }
.login-box input { min-height: 40px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text); }
.login-err { min-height: 16px; font-size: 12px; color: var(--danger); }

.toast {
  position: fixed; left: 10px; right: 10px; top: max(10px, env(safe-area-inset-top));
  background: var(--blocked); color: #241a05; font-weight: 600;
  padding: 10px 14px; border-radius: 10px; z-index: 30;
  box-shadow: 0 6px 24px rgba(0,0,0,0.4);
}
.sheet-warn {
  padding: 8px 10px; border-radius: 8px; font-size: 12.5px; line-height: 1.35;
  background: rgba(220, 80, 80, 0.14); border: 1px solid rgba(220, 80, 80, 0.45);
  color: var(--danger, #d45050);
}
