/* ── Palantir/Blueprint Dark Theme ──
 * 4-layer elevation system:
 *   L0 --bg         terminal content well (deepest)
 *   L1 --surface    structural frame (header, tab bar)
 *   L2 --dock       action dock (qbar, input bar)
 *   L3 --surface-alt interactive elements (buttons, pill, inputs)
 *
 * Colorblind-safe: status uses shape+color, attention uses icon+color,
 * intent colors chosen for deuteranopia/protanopia distinguishability.
 * All text meets WCAG AA (4.5:1), UI elements meet 3:1.
 */
:root {
  --bg:            #111418;
  --surface:       #1c2127;
  --dock:          #181d23;
  --surface-alt:   #252a31;
  --surface-hover: #2f343c;
  --border:        #404854;
  --border-muted:  #313840;
  --text:          #f6f7f9;
  --text-sec:      #abb3bf;
  --dim:           #738091;
  --accent:        #2d72d2;
  --accent-light:  #4c90f0;
  --accent-muted:  #184a90;
  --intent-ok:     #238551;
  --intent-danger: #cd4246;
  --intent-warn:   #c87619;
  --cerulean:      #147eb3;
  --radius:        4px;
  --shadow-inset:  inset 0 0 0 1px rgba(255,255,255,0.05);
  --shadow-1:      inset 0 0 0 1px rgba(255,255,255,0.05), 0 1px 2px rgba(0,0,0,0.4);
  --transition:    cubic-bezier(0.4, 1, 0.75, 0.9);
  --term-bg:       #111418;
  --term-fg:       #f6f7f9;
  --term-cursor:   #2d72d2;
}
/* ── Light Mode ── */
.light {
  --bg:            #f6f7f9;
  --surface:       #ffffff;
  --dock:          #eef0f3;
  --surface-alt:   #e5e8eb;
  --surface-hover: #dce0e5;
  --border:        #c5cbd3;
  --border-muted:  #d8dce2;
  --text:          #1c2127;
  --text-sec:      #5c7080;
  --dim:           #8a9ba8;
  --accent:        #2d72d2;
  --accent-light:  #4c90f0;
  --accent-muted:  rgba(45,114,210,0.12);
  --intent-ok:     #238551;
  --intent-danger: #cd4246;
  --intent-warn:   #c87619;
  --cerulean:      #147eb3;
  --shadow-inset:  inset 0 0 0 1px rgba(17,20,24,0.1);
  --shadow-1:      inset 0 0 0 1px rgba(17,20,24,0.12), 0 1px 1px rgba(17,20,24,0.08);
  --term-bg:       #f6f7f9;
  --term-fg:       #1c2127;
  --term-cursor:   #2d72d2;
}
/* Light mode element overrides */
.light #msg { background: var(--surface); }
.light .totp-input { background: var(--surface); }
.light .qb { background: var(--surface); }
.light #send { color: #fff; background: linear-gradient(180deg, #4c90f0 0%, #2d72d2 100%); }
.light #send:active { background: var(--accent); }
.light #tab-pill { background: var(--surface); }
.light .scroll-divider { opacity: 0; }
.light #term-area { border-top: 1px solid var(--border); }
.light #diag { background: rgba(45,114,210,0.06); color: var(--accent); }
.light .switcher-item:active { background: var(--accent-muted); }
.light #autocomplete { box-shadow: 0 -4px 12px rgba(17,20,24,0.08); }
.light #tab-switcher { box-shadow: 0 -8px 24px rgba(17,20,24,0.08); }
.light .auth-primary { background: linear-gradient(180deg, #32a467 0%, #238551 100%); }
.light .auth-btn { background: linear-gradient(180deg, #4c90f0 0%, #2d72d2 100%); }
.light #img-btn { background: var(--surface); }
.light .auth-card { border-color: var(--border-muted); }

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px; line-height: 1.286;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#app { display: none; flex-direction: column; height: 100dvh; position: relative; overflow: hidden; }

/* ── Auth Screen (login + lock modes) ── */
#auth-screen {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; height: 100dvh; gap: 20px;
  animation: fadeIn 0.3s var(--transition);
}
#auth-screen.active {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--bg); height: auto;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
#auth-screen h1 {
  font-size: 15px; font-weight: 600; letter-spacing: 3px;
  color: var(--dim); text-transform: uppercase;
}
#auth-screen .lock-icon {
  font-size: 32px; color: var(--intent-warn); margin-bottom: 8px;
}
#auth-screen .sub { color: var(--dim); font-size: 13px; text-align: center; max-width: 280px; }
.auth-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px;
  box-shadow: var(--shadow-1);
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  width: 300px;
}
.auth-primary {
  display: none; width: 100%; padding: 12px; font-size: 14px; font-weight: 600;
  background: var(--intent-ok); color: #fff; border: none;
  border-radius: var(--radius); cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.1), 0 1px 1px rgba(0,0,0,0.2);
  transition: all 0.1s var(--transition);
}
.auth-primary:active { transform: scale(0.98); opacity: 0.9; }
#totp-section { display: none; text-align: center; width: 100%; }
#totp-section .sub { margin-bottom: 12px; font-size: 12px; }
.totp-input {
  width: 100%; padding: 12px; text-align: center; font-size: 20px;
  letter-spacing: 8px; background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); outline: none;
  box-shadow: inset 0 1px 1px rgba(0,0,0,0.3);
}
.totp-input:focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), inset 0 1px 1px rgba(0,0,0,0.2); }
.auth-btn {
  width: 100%; padding: 10px; margin-top: 8px; font-size: 14px; font-weight: 600;
  background: var(--accent); color: #fff; border: none;
  border-radius: var(--radius); cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.1), 0 1px 1px rgba(0,0,0,0.2);
  transition: all 0.1s var(--transition);
}
.auth-btn:active { background: var(--accent-muted); transform: scale(0.98); }
#auth-error { color: var(--intent-danger); font-size: 12px; display: none; font-weight: 500; }
#auth-status { color: var(--dim); font-size: 12px; display: none; }
#setup-msg { display: none; text-align: center; }
#setup-msg .sub { color: var(--intent-warn); font-size: 12px; }

/* ── Header (Blueprint Toolbar) ── */
#header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-inset);
  padding-top: max(6px, env(safe-area-inset-top));
  gap: 8px;
}
.sess-name {
  font-size: 16px; font-weight: 600;
  background: none; border: none; border-bottom: 1px dashed transparent;
  color: var(--text); outline: none;
  width: auto; max-width: 180px; min-width: 60px;
  font-family: 'SF Mono', 'Menlo', monospace;
  padding: 2px 0; text-transform: uppercase;
}
.sess-name:focus { border-bottom-color: var(--accent); }
.sess-dir {
  font-size: 11px; color: var(--dim); margin-top: 1px;
  font-family: 'SF Mono', 'Menlo', monospace;
}
/* ── Theme Toggle (bottom-left) ── */
#theme-toggle {
  flex-shrink: 0; width: 34px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; color: var(--text-sec);
  cursor: pointer; transition: color 0.15s var(--transition);
  padding: 0; font-size: 16px;
}
#theme-toggle:active { color: var(--accent-light); }
/* ── Notification pulse on pill ── */
#tab-pill .pill-notify {
  display: none; width: 8px; height: 8px; border-radius: 50%;
  background: var(--intent-danger); flex-shrink: 0;
  box-shadow: 0 0 0 2px var(--surface);
  animation: pulse 1.5s infinite;
}
#tab-pill.needs-input .pill-notify { display: block; }
/* Status dot: shape+color for colorblind safety
   connected = filled circle, disconnected = hollow ring */
.dot {
  width: 6px; height: 6px; border-radius: 50%;
  display: inline-block; margin-right: 6px; flex-shrink: 0;
}
.dot.on { background: var(--intent-ok); }
.dot.off { background: none; border: 1.5px solid var(--intent-danger); }

/* ── Terminal ── */
#term-area {
  flex: 1; min-height: 0; overflow: hidden; position: relative;
  background: var(--bg);
}
#scroll-zones {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  z-index: 10; display: none; pointer-events: none;
}
#term-area:has(.term-wrap.active) #scroll-zones { display: block; pointer-events: auto; }
.scroll-zone { position: absolute; top: 0; bottom: 0; }
/* Left/right split -- consistent at all viewport heights */
.scroll-zone-up { left: 0; width: 50%; }
.scroll-zone-down { right: 0; width: 50%; }
.scroll-divider {
  position: absolute; top: 16px; bottom: 16px; left: 50%;
  width: 1px; background: var(--border); opacity: 0.3;
  pointer-events: none;
}
.scroll-flash {
  position: absolute; top: 0; bottom: 0; width: 6px;
  background: var(--accent); opacity: 0;
  transition: opacity 0.15s var(--transition);
}
.scroll-zone-up .scroll-flash { left: 0; }
.scroll-zone-down .scroll-flash { right: 0; }
/* Arrow icon overlay -- fades in on touch, centered in zone */
.scroll-icon {
  position: absolute; bottom: 8px;
  font-size: 16px; color: var(--dim); opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s var(--transition);
}
.scroll-zone-up .scroll-icon { left: 12px; }
.scroll-zone-down .scroll-icon { right: 12px; }
.scroll-zone:active .scroll-icon { opacity: 0.6; }
.term-wrap {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  display: none; overflow: hidden;
  will-change: transform;
}
.term-wrap.active { display: block; }
.term-wrap.swipe-visible { display: block; }
.term-wrap.swipe-animate {
  transition: transform 0.28s cubic-bezier(0.25, 1, 0.5, 1);
}
.term-wrap .xterm { height: 100% !important; }
.term-wrap .xterm-viewport {
  overflow-y: scroll !important;
  -webkit-overflow-scrolling: touch !important;
  scrollbar-width: none;
}
.term-wrap .xterm-viewport::-webkit-scrollbar { display: none; }

/* T19 of W5 (render-pipeline): grid renderer styles. Mirrors the
   font/colour shape of xterm so the two modes look the same on screen. */
.grid-term {
  width: 100%; height: 100%;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  font-family: 'SF Mono', 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.286;
  white-space: pre;
  background: var(--bg, #111418);
  color: var(--fg, #f6f7f9);
  padding: 0;
  box-sizing: border-box;
}
.grid-term::-webkit-scrollbar { display: none; }
.grid-row {
  display: block;
  white-space: pre;
  min-height: 1.286em;
}
.grid-row .reverse {
  filter: invert(1);
}
#empty-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; height: 100%; gap: 12px;
  color: var(--dim); font-size: 13px;
}
#empty-state .empty-icon {
  width: 40px; height: 40px; border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; align-items: center;
  justify-content: center; font-size: 20px; color: var(--dim);
  box-shadow: var(--shadow-inset);
}

/* ── Diagnostics ── */
#diag {
  display: none; padding: 4px 12px;
  background: rgba(45,114,210,0.15); border-top: 1px solid var(--accent);
  font-size: 11px; color: var(--accent-light);
  font-family: 'SF Mono', 'Menlo', monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#diag.show { display: block; }

/* ── Quick Actions (Blueprint Button Group) ── */
#qbar {
  display: flex; gap: 1px; padding: 2px 10px; overflow-x: auto;
  background: var(--dock); border-top: 1px solid var(--border-muted);
  -webkit-overflow-scrolling: touch;
}
#qbar::-webkit-scrollbar { display: none; }
.qb {
  flex: 1; padding: 4px 6px; font-size: 11px;
  min-height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-alt); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text-sec); cursor: pointer;
  font-family: 'SF Mono', 'Menlo', monospace;
  box-shadow: var(--shadow-inset);
  transition: all 0.1s var(--transition);
  letter-spacing: 0.3px;
}
.qb:active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ── Image Preview ── */
#img-preview {
  display: none; align-items: center; gap: 8px; padding: 6px 12px;
  background: var(--surface-alt); border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-sec);
}
#img-preview.show { display: flex; }
#img-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: 'SF Mono', 'Menlo', monospace;
}
#img-clear {
  background: none; border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--dim); font-size: 11px;
  font-weight: 600; padding: 4px 8px; cursor: pointer;
  min-height: 44px; min-width: 44px;
  transition: all 0.1s var(--transition);
}

/* ── Input (Blueprint InputGroup) ── */
#input-bar {
  display: flex; gap: 4px; padding: 3px 10px;
  background: var(--dock); border-top: 1px solid var(--border-muted);
  align-items: flex-end;
}
#img-btn {
  display: flex; align-items: center; justify-content: center;
  padding: 4px; min-height: 32px; min-width: 32px;
  background: var(--surface-alt); color: var(--dim);
  border: 1px solid var(--border); border-radius: var(--radius);
  font-weight: 600; font-size: 11px; cursor: pointer;
  white-space: nowrap;
  font-family: 'SF Mono', 'Menlo', monospace;
  box-shadow: var(--shadow-inset);
  transition: all 0.1s var(--transition);
  -webkit-tap-highlight-color: transparent;
}
#img-btn:active { background: var(--accent); color: #fff; border-color: var(--accent); }
#img-btn.has-img { background: var(--accent-muted); color: var(--accent-light); border-color: var(--accent); }
#msg {
  flex: 1; padding: 6px 10px; font-size: 16px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text); outline: none;
  font-family: 'SF Mono', 'Menlo', monospace;
  resize: none; min-height: 32px; max-height: 64px;
  line-height: 1.4; overflow-y: auto;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.1);
}
#msg:focus { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
#send {
  padding: 4px 10px; min-height: 32px; min-width: 44px;
  background: var(--accent); color: #fff;
  border: none; border-radius: var(--radius); font-weight: 600; font-size: 13px;
  cursor: pointer; white-space: nowrap;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.1), 0 1px 1px rgba(0,0,0,0.2);
  transition: all 0.1s var(--transition);
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none; user-select: none;
}
#send:active { background: #215db0; transform: scale(0.98); }

/* ── Autocomplete ── */
#autocomplete {
  display: none; position: absolute; bottom: 100%; left: 0; right: 0;
  max-height: 200px; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface-alt); border: 1px solid var(--border);
  border-bottom: none; border-radius: var(--radius) var(--radius) 0 0; z-index: 50;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.3);
}
#autocomplete.show { display: block; }
.ac-item {
  display: block; width: 100%; padding: 14px 12px; border: none;
  background: none; color: var(--text); font-size: 13px; text-align: left;
  cursor: pointer; border-bottom: 1px solid var(--border);
  font-family: 'SF Mono', 'Menlo', monospace;
  min-height: 44px;
  transition: background 0.1s;
}
.ac-item:active { background: var(--accent-muted); }
.ac-item .ac-cmd { font-weight: 600; color: var(--accent-light); }
.ac-item .ac-desc { color: var(--dim); font-size: 12px; margin-left: 8px; }

/* ── Tab Strip (Blueprint Panel Footer) ── */
#tabs {
  display: flex; align-items: center; gap: 6px;
  background: var(--surface);
  padding: 6px 10px; padding-bottom: calc(6px + max(12px, env(safe-area-inset-bottom)));
  border-top: 1px solid var(--border);
  min-height: 46px; position: relative;
}
/* Active session pill */
#tab-pill {
  flex: 1; display: flex; align-items: center; gap: 6px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0 10px;
  height: 44px; min-width: 0; cursor: pointer;
  transition: border-color 0.15s var(--transition);
  overflow: hidden;
}
#tab-pill:active { border-color: var(--accent); }
#tab-pill .pill-name {
  flex: 1; font-size: 12px; color: var(--text);
  font-family: 'SF Mono', 'Menlo', monospace;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  line-height: 44px;
}
#tab-pill .pill-dir {
  font-size: 10px; color: var(--dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 100px;
}
/* Tab count badge */
#tab-count-btn {
  flex-shrink: 0; width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text-sec);
  font-size: 12px; font-weight: 700; cursor: pointer;
  font-family: 'SF Mono', 'Menlo', monospace;
  transition: all 0.15s var(--transition);
  position: relative;
}
#tab-count-btn:active { border-color: var(--accent); color: var(--accent-light); }
/* Notification badge on tab count */
#tab-count-btn .attn-badge {
  display: none; position: absolute; top: -4px; right: -4px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--intent-danger);
  border: 2px solid var(--surface);
  animation: pulse 1.5s infinite;
}
#tab-count-btn.has-attn .attn-badge { display: block; }
/* Tab switcher overlay */
#tab-switcher {
  display: none; position: absolute; bottom: 100%; left: 0; right: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  max-height: 60vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  z-index: 50; box-shadow: 0 -8px 24px rgba(0,0,0,0.4);
  overscroll-behavior: contain;
}
#tab-switcher.open { display: block; }
.switcher-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 10px 12px; border: none;
  background: none; color: var(--text); font-size: 12px;
  text-align: left; cursor: pointer;
  font-family: 'SF Mono', 'Menlo', monospace;
  border-bottom: 1px solid var(--border);
  min-height: 44px;
  transition: background 0.1s;
}
.switcher-item:active { background: var(--surface-hover); }
.switcher-item.active { background: var(--surface-alt); border-left: 2px solid var(--accent); }
.switcher-item .sw-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.switcher-item .sw-attn {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--intent-warn); flex-shrink: 0;
  box-shadow: 0 0 0 2px rgba(200,118,25,0.3);
  animation: pulse 1.5s infinite;
}
.switcher-item .sw-close {
  flex-shrink: 0; width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--dim); font-size: 11px;
  font-weight: 600; cursor: pointer;
  transition: all 0.1s var(--transition);
}
.switcher-item .sw-close:active { background: var(--intent-danger); color: #fff; border-color: var(--intent-danger); }
/* Swipe hint edges */
#swipe-hint-left, #swipe-hint-right {
  position: absolute; top: 0; bottom: 0; width: 4px;
  opacity: 0; transition: opacity 0.15s;
  pointer-events: none; z-index: 20;
}
#swipe-hint-left { left: 0; background: linear-gradient(to right, var(--accent), transparent); }
#swipe-hint-right { right: 0; background: linear-gradient(to left, var(--accent), transparent); }
#swipe-hint-left.flash, #swipe-hint-right.flash { opacity: 1; }
/* New session pull indicator */
#new-session-hint {
  position: absolute; right: 0; top: 0; bottom: 0; width: 0;
  background: rgba(45,114,210,0.15); display: flex;
  align-items: center; justify-content: center;
  overflow: hidden; transition: width 0.1s; z-index: 15;
  pointer-events: none;
}
#new-session-hint span {
  color: var(--accent-light); font-size: 18px; font-weight: 700;
  opacity: 0; transition: opacity 0.15s;
}
#new-session-hint.ready { background: rgba(45,114,210,0.25); }
#new-session-hint.ready span { opacity: 1; }
@keyframes pulse {
  0%,100% { opacity:1; transform:scale(1); }
  50% { opacity:0.6; transform:scale(0.8); }
}
/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
