:root {
  color-scheme: dark;
  --bg: #1a1d19;
  --bg-soft: #21241f;
  --surface: #282b26;
  --surface-hover: #2f322d;
  --surface-raised: #353833;
  --panel: rgba(40, 43, 38, 0.96);
  --panel-soft: rgba(48, 52, 46, 0.98);
  --border: rgba(255, 255, 255, 0.07);
  --border-hover: rgba(255, 255, 255, 0.12);
  --ink: #edf2ea;
  --ink-secondary: #b3beb1;
  --ink-muted: #7a8578;
  --accent: #8fe0b4;
  --accent-strong: #6fc99c;
  --accent-soft: rgba(143, 224, 180, 0.10);
  --accent-border: rgba(143, 224, 180, 0.30);
  --accent-glow: rgba(143, 224, 180, 0.06);
  --success: #8fe0b4;
  --warn: #d4bb6f;
  --danger: #d6837a;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.30);
  --radius-xs: 8px;
  --radius-sm: 12px;
  --radius-md: 16px;
  --radius-lg: 22px;
  --radius-xl: 28px;
  --radius-pill: 999px;
  --transition: 140ms ease;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }

body {
  font-family: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  color: var(--ink);
  background:
    radial-gradient(ellipse 60% 40% at 50% 0%, var(--accent-glow), transparent),
    linear-gradient(180deg, #1e211c, var(--bg));
}

button, textarea, input { font: inherit; border: none; outline: none; }
a { color: var(--accent); }

.sr-only {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap;
}

/* ═══════════════════════ LAYOUT ═══════════════════════ */
.app-shell { display: flex; min-height: 100vh; }
.main-wrap {
  flex: 1; min-height: 100vh;
  display: grid; grid-template-rows: auto 1fr auto;
}

/* ═══════════════════════ SIDEBAR ═══════════════════════ */
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 280px; background: var(--bg-soft);
  border-right: 1px solid var(--border);
  z-index: 200;
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform 220ms ease;
}
.sidebar.open { transform: translateX(0); }

.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.45); z-index: 199;
  backdrop-filter: blur(2px);
}
.sidebar-overlay.visible { display: block; }

.sidebar-header {
  display: flex; align-items: center;
  justify-content: space-between;
  padding: 14px 14px 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; }
.sidebar-logo {
  width: 30px; height: 30px; border-radius: 10px;
  background: linear-gradient(135deg, rgba(111,201,156,0.15), rgba(74,168,122,0.08));
  display: flex; align-items: center; justify-content: center;
}
.sidebar-logo svg { width: 24px; height: 24px; }
.sidebar-title { font-weight: 700; font-size: 15px; }

.sidebar-new-chat-btn {
  display: flex; align-items: center; gap: 8px;
  margin: 10px 10px 2px; padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--surface); border: 1px solid var(--border);
  color: var(--ink); cursor: pointer;
  font-size: 13px; font-weight: 600;
  transition: all var(--transition);
}
.sidebar-new-chat-btn:hover {
  background: var(--surface-hover); border-color: var(--border-hover);
}
.sidebar-new-chat-btn svg {
  width: 15px; height: 15px; stroke: var(--accent);
  fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.sidebar-label {
  padding: 14px 16px 6px;
  font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--ink-muted);
}

.chat-history-list { flex: 1; overflow-y: auto; padding: 2px 8px 12px; }

.chat-history-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: var(--radius-xs);
  color: var(--ink-secondary); font-size: 13px;
  cursor: pointer; transition: all var(--transition);
  background: transparent; width: 100%; text-align: left;
  border: none;
}
.chat-history-item:hover { background: var(--surface); color: var(--ink); }
.chat-history-item.active {
  background: var(--accent-soft); color: var(--accent);
}
.chat-history-item svg {
  width: 15px; height: 15px; flex-shrink: 0;
  stroke: currentColor; fill: none; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.chat-history-text {
  flex: 1; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.chat-history-delete {
  opacity: 0; width: 24px; height: 24px;
  border-radius: 6px; background: transparent;
  color: var(--ink-muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; border: none;
  transition: all var(--transition);
}
.chat-history-item:hover .chat-history-delete { opacity: 1; }
.chat-history-delete:hover {
  background: rgba(214,131,122,0.15); color: var(--danger);
}
.chat-history-delete svg { width: 13px; height: 13px; }

.chat-history-empty {
  padding: 24px 16px; color: var(--ink-muted);
  font-size: 13px; text-align: center;
}

/* ═══════════════════════ TOPBAR ═══════════════════════ */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 18px;
  position: sticky; top: 0; z-index: 50;
  background: rgba(26,29,25,0.85);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}
.topbar-left, .topbar-right {
  display: flex; align-items: center; gap: 10px;
}

.brand { display: inline-flex; align-items: center; gap: 10px; }
.brand-glyph {
  width: 30px; height: 30px; border-radius: 10px;
  background: linear-gradient(135deg, rgba(111,201,156,0.15), rgba(74,168,122,0.08));
  display: flex; align-items: center; justify-content: center;
}
.brand-glyph svg { width: 24px; height: 24px; }
.brand-name { font-weight: 700; font-size: 15px; }
.brand-note { color: var(--ink-muted); font-size: 12px; }

.icon-btn {
  width: 34px; height: 34px; border-radius: var(--radius-xs);
  background: transparent; color: var(--ink-secondary);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all var(--transition); border: none;
}
.icon-btn:hover { color: var(--ink); background: var(--surface); }
.icon-btn svg {
  width: 18px; height: 18px; stroke: currentColor;
  fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

.new-chat-icon { border: 1px solid var(--border); }
.new-chat-icon:hover { border-color: var(--border-hover); }

.status-pill {
  display: inline-flex; align-items: center; gap: 7px;
  border-radius: var(--radius-pill); padding: 6px 13px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  font-size: 12px; font-weight: 600; color: var(--ink-secondary);
}
.status-pill::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: var(--ink-muted);
  flex-shrink: 0;
}
.status-pill.success { color: var(--success); }
.status-pill.success::before { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-pill.warn { color: var(--warn); }
.status-pill.warn::before { background: var(--warn); }
.status-pill.danger { color: var(--danger); }
.status-pill.danger::before { background: var(--danger); }
.status-pill.loading { color: var(--ink-muted); }
.status-pill.loading::before {
  background: var(--ink-muted);
  animation: pulse-dot 1.4s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.meta-pill {
  display: inline-flex; align-items: center;
  border-radius: var(--radius-pill); padding: 6px 12px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  font-size: 12px; color: var(--ink-muted);
}
.meta-pill.small { padding: 4px 10px; font-size: 11px; }

/* ═══════════════════════ HERO ═══════════════════════ */
.main-stage {
  display: grid; width: min(860px, 100%);
  margin: 0 auto; padding: 0 18px;
  align-content: center; justify-items: center;
}

.hero {
  width: 100%; text-align: center; padding: 64px 0 30px;
}
.hero.hidden { display: none; }

.hero-glyph {
  width: 56px; height: 56px; margin: 0 auto 18px;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(111,201,156,0.15), rgba(74,168,122,0.08));
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 24px rgba(143,224,180,0.12);
}
.hero-glyph svg { width: 40px; height: 40px; }

.hero h1 {
  margin: 0;
  font-family: "Iowan Old Style", "Palatino Linotype", "Georgia", serif;
  font-size: clamp(2.2rem, 5vw, 3.8rem);
  font-weight: 400; line-height: 1.0; letter-spacing: -0.02em;
}

.selected-skill-bar {
  display: flex; justify-content: center;
  flex-wrap: wrap; gap: 8px; margin-top: 18px;
  min-height: 0;
}

.skill-chip {
  border-radius: var(--radius-pill); padding: 6px 13px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  color: var(--accent); font-size: 12px; font-weight: 600;
}

/* ═══════════════════════ MESSAGES ═══════════════════════ */
.messages {
  width: 100%; display: grid; gap: 4px;
  padding: 8px 0 140px;
}

.message {
  border-radius: var(--radius-lg); padding: 16px 20px;
  border: 1px solid transparent;
  transition: background var(--transition);
}
.message:hover { background: rgba(255,255,255,0.015); }

.message[data-role="user"] {
  background: rgba(255,255,255,0.025);
  border-color: var(--border);
}

.message-head { display: flex; align-items: center; gap: 10px; }

.message-avatar {
  width: 30px; height: 30px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; overflow: hidden;
  font-weight: 800; font-size: 13px;
}
.message-avatar svg { width: 22px; height: 22px; }
.message[data-role="assistant"] .message-avatar {
  background: linear-gradient(135deg, rgba(111,201,156,0.18), rgba(74,168,122,0.10));
  padding: 3px;
}
.message[data-role="user"] .message-avatar {
  background: var(--surface-raised); color: var(--ink-secondary);
}

.message-meta { display: flex; align-items: baseline; gap: 8px; }
.message-author { font-weight: 700; font-size: 13px; }
.message-caption { color: var(--ink-muted); font-size: 12px; }

/* Typing indicator */
.typing-dots {
  display: flex; gap: 5px; padding: 4px 0;
}
.typing-dots span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: typingBounce 1.4s infinite ease-in-out both;
}
.typing-dots span:nth-child(1) { animation-delay: 0s; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

.message-body {
  margin-top: 8px; margin-left: 38px;
  line-height: 1.68; font-size: 14px;
}

.message-tags, .message-attachments {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-top: 10px; margin-left: 38px;
}
.message-tag {
  border-radius: var(--radius-pill); padding: 4px 10px;
  font-size: 11px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
}
.message-attachment {
  border-radius: var(--radius-pill); padding: 4px 10px;
  font-size: 11px; background: var(--surface); color: var(--ink-muted);
}

.message-steps {
  display: grid; gap: 6px;
  margin-top: 12px; margin-left: 38px;
}

.step-card {
  border-radius: var(--radius-md);
  background: rgba(26,28,25,0.5);
  border: 1px solid var(--border);
  padding: 11px 14px;
}
.step-card summary {
  cursor: pointer; font-weight: 700; font-size: 13px;
  color: var(--ink-secondary); list-style: none;
  display: flex; align-items: center; gap: 7px;
}
.step-card summary::-webkit-details-marker { display: none; }
.step-card summary::before {
  content: ""; width: 6px; height: 6px;
  border-radius: 50%; background: var(--success); flex-shrink: 0;
}
.step-card[open] summary { margin-bottom: 6px; }

.step-label {
  margin-top: 10px; color: var(--ink-muted);
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em;
}
.step-card pre {
  margin: 4px 0 0; white-space: pre-wrap; word-break: break-word;
  font: 12px/1.5 "IBM Plex Mono", "SFMono-Regular", monospace;
  color: var(--ink-secondary);
}

/* ═══════════════════════ COMPOSER ═══════════════════════ */
.composer-wrap {
  position: sticky; bottom: 0; z-index: 40;
  width: min(860px, 100%); margin: 0 auto;
  padding: 0 18px 20px;
}

.composer-card {
  border-radius: var(--radius-xl);
  background: var(--panel-soft);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding: 12px;
  backdrop-filter: blur(20px);
}

.composer-main {
  display: flex; align-items: flex-end; gap: 10px;
}

textarea {
  flex: 1; min-height: 48px; max-height: 200px;
  resize: none; padding: 12px 8px;
  background: transparent; color: var(--ink);
  line-height: 1.55; font-size: 14.5px;
}
textarea::placeholder { color: var(--ink-muted); }

.send-btn {
  width: 42px; height: 42px; border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #173127; cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition); border: none;
}
.send-btn:hover { transform: scale(1.06); box-shadow: 0 4px 16px rgba(143,224,180,0.25); }
.send-btn:active { transform: scale(0.96); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; box-shadow: none; }
.send-btn svg {
  width: 18px; height: 18px; stroke: currentColor;
  fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

/* ── Toolbar ── */
.composer-toolbar {
  display: flex; justify-content: space-between;
  gap: 12px; align-items: center;
  margin-top: 10px; flex-wrap: wrap;
}
.toolbar-group { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.toolbar-right { gap: 8px; }

.toolbar-btn {
  display: inline-flex; align-items: center; gap: 6px;
  border-radius: var(--radius-pill); padding: 7px 12px;
  background: transparent; color: var(--ink-muted);
  cursor: pointer; transition: all var(--transition);
  font-size: 12.5px; font-weight: 500; border: none;
}
.toolbar-btn svg {
  width: 14px; height: 14px; stroke: currentColor;
  fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.toolbar-btn:hover { color: var(--ink); background: rgba(255,255,255,0.05); }
.toolbar-btn.active {
  color: var(--accent); background: var(--accent-soft);
}

/* ── Toggle ── */
.toggle {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--ink-muted); font-size: 12.5px; font-weight: 500;
  cursor: pointer; user-select: none;
}
.toggle input { display: none; }
.toggle-track {
  width: 30px; height: 16px; border-radius: var(--radius-pill);
  background: var(--surface-raised); border: 1px solid var(--border);
  position: relative; transition: all var(--transition);
}
.toggle-knob {
  position: absolute; top: 2px; left: 2px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--ink-muted); transition: all var(--transition);
}
.toggle input:checked ~ .toggle-track {
  background: var(--accent-soft); border-color: var(--accent-border);
}
.toggle input:checked ~ .toggle-track .toggle-knob {
  transform: translateX(14px); background: var(--accent);
}

/* ═══════════════════════ PANELS ═══════════════════════ */
.tool-panel {
  margin-bottom: 10px; border-radius: var(--radius-lg);
  background: rgba(34,37,32,0.90);
  border: 1px solid var(--border);
  padding: 14px; max-height: 380px; overflow-y: auto;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.03);
  animation: panel-slide 180ms ease;
}
@keyframes panel-slide {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.tool-panel.hidden { display: none; }

.panel-header {
  display: flex; justify-content: space-between;
  gap: 12px; align-items: flex-start; margin-bottom: 12px;
}
.panel-kicker {
  color: var(--accent); text-transform: uppercase;
  letter-spacing: 0.10em; font-size: 10px; font-weight: 700;
  margin-bottom: 3px;
}
.panel-header h2 { margin: 0; font-size: 14px; font-weight: 700; }
.panel-content { display: block; }

.panel-grid {
  display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
}

.panel-card {
  width: 100%; text-align: left;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--ink); border-radius: var(--radius-md);
  padding: 12px 14px; cursor: pointer;
  transition: all var(--transition);
}
.panel-card:hover {
  border-color: var(--border-hover);
  background: var(--surface-hover);
  transform: translateY(-1px);
}
.panel-card.active {
  border-color: var(--accent-border);
  background: var(--accent-soft);
}
.panel-card.active .panel-card-title { color: var(--accent); }
.panel-card-title { font-weight: 700; line-height: 1.35; font-size: 13px; }
.panel-card-copy {
  margin-top: 4px; color: var(--ink-secondary);
  line-height: 1.45; font-size: 12px;
}

.tool-card { position: relative; padding-right: 70px; cursor: default; }
.tool-card:hover { transform: none; }

.mini-badge {
  position: absolute; top: 10px; right: 10px;
  border-radius: var(--radius-pill); padding: 3px 8px;
  font-size: 10px; font-weight: 700; letter-spacing: 0.03em;
}
.mini-badge.read { background: rgba(143,224,180,0.10); color: var(--accent); }
.mini-badge.write { background: rgba(214,131,122,0.10); color: var(--danger); }

.upload-row { margin-top: 6px; }

/* ═══════════════════════ ATTACHMENTS ═══════════════════════ */
.attachment-strip {
  display: none; flex-wrap: wrap; gap: 6px; padding: 0 0 8px;
}
.attachment-strip.active { display: flex; }

.attachment-chip {
  display: inline-flex; align-items: center; gap: 8px;
  border-radius: var(--radius-sm); padding: 7px 10px;
  background: var(--surface); border: 1px solid var(--border);
}
.attachment-chip-text { display: grid; gap: 1px; }
.attachment-name { font-size: 12px; font-weight: 700; }
.attachment-meta { color: var(--ink-muted); font-size: 10.5px; }

.attachment-remove {
  width: 22px; height: 22px; border-radius: 6px;
  background: transparent; color: var(--ink-muted);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 14px; transition: all var(--transition); border: none;
}
.attachment-remove:hover {
  background: rgba(214,131,122,0.15); color: var(--danger);
}

/* ═══════════════════════ STATES ═══════════════════════ */
button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ═══════════════════════ SCROLLBAR ═══════════════════════ */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface-raised); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--ink-muted); }

/* ═══════════════════════ RESPONSIVE ═══════════════════════ */
@media (max-width: 720px) {
  .main-stage { padding: 0 12px; }
  .composer-wrap { padding: 0 10px 14px; }
  .topbar { padding: 8px 12px; }
  .brand-note { display: none; }
  .composer-toolbar { flex-direction: column; align-items: flex-start; }
  .toolbar-group { width: 100%; }
  .messages { padding-bottom: 160px; }
  .hero { padding: 40px 0 20px; }
}
