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

:root {
  --bg: #060504;
  --surface: #0C0B0A;
  --border: rgba(201, 169, 98, 0.15);
  --text: #E8E4DC;
  --text-muted: rgba(232, 228, 220, 0.55);
  --accent: #C9A962;
  --accent-light: #E8D5A3;
  --accent-bg: rgba(201, 169, 98, 0.06);
  --accent-glow: rgba(201, 169, 98, 0.2);
  --user-msg: rgba(201, 169, 98, 0.08);
  --bot-msg: #0C0B0A;
  --online: #4A7C59;
  --offline: #8B1E3F;
  --warning: #C9A962;
}

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

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 768px;
  margin: 0 auto;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

header h1 {
  font-family: 'Cinzel Decorative', serif;
  font-size: 20px;
  font-weight: 400;
  letter-spacing: 0.08em;
  color: var(--accent-light);
}

#status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: 'Cormorant Garamond', serif;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.02em;
}

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

#status-dot.online { background: var(--online); }
#status-dot.offline { background: var(--offline); }
#status-dot.connecting { background: var(--warning); }

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

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

.message.bot { white-space: normal; }
.message.user { white-space: pre-wrap; }

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

.message.bot {
  align-self: flex-start;
  background: var(--bot-msg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message .timestamp {
  display: block;
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
}

.message.bot code {
  background: rgba(201, 169, 98, 0.1);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  color: var(--accent-light);
}

.message.bot pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 6px 0;
}

.message.bot pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  color: var(--text);
}

.message.bot strong { color: var(--accent-light); }

.message.bot a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message.bot ul, .message.bot ol {
  padding-left: 18px;
  margin: 2px 0;
}

.message.bot li { margin: 1px 0; }
.message.bot li p { margin: 0; }

.message.bot table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13px;
}

.message.bot th,
.message.bot td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.message.bot th {
  background: var(--accent-bg);
  font-weight: 600;
  color: var(--accent-light);
}

.message.bot tr:nth-child(even) td {
  background: rgba(201, 169, 98, 0.03);
}

.message.bot h1, .message.bot h2, .message.bot h3 {
  margin: 6px 0 2px;
  color: var(--accent-light);
  font-family: 'Cormorant Garamond', serif;
}

.message.bot h1 { font-size: 17px; font-weight: 600; }
.message.bot h2 { font-size: 15px; font-weight: 600; }
.message.bot h3 { font-size: 14px; font-weight: 600; }
.message.bot p { margin: 2px 0; }
.message.bot p:first-child { margin-top: 0; }
.message.bot p:last-child { margin-bottom: 0; }
.message.bot > span > p:empty { display: none; }

.message.bot blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin: 6px 0;
  color: var(--text-muted);
  font-style: italic;
}

.message.bot hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 8px 0;
}

.stream-text table, .stream-text th, .stream-text td {
  border: 1px solid var(--border);
  border-collapse: collapse;
  padding: 6px 10px;
  font-size: 13px;
}

.stream-text th {
  background: var(--accent-bg);
  font-weight: 600;
  color: var(--accent-light);
}

#typing-indicator {
  align-self: flex-start;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
  display: none;
}

#typing-indicator.visible { display: block; }

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

#input-form {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

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

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

#send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

#send-btn:hover {
  background: var(--accent-light);
  border-color: var(--accent-light);
}

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

#mic-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

#mic-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#mic-btn.recording {
  background: var(--offline);
  border-color: var(--offline);
  color: #E8E4DC;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(139, 30, 63, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(139, 30, 63, 0); }
}

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

main#messages::-webkit-scrollbar { width: 4px; }
main#messages::-webkit-scrollbar-track { background: transparent; }
main#messages::-webkit-scrollbar-thumb { background: rgba(201, 169, 98, 0.2); border-radius: 2px; }

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

#tts-toggle,
#verbose-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: all 0.2s;
}

#tts-toggle.active,
#verbose-toggle.active {
  color: var(--accent);
  border-color: var(--accent);
  background: rgba(201, 169, 98, 0.1);
}

.stream-container {
  align-self: flex-start;
  width: 100%;
  max-width: 85%;
  display: flex;
  flex-direction: column;
  gap: 4px;
  animation: fadeIn 0.2s ease;
}

.tools-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  font-size: 13px;
}

.tools-group-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  cursor: pointer;
  color: var(--text-muted);
  user-select: none;
  list-style: none;
  transition: color 0.2s;
}

.tools-group-summary::-webkit-details-marker { display: none; }

.tools-group-summary::before {
  content: '▶';
  font-size: 9px;
  transition: transform 0.2s;
  flex-shrink: 0;
}

.tools-group[open] > .tools-group-summary::before {
  transform: rotate(90deg);
}

.tools-group-summary:hover { color: var(--text); }
.tools-group-icon { font-size: 12px; }
.tools-group-label { font-weight: 500; }

.tools-group-items {
  border-top: 1px solid var(--border);
  padding: 4px 0;
  max-height: 300px;
  overflow-y: auto;
}

.stream-tool {
  padding: 4px 14px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}

.tool-name {
  color: var(--accent);
  font-weight: 500;
  flex-shrink: 0;
}

.tool-detail {
  opacity: 0.6;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 280px;
}

.tool-result-text {
  opacity: 0.5;
  font-size: 11px;
}

.stream-text {
  padding: 8px 12px;
  background: var(--bot-msg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
}

.stream-text p { margin: 2px 0; }
.stream-text p:first-child { margin-top: 0; }
.stream-text p:last-child { margin-bottom: 0; }
.stream-text ul, .stream-text ol { padding-left: 18px; margin: 2px 0; }
.stream-text li { margin: 1px 0; }
.stream-text li p { margin: 0; }

.stream-footer {
  font-size: 11px;
  color: var(--text-muted);
  padding: 2px 4px;
  opacity: 0.6;
}

/* Voice overlay */
#voice-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(6, 5, 4, 0.96);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 32px;
  transition: opacity 0.3s;
}

#voice-overlay.hidden { display: none; }

#voice-label {
  font-family: 'Cormorant Garamond', serif;
  font-size: 18px;
  color: var(--text-muted);
  letter-spacing: 0.04em;
}

#voice-cancel {
  position: absolute;
  bottom: 48px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

#voice-cancel:hover {
  border-color: var(--offline);
  color: var(--offline);
}

#voice-orb {
  position: relative;
  width: 160px;
  height: 160px;
}

.orb-core {
  position: absolute;
  inset: 30%;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 40px rgba(201, 169, 98, 0.4);
  transition: transform 0.1s, box-shadow 0.1s;
}

.orb-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(201, 169, 98, 0.2);
  transition: transform 0.15s, border-color 0.15s;
}

.orb-ring-1 { inset: 10%; }
.orb-ring-2 { inset: 0; }
.orb-ring-3 { inset: -10%; }

#voice-overlay.listening .orb-core {
  animation: orbPulse 1.2s ease-in-out infinite;
  background: var(--accent);
}

#voice-overlay.listening .orb-ring-1 { animation: ringExpand 1.2s ease-in-out infinite; }
#voice-overlay.listening .orb-ring-2 { animation: ringExpand 1.2s ease-in-out infinite 0.2s; }
#voice-overlay.listening .orb-ring-3 { animation: ringExpand 1.2s ease-in-out infinite 0.4s; }

#voice-overlay.listening .orb-core {
  transform: scale(calc(1 + var(--vol, 0) * 0.4));
  box-shadow: 0 0 calc(40px + var(--vol, 0) * 30px) rgba(201, 169, 98, calc(0.3 + var(--vol, 0) * 0.4));
}

#voice-overlay.speaking .orb-core {
  background: var(--online);
  box-shadow: 0 0 40px rgba(74, 124, 89, 0.5);
  animation: orbSpeak 0.6s ease-in-out infinite;
}

#voice-overlay.speaking .orb-ring {
  border-color: rgba(74, 124, 89, 0.3);
}

#voice-overlay.speaking .orb-ring-1 { animation: ringSpeakPulse 0.8s ease-in-out infinite; }
#voice-overlay.speaking .orb-ring-2 { animation: ringSpeakPulse 0.8s ease-in-out infinite 0.15s; }
#voice-overlay.speaking .orb-ring-3 { animation: ringSpeakPulse 0.8s ease-in-out infinite 0.3s; }

#voice-overlay.transcribing .orb-core {
  background: var(--accent-light);
  box-shadow: 0 0 40px rgba(232, 213, 163, 0.4);
  animation: orbPulse 0.8s ease-in-out infinite;
}

@keyframes orbPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

@keyframes orbSpeak {
  0%, 100% { transform: scale(1); }
  25% { transform: scale(1.2); }
  75% { transform: scale(0.95); }
}

@keyframes ringExpand {
  0%, 100% { transform: scale(1); opacity: 0.3; }
  50% { transform: scale(1.08); opacity: 0.6; }
}

@keyframes ringSpeakPulse {
  0%, 100% { transform: scale(1); opacity: 0.2; }
  50% { transform: scale(1.12); opacity: 0.5; }
}
