/* ============================================================
   CSS Custom Properties — dark terminal color palette
   ============================================================ */
:root {
  --bg-primary:      #0d0d0d;
  --bg-secondary:    #1a1a1a;
  --bg-elevated:     #242424;
  --bg-button:       #2a2a2a;
  --bg-button-hover: #383838;
  --bg-active:       #1e4a6e;

  --border-color:    #3a3a3a;
  --border-active:   #4a90d9;

  --text-primary:    #e0e0e0;
  --text-muted:      #888888;
  --text-button:     #cccccc;

  --status-connected:    #1a5c1a;
  --status-reconnecting: #7a4a00;
  --status-disconnected: #5c1a1a;

  --status-text-connected:    #4caf50;
  --status-text-reconnecting: #ff9800;
  --status-text-disconnected: #f44336;

  --keyboard-bar-height: 52px;
  --status-bar-height:   24px;
}

/* ============================================================
   Reset & Base
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  /* Prevent iOS bounce scroll */
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: auto;
  height: 100%;
  width: 100%;
}

body {
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Courier New', Courier, monospace;
  display: flex;
  flex-direction: column;
  height: 100vh;
  /* Account for iOS safe areas (notch / home bar) */
  padding-bottom: env(safe-area-inset-bottom);
}

/* ============================================================
   Status Bar
   — Fixed at top, shows connection state
   — Hidden by default; JS adds state classes to reveal it
   ============================================================ */
#status-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--status-bar-height);
  line-height: var(--status-bar-height);
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  z-index: 100;
  /* Hidden until a state class is applied */
  display: none;
}

#status-bar.connected {
  display: block;
  background-color: var(--status-connected);
  color: var(--status-text-connected);
}

#status-bar.reconnecting {
  display: block;
  background-color: var(--status-reconnecting);
  color: var(--status-text-reconnecting);
}

#status-bar.disconnected {
  display: block;
  background-color: var(--status-disconnected);
  color: var(--status-text-disconnected);
}

/* ============================================================
   Login Screen
   — Centered overlay, shown by default
   — Hidden by JS once authentication succeeds
   ============================================================ */
#login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--bg-primary);
  z-index: 50;
  padding: 24px;
}

.login-box {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 0.04em;
}

.login-subtitle {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Token input field */
#token-input {
  background-color: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: 'Courier New', Courier, monospace;
  font-size: 15px;
  padding: 12px 14px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s ease;
}

#token-input::placeholder {
  color: var(--text-muted);
}

#token-input:focus {
  border-color: var(--border-active);
}

/* Connect submit button */
#login-form button[type="submit"] {
  background-color: var(--bg-active);
  border: 1px solid var(--border-active);
  border-radius: 4px;
  color: var(--text-primary);
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.05em;
  min-height: 44px;
  padding: 10px 20px;
  text-transform: uppercase;
  transition: background-color 0.15s ease, opacity 0.15s ease;
  width: 100%;
}

#login-form button[type="submit"]:hover {
  background-color: #2a6090;
}

#login-form button[type="submit"]:active {
  opacity: 0.8;
}

/* ============================================================
   Session Picker Screen
   — Shown after login; user selects or creates a tmux session
   — Same overlay pattern as #login-screen
   ============================================================ */
#session-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--bg-primary);
  z-index: 50;
  padding: 24px;
}

.session-box {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.session-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: 0.04em;
}

.session-subtitle {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

#session-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 50vh;
  overflow-y: auto;
  margin-bottom: 16px;
}

/* Session row — holds item + kill button side by side */
.session-row {
  display: flex;
  flex-direction: row;
  gap: 6px;
}

/* Individual session entry — clickable card */
.session-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  flex: 1;
  min-height: 48px;
  padding: 12px 16px;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background-color: var(--bg-button);
  color: var(--text-primary);
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  text-align: left;
  transition: background-color 0.1s ease;
  user-select: none;
  -webkit-user-select: none;
}

.session-item:active {
  background-color: var(--bg-button-hover);
}

.session-item .session-name {
  font-weight: 600;
  font-size: 15px;
}

.session-item .session-info {
  font-size: 12px;
  color: var(--text-muted);
  text-align: right;
}

/* Kill session button */
.session-kill {
  min-width: 44px;
  min-height: 48px;
  border-radius: 4px;
  border: 1px solid var(--status-disconnected);
  background-color: var(--bg-button);
  color: var(--status-text-disconnected);
  cursor: pointer;
  font-size: 16px;
  font-weight: 700;
  transition: background-color 0.1s ease;
  user-select: none;
  -webkit-user-select: none;
}

.session-kill:active {
  background-color: var(--status-disconnected);
  color: #ffffff;
}

.session-new {
  margin-top: 16px;
}

#session-form {
  display: flex;
  gap: 8px;
}

/* Session name text input */
#session-name-input {
  flex: 1;
  background-color: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: 'Courier New', Courier, monospace;
  font-size: 15px;
  padding: 12px 14px;
  outline: none;
  transition: border-color 0.15s ease;
}

#session-name-input::placeholder {
  color: var(--text-muted);
}

#session-name-input:focus {
  border-color: var(--border-active);
}

/* Create button */
#session-form button[type="submit"] {
  background-color: var(--bg-active);
  border: 1px solid var(--border-active);
  border-radius: 4px;
  color: var(--text-primary);
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.05em;
  min-height: 44px;
  padding: 12px 20px;
  text-transform: uppercase;
  transition: background-color 0.15s ease, opacity 0.15s ease;
  width: auto;
}

#session-form button[type="submit"]:hover {
  background-color: #2a6090;
}

#session-form button[type="submit"]:active {
  opacity: 0.8;
}

/* ============================================================
   Terminal Container
   — Hidden by default; revealed by JS after login
   — Fills all remaining vertical space
   ============================================================ */
#terminal-container {
  display: none;
  flex: 1;
  flex-direction: column;
  /* Reserve space for the status bar when visible */
  padding-top: 0;
  overflow: hidden;
  position: relative;
}

/* Make xterm fill its container */
#terminal {
  width: 100%;
  height: 100%;
  flex: 1;
  min-height: 0; /* required for flex children to shrink correctly */
  position: relative; /* anchor for #select-overlay */
}

/* ============================================================
   Toolbar
   — Sits below the status bar, above the terminal
   — Always visible when terminal is shown
   ============================================================ */
#toolbar {
  display: flex;
  align-items: center;
  height: 32px;
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 0 8px;
  gap: 6px;
  flex-shrink: 0;
}

.toolbar-btn {
  background-color: var(--bg-button);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  color: var(--text-button);
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  min-height: 26px;
  user-select: none;
  -webkit-user-select: none;
  transition: background-color 0.1s ease;
}

.toolbar-btn:active {
  background-color: var(--bg-button-hover);
}

#font-size-display {
  color: var(--text-muted);
  font-size: 11px;
  min-width: 20px;
  text-align: center;
}

.toolbar-spacer {
  flex: 1;
}

/* Copy feedback: button turns green briefly after copying */
#copy-console.copied {
  background-color: var(--status-connected);
  color: var(--status-text-connected);
  border-color: var(--status-text-connected);
}

/* xterm.js internal element overrides */
.xterm {
  width: 100%;
  height: 100%;
  padding: 4px;
}

.xterm-viewport {
  /* Prevent double scrollbar */
  overflow-y: hidden !important;
}

/* ============================================================
   Select Mode Overlay
   — Transparent layer over terminal for touch-based text selection
   — Active only when #terminal-container has .select-active class
   ============================================================ */
#select-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  pointer-events: none;
}

#terminal-container.select-active #select-overlay {
  pointer-events: auto;
  cursor: text;
}

#select-mode-btn.active {
  background-color: var(--bg-active);
  color: #ffffff;
  border-color: var(--border-active);
}

#terminal-container.select-active #terminal {
  border-top: 2px solid var(--bg-active);
}

/* Selection drag handles — small circles at start/end of selection */
.select-handle {
  display: none;
  position: absolute;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background-color: var(--bg-active);
  border: 2px solid #ffffff;
  z-index: 20;
  transform: translate(-50%, 0);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  touch-action: none;
}

.select-handle.visible {
  display: block;
}

/* Thin line connecting handle to text */
.select-handle::before {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 6px;
  background-color: var(--bg-active);
}

#select-handle-start::before {
  bottom: 100%;
}

#select-handle-end::before {
  bottom: 100%;
}

/* Hide select button on desktop — desktop can already click-drag to select */
@media (min-width: 769px) {
  #select-mode-btn {
    display: none;
  }
}

/* ============================================================
   Virtual Keyboard Bar
   — Hidden on desktop; shown on mobile via media query
   — Fixed at bottom of screen
   ============================================================ */
#keyboard-bar {
  display: none; /* hidden by default — shown by media query below */
  flex-direction: row;
  align-items: stretch;
  background-color: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--keyboard-bar-height);
  z-index: 90;
  /* Respect iOS home bar */
  padding-bottom: env(safe-area-inset-bottom);
}

/* Individual key button */
.key-btn {
  flex: 1;
  min-height: 44px; /* minimum touch target size */
  background-color: var(--bg-button);
  border: none;
  border-right: 1px solid var(--border-color);
  color: var(--text-button);
  cursor: pointer;
  font-family: 'Courier New', Courier, monospace;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  padding: 0 2px;
  text-align: center;
  text-transform: uppercase;
  transition: background-color 0.1s ease;
  /* Prevent text selection on rapid taps */
  user-select: none;
  -webkit-user-select: none;
}

.key-btn:last-child {
  border-right: none;
}

.key-btn:active {
  background-color: var(--bg-button-hover);
}

/* Toggle keys (CTRL / ALT / SHIFT) highlight when active */
.key-btn.active {
  background-color: var(--bg-active);
  color: #ffffff;
  border-right-color: var(--border-active);
}

/* ============================================================
   Mobile Adjustments
   — Show keyboard bar and shift terminal up accordingly
   ============================================================ */
@media (max-width: 768px) {
  #keyboard-bar {
    display: flex;
  }

  /* Push terminal content above the keyboard bar.
     This acts as a CSS fallback before keyboard.js initialises.
     Once the visualViewport listener fires, keyboard.js overrides
     this with an inline style that also accounts for the native
     software keyboard height. */
  #terminal-container {
    margin-bottom: var(--keyboard-bar-height);
  }
}
