/* TyapTerm — layout: app shell, titlebar, sidebar, content panels, terminal area */

/* ===============================================
   APP SHELL
   =============================================== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

/* ===============================================
   TITLE BAR
   =============================================== */
.titlebar {
  display: flex;
  align-items: center;
  height: var(--titlebar-h);
  min-height: var(--titlebar-h);
  background: var(--titlebar-bg);
  border-bottom: 1px solid var(--border-1);
  user-select: none;
  position: relative;
  z-index: 100;
}

.titlebar-left {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-left: 6px;
  flex-shrink: 0;
}

.titlebar-btn {
  width: 42px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  border-radius: var(--border-r);
  transition: all var(--dur-fast) ease;
  font-size: 17px;
  position: relative;
}
.titlebar-btn:hover { background: var(--surface-3); color: var(--text-1); }
.titlebar-btn.active { color: var(--text-1); background: var(--surface-2); }
.titlebar-btn.active::after {
  content: '';
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 16px;
  height: 2px;
  border-radius: 1px;
}
.titlebar-btn.sidebar-btn.active::after { background: var(--ssh-color); }
.titlebar-btn.dashboard-btn.active::after { background: var(--orange); }

.titlebar-center {
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  height: 100%;
}

.titlebar-right {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 0;
  flex-shrink: 0;
  height: 100%;
}

/* Window controls group — subtle background, separated from panel toggles */
.window-controls {
  display: flex;
  align-items: flex-start;
  align-self: flex-start;
  background: var(--surface);
  border-radius: 0 0 0 6px;
  margin-left: 6px;
  border: 1px solid var(--border-1);
  border-top: none;
  border-right: none;
}

/* Window control buttons (frameless mode) */
.window-btn {
  width: 37px;
  height: 29px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: background var(--dur-fast) ease, color var(--dur-fast) ease;
}
.window-btn:hover { background: var(--surface-3); color: var(--text-1); }
.window-btn-close:hover { background: #E81123; color: #ffffff; }
body.maximized .window-controls { border-radius: 0; }
/* Fitts's Law: sidebar button extends to top-left corner when maximized */
body.maximized .titlebar-left { padding-left: 0; }
body.maximized .titlebar-btn.sidebar-btn { border-radius: 0; padding-left: 6px; }

/* Titlebar drag: handled by JS mousedown → pywebview api.drag_start() → frameless.py polling loop */

/* ===============================================
   MAIN CONTENT AREA
   =============================================== */
.main-area {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* ===============================================
   SIDEBAR
   =============================================== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border-1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
  transition: width var(--dur-slide) var(--ease-out), min-width var(--dur-slide) var(--ease-out), opacity var(--dur-slide) var(--ease-out);
  z-index: 50;
}
.sidebar.hidden {
  width: 0;
  min-width: 0;
  border-right-width: 0;
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 8px;
  flex-shrink: 0;
}
.sidebar-title {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-3);
}
.sidebar-actions {
  display: flex;
  gap: 2px;
}
.sidebar-action-btn {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: transparent;
  color: var(--text-3);
  border-radius: var(--border-r);
  cursor: pointer;
  font-size: 15px;
  transition: all var(--dur-fast) ease;
}
.sidebar-action-btn:hover { background: var(--surface-3); color: var(--text-1); }

/* Quick Connect */
.quick-connect {
  padding: 4px 14px 10px;
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.quick-connect select,
.quick-connect input {
  height: 32px;
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--text-1);
  border-radius: var(--border-r);
  padding: 0 8px;
  font-family: var(--font-ui);
  font-size: 14px;
  outline: none;
  transition: border-color var(--dur-fast) ease;
}
.quick-connect select { width: 90px; cursor: pointer; flex-shrink: 0; }
.quick-connect input { flex: 1; min-width: 0; }
.quick-connect select:focus,
.quick-connect input:focus { border-color: var(--input-focus); }

.quick-connect-btn {
  height: 30px; width: 30px;
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: var(--border-r);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  font-size: 15px;
  transition: background var(--dur-fast) ease;
}
.quick-connect-btn:hover { background: var(--accent-hover); }

/* Session List */
.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.session-group-header {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 6px 8px 4px;
  margin-top: 8px;
}
.session-group-header:first-child { margin-top: 2px; }
.session-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  padding: 4px 8px;
  border-radius: var(--border-r);
  cursor: pointer;
  transition: background var(--dur-fast) ease;
  position: relative;
}
.session-item:hover { background: var(--sidebar-hover); }
.session-item.selected { background: var(--accent-glow); }

.session-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  position: relative;
}
.session-dot.online { background: var(--green); box-shadow: 0 0 6px var(--green-glow); }
.session-dot.offline { background: var(--red-dim); }
.session-dot.checking { background: var(--text-disabled); animation: dotPulse 1.5s ease-in-out infinite; }
@keyframes dotPulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.session-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.session-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-host {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-proto-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  padding: 1px 5px;
  border-radius: 3px;
  flex-shrink: 0;
  opacity: 0.85;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  height: var(--footer-h);
  padding: 0 14px;
  border-top: 1px solid var(--border-1);
  background: var(--surface);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-3);
  flex-shrink: 0;
  gap: 10px;
}
.sidebar-footer-spacer { flex: 1; }

/* ===============================================
   DASHBOARD (left panel — replaces sidebar)
   =============================================== */
.dashboard {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border-1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
  transition: width var(--dur-slide) var(--ease-out), min-width var(--dur-slide) var(--ease-out), opacity var(--dur-slide) var(--ease-out);
  z-index: 50;
}
.dashboard.hidden {
  width: 0;
  min-width: 0;
  border-right-width: 0;
  opacity: 0;
  pointer-events: none;
}

.dashboard-header {
  display: flex;
  align-items: center;
  padding: 12px 14px 8px;
}
.dashboard-title {
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-3);
}

.dashboard-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 6px;
}
.dashboard-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 6px 8px;
  border-radius: var(--border-r);
  cursor: pointer;
  transition: background var(--dur-fast) ease;
}
.dashboard-item:hover { background: var(--sidebar-hover); }

.dashboard-item-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.dashboard-item-host {
  font-size: 13px;
  color: var(--text-1);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.dashboard-item-proto {
  font-size: 11px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dashboard-empty {
  padding: 24px 14px;
  font-family: var(--font-ui);
  font-size: 14px;
  color: var(--text-3);
  text-align: center;
}

/* ===============================================
   CONTENT PANEL (Terminal + File Browser side by side)
   =============================================== */
.content-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  position: relative;
}

/* Scrim overlay — covers terminal only when sidebar/dashboard is open */
.panel-scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-slide) var(--ease-out);
  z-index: 10;
  cursor: pointer;
}
.main-area:has(> .sidebar:not(.hidden)) .panel-scrim,
.main-area:has(> .dashboard:not(.hidden)) .panel-scrim {
  opacity: 1;
  pointer-events: auto;
}

/* Terminal + File Browser horizontal split */
.terminal-split {
  flex: 1;
  display: flex;
  flex-direction: row;
  overflow: hidden;
}

/* Terminal Area */
.terminal-area {
  flex: 1;
  background: var(--terminal-bg);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  will-change: contents;
  contain: strict;
}

/* Copy flash overlay — must be above xterm.js canvases */
.copy-flash {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.25);
  opacity: 0;
  pointer-events: none;
  z-index: 999;
}
.copy-flash.flash {
  animation: copyFlashAnim 0.25s ease-out;
}
@keyframes copyFlashAnim {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* Terminal Status Bar */
.terminal-statusbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  column-gap: 12px;
  align-items: center;
  height: var(--footer-h);
  padding: 0 12px;
  border-top: 1px solid var(--border-1);
  background: var(--surface);
  flex-shrink: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-3);
  position: relative;
}
.terminal-statusbar-title {
  min-width: 0;
  justify-self: start;
  overflow: hidden;
}
.terminal-statusbar-title .terminal-statusbar-item {
  min-width: 0;
}
.terminal-statusbar-accounts {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-width: 0;
  max-width: 42vw;
  justify-self: center;
}
.terminal-statusbar-metrics {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-width: 0;
  justify-self: end;
}
.terminal-statusbar-item {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}
.terminal-statusbar-item .label { color: var(--text-disabled); text-transform: uppercase; letter-spacing: 0.06em; }
/* Statusbar protocol label — colored by protocol */
#sbProto { font-weight: 600; text-transform: none; letter-spacing: normal; }
#sbProto[data-proto="local"] { color: var(--local-color); }
#sbProto[data-proto="powershell"] { color: var(--powershell-color); }
#sbProto[data-proto="ssh"] { color: var(--ssh-color); }
#sbProto[data-proto="telnet"] { color: var(--telnet-color); }
#sbProto[data-proto="ftp"] { color: var(--ftp-color); }
#sbProto[data-proto="remote"] { color: var(--remote-color); }
.terminal-statusbar-item .value { color: var(--text-2); font-weight: 500; }
#sbTitle {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.terminal-statusbar-item.claude-account {
  cursor: pointer;
  max-width: 190px;
}
.terminal-statusbar-item.claude-account:hover .value { color: var(--text-1); }
.terminal-statusbar-item.claude-account .label { color: #E87B35; }
.terminal-statusbar-item.claude-account .value { color: var(--text-2); font-size: 11px; }
.terminal-statusbar-item.claude-account .value,
.terminal-statusbar-item.codex-account .value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.terminal-statusbar-item.codex-account {
  cursor: pointer;
  max-width: 190px;
}
.terminal-statusbar-item.codex-account:hover .value { color: var(--text-1); }
.terminal-statusbar-item.codex-account .value { color: var(--text-2); font-size: 11px; }
.terminal-statusbar-account-icon {
  display: block;
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  object-fit: contain;
}
.terminal-statusbar-item.codex-account .terminal-statusbar-account-icon { opacity: 0.95; }

.sb-theme-toggle {
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: transparent;
  color: var(--text-3); border-radius: var(--border-r);
  cursor: pointer; padding: 0;
  transition: color var(--dur-fast) ease;
}
.sb-theme-toggle:hover { color: var(--text-1); }


/* ===============================================
   RESIZE HANDLES (hidden when maximized)
   =============================================== */
.rh { position:fixed; z-index:9999; }
.rh-n  { top:0; left:8px; right:8px; height:5px; cursor:n-resize; }
.rh-s  { bottom:0; left:8px; right:8px; height:5px; cursor:s-resize; }
.rh-w  { left:0; top:8px; bottom:8px; width:5px; cursor:w-resize; }
.rh-e  { right:0; top:8px; bottom:8px; width:5px; cursor:e-resize; }
.rh-nw { top:0; left:0; width:8px; height:8px; cursor:nw-resize; }
.rh-ne { top:0; right:0; width:8px; height:8px; cursor:ne-resize; }
.rh-sw { bottom:0; left:0; width:8px; height:8px; cursor:sw-resize; }
.rh-se { bottom:0; right:0; width:8px; height:8px; cursor:se-resize; }
body.maximized .rh { display:none; }

/* Snap preview overlay */
#snapOverlay {
  position:fixed; inset:0; z-index:99998; pointer-events:none;
  background:rgba(0,120,212,.12); border:2px solid rgba(0,120,212,.4);
  border-radius:8px; backdrop-filter:blur(8px);
  display:none; transition:opacity .12s;
}

/* SVG icons via <img> — recolor with filter (not applied to inline SVGs like #dashboardIcon) */
.icon { filter: brightness(0) invert(.7); pointer-events:none; }
.titlebar button:hover .icon, .window-btn:hover .icon, .sidebar-action-btn:hover .icon { filter: brightness(0) invert(.95); }
.window-btn-close:hover .icon { filter: brightness(0) invert(1); }
[data-theme="light"] .icon { filter: brightness(0) invert(.35); }
[data-theme="light"] .titlebar button:hover .icon, [data-theme="light"] .sidebar-action-btn:hover .icon { filter: brightness(0) invert(.1); }
[data-theme="light"] .window-btn-close:hover .icon { filter: brightness(0) invert(1); }

/* === Gateway mode === */
body.gateway-mode .window-controls { display: none; }
body.gateway-mode .rh { display: none; }
body.gateway-mode #snapOverlay { display: none !important; }
body.gateway-mode .instance-selector { display: flex !important; align-items: center; margin-right: 8px; }
body.gateway-mode .titlebar { -webkit-app-region: unset; }

.instance-selector select {
  background: var(--surface);
  color: var(--text-1);
  border: 1px solid var(--border-1);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 12px;
  height: 26px;
  cursor: pointer;
  outline: none;
}
.instance-selector select:hover { border-color: var(--accent); }

/* Gateway login overlay */
.gateway-login-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--bg); display: flex;
  align-items: center; justify-content: center;
}
.gateway-login-box {
  background: var(--surface); border: 1px solid var(--border-1);
  border-radius: 12px; padding: 32px;
  width: 320px; display: flex; flex-direction: column; gap: 12px;
}
.gateway-login-box h2 {
  margin: 0 0 8px; font-size: 18px; font-weight: 600;
  color: var(--text-1); text-align: center;
}
.gateway-login-box input {
  background: var(--bg); color: var(--text-1); border: 1px solid var(--border-1);
  border-radius: 6px; padding: 8px 12px; font-size: 14px; outline: none;
}
.gateway-login-box input:focus { border-color: var(--accent); }
.gateway-login-box button {
  background: var(--accent); color: #fff; border: none;
  border-radius: 6px; padding: 8px; font-size: 14px; cursor: pointer;
  font-weight: 500;
}
.gateway-login-box button:hover { filter: brightness(1.1); }
.gateway-login-error { color: var(--red); font-size: 13px; text-align: center; min-height: 18px; }
