:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b7280;
  --border: #d1d5db;
  --accent: #2563eb;
  --bubble-user: #2563eb;
  --bubble-user-fg: #ffffff;
  --bubble-assistant: #f3f4f6;
  --bubble-assistant-fg: #1a1a1a;
  --sidebar-bg: #f7f7f8;
  --code-bg: rgba(0, 0, 0, 0.06);
  --ok: #15803d;
  --warn: #b45309;
  --bad: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --fg: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2e37;
    --accent: #60a5fa;
    --bubble-user: #2563eb;
    --bubble-user-fg: #ffffff;
    --bubble-assistant: #1f2430;
    --bubble-assistant-fg: #e5e7eb;
    --sidebar-bg: #0b0d11;
    --code-bg: rgba(255, 255, 255, 0.08);
    --ok: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
}

/* ---------- App shell: sidebar + main ---------- */

.app-shell {
  display: flex;
  width: 100%;
  height: 100dvh;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 940;
}

.sidebar-backdrop.hidden {
  display: none;
}

.sidebar {
  width: 260px;
  flex: 0 0 260px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.75rem;
  overflow: hidden;
  transition: width 0.2s ease, padding 0.2s ease, transform 0.2s ease;
}

@media (min-width: 769px) {
  .sidebar.collapsed {
    width: 0;
    flex-basis: 0;
    padding: 0;
    border-right: 0;
  }
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 950;
    width: 280px;
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.3);
  }

  .sidebar.collapsed {
    transform: translateX(-100%);
  }
}

.sidebar-top {
  flex: 0 0 auto;
}

.icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.sidebar-nav-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: none;
  background: transparent;
  color: var(--fg);
  border-radius: 8px;
  font-size: 0.88rem;
  cursor: pointer;
  text-align: left;
}

.sidebar-nav-item:hover {
  background: var(--bubble-assistant);
}

.sidebar-section {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.sidebar-section-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 0.25rem 0.4rem;
}

.sidebar-clear-link {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.72rem;
  text-decoration: underline;
  cursor: pointer;
  padding: 0.25rem 0.4rem;
}

.sidebar-clear-link:hover {
  color: var(--fg);
}

.sidebar-chats {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.sidebar-chat-empty {
  padding: 0.5rem 0.6rem;
  font-size: 0.82rem;
  color: var(--muted);
  font-style: italic;
}

.sidebar-chat-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.5rem 0.5rem 0.6rem;
  border-radius: 8px;
  font-size: 0.85rem;
  color: var(--fg);
  cursor: pointer;
}

.sidebar-chat-item:hover {
  background: var(--bubble-assistant);
}

.sidebar-chat-item.active {
  background: var(--bubble-assistant);
  font-weight: 600;
}

.sidebar-chat-title {
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-chat-delete {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
}

.sidebar-chat-delete .icon {
  width: 14px;
  height: 14px;
}

.sidebar-chat-item:hover .sidebar-chat-delete,
.sidebar-chat-delete:focus-visible {
  opacity: 1;
}

.sidebar-chat-delete:hover {
  background: rgba(220, 38, 38, 0.15);
  color: #dc2626;
}

.sidebar-bottom {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.sidebar-user {
  font-size: 0.78rem;
  color: var(--muted);
  padding: 0.25rem 0.2rem 0;
}

.main {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ---------- Topbar ---------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.9rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--bubble-assistant);
}

.status {
  flex: 0 0 auto;
  min-height: 1.4rem;
  padding: 0.35rem 1rem 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.status.error {
  color: #dc2626;
}

/* ---------- Sidebar model controls (reused .control-group) ---------- */

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.control-group label {
  font-size: 0.75rem;
  color: var(--muted);
}

.control-group select {
  width: 100%;
  min-width: 0;
  /* iOS Safari auto-zooms the whole page on focus if a form control's
     computed font-size is under 16px — this is the minimum that avoids it. */
  font-size: 16px;
  padding: 0.45rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
}

#load-btn {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  font-weight: 600;
}

#load-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Chat area ---------- */

.chat {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.chat-inner {
  max-width: 48rem;
  margin: 0 auto;
  min-height: 100%;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.msg-row {
  display: flex;
  gap: 0.65rem;
  max-width: min(92%, 44rem);
}

.msg-row.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-row.assistant {
  align-self: flex-start;
}

.avatar {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
}

.avatar.user {
  background: var(--accent);
  color: #fff;
}

.avatar.assistant {
  background: var(--bubble-assistant);
  color: var(--fg);
}

.msg-col {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.msg-row.user .msg-col {
  align-items: flex-end;
}

.msg {
  min-width: 0;
  padding: 0.55rem 0.85rem;
  border-radius: 14px;
  line-height: 1.45;
  font-size: 0.92rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg-stats {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.2rem;
  padding: 0 0.15rem;
}

.msg.user {
  background: var(--bubble-user);
  color: var(--bubble-user-fg);
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  background: var(--bubble-assistant);
  color: var(--bubble-assistant-fg);
  border-bottom-left-radius: 4px;
  /* Content here is marked.js-rendered HTML, not plain text — its own
     elements (p, li, ...) handle spacing, so don't also preserve the
     whitespace/newlines between tags in marked's HTML output. */
  white-space: normal;
}

.msg.assistant.pending::after {
  content: "▍";
  opacity: 0.6;
}

/* Markdown rendered inside assistant bubbles via marked.js */
.msg.assistant p {
  margin: 0 0 0.6em;
}

.msg.assistant p:last-child {
  margin-bottom: 0;
}

.msg.assistant ul,
.msg.assistant ol {
  margin: 0 0 0.6em;
  padding-left: 1.4em;
}

.msg.assistant li {
  margin: 0.15em 0;
}

.msg.assistant pre {
  background: var(--code-bg);
  padding: 0.6em 0.75em;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0 0 0.6em;
}

.msg.assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
}

.msg.assistant :not(pre) > code {
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.msg.assistant pre code {
  background: none;
  padding: 0;
}

.msg.assistant blockquote {
  margin: 0 0 0.6em;
  padding-left: 0.75em;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.msg.assistant a {
  color: var(--accent);
}

.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3 {
  margin: 0.5em 0 0.4em;
  font-size: 1.05em;
}

.msg.assistant h1:first-child,
.msg.assistant h2:first-child,
.msg.assistant h3:first-child {
  margin-top: 0;
}

.msg.assistant hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.6em 0;
}

.msg .thumb {
  display: block;
  max-width: 100%;
  max-height: 220px;
  border-radius: 8px;
}

/* ---------- Composer ---------- */

.composer {
  flex: 0 0 auto;
  padding: 0.75rem 1rem 1rem;
}

/* Before any messages exist, the composer floats centered in the pane
   (like Grok/OpenWebUI's empty state) instead of sitting pinned to the
   bottom; .chat is simply empty behind it at that point. */
.main.is-empty .composer {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  max-width: 48rem;
  padding-left: 1rem;
  padding-right: 1rem;
}

.composer-hero {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
  text-align: center;
}

.main.is-empty .composer-hero {
  display: flex;
}

.composer-hero-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--bubble-assistant);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fg);
}

.composer-hero-icon .icon {
  width: 22px;
  height: 22px;
}

#composer-hero-title {
  font-size: 1.3rem;
  font-weight: 600;
}

.composer-hero-load-btn {
  margin-top: 0.15rem;
}

.composer-hero-load-btn.hidden {
  display: none;
}

.chat-form {
  max-width: 48rem;
  margin: 0 auto;
  display: flex;
  gap: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.4rem 0.4rem 0.4rem 1.1rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.input-mode {
  display: flex;
  flex: 1;
  min-width: 0;
  align-items: center;
  gap: 0.5rem;
}

.input-mode.hidden {
  display: none;
}

#vqa-attach-label.hidden {
  display: none;
}

#mic-btn.hidden {
  display: none;
}

#mic-btn .icon-recording {
  display: none;
}

#mic-btn.recording .icon-idle {
  display: none;
}

#mic-btn.recording .icon-recording {
  display: block;
}

#mic-btn.recording {
  background: #dc2626;
  border-color: #dc2626;
  color: #fff;
  animation: mic-pulse 1.5s ease-in-out infinite;
}

#mic-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

@keyframes mic-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.45);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(220, 38, 38, 0);
  }
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.upload-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-size: 0.95rem;
}

.upload-btn.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

#chat-input {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0.25rem;
  border: none;
  background: transparent;
  color: var(--fg);
  /* Same iOS Safari auto-zoom-on-focus threshold as .control-group select. */
  font-size: 16px;
  outline: none;
}

#send-btn,
#stop-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#send-btn .icon,
#stop-btn .icon {
  width: 18px;
  height: 18px;
}

#send-btn.hidden,
#stop-btn.hidden {
  display: none;
}

#send-btn:disabled,
#chat-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Modals (download confirm/progress, storage inspector) ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.modal-card h2 {
  margin: 0 0 0.35rem;
  font-size: 1.2rem;
}

.modal-model-name {
  margin: 0 0 1.25rem;
  font-size: 0.9rem;
  color: var(--muted);
  word-break: break-word;
}

.progress-track {
  height: 10px;
  border-radius: 999px;
  background: var(--bubble-assistant);
  overflow: hidden;
  margin-bottom: 0.75rem;
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.2s ease;
}

.progress-fill.indeterminate {
  width: 35%;
  animation: indeterminate-slide 1.1s ease-in-out infinite;
}

@keyframes indeterminate-slide {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(285%);
  }
}

.modal-status {
  min-height: 1.3rem;
  margin: 0 0 1rem;
  font-size: 0.85rem;
}

.modal-status.error {
  color: #dc2626;
}

.modal-note {
  margin: 5;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.modal-close-btn {
  margin-top: 1.25rem;
  padding: 0.5rem 1.25rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  font-size: 0.9rem;
}

.modal-close-btn.hidden {
  display: none;
}

#modal-confirm-section.hidden,
#modal-progress-section.hidden {
  display: none;
}

.modal-size-warning {
  margin: 0 0 1rem;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  border: 1px solid rgba(217, 119, 6, 0.35);
  background: rgba(245, 158, 11, 0.12);
  color: #b45309;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: left;
  line-height: 1.4;
}

@media (prefers-color-scheme: dark) {
  .modal-size-warning {
    border-color: rgba(245, 158, 11, 0.4);
    background: rgba(245, 158, 11, 0.15);
    color: #fbbf24;
  }
}

.modal-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 0.25rem;
}

.btn {
  padding: 0.55rem 1.4rem;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  border: 1px solid transparent;
}

.btn-secondary {
  background: var(--bg);
  border-color: var(--border);
  color: var(--fg);
}

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

.btn-secondary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.settings-card {
  max-width: 480px;
  text-align: left;
}

.settings-card #modal-title,
.settings-card h2 {
  text-align: left;
}

.settings-card .modal-note {
  text-align: left;
  margin-bottom: 1rem;
}

.settings-card .modal-actions {
  justify-content: flex-end;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 1rem;
}

.menu-link {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.6rem 0.65rem;
  border: none;
  background: transparent;
  color: var(--fg);
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
}

.menu-link:hover {
  background: var(--bubble-assistant);
}

.settings-list {
  max-height: 45vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.settings-row-name {
  font-size: 0.9rem;
  font-weight: 600;
}

.settings-row-meta {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.settings-row-clear {
  flex: 0 0 auto;
  padding: 0.4rem 0.9rem;
  font-size: 0.82rem;
}

/* Pass/fail (and in-between) coloring for capability rows — ok/warn/bad map
   to green/amber/red so support status reads at a glance, without needing to
   parse the ✓/✗ text. Only rows with real pass/fail meaning get a class;
   purely informational rows (CPU cores, device memory) stay neutral. */
.settings-row.status-ok .settings-row-meta {
  color: var(--ok);
}

.settings-row.status-warn .settings-row-meta {
  color: var(--warn);
}

.settings-row.status-bad .settings-row-meta {
  color: var(--bad);
}

/* The capabilities modal has two .settings-list blocks in one card (device
   info + provider support) — giving each its own 45vh scroll region nests
   two independently-scrollable areas inside a modal, which is cramped and
   fights touch scrolling on mobile. Let them flow as plain content instead,
   so the single .modal-card scroll (above) handles any overflow as one unit.
   They're also laid out as a grid rather than the Storage modal's single
   stacked column — with ~12 short rows, one column reads as a tall, cramped
   wall; tiling them sideways uses the extra width from .capabilities-card
   below instead of forcing the user to scroll through every row vertically. */
#capabilities-list,
#capabilities-providers {
  max-height: none;
  overflow-y: visible;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
}

/* Rows carrying a long value (the WebGPU adapter string, the WebGL unmasked
   renderer string) span every column instead of tiling — at grid-item width
   those wrap into a tall, awkward sliver next to much shorter rows. */
.settings-row--wide {
  grid-column: 1 / -1;
}

#capabilities-list .settings-row-name,
#capabilities-providers .settings-row-name {
  font-size: 0.82rem;
}

#capabilities-list .settings-row-meta,
#capabilities-providers .settings-row-meta {
  font-size: 0.74rem;
}

.capabilities-card {
  max-width: 46rem;
}

@media (max-width: 640px) {
  #capabilities-list,
  #capabilities-providers {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .chat-inner {
    padding: 0.75rem;
    gap: 0.75rem;
  }

  .msg-row {
    max-width: 96%;
  }

  .composer {
    padding: 0.5rem 0.5rem 0.75rem;
  }

  .settings-row {
    flex-wrap: wrap;
  }
}
