/* ─── Passaggio Codex — Mobile PWA Styles ────────────────────────────────── */
/* Mobile-first design with Tailwind CDN + custom overrides                    */

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #16213e;
  --bg-card: #1e1e36;
  --bg-input: #2a2a4a;
  --text-primary: #e8e8f0;
  --text-secondary: #9898b8;
  --text-muted: #6b6b8a;
  --accent-green: #22c55e;
  --accent-red: #ef4444;
  --accent-blue: #3b82f6;
  --accent-yellow: #eab308;
  --accent-purple: #a855f7;
  --border-color: #2a2a4a;
  --success-bg: rgba(34, 197, 94, 0.1);
  --error-bg: rgba(239, 68, 68, 0.1);
  --warning-bg: rgba(234, 179, 8, 0.1);
  --tab-height: 56px;
  --header-height: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'SF Pro', 'Inter', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
  height: 100dvh;
  height: 100vh;
  width: 100vw;
  position: fixed;
  top: 0;
  left: 0;
  -webkit-font-smoothing: antialiased;
}

/* ─── Connection Banner ──────────────────────────────────────────────────── */

#connection-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;
  height: var(--header-height);
  padding-top: var(--safe-top);
}

#connection-banner.connected {
  background: var(--success-bg);
  color: var(--accent-green);
}

#connection-banner.disconnected {
  background: var(--error-bg);
  color: var(--accent-red);
}

#connection-banner.reconnecting {
  background: var(--warning-bg);
  color: var(--accent-yellow);
}

.connection-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.connection-dot.connected { background: var(--accent-green); }
.connection-dot.disconnected { background: var(--accent-red); }
.connection-dot.reconnecting { background: var(--accent-yellow); animation: pulse 1s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ─── App Container ──────────────────────────────────────────────────────── */

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
  padding-top: var(--header-height);
}

/* ─── Tab Content ────────────────────────────────────────────────────────── */

.tab-content {
  flex: 1;
  overflow: hidden;
  display: none;
  position: relative;
}

.tab-content.active {
  display: flex;
  flex-direction: column;
}

/* ─── Bottom Tab Bar ──────────────────────────────────────────────────────── */

#tab-bar {
  display: flex;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  height: var(--tab-height);
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
  z-index: 50;
}

.tab-button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  padding: 4px 0;
  -webkit-user-select: none;
  user-select: none;
}

.tab-button .tab-icon {
  font-size: 20px;
  line-height: 1;
}

.tab-button.active {
  color: var(--accent-blue);
}

.tab-button:active {
  opacity: 0.7;
}

/* ─── Chat Tab ────────────────────────────────────────────────────────────── */

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  animation: messageIn 0.2s ease;
}

@keyframes messageIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  align-self: flex-end;
  background: var(--accent-blue);
  color: white;
  border-bottom-right-radius: 4px;
}

.message.agent {
  align-self: flex-start;
  background: var(--bg-card);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.message.system {
  align-self: center;
  background: var(--bg-tertiary);
  color: var(--text-muted);
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 20px;
}

.message.error {
  align-self: center;
  background: var(--error-bg);
  color: var(--accent-red);
  border: 1px solid var(--accent-red);
}

.message.agent .tool-call {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-input);
  padding: 4px 8px;
  border-radius: 6px;
  margin-top: 6px;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.message.agent .tool-result {
  font-size: 12px;
  color: var(--accent-green);
  background: var(--success-bg);
  padding: 4px 8px;
  border-radius: 6px;
  margin-top: 4px;
}

.message .typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.message .typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typing 1.4s infinite;
}

.message .typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.message .typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ─── Chat Input ──────────────────────────────────────────────────────────── */

#chat-input-container {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: calc(8px + var(--safe-bottom));
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
}

#chat-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  color: var(--text-primary);
  font-size: 15px;
  padding: 10px 16px;
  outline: none;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  font-family: inherit;
  line-height: 1.4;
}

#chat-input:focus {
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

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

.chat-action-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

#send-btn {
  background: var(--accent-blue);
  color: white;
}

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

#mic-btn {
  background: var(--bg-card);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

#mic-btn.listening {
  background: var(--accent-red);
  color: white;
  border-color: var(--accent-red);
  animation: micPulse 1.5s infinite;
}

@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

/* ─── Files Tab ───────────────────────────────────────────────────────────── */

#files-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#files-header .path {
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

#files-header .refresh-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
}

#files-tree {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  -webkit-overflow-scrolling: touch;
}

.file-tree-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 14px;
  -webkit-user-select: none;
  user-select: none;
}

.file-tree-item:active {
  background: var(--bg-tertiary);
}

.file-tree-item .icon {
  font-size: 16px;
  width: 20px;
  text-align: center;
  flex-shrink: 0;
}

.file-tree-item .name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-tree-item .size {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.file-tree-item.directory {
  font-weight: 500;
}

.file-tree-item .chevron {
  font-size: 12px;
  color: var(--text-muted);
  transition: transform 0.2s;
  flex-shrink: 0;
}

.file-tree-item .chevron.open {
  transform: rotate(90deg);
}

/* ─── File Content Viewer ────────────────────────────────────────────────── */

#file-viewer {
  display: none;
  flex-direction: column;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-primary);
  z-index: 10;
}

#file-viewer.open {
  display: flex;
}

#file-viewer-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#file-viewer-header .back-btn {
  background: none;
  border: none;
  color: var(--accent-blue);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
}

#file-viewer-header .file-name {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#file-viewer-header .edit-btn {
  background: var(--accent-blue);
  border: none;
  color: white;
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 12px;
  cursor: pointer;
}

#file-content {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-primary);
  -webkit-overflow-scrolling: touch;
}

/* ─── Terminal Tab ────────────────────────────────────────────────────────── */

#terminal-container {
  flex: 1;
  background: #000;
  overflow: hidden;
}

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

#terminal-container .xterm-viewport {
  scrollbar-width: thin;
  scrollbar-color: var(--bg-input) transparent;
}

#terminal-actions {
  display: flex;
  gap: 8px;
  padding: 6px 12px;
  padding-bottom: calc(6px + var(--safe-bottom));
  background: #000;
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.terminal-btn {
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
}

.terminal-btn:active {
  background: var(--bg-tertiary);
}

/* ─── Status Tab ──────────────────────────────────────────────────────────── */

#status-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.status-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
}

.status-card h3 {
  font-size: 13px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.status-card .row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  font-size: 14px;
}

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

.status-card .row .value {
  color: var(--text-primary);
  font-weight: 500;
}

.status-card .row .value.green { color: var(--accent-green); }
.status-card .row .value.red { color: var(--accent-red); }
.status-card .row .value.yellow { color: var(--accent-yellow); }

/* ─── Approval Modal ──────────────────────────────────────────────────────── */

#approval-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  z-index: 200;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: calc(40px + var(--safe-bottom));
}

#approval-overlay.open {
  display: flex;
}

#approval-modal {
  background: var(--bg-secondary);
  border-radius: 16px 16px 0 0;
  padding: 24px 20px;
  padding-bottom: calc(24px + var(--safe-bottom));
  max-width: 400px;
  width: 100%;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

#approval-modal .modal-icon {
  font-size: 40px;
  text-align: center;
  margin-bottom: 12px;
}

#approval-modal h2 {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  margin-bottom: 4px;
}

#approval-modal .modal-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 16px;
}

#approval-modal .command-box {
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  color: var(--text-primary);
  margin-bottom: 8px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

#approval-modal .description-box {
  background: var(--bg-tertiary);
  border-radius: 8px;
  padding: 10px;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 16px;
  line-height: 1.4;
}

#approval-modal .approval-actions {
  display: flex;
  gap: 12px;
}

.approval-btn {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.approval-btn.approve {
  background: var(--accent-green);
  color: white;
}

.approval-btn.reject {
  background: var(--accent-red);
  color: white;
}

.approval-btn:active {
  transform: scale(0.97);
  opacity: 0.9;
}

/* ─── Scrollbar Styling ──────────────────────────────────────────────────── */

::-webkit-scrollbar {
  width: 4px;
}

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

::-webkit-scrollbar-thumb {
  background: var(--bg-input);
  border-radius: 2px;
}

/* ─── Media Queries ───────────────────────────────────────────────────────── */

@media (min-width: 768px) {
  #app {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
  }
}

@media (display-mode: standalone) {
  #connection-banner {
    padding-top: calc(6px + var(--safe-top));
  }
}

/* ─── Code Highlighting (Lightweight) ─────────────────────────────────────── */

.code-keyword { color: #c792ea; }
.code-string { color: #c3e88d; }
.code-number { color: #f78c6c; }
.code-comment { color: #546e7a; font-style: italic; }
.code-function { color: #82aaff; }
.code-operator { color: #89ddff; }
.code-property { color: #f07178; }