/* Claude Code Remote - Functional Terminal Interface */

@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&display=swap');

:root {
  /* GitHub Dark Theme Base */
  --bg-deep: #0d1117;
  --bg-primary: #161b22;
  --bg-elevated: #1c2128;
  --bg-surface: #21262d;
  --bg-hover: #30363d;

  /* Text hierarchy */
  --text-primary: #f0f6fc;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;

  /* Accent - Surgical use only */
  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --accent-soft: rgba(88, 166, 255, 0.15);

  /* Semantic colors */
  --success: #3fb950;
  --error: #f85149;
  --warning: #d29922;

  /* Borders & Shadows */
  --border: rgba(240, 246, 252, 0.1);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);

  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Menlo, Monaco, monospace;

  /* Sizing */
  --radius: 6px;

  /* Transitions - functional only */
  --transition: 150ms ease;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-deep);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  line-height: 1.5;
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 1;
}

/* ===== SCREENS ===== */
.screen {
  display: none;
  width: 100%;
  height: 100%;
  min-height: 0;
  flex-direction: column;
}

.screen.active {
  display: flex;
  flex: 1;
}

/* ===== AUTH SCREEN ===== */
#auth-screen {
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 1.5rem;
  background: var(--bg-deep);
}

.auth-container {
  width: 100%;
  max-width: 380px;
  text-align: center;
}

/* Auth Logo */
.auth-logo {
  margin-bottom: 1.5rem;
}

.auth-container h1 {
  font-size: 1.75rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.auth-container .subtitle {
  color: var(--text-secondary);
  font-size: 0.9375rem;
  margin-bottom: 2rem;
  line-height: 1.5;
}

/* Auth Input */
.auth-container input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 0.9375rem;
  font-family: var(--font-mono);
  letter-spacing: 0.05em;
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  text-align: center;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.auth-container input::placeholder {
  color: var(--text-muted);
  letter-spacing: 0;
}

.auth-container input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.auth-container .error {
  color: var(--error);
  font-size: 0.875rem;
  margin-top: 1rem;
  min-height: 1.5rem;
  padding: 0.5rem;
  border-radius: var(--radius);
  background: rgba(248, 81, 73, 0.1);
}

.auth-container .error:empty {
  display: none;
}

/* ===== BUTTONS ===== */
.btn-primary {
  width: 100%;
  padding: 0.75rem 1.25rem;
  font-size: 0.9375rem;
  font-weight: 500;
  font-family: var(--font-sans);
  background: var(--accent);
  color: var(--bg-deep);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(1px);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Button loading state */
.btn-primary .btn-text,
.btn-primary .btn-loading {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-primary .btn-loading {
  display: none;
}

.btn-primary.loading .btn-text {
  display: none;
}

.btn-primary.loading .btn-loading {
  display: inline-flex;
}

.btn-primary .spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.btn-secondary {
  padding: 0.625rem 1rem;
  font-size: 0.875rem;
  font-family: var(--font-sans);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition);
}

.btn-secondary:hover {
  background: var(--bg-hover);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.btn-icon:hover {
  color: var(--text-primary);
  background: rgba(240, 246, 252, 0.05);
  border-color: var(--text-muted);
}

.btn-icon:active {
  transform: scale(0.96);
}

.btn-icon-minimal {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  color: var(--text-secondary);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.btn-icon-minimal:hover {
  color: var(--text-primary);
  background: rgba(240, 246, 252, 0.05);
}

.btn-icon-minimal:active {
  transform: scale(0.96);
}

/* Chevron rotation for toggle button */
.btn-icon .chevron-icon,
.btn-icon-minimal .chevron-icon {
  transition: transform var(--transition);
}

.btn-icon[aria-expanded="false"] .chevron-icon,
.btn-icon-minimal[aria-expanded="false"] .chevron-icon {
  transform: rotate(180deg);
}


/* ===== MAIN SCREEN ===== */
#main-screen {
  background: var(--bg-deep);
}

#main-screen header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.875rem 1.25rem;
  padding-top: max(0.875rem, env(safe-area-inset-top));
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  flex-shrink: 0;
  transition: transform var(--transition), opacity var(--transition);
  z-index: 10;
  min-height: 56px;
}

#main-screen header.collapsed {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

/* Floating expand button */
.floating-btn {
  position: absolute;
  top: max(0.5rem, env(safe-area-inset-top));
  right: max(0.5rem, env(safe-area-inset-right));
  z-index: 5;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

/* Reconnect Indicator */
.reconnect-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  opacity: 0.7;
}

.reconnect-indicator svg {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.btn-small {
  padding: 0.5rem 1rem;
  font-size: 0.8125rem;
}

.header-left, .header-right {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.header-left-actions {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin-left: 0.25rem;
}

/* Session Select (Mobile) */
#session-select {
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 140px;
  max-width: 200px;
  cursor: pointer;
  transition: border-color var(--transition);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238b949e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.625rem center;
}

#session-select:hover {
  border-color: var(--text-muted);
}

#session-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* Session Tabs (Desktop) */
.session-tabs {
  display: flex;
  gap: 0.375rem;
  align-items: center;
  overflow: visible;
  max-width: 50vw;
}

.session-tabs::-webkit-scrollbar {
  display: none;
}

.session-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
  position: relative;
}

.session-tab:hover {
  color: var(--text-primary);
  background: rgba(240, 246, 252, 0.03);
}

.session-tab[aria-selected="true"] {
  color: var(--text-primary);
  background: rgba(88, 166, 255, 0.1);
  border-color: var(--accent);
}

.session-tab:active {
  transform: scale(0.98);
}

.session-tab-name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  font-size: 0.875rem;
  line-height: 1;
  color: var(--text-muted);
  border-radius: 3px;
  transition: all var(--transition);
  margin-left: 0.375rem;
  margin-right: -0.25rem;
  opacity: 0.6;
}

.session-tab:hover .session-tab-close {
  opacity: 1;
}

.session-tab-close:hover {
  background: rgba(248, 81, 73, 0.15);
  color: var(--error);
}

.session-tab-empty {
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-style: italic;
  background: transparent;
  border: none;
}

/* Activity Status Indicators */
.activity-indicator {
  font-size: 0.625rem;
  line-height: 1;
  flex-shrink: 0;
  width: 1em;
  text-align: center;
  transition: color var(--transition);
}

.activity-indicator[data-status="busy"] {
  color: var(--success);
}

.activity-indicator[data-status="idle"] {
  color: var(--text-muted);
}

.activity-indicator[data-status="unknown"] {
  color: var(--text-muted);
  opacity: 0.5;
}

/* Highlight busy sessions in the dropdown */
#session-select option[data-status="busy"] {
  color: var(--success);
}

/* External Sessions Button */
.external-sessions-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.75rem;
  font-family: var(--font-sans);
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
  position: relative;
}

.external-sessions-btn:hover {
  color: var(--text-primary);
  background: rgba(240, 246, 252, 0.03);
}

.external-sessions-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 0.25rem;
  font-size: 0.6875rem;
  font-weight: 600;
  background: var(--accent);
  color: white;
  border-radius: 9px;
  line-height: 1;
}

/* External Sessions Dropdown */
.external-sessions-dropdown {
  position: absolute;
  top: calc(100% + 0.5rem);
  left: 0;
  min-width: 240px;
  max-width: 320px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-0.5rem);
  transition: all var(--transition);
}

.external-sessions-dropdown.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.external-session-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition);
  border-bottom: 1px solid var(--border);
}

.external-session-item .activity-indicator {
  font-size: 0.75rem;
}

.external-session-item:last-child {
  border-bottom: none;
}

.external-session-item:hover {
  background: rgba(240, 246, 252, 0.05);
}

.external-session-info {
  flex: 1;
  min-width: 0;
}

.external-session-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 0.25rem;
}

.external-session-path {
  font-size: 0.6875rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.external-session-pid {
  display: none;
  border-radius: 3px;
  margin-left: 0.75rem;
  flex-shrink: 0;
}

/* Responsive: Mobile/Desktop visibility */
.mobile-only {
  display: block;
}

.desktop-only {
  display: none;
}

@media (min-width: 768px) {
  .mobile-only {
    display: none;
  }

  .desktop-only {
    display: flex;
  }
}

/* ===== TERMINAL CONTAINER ===== */
#terminal-container {
  flex: 1;
  overflow: hidden;
  background: var(--bg-deep);
  position: relative;
  /* Create stacking context so overlay z-index is contained within */
  z-index: 0;
}

/* Terminal top separator */
#terminal-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--border);
  pointer-events: none;
  z-index: 1;
  opacity: 0.5;
}

#terminal-container .xterm {
  height: 100%;
  padding: 8px 12px;
}

#terminal-container .xterm-viewport {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
}

#terminal-container .xterm-viewport::-webkit-scrollbar {
  width: 8px;
}

#terminal-container .xterm-viewport::-webkit-scrollbar-track {
  background: transparent;
}

#terminal-container .xterm-viewport::-webkit-scrollbar-thumb {
  background: var(--bg-hover);
  border-radius: 4px;
}

#terminal-container .xterm-viewport::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ===== PREVIEW SCREEN ===== */
#preview-screen {
  background: var(--bg-deep);
}

#preview-screen header {
  display: flex;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  padding-top: max(0.75rem, env(safe-area-inset-top));
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#port-select {
  flex: 1;
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color var(--transition);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238b949e' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.625rem center;
}

#port-select:hover {
  border-color: var(--text-muted);
}

#port-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

#port-input {
  width: 80px;
  padding: 0.5rem 0.625rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition);
  -moz-appearance: textfield;
}

#port-input::-webkit-outer-spin-button,
#port-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#port-input:hover {
  border-color: var(--text-muted);
}

#port-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

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

/* Address Bar */
.preview-address-bar {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--bg-deep);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#address-input {
  flex: 1;
  padding: 0.5rem 0.75rem;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition);
}

#address-input:hover {
  border-color: var(--text-muted);
}

#address-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

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

#preview-frame {
  flex: 1;
  width: 100%;
  border: none;
  background: #fff;
  border-radius: 0;
}

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(13, 17, 23, 0.8);
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-content {
  width: 100%;
  max-width: 400px;
  padding: 1.5rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.modal-content h2 {
  margin-bottom: 0.5rem;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary);
}

.modal-description {
  color: var(--text-secondary);
  font-size: 0.875rem;
  margin-bottom: 1rem;
  line-height: 1.5;
}

.modal-content input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  font-size: 0.875rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.modal-content input::placeholder {
  color: var(--text-muted);
}

.modal-content input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

.modal-actions .btn-secondary {
  flex: 1;
  max-width: 120px;
}

.modal-actions .btn-primary {
  flex: 1;
  max-width: 140px;
}

/* Autocomplete */
.autocomplete-wrapper {
  position: relative;
  margin-bottom: 1.25rem;
}

.autocomplete-wrapper input {
  margin-bottom: 0;
}

.suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 0.25rem;
  padding: 0.375rem 0;
  list-style: none;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 200px;
  overflow-y: auto;
  z-index: 10;
}

.suggestions li {
  padding: 0.5rem 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition);
}

.suggestions li:hover,
.suggestions li.selected {
  background: var(--bg-hover);
}

.suggestions li .dir-name {
  color: var(--text-primary);
}

.suggestions li .dir-path {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-left: 0.5rem;
}

/* ===== SETTINGS MODAL ===== */
.settings-content {
  max-width: 380px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.modal-header h2 {
  margin-bottom: 0;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 0;
}

.setting-info {
  flex: 1;
}

.setting-info label {
  display: block;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.setting-description {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin: 0;
  line-height: 1.4;
}

/* Toggle Switch */
.toggle {
  position: relative;
  width: 44px;
  height: 24px;
  padding: 0;
  background: none;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: background var(--transition), border-color var(--transition);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform var(--transition), background var(--transition);
}

.toggle[aria-checked="true"] .toggle-track {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle[aria-checked="true"] .toggle-thumb {
  transform: translateX(20px);
  background: white;
}

.toggle:hover .toggle-track {
  border-color: var(--text-muted);
}

.toggle[aria-checked="true"]:hover .toggle-track {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.toggle:focus-visible .toggle-track {
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* ===== UTILITIES ===== */
.hidden {
  display: none !important;
}

/* ===== RESPONSIVE DESIGN ===== */

/* Large phones and up */
@media (min-width: 390px) {
  .auth-container h1 {
    font-size: 2rem;
  }

  .auth-container .subtitle {
    font-size: 1rem;
  }
}

/* Mobile phones */
@media (max-width: 600px) {
  .preview-address-bar {
    padding: 0.375rem 0.75rem;
  }

  #address-input {
    font-size: 0.75rem;
    padding: 0.375rem 0.625rem;
  }

  #main-screen header {
    flex-wrap: wrap;
    gap: 0.375rem;
    padding: 0.5rem 0.75rem;
    padding-top: max(0.5rem, env(safe-area-inset-top));
  }

  /* Row 1: Session selector + actions on right */
  .header-left {
    order: 1;
    width: 100%;
    gap: 0.25rem;
  }

  #session-select {
    flex: 1;
    min-width: 0;
    max-width: none;
    padding: 0.5rem 1.75rem 0.5rem 0.625rem;
    font-size: 0.8125rem;
  }

  .header-left-actions {
    margin-left: auto;
  }

  /* Row 2: Action buttons */
  .header-right {
    order: 2;
    width: 100%;
    justify-content: flex-start;
    gap: 0.5rem;
  }

  .btn-icon {
    min-width: 36px;
    height: 36px;
    padding: 0 0.5rem;
  }

  .btn-icon svg {
    width: 16px;
    height: 16px;
  }
}

/* Small phones */
@media (max-width: 389px) {
  #main-screen header {
    padding: 0.375rem 0.5rem;
    padding-top: max(0.375rem, env(safe-area-inset-top));
  }

  .header-left {
    gap: 0.25rem;
  }

  .header-right {
    gap: 0.125rem;
  }

  #session-select {
    font-size: 0.75rem;
    padding: 0.375rem 1.5rem 0.375rem 0.5rem;
    background-position: right 0.5rem center;
  }

  .btn-icon {
    min-width: 32px;
    height: 32px;
    padding: 0 0.375rem;
  }

  .btn-icon svg {
    width: 14px;
    height: 14px;
  }

  .auth-container {
    max-width: 320px;
  }

  .modal-content {
    padding: 1.25rem;
  }
}

/* Tablet and up */
@media (min-width: 768px) {
  .auth-container {
    max-width: 400px;
  }

  .auth-container h1 {
    font-size: 2.25rem;
  }

  #main-screen header {
    padding: 0.875rem 1.5rem;
  }

  #terminal-container .xterm {
    padding: 12px 16px;
  }
}

/* Landscape orientation adjustments */
@media (orientation: landscape) and (max-height: 500px) {
  #auth-screen {
    padding: 1rem;
  }

  .auth-container h1 {
    font-size: 1.5rem;
    margin-bottom: 0.25rem;
  }

  .auth-container .subtitle {
    margin-bottom: 1rem;
  }

  .auth-container input {
    padding: 0.75rem 1rem;
  }

  .btn-primary {
    padding: 0.75rem 1.25rem;
  }
}

/* Prevent iOS zoom on input focus */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
  input, select, textarea {
    font-size: 16px;
  }
}

/* Touch-friendly terminal */
@media (pointer: coarse) {
  .btn-icon {
    min-width: 44px;
    height: 44px;
  }
}

/* When touch scroll is active, disable native scrolling on xterm elements
   This applies regardless of pointer type since we detect touch capability in JS
   The .touch-scroll-active class is added by TouchScrollManager */
#terminal-container.touch-scroll-active .xterm-screen {
  touch-action: none;
}

#terminal-container.touch-scroll-active .xterm-viewport {
  -webkit-overflow-scrolling: auto;
  overflow-y: hidden !important;
}

/* ===== TOUCH SCROLL OVERLAY ===== */
/* CRITICAL: This overlay captures touch events for momentum scrolling on mobile.
   DO NOT lower the z-index below 9999 or mobile scrolling will break.
   The overlay must sit above ALL other positioned elements including:
   - External sessions dropdown (z-index: 100)
   - Modals (z-index: 100)
   - Tooltips (z-index: 1000)
   - Any future UI elements

   Regression history:
   - 96cebff: Original momentum scrolling implementation
   - f47d40c: Fixed touch-action CSS
   - e1a8508: Raised z-index from 10 to 1000 (dropdown interference)
   - Current: Raised to 9999 with CSS class for reliability
*/
.touch-scroll-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  touch-action: none;
  /* Invisible but captures all touch events */
  background: transparent;
}

/* Safe area support for notched devices */
@supports (padding: max(0px)) {
  #main-screen header,
  #preview-screen header {
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .preview-address-bar {
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  #terminal-container {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ===== FOCUS VISIBLE ===== */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* ===== REDUCED MOTION ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== DARK MODE SCROLLBAR (non-terminal) ===== */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: var(--bg-deep);
}

::-webkit-scrollbar-thumb {
  background: var(--bg-hover);
  border-radius: 5px;
  border: 2px solid var(--bg-deep);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ===== SELECTION ===== */
::selection {
  background: var(--accent-soft);
  color: var(--text-primary);
}

/* ===== CONNECTION STATUS ===== */
.connection-status {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.375rem 0.75rem;
  font-size: 0.75rem;
  background: rgba(63, 185, 80, 0.1);
  color: var(--success);
  border-radius: var(--radius);
}

.connection-status::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.connection-status.disconnected {
  background: rgba(248, 81, 73, 0.1);
  color: var(--error);
}

/* ===== EMPTY STATE ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}

.empty-state svg {
  margin-bottom: 1rem;
  opacity: 0.5;
}

.empty-state p {
  font-size: 0.9rem;
  max-width: 240px;
}

/* ===== TOUCH FEEDBACK ===== */
@media (pointer: coarse) {
  .btn-primary:active:not(:disabled),
  .btn-secondary:active,
  .btn-icon:active {
    opacity: 0.7;
  }
}

/* ===== SKELETON LOADING ===== */
.skeleton {
  background: var(--bg-surface);
  border-radius: var(--radius);
}

/* ===== TOOLTIP ===== */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(-0.25rem);
  padding: 0.375rem 0.625rem;
  font-size: 0.75rem;
  white-space: nowrap;
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition), visibility var(--transition);
  pointer-events: none;
  z-index: 1000;
}

[data-tooltip]:hover::after {
  opacity: 1;
  visibility: visible;
}

/* ===== KEYBOARD SHORTCUT HINT ===== */
kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.5rem;
  height: 1.5rem;
  padding: 0 0.375rem;
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  background: var(--bg-surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 3px;
}

/* ===== MOBILE CONTROL KEYS TOOLBAR ===== */
/* CRITICAL: z-index must be higher than touch-scroll-overlay (9999) to receive taps.
   The overlay doesn't create a stacking context, so its 9999 z-index competes directly. */
.mobile-keys {
  position: fixed;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  z-index: 10000;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
  /* bottom is set dynamically by JS */
}

.mobile-keys.visible {
  opacity: 1;
  pointer-events: auto;
}

.mobile-key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  height: 40px;
  padding: 0 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  background: var(--bg-surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--transition), color var(--transition);
}

.mobile-key:active,
.mobile-key.pressed {
  background: var(--bg-hover);
}

/* Modifier key (Ctrl) - sticky toggle style */
.mobile-key-modifier[aria-pressed="true"] {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* Only show on touch devices */
@media (pointer: fine) {
  .mobile-keys {
    display: none !important;
  }
}

/* Adjust terminal container when toolbar is visible */
.mobile-keys.visible + .floating-btn,
.mobile-keys.visible ~ .floating-btn {
  bottom: calc(56px + max(0.5rem, env(safe-area-inset-bottom)));
}

/* When mobile keys are visible, add padding to terminal */
#main-screen.mobile-keys-visible #terminal-container {
  padding-bottom: calc(56px + env(safe-area-inset-bottom));
}

/* ===== SCROLL TO BOTTOM BUTTON ===== */
.scroll-bottom-btn {
  position: fixed;
  bottom: max(1rem, env(safe-area-inset-bottom));
  right: 1rem;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition), color var(--transition);
  z-index: 40;
  box-shadow: var(--shadow);
}

.scroll-bottom-btn:not(.hidden) {
  opacity: 0.9;
}

.scroll-bottom-btn:hover {
  color: var(--text-primary);
  border-color: var(--text-muted);
}

/* Hide when mobile keyboard is visible */
#main-screen.mobile-keys-visible .scroll-bottom-btn {
  display: none;
}

/* ===== SCHEDULES SCREEN ===== */
#schedules-screen {
  background: var(--bg-deep);
  overflow-y: auto;
  /* Ensure screen covers terminal layer beneath */
  position: relative;
  z-index: 2;
}

.schedules-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1.25rem;
  padding-top: max(0.875rem, env(safe-area-inset-top));
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 56px;
  position: sticky;
  top: 0;
  z-index: 1;
}

.schedules-header .btn-primary {
  width: auto;
  flex-shrink: 0;
}

.schedules-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
  white-space: nowrap;
}

.schedules-list {
  padding: 1rem;
  padding-bottom: env(safe-area-inset-bottom, 1rem);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Schedule Badge */
.schedule-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 0.375rem;
  font-size: 0.6875rem;
  font-weight: 600;
  background: var(--accent);
  color: white;
  border-radius: 10px;
  line-height: 1;
}

/* Schedule Tab Button */
.schedule-tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  font-family: var(--font-sans);
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
  position: relative;
}

.schedule-tab-btn:hover {
  color: var(--text-primary);
  background: rgba(240, 246, 252, 0.03);
}

.schedule-tab-btn[aria-selected="true"] {
  color: var(--text-primary);
  background: rgba(88, 166, 255, 0.1);
  border-color: var(--accent);
}

.schedule-tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 0.25rem;
  font-size: 0.625rem;
  font-weight: 600;
  background: var(--accent);
  color: white;
  border-radius: 8px;
  line-height: 1;
}

/* Schedule Card */
.schedule-card {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.schedule-card-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
}

.schedule-card-name {
  flex: 1;
  font-weight: 500;
  font-size: 0.9375rem;
  color: var(--text-primary);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.schedule-card-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.schedule-trigger-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  padding: 0;
}

.schedule-trigger-btn:hover {
  background: rgba(63, 185, 80, 0.15);
  color: var(--success);
}

.schedule-delete-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  padding: 0;
}

.schedule-delete-btn:hover {
  background: rgba(248, 81, 73, 0.15);
  color: var(--error);
}

.schedule-card-meta {
  padding: 0 1rem 0.875rem;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.schedule-meta-row {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  min-width: 0;
}

.schedule-meta-label {
  color: var(--text-muted);
  flex-shrink: 0;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.schedule-meta-value {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.schedule-meta-value.prompt-value {
  font-family: var(--font-mono);
  font-size: 0.75rem;
}

.schedule-last-run {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.75rem;
}

.schedule-last-run .run-status {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.schedule-last-run .run-status.success {
  background: var(--success);
}

.schedule-last-run .run-status.failure {
  background: var(--error);
}

.schedule-last-run .run-status.pending {
  background: var(--text-muted);
}

/* Schedule Runs */
.schedule-runs-toggle {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  font-family: var(--font-sans);
  background: var(--bg-elevated);
  color: var(--text-muted);
  border: none;
  border-top: 1px solid var(--border);
  cursor: pointer;
  text-align: left;
  transition: color var(--transition);
}

.schedule-runs-toggle:hover {
  color: var(--text-secondary);
}

.schedule-runs-list {
  border-top: 1px solid var(--border);
  max-height: 200px;
  overflow-y: auto;
}

.run-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  font-family: var(--font-mono);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background var(--transition);
  border-bottom: 1px solid var(--border);
}

.run-item:last-child {
  border-bottom: none;
}

.run-item:hover {
  background: rgba(240, 246, 252, 0.03);
}

.run-item-status {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.run-item-status.success {
  background: var(--success);
}

.run-item-status.failure {
  background: var(--error);
}

.run-item-status.running {
  background: var(--warning);
}

.run-item-time {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-item-duration {
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Schedule Form */
.schedule-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.schedule-form input,
.schedule-form textarea {
  width: 100%;
  padding: 0.625rem 0.75rem;
  font-size: 0.875rem;
  font-family: var(--font-mono);
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition), box-shadow var(--transition);
  margin-bottom: 0;
}

.schedule-form textarea {
  resize: vertical;
  min-height: 60px;
  font-family: var(--font-mono);
  line-height: 1.5;
}

.schedule-form input::placeholder,
.schedule-form textarea::placeholder {
  color: var(--text-muted);
}

.schedule-form input:focus,
.schedule-form textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}


.schedule-form .autocomplete-wrapper {
  margin-bottom: 0;
}

/* Run Log Modal */
.run-log-content {
  max-width: 640px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.run-log-pre {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 60vh;
  margin: 0;
}

/* Responsive: Mobile schedule styles */
@media (max-width: 600px) {
  .schedules-header {
    padding: 0.5rem 0.75rem;
    padding-top: max(0.5rem, env(safe-area-inset-top));
    gap: 0.5rem;
  }

  .schedules-list {
    padding: 0.75rem;
    gap: 0.5rem;
  }

  .schedule-card-header {
    padding: 0.75rem;
  }

  .schedule-card-meta {
    padding: 0 0.75rem 0.75rem;
  }

  .run-log-content {
    max-height: 90vh;
  }

  .run-log-pre {
    max-height: 70vh;
  }
}
