/* ═══════════════════════════════
   Tokens — Desertant-inspired
   ═══════════════════════════════ */
:root {
  --cream: #fbfaf4;
  --paper: #f5f3eb;
  --sand: #eae7dc;
  --stone: #d4d0c5;
  --ash: #9e9a8f;
  --ink-light: #6b675d;
  --ink: #3d3a33;
  --ink-dark: #1a1814;
  --accent: #c45d3e;
  --accent-soft: rgba(196, 93, 62, 0.08);
  --accent-hover: #a84e33;
  --green: #4a7c59;
  --green-soft: rgba(74, 124, 89, 0.08);

  --sans: 'Inter', system-ui, -apple-system, sans-serif;
  --serif: 'Source Serif 4', 'Georgia', serif;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;

  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

/* ═══════════════════════════════
   Reset
   ═══════════════════════════════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; }
body {
  font-family: var(--sans);
  background: var(--cream);
  color: var(--ink);
  min-height: 100vh;
  overflow: hidden;
}

::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--stone); border-radius: 3px; }

/* ═══════════════════════════════
   App Layout
   ═══════════════════════════════ */
#app {
  display: flex;
  height: 100vh;
}

/* ═══════════════════════════════
   Sidebar — Conversations
   ═══════════════════════════════ */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--paper);
  border-right: 1px solid var(--sand);
  display: flex;
  flex-direction: column;
  transition: transform 350ms var(--ease);
  z-index: 10;
}

.sidebar-head {
  padding: 20px 20px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--ink-dark);
}

.sidebar-brand svg {
  width: 28px;
  height: 28px;
}

.sidebar-brand span {
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.btn-new {
  width: 32px;
  height: 32px;
  border: 1px solid var(--sand);
  border-radius: var(--r-sm);
  background: var(--cream);
  color: var(--ink-light);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms;
}

.btn-new:hover {
  background: var(--sand);
  color: var(--ink);
}

/* Agents */
.agent-row { display: flex; gap: 6px; padding: 0 12px 12px; }
.agent-row select, .ag-dialog input, .ag-dialog textarea {
  font-family: var(--sans); font-size: 13px; color: var(--ink);
  background: var(--cream); border: 1px solid var(--sand);
  border-radius: var(--r-sm); padding: 6px 8px;
}
.agent-row select { flex: 1; min-width: 0; }
.ag-dialog {
  margin: auto; width: min(480px, 92vw); padding: 20px;
  background: var(--paper); color: var(--ink);
  border: 1px solid var(--sand); border-radius: var(--r-md);
}
.ag-dialog::backdrop { background: rgba(26,24,20,0.4); }
.ag-dialog label { display: block; font-size: 11px; font-weight: 600; color: var(--ash); margin: 10px 0 4px; text-transform: uppercase; }
.ag-dialog input, .ag-dialog textarea { width: 100%; }
.ag-dialog textarea { min-height: 160px; resize: vertical; line-height: 1.5; }
.ag-actions { display: flex; gap: 8px; margin-top: 14px; }
.ag-actions button { padding: 8px 14px; border-radius: var(--r-sm); border: 1px solid var(--sand); background: var(--cream); color: var(--ink); cursor: pointer; font-family: var(--sans); }
.ag-actions .primary { background: var(--ink-dark); color: var(--cream); border-color: var(--ink-dark); }
.ag-actions .danger { color: var(--accent); margin-left: auto; }

/* Conversation list */
.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 12px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background 150ms;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: var(--sans);
  font-size: 13px;
  color: var(--ink-light);
  line-height: 1.4;
}

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 200;
  max-width: 90vw; padding: 10px 16px; border-radius: var(--r-md);
  background: var(--ink-dark); color: var(--cream); font-size: 13px; box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
.conv-row { position: relative; }
.conv-row .conv-item { padding-right: 34px; }
.conv-del {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border: none; border-radius: 6px; background: none;
  color: var(--ash); font-size: 12px; cursor: pointer; opacity: 0; transition: opacity 150ms, background 150ms;
}
.conv-row:hover .conv-del, .conv-del:focus-visible { opacity: 1; }
.conv-del:hover { background: var(--sand); color: var(--accent); }
@media (hover: none) { .conv-del { opacity: .6; } }  /* no hover on touch screens */
.conv-item:hover { background: var(--sand); }
.conv-item.active {
  background: var(--cream);
  color: var(--ink-dark);
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

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

.conv-item.active .conv-dot { background: var(--accent); }

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

.conv-empty {
  padding: 20px;
  text-align: center;
  color: var(--ash);
  font-size: 13px;
  line-height: 1.5;
}

/* Sidebar footer */
.sidebar-foot {
  padding: 14px 20px;
  border-top: 1px solid var(--sand);
  font-size: 11px;
  color: var(--ash);
  display: flex;
  align-items: center;
  gap: 6px;
}

.sidebar-credits { padding: 0 20px 12px; font-size: 11px; color: var(--ash); }
.sidebar-credits a { color: var(--ink-light); text-decoration: none; }
.sidebar-credits a:hover { color: var(--accent); text-decoration: underline; }

.sidebar-foot .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-foot.clickable { cursor: pointer; }
.sidebar-foot.clickable:hover { background: var(--sand); }
.sidebar-foot .swap { margin-left: auto; display: none; }
.sidebar-foot .foot-btn { border: none; background: none; color: var(--ash); cursor: pointer; font-size: 14px; padding: 2px 6px; border-radius: 6px; }
.sidebar-foot .foot-btn:hover { background: var(--stone); color: var(--accent); }
.sidebar-foot .eject { display: none; }
.sidebar-foot.clickable .eject { display: inline; }
.sidebar-foot:not(.clickable) .storage { margin-left: auto; }
.st-row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--sand); font-size: 13px; }
.st-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-row .sz { color: var(--ash); font-size: 12px; }
.st-row button { border: none; background: none; color: var(--ash); cursor: pointer; padding: 2px 6px; border-radius: 6px; }
.st-row button:hover { color: var(--accent); background: var(--sand); }
.sidebar-foot.clickable .swap { display: inline; margin-right: 2px; }
.sidebar-foot .dot.off { background: var(--stone); }
.sidebar-foot .dot.on { background: var(--green); }

/* ═══════════════════════════════
   Main Area
   ═══════════════════════════════ */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;   /* without this a long chat grows the page instead of scrolling */
  position: relative;
}

/* ═══════════════════════════════
   Onboarding — Model picker
   ═══════════════════════════════ */
.onboard {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 24px 40px;
  overflow-y: auto;
}

.onboard-card {
  max-width: 480px;
  width: 100%;
  text-align: center;
}

.onboard-card h1 {
  font-family: var(--serif);
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.5px;
  color: var(--ink-dark);
  margin-bottom: 8px;
}

.onboard-card p {
  font-size: 15px;
  color: var(--ink-light);
  line-height: 1.6;
  margin-bottom: 28px;
}

.model-picker {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
  margin-bottom: 20px;
}

.model-option {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--paper);
  border: 1.5px solid var(--sand);
  border-radius: var(--r-md);
  cursor: pointer;
  transition: all 200ms var(--ease);
  font-family: var(--sans);
  width: 100%;
  text-align: left;
}

.model-option:hover {
  border-color: var(--stone);
  background: var(--cream);
}

.model-option.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.model-option input[type="radio"] {
  display: none;
}

.model-radio {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid var(--stone);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 200ms;
}

.model-option.selected .model-radio {
  border-color: var(--accent);
}

.model-radio::after {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  transform: scale(0);
  transition: transform 200ms var(--ease);
}

.model-option.selected .model-radio::after {
  transform: scale(1);
}

.model-info { flex: 1; }

.model-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--ink-dark);
  margin-bottom: 2px;
}

.model-desc {
  font-size: 12px;
  color: var(--ash);
}

.btn-start {
  width: 100%;
  padding: 14px;
  background: var(--ink-dark);
  border: none;
  border-radius: var(--r-md);
  color: var(--cream);
  font-size: 15px;
  font-weight: 500;
  font-family: var(--sans);
  cursor: pointer;
  transition: all 200ms var(--ease);
  letter-spacing: -0.2px;
}

.btn-start:hover {
  background: var(--ink);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.btn-start:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* Progress */
.onboard-progress {
  margin-top: 16px;
  display: none;
}

.onboard-progress.active { display: block; }

.progress-track {
  height: 3px;
  background: var(--sand);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 200ms ease-out;
}

.progress-text {
  font-size: 12px;
  color: var(--ash);
  text-align: center;
}

/* ═══════════════════════════════
   Chat View (hidden until model loads)
   ═══════════════════════════════ */
.chat-view {
  flex: 1;
  min-height: 0;
  display: none;
  flex-direction: column;
}

.chat-view.active { display: flex; }

/* Chat messages */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 24px 24px 16px;
}

.chat-messages-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.msg {
  animation: msgIn 300ms var(--ease);
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-role {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.msg.user .msg-role { color: var(--accent); }
.msg.assistant .msg-role { color: var(--ink-light); }

.msg-body {
  font-size: 15px;
  line-height: 1.75;
  color: var(--ink);
}

.msg.assistant .msg-body {
  font-family: var(--serif);
  font-size: 16px;
}

/* Op badge */
.msg-op-badge {
  display: inline-block;
  padding: 2px 8px;
  background: var(--accent-soft);
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
  color: var(--accent);
  margin-bottom: 6px;
  font-family: var(--sans);
}

/* Copy */
.msg-actions {
  margin-top: 6px;
  opacity: 0;
  transition: opacity 150ms;
}

.msg:hover .msg-actions { opacity: 1; }

.btn-copy-msg {
  padding: 3px 8px;
  background: none;
  border: 1px solid var(--sand);
  border-radius: 4px;
  font-size: 11px;
  color: var(--ash);
  cursor: pointer;
  font-family: var(--sans);
  transition: all 150ms;
}

.btn-copy-msg:hover {
  border-color: var(--stone);
  color: var(--ink-light);
}

/* Typing */
.typing { display: flex; gap: 4px; padding: 4px 0; }
.typing span {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--stone);
  animation: typeBounce 1.2s ease-in-out infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typeBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Markdown basics */
.msg-body code {
  background: var(--sand);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 13px;
}
.msg-body pre {
  background: var(--paper);
  border: 1px solid var(--sand);
  padding: 12px;
  border-radius: var(--r-sm);
  overflow-x: auto;
  margin: 8px 0;
}
.msg-body pre code { background: none; padding: 0; }
.msg-body strong { font-weight: 600; }
.msg-body ul, .msg-body ol { padding-left: 20px; margin: 4px 0; }

/* ═══════════════════════════════
   Input Bar
   ═══════════════════════════════ */
.input-bar {
  padding: 12px 24px 20px;
  border-top: 1px solid var(--sand);
  background: var(--cream);
}

.stats {
  max-width: 680px;
  margin: 6px auto 0;
  font-size: 11px;
  color: var(--ink-light);
  min-height: 14px;
}

.input-bar-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input-box {
  flex: 1;
  position: relative;
}

.chat-input {
  width: 100%;
  padding: 12px 14px;
  padding-right: 44px;
  background: var(--paper);
  border: 1.5px solid var(--sand);
  border-radius: var(--r-md);
  font-size: 14px;
  font-family: var(--sans);
  color: var(--ink);
  resize: none;
  min-height: 44px;
  max-height: 140px;
  line-height: 1.5;
  transition: border-color 150ms;
}

.chat-input:focus {
  outline: none;
  border-color: var(--stone);
}

.chat-input::placeholder { color: var(--ash); }

.btn-send {
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: 32px;
  height: 32px;
  background: var(--ink-dark);
  border: none;
  border-radius: var(--r-sm);
  color: var(--cream);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms;
}

.btn-send:hover { background: var(--ink); }
.btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

.btn-stop {
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: 32px;
  height: 32px;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--r-sm);
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}

/* ═══════════════════════════════
   Commands popover (/ menu)
   ═══════════════════════════════ */
.cmd-trigger {
  width: 32px;
  height: 32px;
  background: none;
  border: 1px solid var(--sand);
  border-radius: var(--r-sm);
  color: var(--ash);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 150ms;
  flex-shrink: 0;
  align-self: flex-end;
  margin-bottom: 6px;
}

.cmd-trigger:hover {
  background: var(--sand);
  color: var(--ink);
}

.conv-label small { display: block; font-size: 10px; color: var(--ash); font-weight: 400; }
.chips { max-width: 680px; margin: 0 auto; display: flex; flex-wrap: wrap; gap: 6px; }
.chips:not(:empty) { margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 4px 3px 10px; font-size: 12px; background: var(--paper); border: 1px solid var(--sand); border-radius: 999px; color: var(--ink-light); }
.chip.err { color: var(--accent); border-color: var(--accent); }
.chip button { border: none; background: none; cursor: pointer; color: var(--ash); font-size: 11px; padding: 2px 6px; }
.cmd-trigger.on { background: var(--accent-soft); border-color: var(--accent); }

.cmd-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  right: 0;
  background: var(--cream);
  border: 1px solid var(--sand);
  border-radius: var(--r-md);
  box-shadow: 0 8px 32px rgba(0,0,0,0.08);
  padding: 6px;
  display: none;
  z-index: 20;
  max-height: 320px;
  overflow-y: auto;
}

.cmd-popover.open { display: block; }

.cmd-popover-title {
  padding: 8px 10px 6px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--ash);
}

.cmd-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: none;
  background: none;
  border-radius: 6px;
  width: 100%;
  text-align: left;
  font-family: var(--sans);
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
  transition: background 100ms;
}

.cmd-item:hover { background: var(--paper); }

.cmd-icon {
  width: 20px;
  text-align: center;
  font-size: 14px;
  flex-shrink: 0;
}

.cmd-label { font-weight: 500; }
.cmd-desc {
  font-size: 11px;
  color: var(--ash);
  margin-top: 1px;
}

/* ═══════════════════════════════
   Responsive
   ═══════════════════════════════ */
.sidebar-overlay {
  position: fixed; inset: 0;
  background: rgba(26,24,20,0.3);
  z-index: 9; opacity: 0;
  pointer-events: none;
  transition: opacity 250ms;
}
.sidebar-overlay.active { opacity: 1; pointer-events: all; }

.btn-menu {
  display: none;
  width: 36px; height: 36px;
  background: none;
  border: 1px solid var(--sand);
  border-radius: var(--r-sm);
  color: var(--ink-light);
  font-size: 18px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 14px; left: 14px;
  z-index: 5;
}

@media (max-width: 720px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    z-index: 20;
    box-shadow: 4px 0 24px rgba(0,0,0,0.08);
  }
  .sidebar.open { transform: translateX(0); }
  .btn-menu { display: flex; }
  .onboard-card h1 { font-size: 26px; }
}

/* ═══════════════════════════════
   Dark theme overrides
   ═══════════════════════════════ */
html.dark {
  --cream: #1a1814;
  --paper: #23211b;
  --sand: #2e2b24;
  --stone: #3d3a33;
  --ash: #8a867b;
  --ink-light: #c4bfb5;
  --ink: #e8e4dc;
  --ink-dark: #fbfaf4;
  --accent: #d4744f;
  --accent-soft: rgba(212, 116, 79, 0.12);
  --accent-hover: #e08a5f;
  --green: #6fa87d;
  --green-soft: rgba(111, 168, 125, 0.12);
}

html.dark .sidebar {
  border-right-color: var(--sand);
}

html.dark .msg-body pre {
  background: var(--sand);
  border-color: var(--stone);
}

/* ═══════════════════════════════
   WebGPU warning
   ═══════════════════════════════ */
.gpu-warning {
  display: none;
  position: fixed; inset: 0;
  background: var(--cream);
  z-index: 100;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  padding: 40px;
}
.gpu-warning.visible { display: flex; }
.gpu-warning h2 { font-family: var(--serif); font-size: 22px; margin: 12px 0 6px; }
.gpu-warning p { color: var(--ink-light); max-width: 360px; font-size: 14px; line-height: 1.6; }
