/* owled Brain: brand tokens taken from owled OS (Geist, Owled orange, neutral greys, 10-12px radius). */
:root {
  --background: hsl(0 0% 98%);
  --foreground: hsl(0 0% 10%);
  --card: hsl(0 0% 100%);
  --primary: hsl(16 100% 56%);
  --primary-foreground: hsl(0 0% 100%);
  --secondary: hsl(0 0% 96%);
  --muted: hsl(0 0% 94%);
  --muted-foreground: hsl(0 0% 45%);
  --accent: hsl(16 80% 95%);
  --accent-foreground: hsl(16 100% 40%);
  --destructive: hsl(0 72% 51%);
  --border: hsl(0 0% 90%);
  --sidebar: hsl(0 0% 97%);
  --sidebar-foreground: hsl(0 0% 35%);
  --sidebar-accent: hsl(0 0% 92%);
  --radius: 0.75rem;
  --radius-sm: 10px;
  --font: "Geist", "Geist Sans", system-ui, sans-serif;
  --shadow: 0 1px 2px hsl(0 0% 0% / 0.04), 0 8px 24px hsl(0 0% 0% / 0.06);
  color-scheme: light;
}

/* Light by default; dark only when asked for with ?theme=dark. */
:root[data-theme="dark"] {
  --background: hsl(0 0% 4.3%);
  --foreground: hsl(0 0% 85%);
  --card: hsl(0 0% 7.1%);
  --secondary: hsl(0 0% 10.2%);
  --muted: hsl(0 0% 12%);
  --muted-foreground: hsl(0 0% 60%);
  --accent: hsl(16 80% 15%);
  --accent-foreground: hsl(16 100% 65%);
  --destructive: hsl(0 62% 60%);
  --border: hsl(0 0% 16.5%);
  --sidebar: hsl(0 0% 6%);
  --sidebar-foreground: hsl(0 0% 65%);
  --sidebar-accent: hsl(0 0% 12%);
  --shadow: 0 8px 32px hsl(0 0% 0% / 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--background);
  color: var(--foreground);
  font: 400 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent-foreground); }
.error { color: var(--destructive); }

/* ---------- controls ---------- */
input, textarea {
  width: 100%;
  background: var(--background);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.8rem;
  font-size: 14px;
}
input:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--accent); }
label { display: block; font-size: 14px; font-weight: 500; margin: 0.9rem 0 0.4rem; }
.btn-primary {
  background: var(--primary); color: var(--primary-foreground);
  border: 0; border-radius: var(--radius-sm);
  height: 40px; padding: 0 1rem; font-size: 14px; font-weight: 500; cursor: pointer;
}
.btn-primary:hover { filter: brightness(0.95); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary.wide { width: 100%; margin-top: 1rem; }
.btn-outline {
  display: inline-flex; align-items: center; gap: 0.45rem;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm);
  height: 36px; padding: 0 0.85rem; font-size: 14px; font-weight: 500; cursor: pointer;
}
.btn-outline:hover { background: var(--secondary); }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px;
  background: none; border: 0; border-radius: 8px; cursor: pointer; color: var(--muted-foreground);
}
.icon-btn:hover { background: var(--secondary); color: var(--foreground); }
.only-mobile { display: none; }

/* ---------- gate ---------- */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.gate-card {
  width: min(330px, 100%); background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem 1.3rem 1.3rem;
}
.gate-title { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
.gate-sub { color: var(--muted-foreground); font-size: 13px; margin: 0.15rem 0 0.4rem; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 264px 1fr; height: 100vh; }

.sidebar {
  background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0; padding: 0.75rem;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 0.35rem 0.5rem 0.9rem; }
.brand { font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.brand strong { color: var(--primary); font-weight: 600; }
.new-chat {
  display: flex; align-items: center; gap: 0.5rem; width: 100%;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm);
  height: 38px; padding: 0 0.75rem; font-size: 14px; font-weight: 500; cursor: pointer;
}
.new-chat:hover { border-color: var(--primary); }
.chat-list { flex: 1; overflow-y: auto; margin-top: 1rem; }
.chat-group { font-size: 12px; font-weight: 500; color: var(--muted-foreground); margin: 1rem 0.6rem 0.35rem; }
.chat-group:first-child { margin-top: 0; }
.chat-item {
  display: flex; align-items: center; width: 100%; gap: 0.25rem;
  border-radius: 8px; color: var(--sidebar-foreground);
}
.chat-item:hover, .chat-item[aria-current="true"] { background: var(--sidebar-accent); color: var(--foreground); }
.chat-item[aria-current="true"] { font-weight: 500; }
.chat-open {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; cursor: pointer;
  padding: 0.45rem 0.6rem; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-delete { opacity: 0; width: 28px; height: 28px; }
.chat-item:hover .chat-delete, .chat-delete:focus-visible { opacity: 1; }
.chat-delete svg { width: 15px; height: 15px; }
.chat-empty { color: var(--muted-foreground); font-size: 13px; padding: 0 0.6rem; }
.sidebar-foot { color: var(--muted-foreground); font-size: 12px; margin: 0.75rem 0.5rem 0.25rem; }

/* ---------- main ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 0.75rem;
  height: 56px; padding: 0 1rem 0 1.25rem; border-bottom: 1px solid var(--border); flex: none;
}
.chat-title { flex: 1; min-width: 0; margin: 0; font-size: 15px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.thread { flex: 1; overflow-y: auto; padding: 2rem 1.25rem 1rem; }
.thread:empty { display: none; }
.msg { max-width: 46rem; margin: 0 auto 1.75rem; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble {
  background: var(--secondary); border-radius: 18px 18px 4px 18px;
  padding: 0.6rem 1rem; max-width: 80%; white-space: pre-wrap;
}
.msg-brain { display: grid; grid-template-columns: 28px 1fr; gap: 0.85rem; }
.avatar {
  width: 28px; height: 28px; border-radius: 8px; background: var(--primary);
  display: grid; place-items: center; margin-top: 2px;
}
.avatar svg { width: 18px; height: 18px; stroke: none; fill: #fff; }
.msg-meta { color: var(--muted-foreground); font-size: 12.5px; margin-top: 0.6rem; }
.thinking { color: var(--muted-foreground); display: flex; align-items: center; gap: 0.5rem; padding-top: 3px; }
.dots span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--primary); margin-right: 3px; animation: pulse 1.2s ease-in-out infinite; }
.dots span:nth-child(2) { animation-delay: 0.15s; }
.dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .dots span { animation: none; opacity: 0.7; } }

/* ---------- welcome ---------- */
.shell:not(.chatting) .welcome { margin-top: auto; }
.shell:not(.chatting) .suggest { margin-bottom: auto; }
.welcome { display: flex; flex-direction: column; align-items: center; padding: 0 1.25rem; text-align: center; }
.welcome-title { font-size: clamp(28px, 4vw, 36px); font-weight: 600; letter-spacing: -0.025em; margin: 0; }
.welcome-sub { color: var(--muted-foreground); margin: 0.4rem 0 1rem; }
.suggest { padding: 0.25rem 1.25rem 14vh; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; max-width: 46rem; margin: 0 auto; }
.suggestion {
  text-align: left; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0.8rem 0.95rem; font-size: 14px; cursor: pointer; color: var(--foreground);
}
.suggestion:hover { border-color: var(--primary); background: var(--accent); color: var(--accent-foreground); }
.shell.chatting .welcome, .shell.chatting .suggest { display: none; }

/* ---------- composer ---------- */
.composer { flex: none; padding: 0.75rem 1.25rem 1rem; }
.shell:not(.chatting) .composer { flex: none; }
.composer-box {
  max-width: 46rem; margin: 0 auto; display: flex; align-items: flex-end; gap: 0.5rem;
  background: var(--card); border: 1px solid var(--border); border-radius: 22px;
  padding: 0.5rem 0.5rem 0.5rem 1rem; box-shadow: var(--shadow);
}
.composer-box:focus-within { border-color: var(--primary); }
.composer-box textarea {
  border: 0; background: none; padding: 0.45rem 0; resize: none; max-height: 12rem;
  font-size: 15px; line-height: 1.5; box-shadow: none;
}
.composer-box textarea:focus { box-shadow: none; }
.send {
  flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--primary); color: var(--primary-foreground); display: grid; place-items: center;
}
.send:disabled { opacity: 0.4; cursor: not-allowed; }
.composer-note { max-width: 46rem; margin: 0.5rem auto 0; text-align: center; font-size: 12px; color: var(--muted-foreground); }

/* ---------- markdown (answers and pages) ---------- */
.md { line-height: 1.65; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md p { margin: 0 0 0.85em; }
.md h2, .md h3, .md h4 { font-size: 15px; font-weight: 600; margin: 1.3em 0 0.4em; }
.md ul, .md ol { padding-left: 1.25rem; margin: 0 0 0.85em; }
.md li { margin: 0.25em 0; }
.md blockquote { margin: 0.85em 0; padding: 0.1em 0 0.1em 0.9rem; border-left: 3px solid var(--primary); color: var(--muted-foreground); }
.md code { background: var(--muted); border-radius: 5px; padding: 0.05em 0.35em; font-size: 0.92em; }
.md h3 { font-size: 15.5px; }
.md h4 { font-size: 14.5px; margin-top: 1em; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.1em 0; }
.md .table-wrap { overflow-x: auto; margin: 0.4em 0 1em; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; line-height: 1.45; }
.md th, .md td { text-align: left; vertical-align: top; padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--border); }
.md th { background: var(--secondary); font-weight: 600; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md td a.wiki { white-space: normal; }
.msg-actions { display: flex; align-items: center; gap: 0.75rem; margin-top: 0.6rem; }
.msg-actions .msg-meta { margin: 0; }
.link-btn {
  display: inline-flex; align-items: center; gap: 0.3rem; background: none; border: 0; padding: 0; cursor: pointer;
  font-size: 12.5px; font-weight: 500; color: var(--accent-foreground);
}
.link-btn svg { width: 14px; height: 14px; }
.link-btn:hover { text-decoration: underline; }

/* ---------- graph view ---------- */
.graph-view {
  position: fixed; inset: 0; z-index: 25; display: flex; flex-direction: column;
  background: var(--background);
}
.graph-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: 0.6rem 0.9rem;
  padding: 0.65rem 1rem 0.65rem 1.25rem; border-bottom: 1px solid var(--border); background: var(--card);
}
.graph-head h2 { font-size: 16px; font-weight: 600; margin: 0; }
.graph-head input { width: 220px; height: 34px; padding: 0 0.7rem; }
.graph-legend { display: flex; flex-wrap: wrap; gap: 0.35rem; flex: 1; min-width: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 0.35rem; height: 28px; padding: 0 0.6rem;
  border: 1px solid var(--border); border-radius: 999px; background: var(--card);
  font-size: 12.5px; font-weight: 500; cursor: pointer; color: var(--foreground);
}
.chip .dot { width: 9px; height: 9px; border-radius: 50%; }
.chip[aria-pressed="false"] { opacity: 0.45; }
.graph-note { font-size: 12.5px; color: var(--muted-foreground); }
.graph-canvas { flex: 1; min-height: 0; position: relative; }
.graph-stage { position: absolute; inset: 0; }
.graph-status { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted-foreground); pointer-events: none; }
.graph-hint { position: absolute; left: 1rem; bottom: 0.8rem; font-size: 12px; color: var(--muted-foreground); pointer-events: none; }
@media (max-width: 860px) { .graph-head input { width: 100%; } }
a.wiki {
  color: var(--accent-foreground); background: var(--accent); text-decoration: none;
  border-radius: 6px; padding: 0.05em 0.4em; font-size: 0.93em; font-weight: 500; cursor: pointer; white-space: nowrap;
}
a.wiki:hover { text-decoration: underline; }

/* ---------- add dialog ---------- */
.modal { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 1rem; background: hsl(0 0% 0% / 0.35); }
.modal-card {
  width: min(520px, 100%); max-height: calc(100vh - 2rem); overflow-y: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 1.1rem 1.25rem 1.25rem;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; }
.modal-head h2 { font-size: 17px; font-weight: 600; margin: 0; }
.seg { display: inline-flex; background: var(--secondary); border-radius: var(--radius-sm); padding: 3px; margin-bottom: 0.75rem; }
.seg button { border: 0; background: none; border-radius: 8px; padding: 0.35rem 0.9rem; font-size: 14px; font-weight: 500; cursor: pointer; color: var(--muted-foreground); }
.seg button[aria-selected="true"] { background: var(--card); color: var(--foreground); box-shadow: 0 1px 2px hsl(0 0% 0% / 0.08); }
.drop {
  display: grid; justify-items: center; gap: 0.35rem; text-align: center; margin: 0;
  border: 1.5px dashed var(--border); border-radius: var(--radius); padding: 1.6rem 1rem; cursor: pointer; font-weight: 400;
}
.drop svg { width: 24px; height: 24px; color: var(--primary); }
.drop strong { font-weight: 500; }
.drop span { color: var(--muted-foreground); font-size: 13px; }
.drop:hover, .drop.over { border-color: var(--primary); background: var(--accent); }
#files:focus-visible + .drop { outline: 2px solid var(--primary); outline-offset: 2px; }
.picked { list-style: none; margin: 0.6rem 0 0; padding: 0; font-size: 13px; color: var(--muted-foreground); }
.status { min-height: 1.2rem; font-size: 13px; font-weight: 500; margin: 0.75rem 0 0; }
.tip { font-size: 12.5px; color: var(--muted-foreground); margin: 0.25rem 0 0; }
.queue h3 { font-size: 13px; font-weight: 600; margin: 1.1rem 0 0.4rem; }
.mini-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.mini-list li { display: flex; justify-content: space-between; gap: 1rem; padding: 0.4rem 0; border-top: 1px solid var(--border); }
.mini-list .when { color: var(--muted-foreground); white-space: nowrap; }
.mini-list .empty { color: var(--muted-foreground); }

/* ---------- source drawer ---------- */
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 27; width: min(440px, 100%);
  background: var(--card); border-left: 1px solid var(--border); box-shadow: var(--shadow);
  overflow-y: auto; padding: 1rem 1.25rem 3rem;
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.kind { margin: 0; font-size: 12.5px; color: var(--muted-foreground); }
.drawer-title { font-size: 19px; font-weight: 600; letter-spacing: -0.01em; margin: 0.3rem 0 1rem; }
.page-links { margin-top: 1.5rem; border-top: 1px solid var(--border); padding-top: 0.5rem; font-size: 14px; }
.page-links h3 { font-size: 12.5px; font-weight: 500; color: var(--muted-foreground); margin: 0.9rem 0 0.4rem; }
.page-links ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.4rem; }

/* ---------- small screens ---------- */
.scrim { position: fixed; inset: 0; background: hsl(0 0% 0% / 0.35); z-index: 15; }
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 16; transform: translateX(-100%); transition: transform 0.2s ease; }
  .shell.sidebar-open .sidebar { transform: none; }
  .only-mobile { display: inline-grid; }
  .suggestions { grid-template-columns: 1fr; }
  .suggest { padding-bottom: 1.5rem; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
