:root {
  /* Declare support for both schemes so the browser uses native light/dark
     widgets and does NOT apply its own auto-darkening heuristic. */
  color-scheme: light dark;
  --bg: #f7f7f8;
  --panel: #ffffff;
  --border: #e3e3e8;
  --text: #1c1c1e;
  --muted: #6b6b70;
  --accent: #2563eb;
  --accent-soft: #eff4ff;
  --danger: #b91c1c;
  --user-bubble: #2563eb;
  --user-text: #ffffff;
  --inline-code-bg: rgba(0, 0, 0, 0.07);
  --banner-bg: #fee2e2;
  --banner-text: #991b1b;
  --banner-border: #fecaca;
  --banner-notice-bg: #fef3c7;
  --banner-notice-text: #92400e;
  --banner-notice-border: #fde68a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1b1f;
    --panel: #26262b;
    --border: #3a3a42;
    --text: #e7e7ea;
    --muted: #a1a1aa;
    --accent: #5b8cff;
    --accent-soft: rgba(91, 140, 255, 0.16);
    --danger: #f87171;
    --banner-bg: rgba(248, 113, 113, 0.16);
    --banner-text: #fca5a5;
    --banner-border: rgba(248, 113, 113, 0.32);
    --banner-notice-bg: rgba(251, 191, 36, 0.16);
    --banner-notice-text: #fcd34d;
    --banner-notice-border: rgba(251, 191, 36, 0.32);
    --user-bubble: #2f6bff;
    --user-text: #ffffff;
    --inline-code-bg: rgba(255, 255, 255, 0.12);
  }
}

* {
  box-sizing: border-box;
}

.banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--banner-bg);
  color: var(--banner-text);
  border-bottom: 1px solid var(--banner-border);
  font-size: 0.9rem;
}

.banner[hidden] {
  display: none;
}

.banner.notice {
  background: var(--banner-notice-bg);
  color: var(--banner-notice-text);
  border-bottom-color: var(--banner-notice-border);
}

.banner-close {
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
  font-size: 0.9rem;
}

html,
body {
  margin: 0;
  height: 100%;
  /* Fixed app shell: the page itself never scrolls, only the chat pane does.
     Without this, tall content or classic scrollbars let the whole document
     scroll past the frame in both axes. */
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* Layout: sidebar + main, filling the space below the banner. */
.app {
  flex: 1;
  min-height: 0;
  display: flex;
}

.sidebar {
  flex-shrink: 0;
  width: 260px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.app-title {
  margin: 0;
  font-size: 1.05rem;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* Mobile top bar with the menu button; hidden on wide screens. */
.mobile-bar {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.mobile-title {
  font-weight: 600;
}

/* Push the clear-chat icon to the right edge of the mobile bar. */
#mobile-new-chat-btn {
  margin-left: auto;
}

.sidebar-backdrop {
  display: none;
}

/* Narrow screens: sidebar slides in over the content instead of taking a
   fixed column, so the chat gets the full width. */
@media (max-width: 760px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 40;
  }

  .app.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .mobile-bar {
    display: flex;
  }

  /* The clear-chat entry point lives in the mobile bar here, so drop the
     duplicate inside the drawer and leave it as the Settings panel. */
  .new-chat-btn {
    display: none;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .app.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }
}

.new-chat-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 9px 12px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
}

.new-chat-btn:hover {
  background: var(--bg);
}

.edit-last-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 999px;
  padding: 6px 12px;
  font: inherit;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}

.edit-last-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.edit-last-btn[hidden] {
  display: none;
}

.settings > summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 4px 0;
  user-select: none;
}

.settings-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 12px;
}

/* Full-width scroll container, so the scrollbar sits at the window's right
   edge (like ChatGPT) rather than at the centered column's edge. */
.chat-scroll {
  flex: 1;
  /* min-height: 0 lets this flex item shrink and scroll internally instead of
     growing the page when content is tall (flexbox min-height: auto default). */
  min-height: 0;
  overflow-y: auto;
  /* overflow-y: auto would otherwise promote overflow-x to auto, letting the
     view scroll sideways. Pin it; wide content (code blocks) scrolls itself. */
  overflow-x: hidden;
}

/* The conversation itself stays centered within the full-width scroller. */
.chat-log {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 820px;
  margin: 0 auto;
  padding: 20px 16px;
  min-height: 100%;
}

.empty-state {
  margin: auto;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: 100%;
  max-width: 720px;
  text-align: center;
}

.empty-hint {
  margin: 0;
}

.examples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 8px;
  width: 100%;
}

.example {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.example:hover {
  background: var(--bg);
}

.example-thumb {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--bg);
}

.example-icon {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  background: var(--bg);
  border-radius: 8px;
  flex-shrink: 0;
}

.example-media {
  position: relative;
  flex-shrink: 0;
  line-height: 0;
}

.example-count {
  position: absolute;
  right: -5px;
  bottom: -5px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 600;
  border: 2px solid var(--panel);
}

/* The "show more examples" toggle only exists for the narrow-screen collapse
   below; on a wide screen every example is shown, so the button stays hidden. */
.show-more {
  display: none;
  margin-top: 4px;
  align-self: center;
  border: none;
  background: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  padding: 6px 8px;
}

.show-more:hover {
  text-decoration: underline;
}

/* On a narrow screen the example labels wrap and stack into a long scroll, so
   collapse to the one-per-modality subset (the entries without .secondary) and
   let .show-more reveal the rest. The visible order is unchanged; the hidden
   entries are just removed from the flow until expanded. */
@media (max-width: 760px) {
  .examples:not(.expanded) .example.secondary {
    display: none;
  }

  .show-more {
    display: block;
  }
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 85%;
}

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

.msg.assistant {
  align-self: flex-start;
  max-width: 100%;
}

.bubble {
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  /* User text is plain (textContent), so preserve its newlines. The assistant
     bubble holds rendered markdown HTML and must NOT use pre-wrap, or the
     newlines between tags render as extra blank space. */
  white-space: pre-wrap;
}

/* Default: assistant turns render as plain text (no bubble), like ChatGPT,
   while the user keeps a bubble. */
.msg.assistant .bubble {
  padding: 0;
  border-radius: 0;
}

/* Variant: give the assistant a bubble too. Add `assistant-bubbles` to <body>. */
body.assistant-bubbles .msg.assistant {
  max-width: 85%;
}

body.assistant-bubbles .msg.assistant .bubble {
  padding: 10px 14px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
}

/* Rendered-markdown styling, shared by the assistant answer and the reasoning
   body (both carry the `md` class). */
.md {
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.md > :first-child {
  margin-top: 0;
}

.md > :last-child {
  margin-bottom: 0;
}

.md p,
.md ul,
.md ol,
.md pre,
.md blockquote,
.md table {
  margin: 0.6em 0;
}

.md h1,
.md h2,
.md h3,
.md h4 {
  margin: 0.9em 0 0.4em;
  line-height: 1.3;
}

.md ul,
.md ol {
  padding-left: 1.4em;
}

.md li {
  margin: 0.2em 0;
}

.md pre {
  background: #1e1e2e;
  color: #e6e6ef;
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.45;
}

/* highlight.js sets its own background/padding on the code element; keep our
   block styling and let the theme provide only the token colors. */
.md pre code.hljs {
  background: transparent;
  padding: 0;
}

/* Non-scrolling wrapper around a code block so the copy button stays pinned
   while the code scrolls horizontally inside. */
.code-wrap {
  position: relative;
  margin: 0.6em 0;
}

.code-wrap pre {
  margin: 0;
}

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.1);
  color: #e6e6ef;
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 0.72rem;
  cursor: pointer;
}

.copy-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Keep wide display math from overflowing horizontally. */
.md .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
}

.md code {
  font-family: ui-monospace, "SFMono-Regular", "Menlo", monospace;
}

.md :not(pre) > code {
  background: var(--inline-code-bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.md blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--muted);
}

.md table {
  border-collapse: collapse;
}

.md th,
.md td {
  border: 1px solid var(--border);
  padding: 5px 9px;
  text-align: left;
}

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

/* Typing indicator shown while waiting for the first tokens */
.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-blink 1.2s infinite ease-in-out;
}

.typing i:nth-child(2) {
  animation-delay: 0.2s;
}

.typing i:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing-blink {
  0%,
  80%,
  100% {
    opacity: 0.2;
  }
  40% {
    opacity: 1;
  }
}

.md img,
.md video {
  max-width: 220px;
  border-radius: 8px;
  display: block;
}

/* Media attached to a chat message: capped display size. */
.msg-media {
  max-width: 240px;
  max-height: 240px;
  border-radius: 10px;
  object-fit: contain;
  display: block;
  cursor: zoom-in;
}

.reasoning {
  font-size: 0.9rem;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 12px;
}

.reasoning summary {
  cursor: pointer;
  padding: 6px 0;
  user-select: none;
}

.reasoning .reasoning-body {
  padding-bottom: 8px;
}

/* A short note under an assistant turn explaining how it ended: a quiet marker
   when the user stopped it, a louder one when generation failed. */
.turn-note {
  font-size: 0.85rem;
  margin-top: 2px;
}

.turn-note.stopped {
  color: var(--muted);
}

.turn-note.error {
  color: var(--danger);
}

/* Composer */
.composer {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 10px 16px 14px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#message {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  max-height: 200px;
}

#message:focus {
  outline: none;
  border-color: var(--accent);
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  height: 40px;
  min-width: 40px;
  cursor: pointer;
  font-size: 1.1rem;
}

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

.send-btn {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  height: 40px;
  padding: 0 18px;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}

.send-btn.stop {
  background: #b91c1c;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Thinking toggle: prominent, right under the input */
.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}

.thinking-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  font-weight: 600;
  background: var(--panel);
}

.thinking-toggle:has(input:checked) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.thinking-toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.file-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.file-chips:empty {
  margin-bottom: 0;
}

.preview {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}

.preview-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  cursor: pointer;
}

.preview-file {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 0.7rem;
  padding: 4px;
  word-break: break-all;
  overflow: hidden;
  cursor: pointer;
}

.preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
}

.field textarea,
.field select {
  width: 100%;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font: inherit;
}

/* Only allow vertical resize, so dragging can't exceed the sidebar width. */
.field textarea {
  resize: vertical;
}

.field small {
  color: var(--muted);
}

/* Drag-and-drop overlay */
.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(37, 99, 235, 0.12);
  backdrop-filter: blur(2px);
  pointer-events: none;
}

.drop-overlay[hidden] {
  display: none;
}

.drop-message {
  padding: 24px 36px;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: var(--panel);
  color: var(--accent);
  font-size: 1.2rem;
  font-weight: 600;
}

/* Lightbox for viewing chat media at full size */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.85);
  cursor: zoom-out;
}

.lightbox[hidden] {
  display: none;
}

.lightbox img,
.lightbox video {
  max-width: 95vw;
  max-height: 95vh;
  border-radius: 8px;
}

.lightbox audio {
  width: min(90vw, 420px);
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
}

.lightbox-close:hover {
  background: rgba(0, 0, 0, 0.8);
}
