/* ── Reset & Base ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0d1117;
  --surface:  #161b22;
  --border:   #30363d;
  --text:     #e6edf3;
  --muted:    #8b949e;
  --accent:   #58a6ff;
  --green:    #3fb950;
  --red:      #f85149;
  --radius:   8px;
  --user-bg:  #1f6feb;
  --assist-bg:#21262d;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ── Layout ──────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  font-size: 15px;
  color: var(--accent);
  white-space: nowrap;
}

.status {
  margin-left: auto;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}
.status.connected { color: var(--green); }
.status.error     { color: var(--red); }

/* ── Model selector ──────────────────────────────── */
.model-selector {
  position: relative;
}

.model-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  transition: .15s;
  white-space: nowrap;
}
.model-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.model-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 200px;
  max-height: 60vh;
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.model-dropdown.hidden {
  display: none;
}

.model-option {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: background .1s;
}
.model-option:hover {
  background: rgba(88, 166, 255, 0.1);
}
.model-option.active {
  color: var(--accent);
  font-weight: 600;
}

/* ── Chat container ──────────────────────────────── */
#chat-container {
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

#chat-messages {
  height: 100%;
  overflow-y: auto;
  padding: 16px;
  scroll-behavior: smooth;
}

/* ── Welcome message ─────────────────────────────── */
.welcome-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  height: 100%;
  color: var(--muted);
  font-size: 16px;
  text-align: center;
}

/* ── Message bubbles ─────────────────────────────── */
.message {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  max-width: 85%;
  animation: fadeIn 0.2s ease;
}

/* ── System messages ─────────────────────────────── */
.system-message {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 0;
  margin-bottom: 12px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message.user {
  margin-left: auto;
  flex-direction: row-reverse;
}

.message.assistant {
  margin-right: auto;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
  text-transform: uppercase;
}

.message.user .avatar {
  background: var(--user-bg);
  color: #fff;
}

.message.assistant .avatar {
  background: var(--accent);
  color: #000;
}

.bubble {
  padding: 10px 14px;
  border-radius: 12px;
  line-height: 1.55;
  font-size: 14px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .bubble {
  background: var(--user-bg);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--assist-bg);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* ── Markdown inside assistant bubbles ──────────── */
.message.assistant .bubble p {
  margin-bottom: 8px;
}
.message.assistant .bubble p:last-child {
  margin-bottom: 0;
}

.message.assistant .bubble ul,
.message.assistant .bubble ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message.assistant .bubble li {
  margin-bottom: 4px;
}

.message.assistant .bubble h1,
.message.assistant .bubble h2,
.message.assistant .bubble h3 {
  margin: 12px 0 6px;
  font-size: 1.1em;
}

.message.assistant .bubble code {
  background: rgba(110, 118, 129, 0.25);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.88em;
  font-family: "Cascadia Code", "Fira Code", "SF Mono", Menlo, monospace;
}

.message.assistant .bubble pre code {
  background: none;
  padding: 0;
}

.message.assistant .bubble blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  margin: 8px 0;
  color: var(--muted);
}

.message.assistant .bubble a {
  color: var(--accent);
  text-decoration: none;
}
.message.assistant .bubble a:hover {
  text-decoration: underline;
}

.message.assistant .bubble table {
  border-collapse: collapse;
  margin: 8px 0;
  width: 100%;
}
.message.assistant .bubble th,
.message.assistant .bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.message.assistant .bubble th {
  background: var(--surface);
}

/* ── Code blocks ─────────────────────────────────── */
.code-block {
  position: relative;
  margin: 8px 0;
}

.code-block pre {
  background: #0d1117 !important;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.code-block:hover .copy-btn {
  opacity: 1;
}
.copy-btn:hover {
  color: var(--text);
  background: var(--border);
}

/* ── Thinking dots ───────────────────────────────── */
.thinking-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}
.thinking-dots span {
  animation: dot-bounce 1.4s ease-in-out infinite;
  font-size: 24px;
  line-height: 16px;
  color: var(--muted);
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-bounce {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

/* ── Footer / Input bar ────────────────────────────── */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.mic-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
  transition: .2s;
  -webkit-tap-highlight-color: transparent;
}
.mic-btn:hover, .mic-btn:focus {
  background: rgba(88, 166, 255, 0.15);
}
.mic-btn.listening {
  border-color: var(--red);
  color: var(--red);
  animation: pulse-mic 1.2s ease-in-out infinite;
}

@keyframes pulse-mic {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248,81,73,0.4); }
  50%      { box-shadow: 0 0 0 10px rgba(248,81,73,0); }
}

.textarea-wrap {
  flex: 1;
  min-width: 0;
}

#text-input {
  width: 100%;
  min-height: 44px;
  max-height: 150px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  outline: none;
  resize: none;
  overflow-y: auto;
  transition: border-color .15s;
}
#text-input:focus {
  border-color: var(--accent);
}
#text-input::placeholder {
  color: var(--muted);
}

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: #000;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity .15s;
}
.send-btn:hover { opacity: .85; }

/* ── Voice overlay ───────────────────────────────── */
.voice-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(13, 17, 23, 0.85);
  backdrop-filter: blur(4px);
  z-index: 100;
  transition: opacity .2s;
}
.voice-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.voice-overlay span {
  color: var(--text);
  font-size: 18px;
}

.voice-pulse {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--red);
  animation: voice-ring 1.5s ease-in-out infinite;
}

@keyframes voice-ring {
  0%, 100% { transform: scale(1); opacity: 0.7; }
  50%      { transform: scale(1.3); opacity: 0.3; }
}

/* ── Scrollbar ───────────────────────────────────── */
#chat-messages::-webkit-scrollbar {
  width: 6px;
}
#chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
#chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
#chat-messages::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}

/* ── Mobile responsive ───────────────────────────── */
@media (max-width: 600px) {
  header {
    padding: 6px 10px;
    gap: 8px;
  }

  .logo span {
    display: none;
  }

  .model-btn {
    font-size: 11px;
    padding: 3px 6px;
  }

  .status {
    max-width: 100px;
    font-size: 10px;
  }

  #chat-messages {
    padding: 12px 8px;
  }

  .message {
    max-width: 92%;
  }

  .avatar {
    width: 26px;
    height: 26px;
    font-size: 9px;
  }

  .bubble {
    font-size: 13px;
    padding: 8px 12px;
  }

  #input-bar {
    padding: 6px 8px;
  }

  .mic-btn, .send-btn {
    width: 40px;
    height: 40px;
  }

  #text-input {
    min-height: 40px;
    font-size: 16px; /* prevent iOS zoom on focus */
  }
}
