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

:root {
  /* Colors - OnClaud warm theme */
  --bg-primary: #1c1917;
  --bg-secondary: #292524;
  --bg-tertiary: #0c0a09;
  --text-primary: #fafaf9;
  --text-secondary: #a8a29e;
  --text-muted: #78716c;
  --accent-primary: #ea580c;
  --accent-secondary: #fb923c;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --border: #44403c;

  /* Spacing */
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --input-height: 60px;
  --status-height: 32px;
  --input-area-total: 150px; /* Input + nav keys + padding */
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  position: fixed;
  width: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh; /* Dynamic viewport height - adapts to Firefox address bar */
  max-height: 100dvh;
  width: 100vw;
  max-width: 100vw;
  padding-top: var(--safe-top);
  overflow: hidden;
}

/* Status Bar */
#status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: var(--status-height);
  padding: 0 12px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  flex-shrink: 0;
}

#connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warning);
  transition: background 0.3s;
}

.status-dot.connected {
  background: var(--success);
}

.status-dot.disconnected {
  background: var(--error);
}

.status-text {
  color: var(--text-secondary);
}

#pty-status {
  color: var(--text-muted);
}

#status-buttons {
  display: flex;
  gap: 6px;
}

.status-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.status-btn:active {
  transform: scale(0.95);
}

/* Stop button - red */
#stop-btn {
  background: var(--error);
}

#stop-btn:hover {
  background: #dc2626;
}

/* Toggle buttons - gray/green */
#notify-btn,
#auto-btn-toggle,
#nav-toggle-btn {
  background: #3f3f46;
  font-size: 14px;
  padding: 2px 8px;
}

#notify-btn:hover,
#auto-btn-toggle:hover,
#nav-toggle-btn:hover {
  background: #52525b;
}

#notify-btn.notify-on,
#auto-btn-toggle.notify-on,
#nav-toggle-btn.notify-on {
  background: var(--success);
}

#notify-btn.notify-on:hover,
#auto-btn-toggle.notify-on:hover,
#nav-toggle-btn.notify-on:hover {
  background: #16a34a;
}

/* Terminal Container */
#terminal-container {
  /* Use dvh instead of flex for Firefox compatibility */
  height: calc(100dvh - var(--status-height) - var(--input-area-total) - var(--safe-top) - var(--safe-bottom));
  max-height: calc(100dvh - var(--status-height) - var(--input-area-total) - var(--safe-top) - var(--safe-bottom));
  min-height: 200px; /* Ensure minimum usable space */

  /* Explicit viewport constraints for Firefox */
  width: 100vw;
  max-width: 100vw;

  overflow: hidden;
  position: relative;
  background: #000;
  padding: 0;
}

/* Terminal Element - xterm.js container */
#terminal {
  height: 100%;
  width: 100%;
  padding: 2px;
  overflow: hidden;
}

/* xterm.js overrides for mobile - Native scrolling enabled */
.xterm {
  height: 100%;
  width: 100% !important;
}

/* Enable native momentum scrolling (iOS/Android) */
.xterm-viewport {
  overflow-y: auto !important; /* Allow native scrolling */
  -webkit-overflow-scrolling: touch; /* Enable iOS "flick" momentum */
  overscroll-behavior-y: contain; /* Prevent pull-to-refresh on page */
  width: 100% !important;
}

/* Hide scrollbar but keep functionality */
.xterm-viewport::-webkit-scrollbar {
  display: none;
}

.xterm-screen {
  height: 100% !important;
  width: 100% !important;
  max-width: 100vw !important;
  overflow: hidden !important;
}

.xterm-rows {
  width: 100% !important;
}

/* Welcome Screen */
#welcome, #exit-screen {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
  z-index: 10;
}

.welcome-content, .exit-content {
  text-align: center;
  padding: 24px;
}

.welcome-content h1 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.welcome-content p {
  color: var(--text-secondary);
  margin-bottom: 32px;
}

.exit-content h2 {
  font-size: 24px;
  margin-bottom: 12px;
}

.exit-content p {
  color: var(--text-secondary);
  margin-bottom: 24px;
}

#login-form,
#start-options {
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

#last-dir-label {
  font-size: 13px;
  color: var(--accent-secondary);
  background: var(--bg-secondary);
  padding: 6px 12px;
  border-radius: 6px;
  margin-bottom: 4px;
}

#last-dir-name {
  font-weight: 600;
  color: var(--text-primary);
}

#login-user,
#login-pass,
#start-dir,
#start-args {
  width: 100%;
  max-width: 300px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
}

#login-user::placeholder,
#login-pass::placeholder,
#start-dir::placeholder,
#start-args::placeholder {
  color: var(--text-muted);
}

#login-error {
  color: var(--error);
  font-size: 14px;
  text-align: center;
}

.error-text {
  color: var(--error);
}

.primary-btn {
  padding: 16px 48px;
  background: var(--accent-primary);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

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

.primary-btn:active {
  transform: scale(0.98);
}

/* Input Area */
#input-area {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding: 12px;
  padding-bottom: calc(12px + var(--safe-bottom));
  flex-shrink: 0;
}

/* Navigation Keys */
#nav-keys {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.nav-btn {
  flex: 1;
  min-width: 60px;
  padding: 12px 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.nav-btn:hover {
  background: var(--bg-primary);
  border-color: var(--accent-primary);
}

.nav-btn:active {
  transform: scale(0.95);
  background: var(--accent-primary);
}

/* Options Container */
#options-container {
  margin-bottom: 12px;
}

#options-prompt {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  padding: 0 4px;
}

#options-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.option-btn {
  flex: 1;
  min-width: calc(50% - 4px);
  max-width: 100%;
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  min-height: 48px;
}

.option-btn:hover {
  background: var(--bg-primary);
  border-color: var(--accent-primary);
}

.option-btn:active {
  transform: scale(0.98);
  background: var(--accent-primary);
}

/* Single option (Continue button) should be full width */
#options-buttons .option-btn:only-child {
  min-width: 100%;
}

/* Text Input */
#text-input-container {
  display: flex;
  gap: 8px;
}

#text-input {
  flex: 1;
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

#text-input:focus {
  border-color: var(--accent-primary);
}

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

#send-btn {
  padding: 14px 20px;
  background: var(--accent-primary);
  border: none;
  border-radius: 10px;
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  min-width: 70px;
}

#send-btn:hover {
  background: var(--accent-secondary);
}

#send-btn:active {
  transform: scale(0.98);
}

#send-btn:disabled {
  background: var(--bg-tertiary);
  color: var(--text-muted);
  cursor: not-allowed;
}

/* Utility classes */
.hidden {
  display: none !important;
}

/* Scrollbar styling */
#terminal::-webkit-scrollbar {
  width: 6px;
}

#terminal::-webkit-scrollbar-track {
  background: transparent;
}

#terminal::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

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

/* Mobile keyboard handling */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #input-area {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
}

/* Landscape orientation adjustments */
@media (orientation: landscape) and (max-height: 500px) {
  #terminal {
    font-size: 12px;
  }

  .option-btn {
    padding: 10px 12px;
    min-height: 40px;
  }

  #text-input, #send-btn {
    padding: 10px 14px;
  }
}

/* Larger screens */
@media (min-width: 768px) {
  #terminal {
    font-size: 14px;
    padding: 16px;
  }

  .option-btn {
    min-width: auto;
    flex: 0 1 auto;
    padding: 12px 24px;
  }
}

/* Auth Button - for OAuth links */
#url-banner {
  position: fixed;
  bottom: calc(var(--input-area-total) + var(--safe-bottom) + 16px);
  right: 12px;
  display: flex;
  gap: 0;
  z-index: 100;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  border-radius: 8px;
  overflow: hidden;
}

#url-open-btn {
  background: var(--accent-primary);
  border: none;
  padding: 12px 20px;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

#url-open-btn:active {
  background: var(--accent-secondary);
}

#url-close-btn {
  background: var(--bg-tertiary);
  border: none;
  border-left: 1px solid var(--border);
  padding: 12px 14px;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
}

#url-close-btn:active {
  background: var(--bg-secondary);
}
