:root {
  color-scheme: dark;
  --bg: #0f1117;
  --bg-elev: #161a23;
  --bg-card: #1d2330;
  --bg-user: #23314d;
  --border: #2a3142;
  --text: #e6eaf2;
  --text-muted: #98a2b3;
  --accent: #5b8cff;
  --accent-hover: #4778eb;
  --ok: #32d583;
  --warn: #fdb022;
  --err: #f97066;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}

.visually-hidden {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 320px;
  block-size: 100dvh;
  overflow: clip;
}

.app-shell.memory-collapsed {
  grid-template-columns: 280px minmax(0, 1fr) 0px;
}

.app-shell.memory-collapsed .sidebar-right {
  visibility: hidden;
  overflow: clip;
  border-inline-start: 0;
}

.sidebar-left, .sidebar-right {
  display: flex;
  flex-direction: column;
  min-block-size: 0;
  background: var(--bg-elev);
  border-inline-end: 1px solid var(--border);
}

.sidebar-right {
  border-inline-end: 0;
  border-inline-start: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.85rem 1rem;
  border-block-end: 1px solid var(--border);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  font-size: 1rem;
}

.brand-dot {
  inline-size: 10px;
  block-size: 10px;
  border-radius: 999px;
  background: var(--accent);
}

.sidebar-scroll {
  flex: 1 1 auto;
  min-block-size: 0;
  overflow-y: auto;
  scrollbar-gutter: stable;
  overscroll-behavior: contain;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border-block-start: 1px solid var(--border);
  font-size: 0.8rem;
}

.footer-links {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.btn-link {
  background: transparent;
  border: 0;
  padding: 0;
  font: inherit;
  cursor: pointer;
}

.login-page {
  min-block-size: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}

.login-card {
  inline-size: min(400px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.login-title {
  margin: 0 0 1rem;
  font-size: 1.15rem;
}

.login-error {
  margin: 0 0 0.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  background: rgba(249, 112, 102, 0.14);
  color: var(--err);
  font-size: 0.88rem;
}

.login-info {
  margin: 0 0 0.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  background: rgba(50, 213, 131, 0.14);
  color: var(--ok);
  font-size: 0.88rem;
}

.login-links {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-block-start: 1rem;
  padding-block-start: 0.75rem;
  border-block-start: 1px solid var(--border);
  font-size: 0.85rem;
}

.footer-link {
  color: var(--text-muted);
  text-decoration: none;
}

.footer-link:hover {
  color: var(--text);
}

.health-pill {
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 0.75rem;
}

.health-pill.is-ok { color: var(--ok); }
.health-pill.is-warn { color: var(--warn); }

.notebook-group {
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 0.5rem;
  background: rgba(255, 255, 255, 0.02);
}

.notebook-group.is-active {
  border-color: var(--border);
  background: var(--bg-card);
}

.notebook-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  margin-block-end: 0.35rem;
}

.notebook-title {
  font-weight: 600;
  font-size: 0.9rem;
  min-inline-size: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  border-radius: 8px;
  padding-inline: 0.45rem;
}

.session-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.session-item.is-current {
  background: rgba(91, 140, 255, 0.18);
}

.session-link {
  flex: 1 1 auto;
  min-inline-size: 0;
  padding-block: 0.35rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Main chat */
.chat-main {
  display: flex;
  flex-direction: column;
  min-inline-size: 0;
  min-block-size: 0;
  background: var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-block-end: 1px solid var(--border);
  background: var(--bg-elev);
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-inline-size: 0;
}

.chat-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.chat-subtitle {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.messages-scroller {
  position: relative;
  flex: 1 1 auto;
  min-block-size: 0;
  overflow-y: auto;
  scrollbar-gutter: stable;
  overscroll-behavior: contain;
  padding: 1.25rem;
}

.messages-container {
  max-inline-size: 860px;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding-block-end: 2.5rem;
}

.scroll-top-sentinel, .scroll-bottom-sentinel {
  block-size: 1px;
  inline-size: 1px;
  visibility: hidden;
}

.scroll-to-bottom {
  position: sticky;
  inset-block-end: 12px;
  display: block;
  margin-inline-start: auto;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.messages-scroller.show-scroll-bottom .scroll-to-bottom {
  opacity: 1;
  visibility: visible;
}

.empty-state {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.empty-state h2 {
  margin-block: 0 0.5rem;
  font-size: 1.15rem;
}

.empty-summary {
  margin-block-start: 1rem;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: var(--bg-card);
  font-size: 0.9rem;
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.9rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elev);
  min-inline-size: 0;
}

.msg-user {
  background: var(--bg-user);
  border-color: #32466d;
}

.msg-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.msg-author {
  font-weight: 600;
}

.btn-tts {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.15rem 0.5rem;
  font-size: 0.75rem;
  cursor: pointer;
}

.btn-tts:hover {
  color: var(--text);
  border-color: var(--accent);
}

.thinking-box, .tools-box {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.thinking-pre {
  margin: 0.45rem 0 0;
  white-space: pre-wrap;
  font-family: var(--mono);
  font-size: 0.78rem;
}

.tools-list {
  margin: 0.4rem 0 0;
  padding-inline-start: 1.2rem;
}

.attachments-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.att-thumb img {
  display: block;
  max-inline-size: 220px;
  max-block-size: 160px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.6rem;
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  text-decoration: none;
  font-size: 0.82rem;
}

.msg-body {
  min-inline-size: 0;
  overflow-wrap: anywhere;
}

.msg-body p { margin: 0 0 0.65rem; }
.msg-body p:last-child { margin-block-end: 0; }
.msg-body pre {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.84rem;
}
.msg-body code {
  font-family: var(--mono);
  background: rgba(255, 255, 255, 0.08);
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  font-size: 0.86em;
}
.msg-body pre code {
  background: transparent;
  padding: 0;
}
.msg-body a { color: var(--accent); }

/* Composer */
.composer {
  border-block-start: 1px solid var(--border);
  background: var(--bg-elev);
  padding: 0.75rem 1.25rem;
}

.composer-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  max-inline-size: 860px;
  margin: 0 auto 0.5rem;
}

.composer-row, .composer-toolbar {
  max-inline-size: 860px;
  margin-inline: auto;
}

.composer-row textarea {
  inline-size: 100%;
  resize: vertical;
  min-block-size: 56px;
  max-block-size: 220px;
  padding: 0.65rem 0.8rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
}

.composer-row textarea:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

.composer-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-block-start: 0.5rem;
}

.composer-tools-left, .composer-tools-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
}

/* Memory panel */
.memory-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
}

.memory-card h3, .memory-section-head h3 {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.memory-summary-text {
  margin: 0;
  font-size: 0.85rem;
  white-space: pre-wrap;
}

.memory-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.fact-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.fact-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.6rem 0.75rem;
}

.fact-item.is-global {
  border-color: #364f7c;
}

.fact-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-block-end: 0.25rem;
}

.fact-badge {
  font-size: 0.72rem;
  color: var(--accent);
}

.fact-text {
  margin: 0;
  font-size: 0.84rem;
}

.fact-empty {
  color: var(--text-muted);
  font-size: 0.84rem;
}

/* Buttons & Modals */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 0.45rem 0.9rem;
  font-size: 0.88rem;
  font-family: inherit;
  cursor: pointer;
  background: var(--bg-card);
  color: var(--text);
}

.btn-sm {
  padding: 0.28rem 0.65rem;
  font-size: 0.8rem;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.06);
}

.btn.is-recording {
  border-color: var(--err);
  color: var(--err);
}

.icon-btn {
  background: transparent;
  border: 0;
  color: var(--text);
  cursor: pointer;
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
  font-size: 1rem;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.icon-btn-muted {
  color: var(--text-muted);
}

.modal-dialog {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  inline-size: min(500px, 92dvw);
}

.modal-dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.modal-form h2 {
  margin: 0 0 0.25rem;
  font-size: 1.1rem;
}

.modal-form label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.modal-form input[type="text"],
.modal-form input[type="password"],
.modal-form input[type="email"],
.modal-form input[type="number"],
.modal-form select,
.modal-form textarea {
  inline-size: 100%;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-block-start: 0.5rem;
}

.modal-wide {
  inline-size: min(740px, 95dvw);
  max-block-size: 90dvh;
  overflow-y: auto;
}

.model-pill-btn {
  max-inline-size: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.models-modal {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.models-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.models-modal-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.models-section h3 {
  margin: 0 0 0.5rem;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.models-banner {
  padding: 0.55rem 0.8rem;
  border-radius: 8px;
  background: rgba(91, 140, 255, 0.14);
  border: 1px solid var(--accent);
  font-size: 0.85rem;
}

.models-hint {
  margin: 0.4rem 0 0.75rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.gpu-grid, .aux-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.6rem;
}

.gpu-card, .aux-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.8rem;
  font-size: 0.82rem;
}

.gpu-card-title, .aux-card-title {
  display: flex;
  justify-content: space-between;
  font-weight: 600;
  margin-block-end: 0.35rem;
}

.vram-bar {
  block-size: 8px;
  border-radius: 999px;
  background: var(--bg);
  overflow: hidden;
  margin-block: 0.35rem;
}

.vram-bar-fill {
  block-size: 100%;
  background: var(--accent);
}

.model-switch-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.model-switch-row select {
  flex: 1 1 auto;
  min-inline-size: 0;
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.tune-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem;
}

.tune-grid label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.tune-grid input[type="text"],
.tune-grid input[type="number"],
.tune-grid select {
  inline-size: 100%;
  padding: 0.42rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.tune-full {
  grid-column: 1 / -1;
}

.tune-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 0.85rem;
  padding-block: 0.25rem;
}

.models-add-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.85rem;
  background: rgba(255, 255, 255, 0.02);
  font-size: 0.85rem;
}

.models-add-box summary {
  cursor: pointer;
  font-weight: 600;
}

.mobile-only {
  display: none;
}

@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: 240px minmax(0, 1fr);
  }
  .sidebar-right {
    display: none;
  }
  .app-shell.memory-open-mobile .sidebar-right {
    display: flex;
    position: fixed;
    inset-block: 0;
    inset-inline-end: 0;
    inline-size: min(320px, 88dvw);
    z-index: 40;
  }
}

@media (max-width: 760px) {
  .mobile-only {
    display: inline-flex;
  }
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
  }
  .sidebar-left {
    display: none;
  }
  .app-shell.sidebar-open-mobile .sidebar-left {
    display: flex;
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    inline-size: min(290px, 85dvw);
    z-index: 40;
  }
}
