*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-hover: #1c2333;
  --border: #30363d;
  --user-bubble: #1f6feb;
  --assistant-bubble: #1c2333;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #58a6ff;
  --danger: #f85149;
  --success: #3fb950;
  --radius: 12px;
}

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

body {
  display: flex;
  flex-direction: column;
}

/* ===== Screens ===== */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.screen-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.screen-header h1 {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
}

.screen-header button {
  background: var(--surface-hover);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

.screen-header button:active {
  background: var(--border);
}

/* ===== Status dot ===== */
#status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  flex-shrink: 0;
}

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

#status-dot.thinking {
  background: var(--accent);
  animation: pulse 1s infinite;
}

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

/* ===== Session List ===== */
#session-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  gap: 4px;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.session-item:active {
  background: var(--surface-hover);
}

.session-avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--surface-hover);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  flex-shrink: 0;
}

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

.session-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-preview {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.session-meta {
  text-align: right;
  flex-shrink: 0;
}

.session-time {
  font-size: 11px;
  color: var(--text-muted);
}

.session-badge {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 10px;
  margin-top: 4px;
}

/* ===== Chat Header ===== */
#back-btn {
  font-size: 18px;
  padding: 8px 10px !important;
}

#chat-header-info {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

#chat-title {
  display: block;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chat-subtitle {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chat-model-select {
  background: var(--surface-hover);
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12px;
  font-weight: 600;
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 0;
}

/* ===== Tab bar ===== */
#tab-bar {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.tab {
  flex: 1;
  padding: 8px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ===== Terminal ===== */
#term-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  line-height: 1.5;
}

.term-block {
  margin-bottom: 8px;
}

.term-cmd {
  color: var(--accent);
}

.term-out {
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
}

.term-err {
  color: var(--danger);
  white-space: pre-wrap;
}

#term-footer {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#term-prompt {
  color: var(--accent);
  font-family: 'SF Mono', monospace;
  font-weight: 700;
  font-size: 14px;
}

#term-input {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  font-family: 'SF Mono', monospace;
  outline: none;
}

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

#term-run {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

/* ===== Thinking Bar (indeterminate progress) ===== */
#thinking-bar {
  position: relative;
  height: 2px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  flex-shrink: 0;
}

.indeterminate-bar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 30%;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: indeterminate 1.5s infinite ease-in-out;
}

@keyframes indeterminate {
  0%   { left: -30%; }
  100% { left: 100%; }
}

/* ===== Chat Area ===== */
#chat-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#term-area {
  min-height: 0;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
}

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

.message.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  border-bottom-left-radius: 4px;
}

.message.error {
  align-self: center;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

.message .meta {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
}

.message code {
  background: rgba(255, 255, 255, 0.1);
  padding: 2px 5px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
}

.message pre {
  background: rgba(0, 0, 0, 0.3);
  padding: 10px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 6px 0;
}

.message pre code {
  background: none;
  padding: 0;
}

/* ===== Command autocomplete ===== */
#cmd-popup {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  max-height: 200px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 12px 12px 0 0;
  z-index: 50;
}

.cmd-item {
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}

.cmd-item:active, .cmd-item.active {
  background: var(--surface-hover);
}

.cmd-item .cmd-name {
  color: var(--accent);
  font-weight: 600;
}

/* ===== Image preview ===== */
#image-preview {
  display: flex;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
}

#image-preview img {
  height: 60px;
  border-radius: 8px;
  object-fit: cover;
}

.img-remove {
  position: relative;
}

.img-remove::after {
  content: '×';
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--danger);
  color: #fff;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ===== Input Area ===== */
#attach-btn {
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  padding: 8px 4px;
  flex-shrink: 0;
}

#attach-btn:active {
  color: var(--accent);
}

footer {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

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

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

#plan-toggle {
  background: transparent;
  color: var(--text-muted);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.15s;
}

#plan-toggle.active {
  background: #f59e0b;
  color: #000;
  border-color: #f59e0b;
}

#send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#send-btn:active:not(:disabled) {
  opacity: 0.8;
}

/* ===== Theme grid ===== */
#theme-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.theme-swatch {
  aspect-ratio: 1;
  border-radius: 12px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s;
}

.theme-swatch.active {
  border-color: var(--accent);
}

#bg-upload-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-hover);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
  text-align: center;
}

#bg-clear-btn {
  background: var(--surface-hover);
  color: var(--danger);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
}

.has-bg-image #chat-area,
.has-bg-image #term-area {
  background: rgba(0, 0, 0, 0.5);
}

.has-bg-image .message.user {
  background: rgba(31, 111, 235, 0.85);
}

.has-bg-image .message.assistant {
  background: rgba(28, 35, 51, 0.85);
}

/* ===== Modals ===== */
#dir-modal,
#rename-modal,
#theme-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
  padding: 16px;
}

.modal-content {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 16px;
  padding: 20px;
  width: 100%;
  max-width: 400px;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-content h3 {
  font-size: 17px;
  color: var(--text);
}

#dir-current {
  font-size: 12px;
  color: #58a6ff;
  font-family: 'SF Mono', monospace;
  word-break: break-all;
  padding: 8px;
  background: #0d1117;
  border-radius: 8px;
}

#dir-list {
  overflow-y: auto;
  max-height: 40vh;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.dir-item {
  padding: 10px 12px;
  background: #0d1117;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: #e6edf3;
}

.dir-item:active {
  background: var(--surface-hover);
}

.modal-actions {
  display: flex;
  gap: 8px;
}

.modal-actions button {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  border: 1px solid var(--border);
}

#model-select {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  appearance: none;
  -webkit-appearance: none;
}

#dir-up, #rename-cancel {
  background: var(--surface-hover);
  color: var(--text);
}

#dir-select, #rename-confirm {
  background: var(--accent);
  color: #fff;
  border: none;
  font-weight: 600;
}

#rename-input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 15px;
  font-family: inherit;
  outline: none;
}

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

/* ===== Login Screen ===== */
#login-screen {
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
}

#login-screen h2 {
  font-size: 22px;
}

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

#token-input {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 14px;
  font-family: 'SF Mono', monospace;
  outline: none;
}

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

#token-submit {
  width: 100%;
  max-width: 360px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

#token-submit:active {
  opacity: 0.8;
}

/* ===== Watchdog Banner ===== */
#watchdog-banner {
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Briefing Card ===== */
#briefing-card {
  margin: 10px 12px;
  background: var(--surface);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 14px 16px;
  position: relative;
  display: none;
  flex-shrink: 0;
}

.briefing-text {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-muted);
}

.briefing-text strong {
  font-size: 16px;
  color: var(--text);
}

.briefing-dismiss {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 2px 6px;
}

/* ===== Error Badge ===== */
.session-badge.error {
  background: var(--danger);
}

/* ===== Dashboard ===== */
#dashboard-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.dash-section h2 {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
}

.dash-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.dash-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.dash-card .card-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.dash-card .card-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
  margin-top: 4px;
}

.dash-card .card-value.accent {
  color: var(--accent);
}

.dash-card .card-value.success {
  color: var(--success);
}

.dash-card .card-value.small {
  font-size: 14px;
  font-weight: 600;
  word-break: break-all;
}

.dash-activity-item {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 6px;
}

.dash-activity-item .activity-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-top: 5px;
  flex-shrink: 0;
}

.dash-activity-item .activity-dot.user {
  background: var(--user-bubble);
}

.dash-activity-item .activity-dot.assistant {
  background: var(--accent);
}

.dash-activity-item .activity-body {
  flex: 1;
  min-width: 0;
}

.dash-activity-item .activity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.dash-activity-item .activity-session {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
}

.dash-activity-item .activity-time {
  font-size: 11px;
  color: var(--text-muted);
}

.dash-activity-item .activity-text {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dash-memory-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
}

.dash-memory-item .mem-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  font-family: 'SF Mono', monospace;
}

.dash-memory-item .mem-preview {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
  white-space: pre-wrap;
  max-height: 60px;
  overflow: hidden;
}

.dash-memory-item.expanded .mem-preview {
  max-height: none;
}

.dash-empty {
  font-size: 13px;
  color: var(--text-muted);
  padding: 12px;
  text-align: center;
}

/* Rate limit bar */
#rate-limit-bar {
  padding: 6px 16px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--surface);
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.rl-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #3fb950;
  flex-shrink: 0;
}

.rl-dot.warning {
  background: #d29922;
}

/* Stop button */
#send-btn.stop-mode {
  background: #f85149 !important;
  color: #fff !important;
  opacity: 1 !important;
}
