@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500;600;700&display=swap');

:root {
  /* dark grey base */
  --bg: #1b1c1f;
  --bg-sidebar: #17181b;
  --bg-elevated: #232529;
  --bg-hover: #2a2c31;
  --border: #313339;
  --border-soft: #2a2c31;

  /* text */
  --text: #ececed;
  --text-muted: #93959c;
  --text-faint: #6b6d74;

  /* accent (blue by default; overridden to red in root mode below) */
  --blue: #5b8cff;
  --blue-dim: #3f5fb8;
  --blue-soft: rgba(91, 140, 255, 0.12);
  --blue-soft-strong: rgba(91, 140, 255, 0.2);
  --blue-glow: rgba(91, 140, 255, 0.35);
  --ascii-color: #ffffff;

  --danger: #e2685f;
  --radius: 10px;
  --radius-sm: 6px;

  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, monospace;
}

/* ================= Root mode theme (red) =================
   Overrides the same variable names used everywhere else, so every accent
   (buttons, borders, highlights) automatically flips to red — no duplicate
   rules needed. This is a visual/tone change only; safety rules are
   identical in both modes (see systemPrompt.js). */

body.mode-root {
  --blue: #ff4d4d;
  --blue-dim: #b23333;
  --blue-soft: rgba(255, 77, 77, 0.13);
  --blue-soft-strong: rgba(255, 77, 77, 0.22);
  --blue-glow: rgba(255, 61, 61, 0.5);
  --ascii-color: #ff4d4d;
}

body.mode-root .topbar {
  border-bottom-color: var(--blue-dim);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

.hidden { display: none !important; }

/* ================= Shell / Layout ================= */

.shell {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 30;
}

.overlay.show { display: block; }

/* ================= Sidebar ================= */

.sidebar {
  width: 268px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  transition: transform 0.22s ease;
}

.sidebar-top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 12px 10px;
}

.new-chat-btn {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.new-chat-btn:hover {
  background: var(--bg-hover);
  border-color: var(--blue-dim);
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.mobile-only { display: none; }

.sidebar-section {
  padding: 4px 12px;
}

.section-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 9px 4px;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
}

.section-toggle:hover { color: var(--text); }

.section-toggle svg { transition: transform 0.18s ease; }
.section-toggle.open svg { transform: rotate(180deg); }

.settings-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 4px 12px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.22s ease;
}

.settings-panel.open {
  max-height: 600px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  color: var(--text-faint);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.checkbox-label {
  display: flex !important;
  flex-direction: row !important;
  align-items: center;
  gap: 7px;
  text-transform: none !important;
  letter-spacing: 0 !important;
  color: var(--text-muted) !important;
  font-size: 12px !important;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: var(--blue);
  cursor: pointer;
  flex-shrink: 0;
}

.checkbox-label input[type="checkbox"]:disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

.field-hint {
  color: var(--text-faint);
  font-size: 10px;
  line-height: 1.4;
}

.grounding-field.disabled .checkbox-label span {
  opacity: 0.5;
}

select, textarea, input {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 9px;
  font-family: inherit;
  font-size: 12.5px;
  resize: none;
}

select:focus, textarea:focus, input:focus {
  outline: none;
  border-color: var(--blue-dim);
}

#sysprompt {
  font-size: 11px;
  line-height: 1.5;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.status-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  color: var(--text-faint);
  padding-top: 2px;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  flex-shrink: 0;
}

.dot.ok { background: #5fb37c; }
.dot.err { background: var(--danger); }

.instructions-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.instruction-item {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: 11.5px;
  color: var(--text-muted);
  line-height: 1.4;
}

.instruction-item span {
  flex: 1;
  word-break: break-word;
}

.instruction-item .del-btn {
  background: transparent;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  padding: 1px;
  display: flex;
  border-radius: 4px;
  flex-shrink: 0;
}

.instruction-item .del-btn:hover { color: var(--danger); }

.instruction-empty {
  color: var(--text-faint);
  font-size: 11px;
  font-style: italic;
  padding: 2px;
}

.instruction-add-row {
  display: flex;
  gap: 6px;
}

.instruction-add-row input {
  flex: 1;
  font-size: 12px;
}

.mini-btn {
  background: var(--blue-soft);
  border: 1px solid var(--blue-dim);
  color: var(--blue);
  padding: 0 12px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

.mini-btn:hover { background: var(--blue-soft-strong); }

.history-section {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.section-label {
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 10px 4px 6px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-bottom: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 9px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
}

.history-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.history-item.active {
  background: var(--blue-soft);
  color: var(--text);
}

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

.history-item .del-btn {
  background: transparent;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  opacity: 0;
  padding: 2px;
  display: flex;
  border-radius: 4px;
  flex-shrink: 0;
}

.history-item:hover .del-btn { opacity: 1; }
.history-item .del-btn:hover { color: var(--danger); }

.sidebar-footer {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.logout-btn:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.hint {
  color: var(--text-faint);
  font-size: 10.5px;
  letter-spacing: 0.02em;
}

/* ================= Main ================= */

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

.topbar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
  flex-shrink: 0;
}

.topbar-title {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.03em;
}

.topbar-spacer { flex: 1; }

.mode-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--blue);
  border: 1px solid var(--blue-dim);
  background: var(--blue-soft);
  padding: 3px 8px;
  border-radius: 20px;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}

#voice-mode-btn.active {
  color: var(--blue);
  background: var(--blue-soft);
}

/* ================= Landing ================= */

.landing {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 20px;
  overflow-y: auto;
}

.ascii-logo {
  font-family: var(--font-mono);
  color: var(--ascii-color);
  font-size: 13px;
  line-height: 1.15;
  text-align: center;
  margin: 0;
  white-space: pre;
  text-shadow: 0 0 24px var(--blue-glow);
  letter-spacing: 0;
  transition: color 0.25s ease, text-shadow 0.25s ease;
}

.landing-sub {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  max-width: 480px;
  margin: 0;
}

.landing-composer {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 12px 10px 14px;
  transition: border-color 0.15s;
}

.landing-composer:focus-within {
  border-color: var(--blue-dim);
}

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

.landing-composer textarea {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 4px;
  font-size: 14.5px;
  max-height: 160px;
}

.landing-composer textarea:focus { outline: none; }

.quick-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  max-width: 640px;
}

.quick-chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 8px 13px;
  border-radius: 20px;
  font-size: 12.5px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.quick-chip:hover {
  border-color: var(--blue-dim);
  color: var(--text);
}

/* ================= Chat view ================= */

.chat-view {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.log {
  max-width: 760px;
  margin: 0 auto;
  padding: 28px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.entry {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.entry.user {
  align-items: flex-end;
}

.entry.assistant {
  align-items: flex-start;
}

.entry-meta {
  font-size: 11px;
  color: var(--text-faint);
  letter-spacing: 0.03em;
  padding: 0 3px;
}

.bubble {
  line-height: 1.65;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 14.5px;
}

.entry.user .bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 15px;
  max-width: 85%;
}

.entry.assistant .bubble {
  color: var(--text);
  max-width: 100%;
}

.bubble.error {
  color: var(--danger);
  border-color: var(--danger) !important;
}

.bubble code {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  padding: 2px 5px;
  font-family: var(--font-mono);
  font-size: 13px;
}

.bubble pre {
  background: #141518;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
}

.bubble pre code {
  background: none;
  border: none;
  padding: 0;
}

.typing {
  color: var(--text-faint);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing .tdot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--blue);
  animation: tpulse 1.1s infinite ease-in-out;
}

.typing .tdot:nth-child(2) { animation-delay: 0.15s; }
.typing .tdot:nth-child(3) { animation-delay: 0.3s; }

@keyframes tpulse {
  0%, 60%, 100% { opacity: 0.25; transform: scale(0.85); }
  30% { opacity: 1; transform: scale(1); }
}

/* ================= Composer (chat view) ================= */

.composer {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 20px 18px;
  flex-shrink: 0;
}

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

.composer textarea {
  flex: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
  font-size: 14.5px;
  max-height: 160px;
  transition: border-color 0.15s;
}

.composer textarea:focus-within,
.composer textarea:focus {
  border-color: var(--blue-dim);
  outline: none;
}

.send-btn {
  background: var(--blue);
  color: #10131c;
  border: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

.send-btn:hover { background: #7aa3ff; }
.send-btn:active { transform: scale(0.94); }

.send-btn:disabled {
  background: var(--blue-dim);
  cursor: not-allowed;
}

/* ================= Attachments ================= */

.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 2px 0;
}

.attach-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 5px 8px;
  font-size: 12px;
  color: var(--text-muted);
}

.attach-chip img {
  width: 34px;
  height: 34px;
  object-fit: cover;
  border-radius: 6px;
  display: block;
}

.attach-chip .attach-label {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attach-chip .attach-remove {
  background: none;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  display: flex;
  padding: 2px;
  border-radius: 4px;
  flex-shrink: 0;
}

.attach-chip .attach-remove:hover {
  color: var(--danger);
  background: rgba(226, 104, 95, 0.12);
}

.attach-chip.transcribing {
  color: var(--blue);
  border-color: var(--blue-dim);
}

.msg-image {
  max-width: 260px;
  max-height: 260px;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: block;
  margin-bottom: 6px;
  object-fit: cover;
}

/* ================= Mic / voice ================= */

.mic-btn.listening {
  color: var(--danger);
  background: rgba(226, 104, 95, 0.14);
  animation: mic-pulse 1.4s infinite ease-in-out;
}

@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(226, 104, 95, 0.35); }
  50% { box-shadow: 0 0 0 6px rgba(226, 104, 95, 0); }
}

.voice-status {
  text-align: center;
  color: var(--text-faint);
  font-size: 11.5px;
  padding: 0 2px;
}

/* ================= Toast ================= */

.toast {
  position: fixed;
  bottom: 90px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--bg-elevated);
  border: 1px solid var(--blue-dim);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 60;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ================= Scrollbars ================= */

.history-section::-webkit-scrollbar,
.chat-view::-webkit-scrollbar,
.landing::-webkit-scrollbar {
  width: 8px;
}

.history-section::-webkit-scrollbar-thumb,
.chat-view::-webkit-scrollbar-thumb,
.landing::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* ================= Responsive: mobile ================= */

@media (max-width: 860px) {
  .topbar { display: flex; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    width: 84vw;
    max-width: 300px;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.4);
  }

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

  .mobile-only { display: flex; }

  .ascii-logo {
    font-size: 8px;
  }

  .landing-sub {
    font-size: 13px;
    padding: 0 8px;
  }

  .log { padding: 18px 14px 14px; }

  .composer { padding: 10px 12px 14px; }

  .entry.user .bubble { max-width: 92%; }

  .quick-row { padding: 0 4px; }

  .quick-chip { font-size: 12px; padding: 7px 11px; }
}

@media (max-width: 420px) {
  .ascii-logo { font-size: 6.5px; }
}
