:root,
[data-theme="dawn"] {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface2: #eef1f8;
  --border: #d8deea;
  --text: #1a1f2e;
  --muted: #5c6478;
  --accent: #5b4cdb;
  --accent2: #8b5cf6;
  --danger: #dc2626;
  --link: #4338ca;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="slate"] {
  --bg: #1c2030;
  --surface: #252a3a;
  --surface2: #2f3548;
  --border: #3d4560;
  --text: #eef0f8;
  --muted: #9aa3bc;
  --accent: #7c6cff;
  --accent2: #b06bff;
  --danger: #ef4444;
  --link: #a5b4fc;
}

[data-theme="midnight"] {
  --bg: #0a0e18;
  --surface: #121826;
  --surface2: #1a2236;
  --border: #2a3550;
  --text: #f0f2fa;
  --muted: #8b94ad;
  --accent: #6366f1;
  --accent2: #a855f7;
  --danger: #f87171;
  --link: #818cf8;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
.screen { height: 100%; display: flex; flex-direction: column; }
#setup { justify-content: center; padding: 24px; padding-top: calc(24px + var(--safe-top)); }
.logo {
  width: 72px; height: 72px; border-radius: 20px; margin: 0 auto 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: flex; align-items: center; justify-content: center;
  font-size: 2.2rem; font-weight: 700; color: #fff;
  box-shadow: 0 12px 40px rgba(124, 108, 255, 0.35);
}
#setup h1 { text-align: center; font-size: 1.5rem; }
.sub { text-align: center; color: var(--muted); font-size: 0.85rem; margin: 8px 0 28px; }
label { display: block; font-size: 0.75rem; color: var(--muted); margin: 12px 0 6px; text-transform: uppercase; letter-spacing: 0.06em; }
input, textarea, select {
  width: 100%; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text); padding: 14px 16px; font-size: 1rem;
}
textarea { resize: none; line-height: 1.4; max-height: 120px; }
.primary {
  width: 100%; margin-top: 20px; padding: 16px; border: none; border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff; font-size: 1rem; font-weight: 600;
}
.primary.small { width: auto; margin: 0; padding: 10px 16px; font-size: 0.85rem; }
.danger { width: 100%; margin-top: 12px; padding: 14px; border-radius: 14px; border: 1px solid var(--danger); background: transparent; color: var(--danger); }
.hint { margin-top: 16px; font-size: 0.72rem; color: var(--muted); text-align: center; line-height: 1.4; }
.hint.left { text-align: left; margin-top: 8px; }
code { font-size: 0.65rem; word-break: break-all; }

.app-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 12px 10px 12px 8px;
  padding-top: calc(12px + var(--safe-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.app-title { font-weight: 600; font-size: 1.05rem; }
.title-block { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.badge {
  font-size: 0.62rem; font-weight: 700; padding: 3px 7px; border-radius: 8px;
  background: rgba(34, 197, 94, 0.15); color: #16a34a;
}
[data-theme="slate"] .badge, [data-theme="midnight"] .badge { color: #4ade80; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); flex-shrink: 0; }
.dot.online { background: #22c55e; box-shadow: 0 0 8px #22c55e; }
.icon-btn {
  width: 42px; height: 42px; border: none; border-radius: 12px;
  background: var(--surface2); color: var(--text); font-size: 1.05rem; flex-shrink: 0;
}
.send-btn {
  width: 44px; height: 44px; border: none; border-radius: 22px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff; font-size: 1.2rem; font-weight: 700; flex-shrink: 0;
}
.text-btn { border: none; background: none; color: var(--accent); font-weight: 600; }

.icon-btn.text-icon {
  width: auto;
  min-width: 52px;
  padding: 0 10px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

#chat-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.sheet-menu { padding: 0 8px 12px; }
.menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: var(--surface2);
  color: var(--text);
  padding: 14px 16px;
  border-radius: 12px;
  margin-bottom: 8px;
  font-size: 0.92rem;
  font-weight: 500;
}
.menu-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 16px 16px;
  flex-wrap: wrap;
}

.activity-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 12px 8px;
  padding: 10px 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
  cursor: pointer;
}
.activity-icon {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  background: #94a3b8;
}
.activity-icon.thinking { background: #f59e0b; animation: pulse 0.9s infinite; }
.activity-icon.working { background: #22c55e; animation: pulse 1.1s infinite; }
.activity-icon.queued { background: #6366f1; }
.activity-text { flex: 1; font-size: 0.82rem; font-weight: 600; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.activity-timer { font-size: 0.72rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.live-sheet { max-height: 55vh; }
.live-empty-hint {
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
  padding: 16px;
}
.live-empty-hint.hidden { display: none; }

.live-log {
  overflow-y: auto;
  padding: 4px 16px 10px;
  font-size: 0.8rem;
  line-height: 1.45;
  max-height: 36vh;
}
.live-line {
  display: grid;
  grid-template-columns: 20px 1fr auto;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  align-items: start;
}
.live-line.active .live-msg { color: var(--text); font-weight: 500; }
.live-line.ok .live-step { color: #16a34a; }
.live-line.err .live-step { color: var(--danger); }
.live-line .ts { font-size: 0.65rem; color: var(--muted); }

.msg-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  font-size: 0.68rem;
}
.msg-who { font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.msg-time { color: var(--muted); opacity: 0.8; }
.msg-body { line-height: 1.5; }

.msg.user .msg-meta { flex-direction: row-reverse; }
.msg.user .msg-who { color: rgba(255,255,255,0.75); }
.msg.user .msg-time { color: rgba(255,255,255,0.6); }

.msg.action-card {
  align-self: stretch;
  max-width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 14px;
  padding: 12px 14px;
}
.msg.action-card .msg-body {
  font-size: 0.85rem;
  max-height: 200px;
  overflow-y: auto;
}

.typing {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px;
  font-size: 0.78rem;
  color: var(--muted);
}
.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 0.8s infinite;
}

#messages {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 14px; display: flex; flex-direction: column; gap: 14px;
}
.msg {
  max-width: 88%; padding: 12px 14px; border-radius: 18px;
  font-size: 0.95rem;
}
.msg.assistant a { color: var(--link); text-decoration: underline; }
.msg.user a { color: #fff; }
  background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff;
}
.msg.assistant {
  align-self: flex-start; border-bottom-left-radius: 6px;
  background: var(--surface); border: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
}
.msg.system {
  align-self: center; max-width: 96%; font-size: 0.8rem; color: var(--muted);
  background: var(--surface2); border: none; padding: 10px 12px;
}

.msg-media { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.msg-media img {
  max-width: 100%; border-radius: 12px; max-height: 220px; object-fit: cover;
  border: 1px solid var(--border);
}
.msg-links { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.link-card {
  display: block; padding: 10px 12px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--link); font-size: 0.85rem; text-decoration: none;
}
.link-card:active { opacity: 0.8; }

.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 10px 10px calc(10px + var(--safe-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
}
.composer textarea { flex: 1; border-radius: 22px; padding: 12px 16px; min-height: 44px; }

.file-native {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.attach-label { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.version-tag {
  font-size: 0.62rem; font-weight: 700; padding: 2px 6px; border-radius: 6px;
  background: var(--surface2); color: var(--muted); letter-spacing: 0.04em;
}

.totem-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; background: var(--surface2);
  border-bottom: 1px solid var(--border); font-size: 0.78rem;
}
.totem-pulse {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
  background: #94a3b8;
}
.totem-pulse.working { background: #22c55e; box-shadow: 0 0 10px #22c55e; animation: pulse 1.2s infinite; }
.totem-pulse.thinking { background: #f59e0b; animation: pulse 0.8s infinite; }
.totem-pulse.queued { background: #6366f1; }
.totem-pulse.idle { background: #22c55e; opacity: 0.6; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }
#totem-label { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.totem-text { flex: 1; min-width: 0; }
.totem-detail { display: block; font-size: 0.68rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.activity-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; background: rgba(34, 197, 94, 0.12);
  border-bottom: 1px solid var(--border); font-size: 0.78rem;
  animation: pulseBar 1.5s ease infinite;
}
@keyframes pulseBar { 0%,100% { opacity: 1; } 50% { opacity: 0.7; } }
#activity-text { flex: 1; min-width: 0; font-weight: 600; }
.activity-timer { font-size: 0.7rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.tool-status {
  text-align: center; font-size: 0.75rem; color: #7c6cff;
  padding: 6px 12px; background: rgba(124, 108, 255, 0.08);
  border-top: 1px solid var(--border);
}
.tool-row {
  padding: 12px 14px; border-radius: 12px; margin-bottom: 6px;
  background: var(--surface2); font-size: 0.82rem;
  display: flex; justify-content: space-between; gap: 8px; align-items: center;
}
.tool-row .ok { color: #16a34a; font-weight: 700; }
.tool-row .fail { color: var(--danger); font-weight: 700; }

.typing { text-align: center; font-size: 0.75rem; color: var(--muted); padding: 4px; }

.action-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 12px; background: rgba(124, 108, 255, 0.12);
  border-bottom: 1px solid var(--border); font-size: 0.82rem;
}
.action-bar span { flex: 1; min-width: 120px; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--surface); border-radius: 20px 20px 0 0;
  max-height: 70vh; display: flex; flex-direction: column;
  padding-bottom: var(--safe-bottom);
  box-shadow: 0 -8px 40px rgba(0,0,0,0.15);
  animation: slideUp 0.25s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle { width: 36px; height: 4px; background: var(--border); border-radius: 2px; margin: 10px auto; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 20px 12px; font-weight: 600; }
#chat-list, .files-list { overflow-y: auto; padding: 0 12px 16px; }
.chat-item, .file-item {
  flex: 1;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface2);
  color: var(--muted);
  cursor: pointer;
}
.chat-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.chat-row.active .chat-item { border: 1px solid var(--accent); color: var(--text); }
.chat-del, .chat-tab-del {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  padding: 6px 10px;
  cursor: pointer;
  line-height: 1;
}
.chat-del:active, .chat-tab-del:active { color: #e11d48; }
.chat-tab-wrap {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  border-radius: 12px;
  border: 1px solid var(--border);
  overflow: hidden;
}
.chat-tab-wrap.active { border-color: var(--accent); }
.chat-tab-wrap .chat-tab { border: none; border-radius: 0; }
.chat-tab-del { padding: 4px 8px; font-size: 0.9rem; }
.file-item {
  padding: 14px 16px; border-radius: 14px; margin-bottom: 6px;
  background: var(--surface2); font-size: 0.9rem; color: var(--muted);
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.chat-item.active { border: 1px solid var(--accent); color: var(--text); }
.file-item button {
  border: none; background: var(--accent); color: #fff;
  padding: 6px 10px; border-radius: 10px; font-size: 0.75rem;
}
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 25; }

.badge.cert { background: rgba(34, 197, 94, 0.2); color: #15803d; }
[data-theme="slate"] .badge.cert, [data-theme="midnight"] .badge.cert { color: #4ade80; }

.hint.left.ok { color: #16a34a; font-weight: 600; }

.chat-tabs {
  display: flex; gap: 6px; overflow-x: auto; padding: 8px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.chat-tabs::-webkit-scrollbar { display: none; }
.chat-tab {
  flex: 0 0 auto; max-width: 140px; padding: 8px 12px; border-radius: 12px;
  background: var(--surface2); border: 1px solid transparent;
  font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: var(--muted);
}
.chat-tab.active { border-color: var(--accent); color: var(--text); font-weight: 600; }
.chat-tab.new-tab {
  width: 36px; padding: 8px; text-align: center; font-weight: 700; color: var(--accent);
}

.live-panel {
  border-bottom: 1px solid var(--border); background: var(--surface);
  max-height: 28vh; display: flex; flex-direction: column;
}
.live-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; font-size: 0.72rem; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.live-log {
  overflow-y: auto; padding: 4px 12px 10px; font-size: 0.72rem; line-height: 1.5;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}
.live-line { padding: 3px 0; border-bottom: 1px solid transparent; color: var(--muted); }
.live-line.active { color: var(--text); }
.live-line.ok { color: #16a34a; }
.live-line.err { color: var(--danger); }
.live-line .ts { opacity: 0.55; margin-right: 6px; }

.stop-btn {
  width: 44px; height: 44px; border: none; border-radius: 22px;
  background: var(--danger); color: #fff; font-size: 1rem; font-weight: 700; flex-shrink: 0;
}

.msg.live-card {
  align-self: stretch; max-width: 100%; font-size: 0.8rem;
  background: rgba(124, 108, 255, 0.08); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 12px;
}
.live-card .step { display: flex; gap: 8px; align-items: flex-start; padding: 4px 0; }
.live-card .step-dot { width: 6px; height: 6px; border-radius: 50%; margin-top: 6px; background: var(--accent); flex-shrink: 0; }
.live-card .step.done .step-dot { background: #22c55e; }
.live-card .step.run .step-dot { animation: pulse 0.8s infinite; background: #f59e0b; }

.settings-body { padding: 20px; flex: 1; overflow-y: auto; }
