:root {
  --bg: #212121;
  --sidebar: #171717;
  --surface: #2f2f2f;
  --surface-hover: #3a3a3a;
  --text: #ececec;
  --text-dim: #9b9b9b;
  --accent: #10a37f;
  --danger: #ef4146;
  --radius: 1.5rem;
}

* { box-sizing: border-box; }

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

.app {
  display: flex;
  height: 100vh;
}

/* --- Sidebar --- */

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  gap: 0.75rem;
}

.new-chat button, button.new-chat {
  width: 100%;
  padding: 0.65rem 0.9rem;
  border: 1px solid #3a3a3a;
  border-radius: 0.75rem;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
}
.new-chat button:hover, button.new-chat:hover { background: var(--surface); }

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  border-radius: 0.6rem;
  padding: 0 0.25rem;
}
.chat-item:hover { background: var(--surface); }
.chat-item.current { background: var(--surface); }

.chat-link {
  flex: 1;
  padding: 0.55rem 0.5rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-delete button, button.chat-delete {
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.75rem;
  padding: 0.3rem;
  visibility: hidden;
}
.chat-item:hover .chat-delete button,
.chat-item:hover button.chat-delete { visibility: visible; }
.chat-delete button:hover, button.chat-delete:hover { color: var(--danger); }

.sidebar-footer {
  font-size: 0.75rem;
  color: var(--text-dim);
  padding: 0.5rem;
  border-top: 1px solid #2a2a2a;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.voice-select {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid #3a3a3a;
  border-radius: 0.5rem;
  padding: 0.35rem 0.5rem;
  font-size: 0.8rem;
}

.voice-picker.corner {
  position: absolute;
  top: max(1.25rem, env(safe-area-inset-top));
  right: 1.25rem;
  display: flex;
  gap: 0.4rem;
}
.voice-picker.corner .voice-select { width: auto; background: rgba(33, 33, 33, 0.85); }

/* --- Conversation --- */

.conversation {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.messages-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 2rem 1rem 1rem;
}

#messages {
  max-width: 46rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.message { display: flex; gap: 0.75rem; }

.message.user { justify-content: flex-end; }
.message.user .bubble {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.7rem 1.1rem;
  max-width: 75%;
}

.message.assistant .avatar {
  width: 2rem;
  height: 2rem;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
}
.message.assistant .bubble { padding-top: 0.35rem; max-width: 85%; }

.message.assistant details summary {
  cursor: pointer;
  color: var(--text-dim);
  font-size: 0.85rem;
  user-select: none;
  padding: 0.3rem 0;
  list-style-position: inside;
}
.message.assistant details summary:hover { color: var(--text); }
.message.assistant details[open] summary { margin-bottom: 0.35rem; }

.content {
  white-space: pre-wrap;
  overflow-wrap: break-word;
  line-height: 1.6;
}

/* --- Composer --- */

.composer {
  padding: 0.75rem 1rem 1.25rem;
}

.status-row {
  max-width: 46rem;
  margin: 0 auto 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
}

.meter {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
}
.meter span {
  width: 3px;
  border-radius: 2px;
  background: #3a3a3a;
  transition: background 0.1s;
}
.meter span:nth-child(1) { height: 4px; }
.meter span:nth-child(2) { height: 7px; }
.meter span:nth-child(3) { height: 10px; }
.meter span:nth-child(4) { height: 12px; }
.meter span:nth-child(5) { height: 14px; }
.meter span.lit { background: var(--accent); }

.status {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-dim);
  min-height: 1.2em;
  transition: color 0.2s;
}

[data-voice-state="hearing"] .status { color: var(--accent); }
[data-voice-state="thinking"] .status { color: #d9a648; }
[data-voice-state="speaking"] .status { color: #6fa8ff; }
[data-voice-state="error"] .status { color: var(--danger); }

.composer-row {
  max-width: 46rem;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.mic {
  width: 3.25rem;
  height: 3.25rem;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1px solid #3a3a3a;
  background: var(--surface);
  font-size: 1.3rem;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
}
.mic:hover { background: var(--surface-hover); }
.mic.on {
  background: var(--danger);
  border-color: var(--danger);
  animation: pulse 1.6s ease-out infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 65, 70, 0.5); }
  100% { box-shadow: 0 0 0 14px rgba(239, 65, 70, 0); }
}

.text-form {
  flex: 1;
  display: flex;
  gap: 0.5rem;
  background: var(--surface);
  border: 1px solid #3a3a3a;
  border-radius: var(--radius);
  padding: 0.35rem 0.35rem 0.35rem 1.1rem;
}

.text-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  outline: none;
}

.send {
  border: none;
  border-radius: 1.1rem;
  background: var(--accent);
  color: #fff;
  padding: 0.55rem 1.1rem;
  font-size: 0.9rem;
  cursor: pointer;
}
.send:hover { filter: brightness(1.1); }

.mode-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 1.4rem;
  line-height: 1;
}
.mode-link:hover { color: var(--text); }
.mode-link.inline { padding: 0 0.25rem; }

/* --- Voice mode: the orb --- */

.voice-mode {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2.5rem;
  background: radial-gradient(ellipse at 50% 35%, #26262b, var(--bg) 75%);
  --orb-color: 122, 122, 130;               /* idle: gray */
}
.voice-mode[data-voice-state="listening"],
.voice-mode[data-voice-state="hearing"]  { --orb-color: 16, 163, 127; }   /* green: your turn */
.voice-mode[data-voice-state="thinking"] { --orb-color: 217, 166, 72; }   /* amber: working */
.voice-mode[data-voice-state="speaking"] { --orb-color: 96, 130, 255; }   /* blue: assistant */
.voice-mode[data-voice-state="error"]    { --orb-color: 239, 65, 70; }

.voice-mode .mode-link {
  position: absolute;
  top: max(1.25rem, env(safe-area-inset-top));
  left: 1.25rem;
  font-size: 1.6rem;
}

.orb {
  position: relative;
  width: clamp(11rem, 55vmin, 20rem);
  height: clamp(11rem, 55vmin, 20rem);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  padding: 0;
  background: radial-gradient(circle at 32% 30%,
    rgba(var(--orb-color), 0.95),
    rgba(var(--orb-color), 0.55) 55%,
    rgba(var(--orb-color), 0.25) 100%);
  box-shadow: 0 0 calc(2rem + var(--level, 0) * 7rem) rgba(var(--orb-color), 0.45);
  transform: scale(calc(1 + var(--level, 0) * 0.16));
  transition: box-shadow 80ms linear, background 0.4s;
  -webkit-tap-highlight-color: transparent;
}

.orb-glow {
  position: absolute;
  inset: 12%;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, rgba(255, 255, 255, 0.35), transparent 60%);
  pointer-events: none;
}

.orb-brand {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 34%;
  transform: translate(-50%, -50%);
  filter: brightness(0) invert(1); /* the mark reads white over any orb state color */
  opacity: 0.9;
  pointer-events: none;
}

/* Shazam-style ripples while someone is talking */
.orb::before, .orb::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(var(--orb-color), 0.5);
  opacity: 0;
  pointer-events: none;
}
.voice-mode[data-voice-state="listening"] .orb::before,
.voice-mode[data-voice-state="hearing"] .orb::before,
.voice-mode[data-voice-state="speaking"] .orb::before { animation: ripple 2.4s ease-out infinite; }
.voice-mode[data-voice-state="hearing"] .orb::after,
.voice-mode[data-voice-state="speaking"] .orb::after { animation: ripple 2.4s ease-out 1.2s infinite; }
.voice-mode[data-voice-state="thinking"] .orb { animation: breathe 1.6s ease-in-out infinite; }

@keyframes ripple {
  from { transform: scale(1); opacity: 0.6; }
  to   { transform: scale(1.65); opacity: 0; }
}

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

.voice-mode .status { font-size: 1rem; }

/* Live transcript, hidden behind a pill at the bottom */
.transcript {
  position: absolute;
  bottom: max(1rem, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: min(46rem, 94vw);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.transcript summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  color: var(--text-dim);
  font-size: 0.85rem;
  border: 1px solid #3a3a3a;
  border-radius: 2rem;
  padding: 0.45rem 1.2rem;
  background: rgba(33, 33, 33, 0.85);
}
.transcript summary::-webkit-details-marker { display: none; }
.transcript summary:hover { color: var(--text); }

.transcript .transcript-scroll {
  width: 100%;
  max-height: 42vh;
  overflow-y: auto;
  margin-top: 0.6rem;
  padding: 1rem;
  border: 1px solid #3a3a3a;
  border-radius: 1rem;
  background: rgba(23, 23, 23, 0.92);
}

/* Viewer memory debug panel */
.transcript.viewer-panel { bottom: calc(max(1rem, env(safe-area-inset-bottom)) + 3.2rem); }

.viewer-profile { font-size: 0.85rem; color: var(--text); }
.viewer-profile .empty { color: var(--text-dim); }
.viewer-profile .viewer-name { font-weight: 600; margin: 0 0 0.4rem; }
.viewer-profile ul { margin: 0; padding-left: 1.1rem; display: flex; flex-direction: column; gap: 0.25rem; }
.viewer-profile .stamp { color: var(--text-dim); font-size: 0.72rem; margin: 0.5rem 0 0; }

.sidebar-profile summary {
  cursor: pointer;
  color: var(--text-dim);
  font-size: 0.8rem;
  padding: 0.4rem 0.5rem;
  user-select: none;
}
.sidebar-profile summary:hover { color: var(--text); }
.sidebar-profile .viewer-profile { padding: 0.25rem 0.5rem 0.5rem; }

/* --- Admin --- */

.admin {
  max-width: 72rem;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
}
.admin h1 { margin-top: 0; }
.admin h2 { margin-top: 2rem; font-size: 1.05rem; color: var(--text-dim); }
.admin table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.admin th { text-align: left; color: var(--text-dim); font-weight: 500; padding: 0.4rem 0.6rem; border-bottom: 1px solid #3a3a3a; }
.admin td { padding: 0.4rem 0.6rem; border-bottom: 1px solid #2a2a2a; vertical-align: top; }
.admin td.args { color: var(--text-dim); font-family: ui-monospace, monospace; font-size: 0.78rem; word-break: break-all; }
.admin tr.bad td { color: #ff8489; }
.admin a { color: var(--accent); }
.admin .role-system td { opacity: 0.45; }
.admin .role-tool td { color: #d9a648; }

.admin-stats { display: flex; flex-wrap: wrap; gap: 0.8rem; }
.admin-stats .stat {
  background: var(--surface);
  border-radius: 0.8rem;
  padding: 0.8rem 1.2rem;
  display: flex; flex-direction: column;
}
.admin-stats .stat strong { font-size: 1.4rem; }
.admin-stats .stat span { color: var(--text-dim); font-size: 0.78rem; }
.admin-meta { color: var(--text-dim); font-size: 0.85rem; }
.admin-search { max-width: 16rem; margin: 0.4rem 0.4rem 0.8rem 0; padding: 0.4rem 0.7rem; border: 1px solid #3a3a3a; border-radius: 0.5rem; background: var(--surface); }
.admin-filters { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.admin-profile ul { margin: 0.3rem 0 0; padding-left: 1.2rem; }
.admin-profile small { color: var(--text-dim); font-weight: 400; }

/* --- Mobile --- */

@media (max-width: 700px) {
  .sidebar { display: none; }
  .message.user .bubble, .message.assistant .bubble { max-width: 95%; }
}
