/* ─────────────────────────────────────────────────────────────────
   Modern Conversation view — design tokens + component styles.
   Mirrors the locked mockup. Loaded BEFORE style.css so legacy
   component styles (sidebar, composer) can override where needed.
   ───────────────────────────────────────────────────────────────── */

:root {
  --canvas: #f7f9fc;
  --canvas-raised: #ffffff;
  --ink: #111827;
  --ink-soft: #4b5563;
  --ink-whisper: #9ca3af;
  --rule: #e5e7eb;
  --rule-soft: #f3f4f6;

  --accent: #635bff;
  --accent-hover: #544ada;
  --accent-wash: #f0f0ff;

  --user-paper: #ffffff;
  --user-rule: #e5e7eb;
  --user-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.03);

  --mach-paper: #0f172a;
  --mach-header: #1e293b;
  --mach-ink: #e2e8f0;
  --mach-ink-soft: #94a3b8;
  --mach-rule: #334155;
  --mach-accent: #38bdf8;

  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, monospace;

  --gutter: 2rem;
  --radius: 12px;
}

body.dark {
  --canvas: #020617;
  --canvas-raised: #0f172a;
  --ink: #f8fafc;
  --ink-soft: #94a3b8;
  --ink-whisper: #64748b;
  --rule: #1e293b;
  --rule-soft: #0f172a;

  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-wash: #1e1b4b;

  --user-paper: #0f172a;
  --user-rule: #1e293b;
  --user-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);

  --mach-paper: #000000;
  --mach-header: #0f172a;
  --mach-ink: #cbd5e1;
  --mach-ink-soft: #64748b;
  --mach-rule: #1e293b;
  --mach-accent: #38bdf8;
}

/* ─── MASTHEAD ───────────────────────────────────── */
.masthead {
  max-width: 960px;
  margin: 0 auto;
  padding: 3rem var(--gutter) 2rem;
  border-bottom: 1px solid var(--rule);
}

.masthead-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.masthead-title {
  font-weight: 700;
  font-size: clamp(1.5rem, 4vw, 2rem);
  letter-spacing: -0.04em;
  margin: 0;
  color: var(--ink);
}

.masthead-title em {
  font-style: normal;
  color: var(--accent);
}

.masthead-meta {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
}

.masthead-meta span {
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}

.masthead-meta span strong {
  color: var(--ink);
  font-weight: 600;
  margin-left: 0.25rem;
}

/* ─── CANVAS ─────────────────────────────────────── */
#chat.modern-chat {
  max-width: 960px;
  margin: 0 auto;
  padding: 3rem var(--gutter) 8rem;
  display: flex;
  flex-direction: column;
  gap: 3rem;
}

/* ─── TURN ───────────────────────────────────────── */
.turn {
  display: grid;
  grid-template-columns: 6rem 1fr;
  gap: 2rem;
  align-items: start;
}

.turn-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-whisper);
  position: sticky;
  top: 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.turn-label .num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--rule);
  color: var(--ink-soft);
  border-radius: 50%;
  font-size: 0.75rem;
  font-weight: 700;
}

.turn.user .turn-label .num {
  background: var(--accent-wash);
  color: var(--accent);
}

.turn-body { min-width: 0; }

/* ─── USER VOICE ─────────────────────────────────── */
.user-quote {
  margin: 0;
  padding: 1.5rem;
  background: var(--user-paper);
  border: 1px solid var(--user-rule);
  border-radius: var(--radius);
  box-shadow: var(--user-shadow);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--ink);
}

.user-quote p { margin: 0 0 1em 0; }
.user-quote p:last-child { margin-bottom: 0; }

.user-quote code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  color: var(--accent);
}

/* ─── ASSISTANT VOICE ────────────────────────────── */
.prose {
  font-size: 1.0625rem;
  line-height: 1.7;
  color: var(--ink);
  max-width: 70ch;
}

.prose p { margin: 0 0 1.25em 0; }
.prose p:last-child { margin-bottom: 0; }

.prose strong {
  font-weight: 600;
  color: var(--ink);
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  padding: 0.15em 0.4em;
  border-radius: 4px;
}

.prose .drop {
  color: var(--accent);
  font-weight: 600;
}

/* ─── MACHINERY (CODE/TOOLS) ─────────────────────── */
.machinery {
  margin: 1.5rem 0 2rem;
  background: var(--mach-paper);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--mach-rule);
  box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
}

.machinery summary {
  cursor: pointer;
  padding: 0.75rem 1.25rem;
  background: var(--mach-header);
  border-bottom: 1px solid var(--mach-rule);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--mach-ink-soft);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  user-select: none;
  transition: background 0.15s ease;
}

.machinery summary::-webkit-details-marker { display: none; }
.machinery summary:hover { background: var(--mach-rule); }

.machinery summary .kind {
  color: var(--mach-accent);
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.machinery summary .divider {
  color: var(--mach-ink-soft);
  opacity: 0.5;
}

.machinery summary .label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--mach-ink);
}

.machinery summary .chev {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.2s ease;
  margin-right: 0.25rem;
}

.machinery[open] summary .chev { transform: rotate(45deg); }
.machinery:not([open]) summary { border-bottom: none; }

.machinery-body {
  padding: 1.25rem;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.6;
  color: var(--mach-ink);
  white-space: pre-wrap;
  overflow-x: auto;
}

.machinery-body .comment { color: var(--mach-ink-soft); }
.machinery-body .ok { color: #10b981; }
.machinery-body .arrow { color: var(--mach-accent); }

.machinery-input,
.machinery-output {
  margin: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  white-space: pre-wrap;
  overflow-x: auto;
}

.machinery-output-label {
  margin: 1rem 0 0.5rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--mach-rule);
  color: var(--mach-ink-soft);
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* ─── METADATA FOOTER ────────────────────────────── */
.metadata {
  margin-top: 2rem;
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  font-size: 0.75rem;
  color: var(--ink-whisper);
  align-items: center;
}

.metadata span {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.metadata .mdot {
  width: 4px;
  height: 4px;
  background: var(--rule);
  border-radius: 50%;
}

.metadata .check {
  color: var(--accent);
  font-weight: 700;
}

.metadata strong {
  color: var(--ink-soft);
  font-weight: 600;
}

/* ─── THEME TOGGLE (header-actions icon) ─────────── */
body.modern-shell #theme-toggle .icon-moon { display: none; }
body.modern-shell #theme-toggle .icon-sun  { display: inline-block; }
body.modern-shell.dark #theme-toggle .icon-sun  { display: none; }
body.modern-shell.dark #theme-toggle .icon-moon { display: inline-block; }

/* ─── CANVAS BODY ────────────────────────────────── */
body.modern-shell {
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Override legacy scroll container + chat area so the modern canvas colour
   reaches edge-to-edge (legacy style.css paints both white). */
body.modern-shell #chat-area,
body.modern-shell #messages {
  background: var(--canvas) !important;
  color: var(--ink);
}

body.modern-shell #messages {
  padding: 0 !important;
}

/* Hide legacy <header> inside chat-area — masthead takes its place */
body.modern-shell #chat-area > header {
  background: var(--canvas);
  border-bottom: 1px solid var(--rule);
  color: var(--ink);
}
body.modern-shell #chat-area > header h1 { color: var(--ink); }

/* ─────────────────────────────────────────────────────────────────
   LEGACY BRIDGE — remap style.css vars (sidebar/composer/etc)
   to the modern palette so the whole chrome matches.
   ───────────────────────────────────────────────────────────────── */
body.modern-shell {
  /* Base surfaces + text */
  --bg: var(--canvas);
  --bg-surface: var(--canvas-raised);
  --bg-msg-user: var(--accent);
  --bg-msg-assistant: var(--canvas-raised);
  --text: var(--ink);
  --text-secondary: var(--ink-soft);
  --text-tertiary: var(--ink-whisper);
  --text-on-accent: #ffffff;

  /* Accent (override brown → indigo); legacy uses --accent in many places */
  --accent: #635bff;
  --accent-hover: #544ada;
  --accent-light: rgba(99, 91, 255, 0.10);

  /* Borders + surfaces */
  --border: var(--rule);
  --border-light: var(--rule-soft);
  --surface: var(--canvas-raised);
  --input-bg: var(--canvas-raised);
  --code-bg: var(--rule-soft);

  /* Legacy three-tier tokens that style.css still references */
  --user-bg: var(--canvas-raised);
  --user-text: var(--ink);
  --user-border: var(--rule);
  --assistant-accent: var(--rule);
  --machinery-bg: var(--mach-paper);
  --machinery-header-bg: var(--mach-header);
  --machinery-border: var(--mach-rule);
  --machinery-text: var(--mach-ink);
}

body.modern-shell.dark {
  /* Same bridge but using the dark canvas tokens */
  --bg: var(--canvas);
  --bg-surface: var(--canvas-raised);
  --text: var(--ink);
  --text-secondary: var(--ink-soft);
  --text-tertiary: var(--ink-whisper);
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-light: rgba(129, 140, 248, 0.16);
  --border: var(--rule);
  --border-light: var(--rule-soft);
  --surface: var(--canvas-raised);
  --input-bg: var(--canvas-raised);
  --code-bg: var(--rule-soft);
  --user-bg: var(--canvas-raised);
  --user-border: var(--rule);
}

/* Sidebar surfaces — style.css uses --bg-surface / --border-light */
body.modern-shell .sidebar {
  background: var(--canvas-raised);
  border-right: 1px solid var(--rule);
  font-family: var(--font-sans);
}

body.modern-shell .sidebar-title { color: var(--ink); letter-spacing: -0.02em; }
body.modern-shell .sidebar-section-label { color: var(--ink-whisper); letter-spacing: 0.08em; }
body.modern-shell .sidebar-item { color: var(--ink-soft); border-radius: 8px; }
body.modern-shell .sidebar-item:hover { background: var(--rule-soft); color: var(--ink); }
body.modern-shell .sidebar-item.active { background: var(--accent-wash); color: var(--accent); }
body.modern-shell .sidebar-item.active .sidebar-item-sub { color: var(--accent); }

/* Top-bar chrome that sits above messages */
body.modern-shell #chat-area > header {
  background: var(--canvas);
  border-bottom: 1px solid var(--rule);
}
body.modern-shell .header-logo {
  background: var(--accent);
  color: #fff;
}

/* Composer */
body.modern-shell #chat-area > footer {
  background: var(--canvas);
  border-top: 1px solid var(--rule);
}
body.modern-shell .composer {
  background: var(--canvas-raised);
  border: 1px solid var(--rule);
  border-radius: 999px;
  box-shadow: var(--user-shadow);
  font-family: var(--font-sans);
}
body.modern-shell .composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}
body.modern-shell #chat-input {
  background: transparent;
  color: var(--ink);
  font-family: var(--font-sans);
}
body.modern-shell .composer-icon.send-icon {
  background: var(--accent);
  color: #fff;
}
body.modern-shell .composer-icon.send-icon:hover { background: var(--accent-hover); }
body.modern-shell .meta-chip {
  background: var(--rule-soft);
  border: 1px solid var(--rule);
  color: var(--ink-soft);
  font-family: var(--font-sans);
  border-radius: 999px;
}

/* Suggestion chips on the empty state */
body.modern-shell .suggestion-chip {
  background: var(--canvas-raised);
  border: 1px solid var(--rule);
  color: var(--ink-soft);
  font-family: var(--font-sans);
  border-radius: 999px;
}
body.modern-shell .suggestion-chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}

body.modern-shell .empty-state h2 { color: var(--ink); }
body.modern-shell .empty-state p  { color: var(--ink-soft); }
body.modern-shell .empty-logo {
  background: var(--accent);
  color: #fff;
}

/* ─── RESPONSIVE ────────────────────────────────── */
@media (max-width: 768px) {
  :root { --gutter: 1.5rem; }

  .turn {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .turn-label {
    position: static;
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
  }

  #chat.modern-chat { gap: 2.5rem; padding-top: 2rem; }
  .masthead { padding: 4rem var(--gutter) 1.5rem; }
}
