:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --border: #dde2ea;
  --text: #1c2330;
  --muted: #5f6b7d;
  --accent: #2671d9;
  --accent-hover: #1d5fbd;
  --accent-text: #ffffff;
  --danger: #d23b3b;
  --ok: #1f9d55;
  --warn: #c98a00;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.06);
  --terminal-bg: #fbfcfe;
  --terminal-text: #1f2937;
  --terminal-err: #c62828;
  --font-ui: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Cascadia Code', Consolas, monospace;

  --ansi-0: #1f2937; --ansi-1: #c62828; --ansi-2: #2e7d32; --ansi-3: #a16207;
  --ansi-4: #1d4ed8; --ansi-5: #a21caf; --ansi-6: #0e7490; --ansi-7: #6b7280;
  --ansi-8: #4b5563; --ansi-9: #dc2626; --ansi-10: #16a34a; --ansi-11: #b45309;
  --ansi-12: #2563eb; --ansi-13: #c026d3; --ansi-14: #0891b2; --ansi-15: #111827;
  --ansi-fg: var(--terminal-text); --ansi-bg: var(--terminal-bg);
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0d1117;
  --surface: #141a24;
  --surface-2: #1b2230;
  --border: #263042;
  --text: #e2e8f0;
  --muted: #8b97ab;
  --accent: #3b8cff;
  --accent-hover: #5ea0ff;
  --accent-text: #ffffff;
  --danger: #ff6b6b;
  --ok: #4ade80;
  --warn: #fbbf24;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.25);
  --terminal-bg: #012456;
  --terminal-text: #eef3fb;
  --terminal-err: #ff8080;

  --ansi-0: #0c0c0c; --ansi-1: #ff5f5f; --ansi-2: #5fd75f; --ansi-3: #f2d36b;
  --ansi-4: #6b9bff; --ansi-5: #d77fd7; --ansi-6: #5fd7d7; --ansi-7: #cccccc;
  --ansi-8: #8a8a8a; --ansi-9: #ff8787; --ansi-10: #87ff87; --ansi-11: #ffff87;
  --ansi-12: #8fb3ff; --ansi-13: #ff87ff; --ansi-14: #87ffff; --ansi-15: #ffffff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Layout ---------- */
body.app { display: grid; grid-template-rows: auto 1fr auto; overflow: hidden; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }

.logo {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #012456, #2671d9);
  color: #fff;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
  box-shadow: 0 4px 14px rgba(38, 113, 217, 0.35);
}
.logo.big { width: 56px; height: 56px; font-size: 22px; border-radius: 14px; }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.divider { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

.workspace {
  --split: 58%;
  display: grid;
  grid-template-columns: minmax(0, var(--split)) 6px minmax(0, 1fr);
  gap: 0;
  padding: 12px;
  min-height: 0;
}
.workspace.resizing { cursor: col-resize; user-select: none; }

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 42px;
  padding: 0 10px 0 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  flex-shrink: 0;
}

.file-tab { display: inline-flex; align-items: center; gap: 8px; font-family: var(--font-mono); font-size: 12.5px; font-weight: 500; }
.file-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.hint { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.editor { flex: 1; min-height: 0; }
.editor-error { padding: 24px; color: var(--danger); }

.splitter { cursor: col-resize; position: relative; }
.splitter::after {
  content: '';
  position: absolute;
  inset: 40% 2px;
  border-radius: 2px;
  background: var(--border);
  transition: background 0.15s;
}
.splitter:hover::after, .splitter:focus-visible::after, .resizing .splitter::after { background: var(--accent); }
.splitter:focus-visible { outline: none; }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-weight: 500;
  font-size: 13.5px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .select:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { height: 28px; padding: 0 10px; font-size: 12.5px; }
.btn.icon { width: 34px; padding: 0; }
.btn svg { width: 16px; height: 16px; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  min-width: 92px;
}
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.primary svg { fill: currentColor; }
.btn.primary .stop-icon { display: none; }
.btn.primary.running { background: var(--danger); border-color: var(--danger); }
.btn.primary.running .run-icon { display: none; }
.btn.primary.running .stop-icon { display: block; }

#btn-theme svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
:root[data-theme='dark'] #btn-theme .icon-moon { display: none; }
:root:not([data-theme='dark']) #btn-theme .icon-sun { display: none; }

.select {
  height: 34px;
  padding: 0 30px 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M7 10l5 5 5-5' fill='none' stroke='%238b97ab' stroke-width='2'/%3E%3C/svg%3E") no-repeat right 8px center / 16px;
  appearance: none;
  cursor: pointer;
  max-width: 200px;
}

.tabs { display: flex; gap: 2px; height: 100%; }
.tab {
  border: 0;
  background: transparent;
  padding: 0 12px;
  height: 100%;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:disabled:active { transform: none; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { background: color-mix(in srgb, var(--danger) 85%, #000); border-color: transparent; }
.btn.danger-hover:not(:disabled):hover { color: var(--danger); }

svg.i {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}
svg.i.caret { width: 14px; height: 14px; margin-left: -2px; opacity: 0.7; }

/* ---------- Script title, lock & save ---------- */
.script-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.file-tab {
  border: 0;
  background: transparent;
  padding: 4px 6px;
  margin-left: -6px;
  border-radius: 6px;
  cursor: pointer;
  min-width: 0;
}
.file-tab:hover { background: var(--surface); }
.file-tab:focus-visible { outline: 2px solid var(--accent); }
#script-name-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 32ch; }
.file-tab .pencil { width: 13px; height: 13px; opacity: 0; transition: opacity 0.15s; }
.file-tab:hover .pencil, .file-tab:focus-visible .pencil { opacity: 0.6; }
.dirty-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); flex-shrink: 0; }

.lock-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px 2px 7px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 14%, transparent);
}
.lock-badge svg.i { width: 13px; height: 13px; }

.pane-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
#btn-lock .when-locked { display: none; }
#btn-lock.active { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
#btn-lock.active .when-locked { display: block; }
#btn-lock.active .when-unlocked { display: none; }
#btn-save { margin-left: 4px; }

/* ---------- My scripts popover ---------- */
.popover-wrap { position: relative; }
.popover {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 50;
  width: min(360px, calc(100vw - 32px));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}
.popover-head { padding: 10px; border-bottom: 1px solid var(--border); }
.popover-head input {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}
.popover-head input:focus { border-color: var(--accent); }
.script-list { list-style: none; margin: 0; padding: 6px; max-height: min(420px, 60vh); overflow: auto; }
.script-row { display: flex; align-items: center; gap: 2px; border-radius: 8px; }
.script-row:hover, .script-row:focus-within { background: var(--surface-2); }
.script-row.current { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.script-item {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 8px 8px;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
  border-radius: 8px;
}
.script-item:focus-visible { outline: 2px solid var(--accent); }
.script-lock { width: 16px; display: inline-flex; color: var(--warn); }
.script-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.script-meta { color: var(--muted); font-size: 12px; white-space: nowrap; }
.script-empty { padding: 18px 12px; text-align: center; color: var(--muted); line-height: 1.6; }

/* ---------- Modal ---------- */
.modal {
  width: min(420px, calc(100vw - 32px));
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.modal::backdrop { background: rgba(5, 10, 20, 0.55); backdrop-filter: blur(2px); }
.modal h2 { margin: 0 0 6px; font-size: 17px; }
.modal p { margin: 0 0 14px; color: var(--muted); line-height: 1.5; }
.modal input {
  width: 100%;
  height: 38px;
  padding: 0 12px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}
.modal input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.modal p.modal-error { margin: -8px 0 14px; color: var(--danger); font-size: 13px; font-weight: 500; }
/* OK comes first in the DOM so Enter submits it; show it on the right. */
.modal-actions { display: flex; flex-direction: row-reverse; gap: 8px; }

/* ---------- Output ---------- */
.terminal, .stdin {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 14px 16px;
  overflow: auto;
  background: var(--terminal-bg);
  color: var(--terminal-text);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  border: 0;
}
.terminal { white-space: pre-wrap; word-break: break-word; tab-size: 4; }
.terminal[hidden], .stdin[hidden] { display: none; }
.stdin { resize: none; outline: none; white-space: pre; }
.stdin::placeholder { color: var(--muted); }

.placeholder { color: var(--muted); font-family: var(--font-ui); }
.placeholder kbd {
  font-family: var(--font-mono);
  font-size: 11.5px;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
}

.stream-err { color: var(--terminal-err); }
.sys { font-family: var(--font-mono); }
.sys-prompt { color: var(--ansi-11); font-weight: 700; }
:root:not([data-theme='dark']) .sys-prompt { color: var(--accent); }
.sys-info { color: var(--muted); }
.sys-ok { color: var(--ok); font-weight: 600; }
.sys-err { color: var(--terminal-err); font-weight: 600; }
.sys-warn { color: var(--warn); font-weight: 600; }
.ansi-bold { font-weight: 700; }
.ansi-dim { opacity: 0.7; }
.ansi-italic { font-style: italic; }
.ansi-underline { text-decoration: underline; }
.line-link { color: inherit; text-decoration: underline dotted; text-underline-offset: 3px; }
.line-link:hover { text-decoration-style: solid; }

/* ---------- Status bar ---------- */
.statusbar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 28px;
  padding: 0 16px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
}
.statusbar .spacer { flex: 1; }
#status-text { color: var(--text); overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; margin-right: -8px; }
.dot[data-state='ok'] { background: var(--ok); }
.dot[data-state='error'] { background: var(--danger); }
.dot[data-state='busy'] { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.problems[data-state='error'] { color: var(--danger); }
.problems[data-state='warn'] { color: var(--warn); }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- Login ---------- */
body.login {
  display: grid;
  place-items: center;
  padding: 16px;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(38, 113, 217, 0.18), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, rgba(1, 36, 86, 0.35), transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 400px;
  padding: 36px 32px 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card h1 { margin: 18px 0 4px; font-size: 21px; letter-spacing: -0.01em; }
.muted { color: var(--muted); margin: 0 0 22px; }
.login-error { margin: -8px 0 16px; padding: 10px 12px; border-radius: 8px; color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); font-size: 13px; }
.login-form { display: grid; gap: 14px; text-align: left; }
.login-form label { display: grid; gap: 6px; font-size: 13px; font-weight: 500; }
.login-form input {
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}
.login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.login-form .btn { height: 40px; margin-top: 6px; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  body.app { overflow: auto; height: auto; min-height: 100%; }
  .topbar { padding: 10px 16px; }
  .subtitle, .divider, .lock-badge { display: none !important; }
  #script-name-text { max-width: 14ch; }
  .toolbar { width: 100%; }
  .toolbar .select { flex: 1; max-width: none; }
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-rows: 60vh 12px 40vh; padding: 10px 16px; }
  .splitter { display: none; }
  .output-pane { grid-row: 3; }
  .statusbar #cursor-pos ~ span:not(#ps-version) { display: none; }
}
