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

:root,
[data-theme="dark"] {
  --bg: #0b0c10;
  --sidebar: #12141a;
  --panel: #181b22;
  --line: #2a2e38;
  --text: #ececf1;
  --muted: #9aa3b2;
  --accent: #10a37f;
  --accent-2: #3b82f6;
  --accent-3: #a855f7;
  --danger: #ef4444;
  --user: #2a3140;
  --radius: 14px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --code-bg: #0a0b10;
  --shadow: rgba(0, 0, 0, 0.35);
}

[data-theme="light"] {
  --bg: #f6f7fb;
  --sidebar: #eceef4;
  --panel: #ffffff;
  --line: #d8dde8;
  --text: #16181d;
  --muted: #5c6573;
  --accent: #0d8a6a;
  --accent-2: #2563eb;
  --accent-3: #9333ea;
  --danger: #dc2626;
  --user: #e8ebf2;
  --code-bg: #f3f4f8;
  --shadow: rgba(22, 24, 29, 0.08);
}

html,
body {
  height: 100%;
  height: var(--app-h, 100dvh);
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
}

button,
.icon-btn,
.send,
.ghost,
.primary {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
textarea {
  font-family: inherit;
}

#app {
  height: 100%;
  height: var(--app-h, 100dvh);
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ---- Auth ---- */
.auth-wrap {
  height: 100%;
  overflow: auto;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 10% -10%, rgba(16, 163, 127, 0.18), transparent 50%),
    radial-gradient(700px 400px at 100% 0%, rgba(59, 130, 246, 0.14), transparent 45%),
    var(--bg);
}

.auth-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: 0 24px 80px var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #0d7a60);
  display: grid;
  place-items: center;
  font-weight: 800;
  letter-spacing: -0.04em;
}

.brand h1 {
  font-size: 22px;
  font-weight: 700;
}

.lede {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 22px;
  line-height: 1.5;
}

.auth-card label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 12px 0 6px;
}

.auth-card input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 16px;
  outline: none;
}

.auth-card input:focus {
  border-color: var(--accent);
}

.primary {
  width: 100%;
  margin-top: 18px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.primary:hover {
  filter: brightness(1.08);
}

.primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.switch {
  text-align: center;
  margin-top: 16px;
  color: var(--muted);
  font-size: 14px;
}

.switch a {
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
}

.err {
  color: #fca5a5;
  font-size: 13px;
  min-height: 18px;
  margin-top: 8px;
}

/* ---- Shell ---- */
.shell {
  display: grid;
  grid-template-columns: 272px 1fr;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.sidebar-backdrop {
  display: none;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-head .new-chat {
  flex: 1;
}

.sidebar-close,
.menu-btn {
  display: none;
}

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 14px;
  min-height: 0;
  overflow: hidden;
}

.new-chat {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  font-weight: 600;
}

.new-chat:hover {
  background: var(--panel);
}

.chats {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  margin: 14px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
}

.chat-item:hover,
.chat-item.active {
  background: var(--panel);
}

.chat-item span {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chat-item .del {
  opacity: 0;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
}

.chat-item:hover .del {
  opacity: 1;
}

.side-foot {
  flex-shrink: 0;
  border-top: 1px solid var(--line);
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ram {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.45;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  flex-wrap: wrap;
}

#btn-train {
  margin-left: auto;
}

.ghost {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
}

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}

.model-switch {
  display: flex;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}

.model-switch button {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}

.model-switch button.active.coding {
  background: var(--accent);
  color: #fff;
}

.model-switch button.active.research {
  background: var(--accent-2);
  color: #fff;
}

.model-switch button.active.phd {
  background: var(--accent-3);
  color: #fff;
}

.status-pill {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #64748b;
}

.dot.on {
  background: #22c55e;
}

.dot.load {
  background: #eab308;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.4;
  }
}

.thread {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 24px 0 8px;
  overscroll-behavior: contain;
}

.thread-inner {
  width: min(820px, calc(100% - 40px));
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.msg {
  display: grid;
  grid-template-columns: 36px 1fr;
  gap: 14px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}

.avatar.user {
  background: #334155;
}

.avatar.assistant.coding {
  background: var(--accent);
}

.avatar.assistant.research {
  background: var(--accent-2);
}

.avatar.assistant.phd {
  background: var(--accent-3);
}

.body {
  padding-top: 6px;
  line-height: 1.65;
  font-size: 15.5px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.msg-tools {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  border-radius: 10px;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.icon-btn:hover,
.icon-btn.on {
  color: var(--text);
  background: var(--bg);
  border-color: #3d4454;
}

.icon-btn.listening {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.body p {
  margin: 0 0 10px;
}

.body pre {
  background: var(--code-bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  overflow: auto;
  margin: 10px 0;
  font-size: 13px;
}

.body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}

.body :not(pre) > code {
  background: var(--user);
  padding: 1px 6px;
  border-radius: 6px;
}

.body ul,
.body ol {
  padding-left: 22px;
  margin: 8px 0;
}

.welcome {
  text-align: center;
  padding: 12vh 16px 24px;
}

.welcome h2 {
  font-size: 32px;
  margin-bottom: 8px;
}

.welcome p {
  color: var(--muted);
  margin-bottom: 28px;
}

.cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  width: min(640px, 100%);
  margin: 0 auto;
}

.card {
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  cursor: pointer;
  color: inherit;
}

.card:hover {
  border-color: #3d4454;
}

.card h3 {
  margin-bottom: 6px;
}

.card p {
  color: var(--muted);
  font-size: 13px;
  margin: 0;
}

.composer-wrap {
  flex-shrink: 0;
  padding: 8px 20px 16px;
  background: linear-gradient(180deg, transparent, var(--bg) 18%);
}

.composer {
  width: min(820px, 100%);
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  padding: 8px 8px 8px 10px;
}

.attach-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 6px 6px 0;
}

.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 8px;
  font-size: 12px;
  max-width: 220px;
}

.chip img {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 6px;
}

.chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chip button {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

.composer textarea {
  flex: 1;
  background: transparent;
  border: 0;
  color: var(--text);
  resize: none;
  outline: none;
  font-size: 16px;
  line-height: 1.5;
  max-height: 180px;
  min-height: 44px;
  padding: 10px 4px;
}

.send {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-size: 18px;
  flex-shrink: 0;
}

.send.mic-mode {
  background: var(--text);
  color: var(--bg);
}

.topbar-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.audio-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}

.audio-controls .icon-btn {
  width: auto;
  height: 32px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  border: 0;
}

.audio-controls .icon-btn.muted {
  background: var(--danger);
  color: #fff;
}

.audio-controls .icon-btn.playing {
  background: var(--accent);
  color: #fff;
}

.stop-bar {
  position: sticky;
  bottom: 8px;
  display: flex;
  justify-content: center;
  pointer-events: none;
  margin-top: -8px;
  padding-bottom: 8px;
}

.stop-bar button {
  pointer-events: auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 16px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 8px 24px var(--shadow);
}

.stop-bar button:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.send.stop {
  background: var(--danger);
  font-size: 16px;
}

.send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.send.stop:disabled {
  opacity: 1;
  cursor: pointer;
}

.icon-btn.vote.on.up {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.icon-btn.vote.on.down {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  margin-top: 8px;
}

.user-files {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.user-files img {
  max-width: 180px;
  max-height: 120px;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.typing::after {
  content: "▍";
  animation: blink 1s step-end infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.voice-sheet {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.voice-sheet-inner {
  width: min(520px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  padding: 18px 20px calc(18px + env(safe-area-inset-bottom, 0px));
  text-align: center;
  position: relative;
  box-shadow: 0 -12px 40px var(--shadow);
}

.voice-close {
  position: absolute;
  top: 12px;
  right: 12px;
}

.voice-kicker {
  color: var(--muted);
  font-size: 14px;
  margin: 8px 0 10px;
}

.voice-transcript {
  min-height: 48px;
  font-size: 18px;
  line-height: 1.45;
  padding: 0 12px;
}

.voice-orb {
  width: 92px;
  height: 92px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 36px;
  cursor: pointer;
  margin: 8px auto 16px;
}

.voice-orb.live {
  animation: orb 1.15s ease-in-out infinite;
}

@keyframes orb {
  50% {
    transform: scale(1.08);
    box-shadow: 0 0 0 18px rgba(16, 163, 127, 0.22);
  }
}

.voice-actions {
  display: flex;
  gap: 10px;
}

.https-banner {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  z-index: 70;
  background: #7c2d12;
  color: #fff;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
  line-height: 1.4;
  box-shadow: 0 8px 24px var(--shadow);
}

.https-banner a {
  color: #fde68a;
}

.voice-sheet.live-chat {
  align-items: stretch;
  background: var(--bg);
}

.voice-sheet.live-chat .voice-sheet-inner {
  width: 100%;
  max-width: none;
  height: 100%;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 24px 20px calc(28px + env(safe-area-inset-bottom, 0px));
  box-shadow: none;
}

.voice-sheet.live-chat .voice-orb {
  width: 128px;
  height: 128px;
  font-size: 48px;
}

.voice-orb.speak {
  background: var(--accent-2);
  animation: orb 1.15s ease-in-out infinite;
}

.voice-actions .ghost {
  flex: 1;
}

@media (max-width: 860px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .menu-btn,
  .sidebar-close {
    display: grid;
  }

  .sidebar-backdrop {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 20;
  }

  .shell.sidebar-open .sidebar-backdrop {
    display: block;
  }

  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(320px, 86vw);
    z-index: 30;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: 8px 0 32px var(--shadow);
  }

  .shell.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .topbar {
    padding: 8px 10px;
    gap: 8px;
    flex-wrap: nowrap;
  }

  .model-switch {
    flex: 1;
    min-width: 0;
  }

  .model-switch button {
    flex: 1;
    padding: 8px 4px;
    font-size: 11px;
  }

  .topbar-actions {
    gap: 4px;
    flex-wrap: nowrap;
  }

  .status-pill span:last-child {
    display: none;
  }

  #btn-voice-mode {
    display: none;
  }

  .audio-controls {
    padding: 2px;
  }

  .audio-controls .icon-btn {
    width: 36px;
    height: 36px;
    padding: 0;
    font-size: 14px;
  }

  .thread {
    padding: 12px 0 8px;
  }

  .thread-inner {
    width: calc(100% - 20px);
    gap: 14px;
  }

  .msg {
    grid-template-columns: 28px 1fr;
    gap: 10px;
  }

  .avatar {
    width: 28px;
    height: 28px;
    font-size: 11px;
    border-radius: 8px;
  }

  .body {
    font-size: 15px;
  }

  .welcome {
    padding: 8vh 14px 20px;
  }

  .welcome h2 {
    font-size: 24px;
  }

  .cards {
    grid-template-columns: 1fr;
  }

  .composer-wrap {
    padding: 6px 10px calc(10px + env(safe-area-inset-bottom, 0px));
  }

  .composer {
    border-radius: 22px;
    padding: 6px;
  }

  .composer-row .icon-btn {
    width: 44px;
    height: 44px;
  }

  #btn-mic {
    display: none;
  }

  .hint {
    display: none;
  }

  .body pre {
    font-size: 12px;
    padding: 10px;
  }
}

@media (max-width: 520px) {
  .auth-wrap {
    padding: 12px;
    align-items: stretch;
  }

  .auth-card {
    padding: 22px 18px;
    border-radius: 16px;
  }

  #btn-voice-mode {
    display: none;
  }

  .user-row {
    font-size: 12px;
  }
}
