:root {
  --bg: #15161a;
  --panel: #1c1e24;
  --panel-2: #23262e;
  --text: #e6e6ea;
  --muted: #8a8f99;
  --accent: #ff9ec7;
  --accent-2: #7cc5ff;
  --neko: #2a2030;          /* purple-tinged dark — Neko bubbles */
  --neko-accent: #c89bff;
  --codex: #1a2540;         /* dark navy — Codex bubbles */
  --codex-accent: #6fb1ff;
  --user-bg: #f4f4f6;       /* white-ish — user bubbles ("black surround in white") */
  --user-fg: #111;
  --user-border: #000;
  --border: #2c2f38;
}

* { box-sizing: border-box; }

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

body { display: flex; flex-direction: column; height: 100%; }

header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 17px; }
.controls { display: flex; gap: 10px; align-items: center; }
.controls select, .controls button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font: inherit;
  cursor: pointer;
}
#toggle-tts.tts-off { opacity: 0.5; filter: grayscale(1); }
#toggle-tts.tts-on  { background: rgba(255, 158, 199, 0.15); border-color: var(--accent); }
#toggle-tts.tts-busy {
  background: rgba(255, 158, 199, 0.25);
  border-color: var(--accent);
  animation: monitor-pulse 1.2s ease-in-out infinite;
}
#voice-id { opacity: 0.5; filter: grayscale(1); }
#voice-id.enrolled {
  opacity: 1;
  filter: none;
  background: rgba(124, 220, 130, 0.15);
  border-color: rgba(124, 220, 130, 0.5);
}
#voice-id.recording {
  background: rgba(220, 60, 60, 0.25);
  border-color: #e74c3c;
  animation: monitor-pulse 0.8s ease-in-out infinite;
  opacity: 1;
}
.model-info { color: var(--muted); font-size: 12px; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 0;
  min-height: 0;
}
body.with-avatar.mode-single .layout { grid-template-columns: 1fr 380px; }
body.with-avatar.mode-dual   .layout { grid-template-columns: 1fr 0; } /* avatar collapses in dual */

.dual {
  display: flex;
  flex-direction: row;
  min-width: 0;
  min-height: 0;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--border);
  transition: flex 0.4s ease;
}
.pane:last-child { border-right: none; }

/* Layout modes */
body.mode-single .pane.codex { display: none; }
body.mode-single .pane.neko  { flex: 1 1 100%; }

body.mode-dual.focus-neko  .pane.neko  { flex: 4 1 0; }
body.mode-dual.focus-neko  .pane.codex { flex: 1 1 0; }
body.mode-dual.focus-codex .pane.neko  { flex: 1 1 0; }
body.mode-dual.focus-codex .pane.codex { flex: 4 1 0; }

/* When a pane is "shrunk" (1/5), dim it slightly so focus is obvious */
body.mode-dual.focus-codex .pane.neko,
body.mode-dual.focus-neko  .pane.codex {
  filter: brightness(0.7) saturate(0.85);
  transition: flex 0.4s ease, filter 0.3s ease;
}

.pane-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.pane.neko  .pane-label { color: var(--neko-accent); }
.pane.codex .pane-label { color: var(--codex-accent); }
.pane-model { font-size: 11px; font-weight: 400; color: var(--muted); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

#avatar-panel {
  position: relative;
  background: linear-gradient(160deg, #1a1320 0%, #15161a 100%);
  border-left: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
body:not(.with-avatar) #avatar-panel,
body.mode-dual #avatar-panel { display: none; }
#avatar-canvas { width: 100%; height: 100%; display: block; }
.avatar-status {
  position: absolute;
  bottom: 8px; left: 0; right: 0;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  pointer-events: none;
}

/* Bubbles */
.msg {
  padding: 10px 14px;
  border-radius: 10px;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-width: 85%;
}
.msg.user {
  background: var(--user-bg);
  color: var(--user-fg);
  border: 2px solid var(--user-border);
  align-self: flex-end;
}
.msg.assistant.neko  { background: var(--neko);  align-self: flex-start; }
.msg.assistant.codex { background: var(--codex); align-self: flex-start; }
.msg .who {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.msg.user .who { color: #555; }

/* Status bar */
.status {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
  min-height: 22px;
  transition: color 0.2s;
}
.status .dot {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.3;
}
.status.idle .status-text::before { content: "○ "; opacity: 0.5; }
.status.working { color: var(--accent); }
.status.working .dot { background: var(--accent); animation: pulse 1.2s infinite ease-in-out; }
.status.working .dot:nth-child(2) { animation-delay: 0.2s; }
.status.working .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* Composer */
.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.input {
  flex: 1;
  resize: none;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
}
.pane.neko  .input:focus { outline: 1px solid var(--neko-accent); border-color: var(--neko-accent); }
.pane.codex .input:focus { outline: 1px solid var(--codex-accent); border-color: var(--codex-accent); }

.send {
  border: none;
  border-radius: 8px;
  padding: 0 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.pane.neko  .send { background: var(--accent);    color: #1a1015; }
.pane.codex .send { background: var(--codex-accent); color: #0b1426; }
.send:disabled { opacity: 0.5; cursor: not-allowed; }

.stop {
  background: #c0392b;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0 14px;
  font: inherit;
  font-size: 16px;
  cursor: pointer;
}
.stop:hover { background: #e74c3c; }

.thinking-chip {
  align-self: flex-start;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #c89bff;
  background: rgba(200, 155, 255, 0.08);
  border: 1px solid rgba(200, 155, 255, 0.3);
  user-select: none;
}
.thinking-chip .pulse {
  width: 8px; height: 8px; border-radius: 50%;
  background: #c89bff;
  animation: thinking-pulse 1.2s ease-in-out infinite;
}
@keyframes thinking-pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.85); }
  50%      { opacity: 1; transform: scale(1.1); }
}

/* Attachments */
.attach, .mic, .wake, .ambient {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 16px;
  cursor: pointer;
}
.attach:hover, .mic:hover, .wake:hover, .ambient:hover { background: var(--panel); }

/* Stacked mic + wake — same horizontal footprint as a single button */
.mic-stack {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 42px;
}
.mic-stack > .stacked {
  flex: 1 1 0;
  padding: 0;
  font-size: 13px;
  border-radius: 6px;
  min-height: 22px;
  width: 100%;
}
.wake.active {
  background: #2d7fc4;            /* solid blue, clearly visible */
  border-color: #7cc5ff;
  color: #fff;
  animation: wake-pulse 2.0s ease-in-out infinite;
}
.wake.processing,
.ambient.processing {
  background: #d4a256;            /* solid amber */
  border-color: #ffd58a;
  color: #1a1015;
}
.ambient.active {
  background: #8552c5;            /* solid purple, clearly visible */
  border-color: #c89bff;
  color: #fff;
  animation: ambient-pulse 2.4s ease-in-out infinite;
}
@keyframes wake-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(124, 197, 255, 0.5); }
  50%      { box-shadow: 0 0 0 8px rgba(124, 197, 255, 0); }
}
@keyframes ambient-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(200, 155, 255, 0.5); }
  50%      { box-shadow: 0 0 0 8px rgba(200, 155, 255, 0); }
}
/* Three-button stack — slightly shorter buttons to fit in the same row height */
.mic-stack-3 > .stacked { min-height: 18px; font-size: 11.5px; }
.mic.recording {
  background: rgba(220, 60, 60, 0.18);
  border-color: #e74c3c;
  animation: mic-pulse 1.0s ease-in-out infinite;
}
.mic.transcribing {
  background: rgba(255, 200, 100, 0.18);
  border-color: #ffd58a;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(231, 76, 60, 0.4); }
  50%      { box-shadow: 0 0 0 8px rgba(231, 76, 60, 0); }
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 16px;
  background: var(--panel);
}
.attachments:empty { padding: 0; }
.attachment-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  padding: 3px 8px 3px 10px;
  background: rgba(124, 197, 255, 0.1);
  border: 1px solid rgba(124, 197, 255, 0.3);
  border-radius: 999px;
  color: var(--accent-2);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-pill .remove {
  cursor: pointer;
  opacity: 0.6;
  font-weight: bold;
}
.attachment-pill .remove:hover { opacity: 1; }
.attachment-pill.uploading {
  background: rgba(255, 200, 100, 0.1);
  border-color: rgba(255, 200, 100, 0.3);
  color: #ffd58a;
}
.attachment-pill.failed {
  background: rgba(255, 100, 100, 0.1);
  border-color: rgba(255, 100, 100, 0.3);
  color: #ff9090;
}

/* Drop target highlight */
.chat.drop-target {
  outline: 2px dashed var(--accent-2);
  outline-offset: -8px;
  background: rgba(124, 197, 255, 0.04);
}

/* Monitor */
.monitor {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  font-size: 18px;
  cursor: pointer;
  transition: filter 0.25s, opacity 0.25s, background 0.25s, transform 0.1s;
}
.monitor.inactive { filter: grayscale(1); opacity: 0.4; cursor: not-allowed; }
.monitor.active   { filter: none; opacity: 1; background: rgba(124, 197, 255, 0.15); border-color: var(--accent-2); cursor: pointer; }
.monitor.active:hover { background: rgba(124, 197, 255, 0.25); transform: scale(1.05); }
.monitor.busy {
  filter: none; opacity: 1;
  background: rgba(255, 200, 100, 0.15);
  border-color: #ffd58a;
  animation: monitor-pulse 1.2s ease-in-out infinite;
}
@keyframes monitor-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 200, 100, 0.4); }
  50%      { box-shadow: 0 0 0 6px rgba(255, 200, 100, 0); }
}

/* Recall + tool chips */
.recall-chip {
  align-self: flex-start;
  font-size: 11px;
  color: var(--accent-2);
  background: rgba(124, 197, 255, 0.08);
  border: 1px solid rgba(124, 197, 255, 0.25);
  padding: 3px 8px;
  border-radius: 999px;
  cursor: help;
  user-select: none;
}
.tool-chip {
  align-self: flex-start;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 999px;
  user-select: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 85%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool-chip.running { color: #ffd58a; background: rgba(255, 200, 100, 0.08); border: 1px solid rgba(255, 200, 100, 0.3); }
.tool-chip.done    { color: #9ee5a3; background: rgba(120, 220, 130, 0.08); border: 1px solid rgba(120, 220, 130, 0.3); }
.tool-chip.failed  { color: #ff9090; background: rgba(255, 100, 100, 0.08); border: 1px solid rgba(255, 100, 100, 0.3); }
.tool-chip .spin {
  width: 10px; height: 10px;
  border: 2px solid rgba(255, 200, 100, 0.3);
  border-top-color: #ffd58a;
  border-radius: 50%;
  display: inline-block;
  animation: tool-spin 0.8s linear infinite;
}
@keyframes tool-spin { to { transform: rotate(360deg); } }

/* Agent step trail */
.agent-trail {
  align-self: stretch;
  margin: 4px 0 4px 8px;
  padding: 8px 12px;
  background: rgba(150, 150, 200, 0.04);
  border-left: 2px solid rgba(255, 200, 100, 0.4);
  border-radius: 4px;
  font-family: "Consolas", "Menlo", monospace;
  font-size: 11.5px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.agent-step .agent-line {
  display: flex; gap: 6px; align-items: baseline; color: var(--text);
}
.agent-step-num { color: var(--muted); width: 22px; flex-shrink: 0; text-align: right; }
.agent-verb { color: #ffd58a; }
.agent-args { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agent-thought { margin-left: 28px; color: var(--muted); font-style: italic; font-size: 11px; }

/* Mobile / narrow viewport: hide avatar entirely and tighten the chrome.
 * 900px catches most phones in landscape. */
@media (max-width: 900px) {
  body.with-avatar .layout { grid-template-columns: 1fr !important; }
  #avatar-panel { display: none !important; }
  body.mode-dual.focus-neko  .pane.codex,
  body.mode-dual.focus-codex .pane.neko { display: none; }
  body.mode-dual.focus-neko  .pane.neko,
  body.mode-dual.focus-codex .pane.codex { flex: 1 1 100%; filter: none; }

  /* Tighter header */
  header { padding: 8px 10px; gap: 6px; }
  .brand { font-size: 14px; }
  .controls { gap: 6px; }
  .controls select, .controls button {
    padding: 5px 8px; font-size: 13px;
  }
  .model-info { display: none; }     /* not useful on phone screens */
  #toggle-avatar { display: none; }  /* avatar is force-hidden anyway */

  /* Tighter pane header */
  .pane-header { padding: 6px 12px; font-size: 12px; }
  .pane-model { display: none; }

  /* Tighter chat */
  main.chat { padding: 12px; gap: 8px; }
  .msg { max-width: 92%; padding: 8px 12px; font-size: 14px; }

  /* Tighter composer with smaller buttons so the row fits */
  .composer { padding: 8px 8px; gap: 6px; }
  .input { padding: 8px 10px; font-size: 14px; }
  .attach, .mic, .wake, .monitor, .send, .stop {
    padding: 0 10px; font-size: 14px; min-width: 36px;
  }
  .mic-stack { width: 36px; }
  .mic-stack > .stacked { font-size: 12px; }
  .send { padding: 0 14px; }

  /* Smaller chips, fit in narrower viewport */
  .recall-chip, .tool-chip, .thinking-chip { font-size: 10.5px; padding: 3px 8px; }
  .agent-trail { font-size: 10.5px; padding: 6px 10px; }

  /* Status bar slightly smaller */
  .status { padding: 3px 10px; font-size: 10.5px; }

  /* Attachments */
  .attachments { padding: 0 8px; }
  .attachment-pill { max-width: 200px; font-size: 10.5px; }
}

/* Touch devices — applied regardless of width, since some phones report
 * unusual viewport widths (foldables, tablets in some modes). This catches
 * everything with a coarse pointer. */
@media (pointer: coarse) {
  /* Hide avatar panel completely (defense in depth — covers the case where
     the width-based query doesn't fire correctly on some Android browsers) */
  body.with-avatar .layout { grid-template-columns: 1fr !important; }
  #avatar-panel { display: none !important; }
  #toggle-avatar { display: none !important; }

  /* Force panes to fill width — guards against any leftover layout slack */
  html, body { overflow-x: hidden; max-width: 100vw; }
  .layout, .dual { width: 100%; max-width: 100vw; }
  .pane { width: 100% !important; max-width: 100vw !important; min-width: 0 !important; }

  /* Tap targets — Apple recommends 44pt, Google 48dp. The stacked mic+wake
     was only ~22px tall per button, way too small to tap reliably. */
  .mic-stack {
    width: 48px !important;
    gap: 3px !important;
  }
  .mic-stack > .stacked {
    min-height: 32px !important;
    font-size: 16px !important;
  }
  .attach, .monitor, .send, .stop {
    min-width: 44px !important;
    min-height: 40px !important;
    font-size: 16px !important;
  }
  .controls select, .controls button {
    min-height: 38px !important;
    padding: 6px 10px !important;
    font-size: 14px !important;
  }
  .input {
    min-height: 44px !important;
    font-size: 16px !important;  /* prevents iOS Safari zoom-on-focus */
  }
}
