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

:root {
  --bg: #1a1a2e;
  --bg-surface: #16213e;
  --bg-input: #0f3460;
  --bg-msg-user: #0f3460;
  --bg-msg-assistant: #1a1a2e;
  --bg-tool: #1b2838;
  --bg-permission: #2a1a3e;
  --text: #e0e0e0;
  --text-dim: #8892a0;
  --text-code: #a8d8ea;
  --accent: #e94560;
  --accent-green: #4ecca3;
  --accent-yellow: #f0c040;
  --border: #2a2a4a;
  --radius: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  height: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ===== Header ===== */
header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

#header-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}
.icon-btn:active { background: var(--border); }

/* Token usage in header */
.token-usage {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  background: var(--bg-input);
  border-radius: 12px;
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
  letter-spacing: 0.2px;
}

/* Connection dot */
.dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot.connected    { background: var(--accent-green); }
.dot.disconnected { background: var(--accent); }
.dot.connecting   { background: var(--accent-yellow); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

/* ===== Screens ===== */
.screen {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===== Settings ===== */
.settings-form {
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 480px;
  margin: 0 auto;
}

.settings-form label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.settings-form input {
  padding: 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  outline: none;
}
.settings-form input:focus { border-color: var(--accent); }

.primary-btn {
  padding: 14px 20px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.primary-btn:active { opacity: 0.8; }

/* ===== Sessions list ===== */
#screen-sessions {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#sessions-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}

.session-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: border-color 0.15s;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}
.session-card-info {
  flex: 1;
  min-width: 0;
}
.session-card:active { border-color: var(--accent); }

.session-card-info .title {
  font-weight: 600;
  margin-bottom: 4px;
}
.session-card-info .meta {
  font-size: 12px;
  color: var(--text-dim);
}
.session-card-info .status {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  margin-top: 6px;
  font-weight: 500;
}
.status-pending { background: var(--accent-yellow); color: #1a1a2e; }
.status-active  { background: var(--accent-green); color: #1a1a2e; }

.btn-clear-session {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.btn-clear-session:hover {
  background: #e74c3c;
  border-color: #e74c3c;
  color: #fff;
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  padding: 48px 16px;
  font-size: 14px;
}

/* ===== Status bar ===== */
.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--accent-yellow);
  flex-shrink: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.status-bar-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-yellow);
  animation: pulse 1s infinite;
  flex-shrink: 0;
}

.status-bar.status-idle {
  color: var(--accent-green);
}
.status-bar.status-idle .status-bar-dot {
  background: var(--accent-green);
  animation: none;
}

.stop-btn {
  margin-left: auto;
  padding: 2px 10px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.stop-btn:active { opacity: 0.7; }
.stop-btn:disabled { opacity: 0.4; cursor: default; }

/* ===== Chat ===== */
#screen-chat {
  display: flex;
  flex-direction: column;
}

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

/* Message bubbles */
.msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg-user {
  background: var(--bg-msg-user);
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}

.msg-assistant {
  background: var(--bg-msg-assistant);
  border: 1px solid var(--border);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}

.msg-system {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
  font-style: italic;
  padding: 6px 12px;
}

/* Tool use blocks */
.tool-block {
  background: var(--bg-tool);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent-yellow);
  border-radius: 6px;
  padding: 10px;
  margin: 6px 0;
  font-size: 13px;
  max-width: 92%;
  align-self: flex-start;
}

.tool-block .tool-header {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  color: var(--accent-yellow);
  margin-bottom: 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.tool-block .tool-input {
  background: rgba(0,0,0,0.3);
  border-radius: 4px;
  padding: 8px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 12px;
  color: var(--text-code);
  white-space: pre-wrap;
  overflow-x: auto;
  max-height: 200px;
  overflow-y: auto;
}

/* Tool result blocks */
.tool-result-block {
  background: var(--bg-tool);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-dim);
  border-radius: 6px;
  padding: 10px;
  margin: 6px 0;
  font-size: 13px;
  max-width: 92%;
  align-self: flex-start;
}

.tool-result-block .result-header {
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.tool-result-block .result-content {
  background: rgba(0,0,0,0.3);
  border-radius: 4px;
  padding: 8px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 12px;
  color: var(--text-code);
  white-space: pre-wrap;
  overflow-x: auto;
  max-height: 200px;
  overflow-y: auto;
}

/* Permission prompt */
.permission-prompt {
  background: var(--bg-permission);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px;
  margin: 6px 0;
  max-width: 92%;
  align-self: flex-start;
}

.permission-prompt .perm-header {
  font-weight: 600;
  color: var(--accent);
  font-size: 13px;
  margin-bottom: 8px;
}

.permission-prompt .perm-detail {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.permission-prompt .perm-input {
  background: rgba(0,0,0,0.3);
  border-radius: 4px;
  padding: 8px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 12px;
  color: var(--text-code);
  white-space: pre-wrap;
  margin: 8px 0;
  max-height: 150px;
  overflow-y: auto;
}

.permission-prompt .perm-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.perm-btn {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.perm-btn:active { opacity: 0.8; }
.perm-btn-allow { background: var(--accent-green); color: #1a1a2e; }
.perm-btn-deny  { background: var(--accent); color: white; }
.perm-btn:disabled { opacity: 0.4; cursor: default; }

/* AskUserQuestion prompt */
.question-prompt {
  background: var(--bg-permission);
  border: 1px solid var(--accent-yellow);
  border-radius: var(--radius);
  padding: 14px;
  margin: 6px 0;
  max-width: 92%;
  align-self: flex-start;
}

.question-prompt .perm-header {
  font-weight: 600;
  color: var(--accent-yellow);
  font-size: 13px;
  margin-bottom: 8px;
}

.question-prompt .perm-buttons {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.q-block { margin-bottom: 14px; }
.q-block:last-of-type { margin-bottom: 0; }

.q-chip {
  display: inline-block;
  background: var(--bg-input);
  color: var(--accent-yellow);
  font-size: 11px;
  font-weight: 600;
  border-radius: 10px;
  padding: 2px 8px;
  margin-bottom: 6px;
}

.q-text {
  font-size: 14px;
  margin-bottom: 8px;
}

.q-option {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 6px;
  color: inherit;
  font-size: 13px;
  cursor: pointer;
}
.q-option.selected {
  border-color: var(--accent-green);
  background: rgba(78, 204, 163, 0.12);
}
.q-option:disabled { opacity: 0.5; cursor: default; }
.q-option-label { font-weight: 600; }
.q-option-desc {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.q-other {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  color: inherit;
  font-size: 13px;
}
.q-other:focus { border-color: var(--accent-yellow); outline: none; }
.q-other:disabled { opacity: 0.5; }

.prompt-dismissed-note {
  margin-top: 10px;
  font-size: 12px;
  font-style: italic;
  color: var(--text-dim);
}

/* Result block */
.result-block {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  text-align: center;
}
.result-block .cost {
  font-size: 11px;
  margin-top: 2px;
  color: var(--accent-yellow);
}

/* Markdown in messages */
.msg code {
  background: rgba(0,0,0,0.3);
  padding: 2px 5px;
  border-radius: 3px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  color: var(--text-code);
}

.msg pre {
  background: rgba(0,0,0,0.4);
  border-radius: 6px;
  padding: 10px;
  margin: 8px 0;
  overflow-x: auto;
  font-size: 12px;
}

.msg pre code {
  background: none;
  padding: 0;
  font-size: 12px;
}

.msg strong { color: #fff; }
.msg em { color: var(--text-dim); }

.msg ul, .msg ol {
  padding-left: 20px;
  margin: 4px 0;
}

.msg p + p { margin-top: 8px; }

/* Streaming indicator */
.streaming-dot::after {
  content: '';
  display: inline-block;
  width: 6px; height: 6px;
  background: var(--accent-green);
  border-radius: 50%;
  margin-left: 6px;
  animation: pulse 0.8s infinite;
  vertical-align: middle;
}

/* ===== Input area ===== */
#chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

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

.send-btn {
  width: 42px; height: 42px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: white;
  flex-shrink: 0;
}
.send-btn:active { opacity: 0.7; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
