/* ── CoderShell — Design System ─────────────────────────────────────── */

:root {
  /* Background layers */
  --bg:           #121212;
  --bg-raised:    #1E1E1E;
  --bg-surface:   #222222;
  --bg-input:     #1A1A1A;

  /* Borders — low-contrast, rgba white */
  --border:       rgba(255, 255, 255, 0.06);
  --border-hover: rgba(255, 255, 255, 0.10);
  --border-focus: rgba(255, 255, 255, 0.16);

  /* Text */
  --text:         #E0E0E0;
  --text-sec:     #9E9E9E;
  --text-muted:   #6D6D6D;

  /* Accents — split-complementary: teal dominant, amber + desaturated red supporting */
  --accent:       #5EAAA8;
  --accent-dim:   #3D7A79;
  --accent-glow:  rgba(94, 170, 168, 0.15);
  --amber:        #D4A574;
  --amber-dim:    #A67D52;
  --red:          #C75B5B;
  --red-dim:      #9A4444;
  --green:        #7CB87C;
  --blue:         #6BA3BE;

  /* Spacing scale — 4px base */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sp-8: 48px;

  /* Shape */
  --radius:     6px;
  --radius-lg:  8px;
  --radius-pill: 100px;

  /* Layout */
  --max-width:  75ch;

  /* Typography */
  --font-body: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;

  /* Transitions */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --duration: 180ms;
}

/* ── Reset ─────────────────────────────────────────────────────────── */

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

html {
  height: 100%;
}

body {
  height: 100vh;       /* fallback */
  height: 100dvh;      /* dynamic viewport height — accounts for mobile browser chrome */
  overflow: hidden;    /* body must not scroll; only #messages scrolls */
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  display: flex;
  flex-direction: column;
}

::selection {
  background: var(--accent-glow);
  color: var(--text);
}

/* ── Top bar ───────────────────────────────────────────────────────── */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-3) var(--sp-5);
  background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 48px;
}

#topbar-brand {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

#brand-indicator {
  width: 3px;
  height: 18px;
  background: var(--accent);
  border-radius: 2px;
}

#topbar h1 {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  letter-spacing: 0.02em;
}

#topbar-controls {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
}

/* Status dot */
.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.dot-ok    { background: var(--green); box-shadow: 0 0 4px rgba(124, 184, 124, 0.4); }
.dot-err   { background: var(--red); box-shadow: 0 0 4px rgba(199, 91, 91, 0.4); }
.dot-busy  { background: var(--amber); animation: pulse 1.2s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.3; }
}

.mono-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#btn-new {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-sec);
  background: transparent;
  border: 1px solid var(--border);
  padding: var(--sp-1) var(--sp-3);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out);
}
#btn-new:hover {
  color: var(--accent);
  border-color: var(--accent-dim);
}
#btn-new:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#btn-restart {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out);
}
#btn-restart:hover {
  color: var(--amber);
  border-color: var(--amber);
}
#btn-restart:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Message area ──────────────────────────────────────────────────── */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-5) var(--sp-5);
  scroll-behavior: smooth;
}

/* Empty state */
#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 300px;
  color: var(--text-muted);
  user-select: none;
  /* Subtle grid pattern */
  background-image:
    linear-gradient(rgba(255,255,255,0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.02) 1px, transparent 1px);
  background-size: 40px 40px;
  border-radius: var(--radius-lg);
}

.empty-icon {
  font-size: 28px;
  color: var(--accent-dim);
  margin-bottom: var(--sp-3);
  opacity: 0.6;
}

.empty-title {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 500;
  color: var(--text-sec);
  letter-spacing: 0.02em;
}

.empty-sub {
  font-size: 14px;
  color: var(--text-muted);
  margin-top: var(--sp-1);
}

#empty-state.hidden { display: none; }

/* ── Messages ──────────────────────────────────────────────────────── */

.message {
  max-width: var(--max-width);
  margin: 0 auto var(--sp-4) auto;
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  word-wrap: break-word;
  overflow-wrap: break-word;
  transition: transform var(--duration) var(--ease-out),
              box-shadow var(--duration) var(--ease-out);
}

.message:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.message-user {
  background: var(--bg-raised);
  margin-left: auto;
  margin-right: auto;
  border-left: 2px solid var(--accent-dim);
}

.message-assistant {
  background: var(--bg-surface);
  margin-left: auto;
  margin-right: auto;
}

.message-label {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: var(--sp-2);
  color: var(--text-muted);
}

.message-user .message-label { color: var(--accent); }

/* ── Think blocks ──────────────────────────────────────────────────── */

.think-block {
  margin: var(--sp-2) 0;
  border-left: 2px solid rgba(255, 255, 255, 0.06);
  padding-left: var(--sp-3);
  border-radius: 0 var(--radius) var(--radius) 0;
}

.think-block summary {
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  user-select: none;
  padding: var(--sp-1) 0;
}
.think-block summary:hover { color: var(--text-sec); }

.think-block pre {
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 13px;
  white-space: pre-wrap;
  margin: var(--sp-1) 0 0 0;
  background: none;
  padding: 0;
  border: none;
  line-height: 1.5;
}

/* ── Status lines ──────────────────────────────────────────────────── */

.status-line {
  font-family: var(--font-mono);
  font-size: 12px;
  padding: var(--sp-1) 0;
  white-space: pre-wrap;
  letter-spacing: 0.01em;
}

.status-tool    { color: var(--blue); }
.status-ok      { color: var(--green); }
.status-fail    { color: var(--red); }
.status-warn    { color: var(--amber); }
.status-search  { color: var(--accent); }
.status-info    { color: var(--text-muted); }
.status-summary { color: var(--text-muted); border-top: 1px solid var(--border); margin-top: var(--sp-2); padding-top: var(--sp-2); }

/* ── Markdown content ──────────────────────────────────────────────── */

.message-content {
  font-size: 15px;
  line-height: 1.65;
}

.message-content p       { margin: 0.5em 0; }
.message-content p:first-child { margin-top: 0; }
.message-content p:last-child  { margin-bottom: 0; }

.message-content h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 1em 0 0.4em 0;
  line-height: 1.2;
}
.message-content h2 {
  font-size: 22px;
  font-weight: 600;
  margin: 0.8em 0 0.4em 0;
  line-height: 1.3;
}
.message-content h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0.8em 0 0.3em 0;
  line-height: 1.3;
}
.message-content h4, .message-content h5, .message-content h6 {
  font-size: 15px;
  font-weight: 600;
  margin: 0.6em 0 0.3em 0;
  line-height: 1.4;
}

.message-content pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-3);
  overflow-x: auto;
  margin: var(--sp-2) 0;
  font-size: 13px;
  line-height: 1.5;
}

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.88em;
}

.message-content :not(pre) > code {
  background: rgba(94, 170, 168, 0.1);
  color: var(--accent);
  padding: 2px 6px;
  border-radius: 3px;
}

.message-content ul, .message-content ol {
  padding-left: 1.5em;
  margin: 0.5em 0;
}
.message-content li { margin: 0.2em 0; }

.message-content table {
  border-collapse: collapse;
  margin: var(--sp-2) 0;
  font-size: 14px;
  width: 100%;
}

.message-content th, .message-content td {
  border: 1px solid var(--border);
  padding: var(--sp-2) var(--sp-3);
  text-align: left;
}

.message-content th {
  background: var(--bg-raised);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-sec);
}

.message-content blockquote {
  border-left: 3px solid var(--accent-dim);
  padding-left: var(--sp-3);
  color: var(--text-sec);
  margin: var(--sp-2) 0;
}

.message-content a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--duration) var(--ease-out);
}
.message-content a:hover {
  color: #7ECAC8;
  text-decoration: underline;
}

.message-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--sp-4) 0;
}

.message-content img {
  max-width: 100%;
  border-radius: var(--radius);
  margin: var(--sp-2) 0;
}

.message-content strong { font-weight: 600; }

/* ── Loading indicator ─────────────────────────────────────────────── */

.loading-dots {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-muted);
}

.loading-dots::after {
  content: "";
  animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
  0%  { content: ""; }
  25% { content: "."; }
  50% { content: ".."; }
  75% { content: "..."; }
}

/* ── Input bar ─────────────────────────────────────────────────────── */

#inputbar {
  display: flex;
  align-items: flex-end;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5);
  background: var(--bg-raised);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#input-wrap {
  flex: 1;
  position: relative;
}

#input {
  width: 100%;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 14px;
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  min-height: 42px;
  max-height: 200px;
  outline: none;
  transition: border-color var(--duration) var(--ease-out),
              box-shadow var(--duration) var(--ease-out);
}

#input:focus {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#input::placeholder { color: var(--text-muted); }

/* Buttons */
#btn-send, #btn-stop {
  border: none;
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.02em;
  padding: 10px 20px;
  transition: background var(--duration) var(--ease-out),
              transform var(--duration) var(--ease-out),
              opacity var(--duration) var(--ease-out);
}

#btn-send {
  background: var(--accent);
  color: #121212;
}
#btn-send:hover {
  background: #6FC2C0;
  transform: translateY(-1px);
}
#btn-send:active {
  transform: translateY(0);
}
#btn-send:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  transform: none;
}
#btn-send:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#btn-stop {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red-dim);
}
#btn-stop:hover {
  background: rgba(199, 91, 91, 0.1);
  border-color: var(--red);
}
#btn-stop:focus-visible {
  outline: 2px solid var(--red);
  outline-offset: 2px;
}

/* ── Voice selector ────────────────────────────────────────────────── */

#voice-select {
  background: var(--bg-surface);
  color: var(--text-sec);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  outline: none;
  transition: border-color var(--duration) var(--ease-out),
              color var(--duration) var(--ease-out);
}
#voice-select:hover, #voice-select:focus {
  border-color: var(--border-hover);
  color: var(--text);
}

/* ── Voice buttons ─────────────────────────────────────────────────── */

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-sec);
  cursor: pointer;
  flex-shrink: 0;
  transition: color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out),
              background var(--duration) var(--ease-out);
}
.btn-icon:hover {
  color: var(--text);
  border-color: var(--border-hover);
  background: rgba(255,255,255,0.04);
}
.btn-icon:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Recording state */
#btn-mic.recording {
  color: var(--red);
  border-color: var(--red-dim);
  background: rgba(199, 91, 91, 0.12);
  animation: mic-pulse 1s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(199, 91, 91, 0.4); }
  50%       { box-shadow: 0 0 0 6px rgba(199, 91, 91, 0); }
}

/* Transcribing state */
#btn-mic.transcribing {
  color: var(--amber);
  border-color: var(--amber-dim);
  background: rgba(212, 165, 116, 0.08);
  cursor: wait;
}

/* Speaker button on messages */
.btn-speak {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--duration) var(--ease-out),
              color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out);
  vertical-align: middle;
  margin-left: var(--sp-2);
}
.message-assistant:hover .btn-speak,
.btn-speak.playing {
  opacity: 1;
}

/* On touch screens there is no hover — always show speaker button */
@media (hover: none) {
  .btn-speak {
    opacity: 0.5;
  }
}
.btn-speak:hover {
  color: var(--accent);
  border-color: var(--border);
}
.btn-speak.playing {
  color: var(--accent);
  animation: speaker-pulse 0.8s ease-in-out infinite alternate;
}
@keyframes speaker-pulse {
  from { opacity: 0.6; }
  to   { opacity: 1; }
}

/* ── Image preview ─────────────────────────────────────────────────── */

#img-preview {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-3);
  background: var(--bg-raised);
}
#img-preview img {
  height: 48px;
  width: auto;
  border-radius: 4px;
  border: 1px solid var(--border);
}
#img-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 6px;
}
#img-clear:hover { color: var(--red); }

/* ── History panel ─────────────────────────────────────────────────── */

#history-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(320px, 90vw);
  background: var(--bg-raised);
  border-left: 1px solid var(--border);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#history-panel.hidden { display: none; }
#history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--text);
}
#btn-history-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
}
#btn-history-close:hover { color: var(--text); }
#history-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-2) 0;
}
.history-item {
  padding: var(--sp-2) var(--sp-4);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.history-item:hover { background: var(--bg-surface); }
.history-date {
  font-size: 11px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  margin-bottom: 2px;
}
.history-preview {
  font-size: 13px;
  color: var(--text-sec);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-loading, .history-empty, .history-error {
  padding: var(--sp-4);
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}
.history-error { color: var(--red); }

/* ── History toggle button in topbar ───────────────────────────────── */

#btn-history {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: color var(--duration) var(--ease-out),
              border-color var(--duration) var(--ease-out);
}
#btn-history:hover {
  color: var(--accent);
  border-color: var(--accent-dim);
}

.hidden { display: none !important; }

/* ── Scrollbar ─────────────────────────────────────────────────────── */

#messages::-webkit-scrollbar { width: 5px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 3px;
}
#messages::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.14);
}

/* Firefox */
#messages {
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.08) transparent;
}

/* ── Focus states — keyboard navigation ────────────────────────────── */

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Responsive ────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  :root {
    --sp-5: 16px;
  }

  #topbar { padding: var(--sp-2) var(--sp-3); }
  #messages { padding: var(--sp-3); }
  #inputbar { padding: var(--sp-2) var(--sp-3); }

  .message {
    margin-left: 0 !important;
    margin-right: 0 !important;
    padding: var(--sp-2) var(--sp-3);
  }

  .message-content h1 { font-size: 22px; }
  .message-content h2 { font-size: 18px; }
  .message-content h3 { font-size: 16px; }

  #btn-send, #btn-stop {
    padding: 10px 14px;
  }

  .mono-label { display: none; }
}

@media (min-width: 1400px) {
  #messages {
    padding-left: calc((100vw - 75ch) / 2 - var(--sp-5));
    padding-right: calc((100vw - 75ch) / 2 - var(--sp-5));
  }
}
