:root {
  color-scheme: dark;
  --bg: #0b0b0d;
  --surface: #17171b;
  --bubble-user: #2a5bd7;
  --bubble-assistant: #1f1f24;
  --text: #f2f2f4;
  --text-dim: #8a8a92;
  --error: #ff6b6b;
  --accent: #7c9cff;
}

* { box-sizing: border-box; }

/* The browser's own [hidden] default (display:none) is UA-origin, so any
   author rule that sets `display` on the same element — even one with lower
   specificity — silently wins and defeats it. This one rule guarantees
   `el.hidden = true` always actually hides the element, for every element,
   forever — instead of re-discovering this bug per component. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#gate {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.gate-box { text-align: center; max-width: 320px; }
.gate-box h1 { font-size: 1.2rem; margin-bottom: 8px; }
.gate-box p { color: var(--text-dim); margin-bottom: 16px; }

#pair-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#pair-code {
  text-align: center;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  font-size: 1.1rem;
  border-radius: 10px;
  border: 1px solid #2c2c33;
  background: #0f0f12;
  color: var(--text);
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
#pair-code:focus { outline: none; border-color: var(--accent); }

#pair-form button {
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #0b0b0d;
  font-weight: 600;
  padding: 12px;
  font-size: 1rem;
}

.error-text {
  color: var(--error);
  margin-top: 12px;
  font-size: 0.85rem;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid #232328;
}
header .title { font-weight: 600; }
#status {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--text-dim);
}
#status.ok { color: #6fd08c; }
#notify-btn {
  background: var(--accent);
  color: #0b0b0d;
  border: none;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.8rem;
  font-weight: 600;
}

#settings-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.2rem;
  line-height: 1;
  padding: 4px;
}

#settings-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  z-index: 10;
}
.settings-sheet {
  background: var(--surface);
  width: 100%;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px max(20px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.settings-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  color: var(--text-dim);
  padding-bottom: 8px;
  border-bottom: 1px solid #232328;
}
.settings-sheet button {
  border: none;
  border-radius: 10px;
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
}
#settings-update-btn {
  background: var(--accent);
  color: #0b0b0d;
}
#settings-close-btn {
  background: #232328;
  color: var(--text);
}

.hint {
  margin: 0;
  padding: 8px 16px;
  font-size: 0.8rem;
  color: var(--text-dim);
  background: var(--surface);
}

.banner {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 10px 16px;
  font-size: 0.85rem;
  color: var(--error);
  background: #2a1515;
  border-bottom: 1px solid var(--error);
}
.banner button {
  align-self: flex-start;
  border: 1px solid var(--error);
  background: none;
  color: var(--error);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.8rem;
}

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

.msg { display: flex; flex-direction: column; max-width: 86%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  word-wrap: break-word;
  line-height: 1.4;
}
.msg.user .bubble { background: var(--bubble-user); color: white; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--bubble-assistant); border-bottom-left-radius: 4px; }
.bubble.error { border: 1px solid var(--error); }

.msg.live .bubble { opacity: 0.85; }

.work-status {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.8rem;
  color: var(--text-dim);
}
.live-text:not(:empty) { margin-top: 8px; }
.work-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  animation: work-pulse 1.2s ease-in-out infinite;
}
.work-time {
  font-variant-numeric: tabular-nums;
  opacity: 0.85;
}
@keyframes work-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1.15); }
}

/* ---- Markdown content rendered inside a bubble ---- */
.bubble > *:first-child { margin-top: 0; }
.bubble > *:last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 0.6em; }
.bubble ul, .bubble ol { margin: 0.3em 0 0.6em; padding-left: 1.3em; }
.bubble li { margin-bottom: 0.2em; }
.bubble li > p { margin: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4, .bubble h5, .bubble h6 {
  margin: 0.7em 0 0.35em;
  line-height: 1.25;
}
.bubble h1 { font-size: 1.25em; }
.bubble h2 { font-size: 1.15em; }
.bubble h3, .bubble h4, .bubble h5, .bubble h6 { font-size: 1.05em; }
.bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.msg.assistant .bubble a { color: var(--accent); }
.bubble strong { font-weight: 700; }
.bubble blockquote {
  margin: 0.5em 0;
  padding-left: 10px;
  border-left: 3px solid rgba(255, 255, 255, 0.25);
  color: var(--text-dim);
}
.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: rgba(255, 255, 255, 0.12);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  word-break: break-word;
}
.bubble pre {
  margin: 0.5em 0;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.bubble pre code {
  background: none;
  padding: 0;
  font-size: 0.8em;
  white-space: pre;
}
.bubble hr {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  margin: 0.7em 0;
}
.bubble table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.85em;
  display: block;
  overflow-x: auto;
}
.bubble th, .bubble td {
  border: 1px solid rgba(255, 255, 255, 0.15);
  padding: 4px 8px;
  text-align: left;
}
.bubble img { max-width: 100%; border-radius: 8px; }

.copy-btn {
  margin-top: 4px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 0.72rem;
  padding: 2px 4px;
  opacity: 0.75;
}
.copy-btn:active { opacity: 1; }
.copy-btn.copied { color: #6fd08c; opacity: 1; }
.copy-btn svg { display: block; flex-shrink: 0; }

.tools {
  margin-top: 4px;
  font-size: 0.75rem;
  color: var(--text-dim);
}
.tools summary {
  cursor: pointer;
  user-select: none;
}
.tool-line {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 4px 0 0 8px;
  white-space: pre-wrap;
  word-break: break-all;
  opacity: 0.85;
}
.tool-line.error { color: var(--error); }

#composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid #232328;
}

#input {
  flex: 1;
  resize: none;
  max-height: 160px;
  border-radius: 18px;
  border: 1px solid #2c2c33;
  background: #0f0f12;
  color: var(--text);
  padding: 10px 14px;
  font-size: 1rem;
  font-family: inherit;
}
#input:focus { outline: none; border-color: var(--accent); }

#composer button[type="submit"] {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #0b0b0d;
  font-size: 1.1rem;
  font-weight: 700;
  flex-shrink: 0;
}
