/* Page is full-height so the chat pane scrolls inside the viewport,
   not the whole document. */
html, body { height: 100%; }
body { display: flex; flex-direction: column; }

/* Mobile-first defaults: single column. The sidebar slides over the
   chat as a drawer; the hamburger in the topbar opens it. Tapping a
   conversation closes it again (handled in JS). */
.layout { flex: 1; min-height: 0; position: relative; display: flex; }
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 84vw; max-width: 320px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform .2s ease;
  z-index: 30;
}
.sidebar.open { transform: translateX(0); box-shadow: 0 0 24px rgba(0,0,0,.18); }
.sidebar-head { padding: .65rem .9rem; border-bottom: 1px solid var(--border-soft); }
.sidebar-head button { width: 100%; padding: .55rem; font-size: .95rem; }
.conv-list { flex: 1; overflow-y: auto; padding: .25rem 0; }
.conv-item {
  padding: .65rem .9rem; cursor: pointer; font-size: .92rem;
  color: #333; border-left: 3px solid transparent;
  display: flex; align-items: center; gap: .4rem;
}
.conv-item .conv-title {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item:hover { background: #f5f5f5; }
.conv-item.active {
  background: #eef3fb; color: var(--text); font-weight: 500;
  border-left-color: var(--brand);
}
/* × button — hidden by default, visible on row hover OR on the
 * currently-open chat so the user always has an obvious way to nuke
 * the conversation they're looking at. Touchscreen "hover" is
 * unreliable, hence the always-on for .active. */
.conv-item .conv-del {
  flex: 0 0 auto; width: 1.5rem; height: 1.5rem; padding: 0;
  background: transparent; border: 0; cursor: pointer;
  color: #999; font-size: 1.2rem; line-height: 1;
  border-radius: 4px;
  opacity: 0; transition: opacity .12s, background .12s, color .12s;
}
.conv-item:hover .conv-del,
.conv-item.active .conv-del,
.conv-item .conv-del:focus { opacity: 1; }
.conv-item .conv-del:hover {
  background: #fdecec; color: #b53232;
}
.hamburger {
  background: transparent; color: var(--text); border: 0;
  padding: .2rem .55rem; font-size: 1.4rem; line-height: 1;
  cursor: pointer;
}
.hamburger:hover { background: #f5f5f5; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--surface); }
.chat-pane {
  flex: 1; overflow-y: auto; padding: 1rem .9rem;
  display: flex; flex-direction: column; gap: .9rem;
}
/* Bubble defaults: role label stacks above the bubble on mobile to
   reclaim horizontal room. Desktop swaps to a side-by-side layout. */
.msg { width: 100%; display: flex; flex-direction: column; gap: .2rem; }
.msg .role {
  font-size: .7rem; color: var(--muted); text-transform: uppercase;
  letter-spacing: .03em;
}
.msg .bubble {
  padding: .55rem .8rem; border-radius: var(--radius);
  line-height: 1.55; font-size: .95rem; word-wrap: break-word;
}
.msg.user .bubble { background: #eef3fb; }
.msg.assistant .bubble { background: #fafafa; border: 1px solid #efefef; }
.msg.assistant .bubble.error {
  background: #fff5f5; border-color: #f3c7c7; color: #7a1f1f;
}
.msg.assistant .bubble.error::before { content: "⚠ "; color: #b53232; font-weight: 600; }
.msg .bubble pre {
  background: var(--code-bg); padding: .5rem; border-radius: 4px;
  overflow-x: auto; font-size: .85em; margin: .4rem 0;
}
.msg .bubble code { background: var(--code-bg); padding: .05rem .25rem; border-radius: 3px; font-size: .9em; }
.msg .bubble pre code { background: transparent; padding: 0; }
.msg .bubble p:first-child { margin-top: 0; }
.msg .bubble p:last-child { margin-bottom: 0; }
.empty { color: var(--muted); margin-top: 3rem; text-align: center; align-self: center; }
.empty h2 { color: #444; font-weight: 500; margin-bottom: .25rem; }
.typing { display: inline-block; color: var(--muted); font-style: italic; }

.composer { border-top: 1px solid var(--border); padding: .65rem .9rem; background: var(--surface); }
.tool-row {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: .25rem 1rem; margin-bottom: .4rem;
}
/* One-shot "Generate as image" / "Search the web" checkboxes. Auto-
 * uncheck after every submit (see chat.js) — both are opt-in per turn,
 * not sticky mode toggles. Image and Search are mutually exclusive;
 * when one is checked the other is hidden from the row. */
.image-toggle {
  display: inline-flex; align-items: center; gap: .4rem;
  font-size: .85rem; color: var(--muted); cursor: pointer;
  user-select: none;
}
.image-toggle input[type="checkbox"] {
  width: auto; margin: 0; cursor: pointer;
}
.image-toggle:hover { color: var(--text); }

/* The browser's UA stylesheet has [hidden] { display: none } but at
 * specificity 0,1,0 — anything matching .image-toggle, .search-mode,
 * or .image-size (display: inline-flex below) outranks it on equal
 * specificity by source order. Without these explicit rules, JS
 * toggling `wrap.hidden = true` would set the attribute but the
 * element would stay visible. */
.image-toggle[hidden],
.search-mode[hidden],
.image-size[hidden],
.edit-controls[hidden] { display: none; }

/* Fast/comprehensive sub-toggle for search. Only shown when the
 * Search checkbox is on; defaults to Fast. Inline radio pair matches
 * the look of the parent checkbox row. */
.search-mode {
  display: inline-flex; align-items: center; gap: .75rem;
  font-size: .8rem; color: var(--muted);
}
.search-mode label {
  display: inline-flex; align-items: center; gap: .25rem; cursor: pointer;
}
.search-mode input[type="radio"] { width: auto; margin: 0; cursor: pointer; }

/* Small/medium/large resolution radios. Mirrors .search-mode visually
 * (same inline radio strip) but lives next to the Image checkbox and
 * only appears when that checkbox is on. The little <em> after each
 * label shows the credit cost (1× / 2× / 4×) so the user can see why
 * Large costs more before they pick it. */
.image-size {
  display: inline-flex; align-items: center; gap: .75rem;
  font-size: .8rem; color: var(--muted);
}
.image-size label {
  display: inline-flex; align-items: center; gap: .25rem; cursor: pointer;
}
.image-size input[type="radio"] { width: auto; margin: 0; cursor: pointer; }
.image-size em {
  font-style: normal; font-size: .7rem; color: var(--muted);
  opacity: .75; margin-left: .15rem;
}

/* Sources strip rendered at the end of a search-answer bubble. Numbered
 * list of clickable domains so the user can verify citations the
 * model emitted as [1], [2] inline. */
.msg .bubble .sources {
  margin-top: .65rem; padding-top: .45rem;
  border-top: 1px dashed var(--border);
  font-size: .8rem; color: var(--muted);
}
.msg .bubble .sources-label {
  text-transform: uppercase; letter-spacing: .05em;
  font-size: .7rem; margin-bottom: .25rem;
}
.msg .bubble .sources ol {
  margin: 0; padding-left: 1.25rem;
}
.msg .bubble .sources a {
  color: var(--brand); text-decoration: none;
}
.msg .bubble .sources a:hover { text-decoration: underline; }

/* Image bubble — assistant rendered as an <img> instead of text. Cap
   the visible width so a 1024x1024 generation doesn't blow out the
   chat column. zoom-in cursor cues the tap-to-view interaction. */
.msg .bubble img.generated {
  display: block; max-width: 100%; max-height: 70vh;
  border-radius: var(--radius); margin-top: .25rem;
  cursor: zoom-in;
}
.msg .bubble .img-prompt {
  color: var(--muted); font-size: .85rem; margin-bottom: .35rem; font-style: italic;
}

/* "Edit an image" controls: file picker + picked filename + a
 * subtle/moderate/strong strength preset, mirroring .image-size's
 * inline-radio look. Only shown when the Edit checkbox is on. */
.edit-controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: .6rem;
  font-size: .8rem; color: var(--muted);
  width: 100%; margin-top: .15rem;
}
.edit-picker-btn {
  width: auto; padding: .25rem .6rem; font-size: .8rem;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer;
}
.edit-picker-btn:hover { background: var(--border-soft); }
.edit-picked-thumb {
  width: 2.2rem; height: 2.2rem; object-fit: cover;
  border-radius: 4px; border: 1px solid var(--border);
  flex: 0 0 auto;
}
.edit-picked-filename {
  max-width: 12rem; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; font-style: italic;
}
.edit-strength {
  display: inline-flex; align-items: center; gap: .75rem;
}
.edit-strength label {
  display: inline-flex; align-items: center; gap: .25rem; cursor: pointer;
}
.edit-strength input[type="radio"] { width: auto; margin: 0; cursor: pointer; }

/* Attached-document chips (PDF/DOCX/TXT/MD/CSV). Sits above the
 * composer row, hidden entirely when nothing's attached. Pending
 * (mid-upload) and failed states reuse the same chip shape with a
 * color swap rather than separate markup — see attachments.js. */
.attachments-row {
  display: flex; flex-wrap: wrap; gap: .35rem; margin-bottom: .4rem;
}
.attachment-chip {
  display: inline-flex; align-items: center;
  max-width: 14rem;
  padding: .2rem .55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: .78rem; color: var(--muted);
}
.attachment-chip-label {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.attachment-chip.pending { opacity: .7; font-style: italic; }
.attachment-chip.failed {
  color: var(--err-text); border-color: var(--err-border); background: var(--err-bg);
}

.composer-inner { display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea {
  flex: 1; min-height: 2.5rem; max-height: 10rem; resize: none;
}
.composer button { width: auto; }

/* Voice mode toggle. Square icon-only button to the left of the
 * textarea — when pressed (aria-pressed=true) it picks up the accent
 * color so a glance at the composer tells the user whether the next
 * response will be spoken. Sized to match the textarea's min-height
 * so the row stays vertically aligned. */
.voice-toggle {
  flex: 0 0 auto;
  width: 2.5rem; height: 2.5rem;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: color 120ms ease, background 120ms ease, border-color 120ms ease;
}
.voice-toggle:hover { color: var(--text); }
.voice-toggle[aria-pressed="true"] {
  background: var(--accent, #2d72d2);
  color: #fff;
  border-color: var(--accent, #2d72d2);
}
.voice-toggle:focus-visible {
  outline: 2px solid var(--accent, #2d72d2);
  outline-offset: 2px;
}
.status { color: var(--muted); font-size: .8rem; text-align: center; min-height: 1em; margin-top: .35rem; }
/* Collapsible context-stats panel. Collapsed shows the token total
   dead-center with a "stats ▴" affordance pinned right; tap anywhere
   on the strip to expand. The whole panel is a <button> so keyboard
   + screen-reader navigation works without extra ARIA wiring. */
/* The base .composer button rule sets width:auto which beats a plain
   .stats-panel { width:100% } on specificity — the button collapses
   to its content width and the strip looks narrower than the expanded
   div below. Scope the override to .composer to match specificity and
   include box-sizing so the inner padding doesn't push past the
   composer's own padding. */
.composer .stats-panel { width: 100%; box-sizing: border-box; }
.stats-panel {
  background: transparent; border: none;
  text-align: center; color: var(--text); font-size: .75rem;
  margin-top: .15rem; padding: .25rem 0;
  cursor: pointer; font-family: inherit;
}
/* base.css paints every button with brand-hover on :hover — override
   here so the collapsible footer stays transparent through hover,
   focus, and active. Without these the strip flashed pink as the user
   moved the mouse over it / mid-click. */
.stats-panel:hover,
.stats-panel:focus,
.stats-panel:active { background: transparent; }
.stats-panel:focus-visible {
  outline: 2px solid var(--brand); outline-offset: 2px;
  border-radius: var(--radius);
}
/* Three-column grid centers the token count by force (1fr equal slots
   on either side of the auto-sized middle) and parks the toggle in the
   right slot. Position:absolute had the toggle pulled out of flow so
   the collapsed strip's flex centering only accounted for the token
   count itself — visually off when the toggle was visible at the
   right edge but the token count was centered against a slot that
   didn't include the toggle's width. Grid solves it cleanly. */
.stats-collapsed {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 1.1em;
}
.stats-collapsed-text {
  grid-column: 2; color: var(--text);
}
.stats-toggle {
  grid-column: 3; justify-self: end; padding-right: .5rem;
  color: var(--text);
}
.stats-expanded {
  text-align: left; padding: .55rem .2rem .3rem;
  margin-top: .25rem; cursor: default;
}
.stats-row {
  margin: .25rem 0; line-height: 1.4; font-size: .75rem;
  color: var(--text);
}
.stats-section { margin: .65rem 0 .35rem; }
.stats-label {
  font-size: .65rem; text-transform: uppercase; letter-spacing: .04em;
  opacity: .55; margin-bottom: .25rem; font-weight: 600;
  display: flex; align-items: center; gap: .35rem;
}
.stats-label-status {
  text-transform: none; letter-spacing: 0; font-weight: normal;
  font-style: italic; opacity: .85; font-size: .65rem;
  display: inline-flex; align-items: center; gap: .25rem;
}
/* Author CSS above ties UA specificity for the `hidden` attribute,
   so without this rule el.hidden = true had no visual effect — the
   spinner stayed on screen forever after the summary landed. */
.stats-label-status[hidden] { display: none; }
.stats-spinner {
  display: inline-block;
  width: .65rem; height: .65rem;
  border: 1.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: stats-spin .8s linear infinite;
}
@keyframes stats-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
.stats-summary {
  font-size: .75rem; opacity: .9; white-space: pre-wrap;
  line-height: 1.45;
}
.stats-explainer {
  margin-top: .55rem; font-size: .7rem; opacity: .55;
  line-height: 1.4; font-style: italic;
}

.retry-btn {
  margin-top: .5rem; padding: .35rem .7rem; font-size: .85rem;
  background: var(--surface); color: #b53232; border: 1px solid #d99;
  border-radius: var(--radius); cursor: pointer; width: auto;
}
.retry-btn:hover:not(:disabled) { background: #fdecec; }
.retry-btn:disabled { cursor: not-allowed; color: #999; border-color: #ddd; background: var(--surface); }

/* Per-message read-aloud icon. Sits next to the assistant bubble
 * (right column on desktop, below on mobile thanks to the column
 * flex). Faded by default so it doesn't compete with the message
 * text; brightens on hover of either the button OR its parent .msg
 * (matching the ChatGPT-style "controls appear when you mouse over
 * the bubble" convention). Pulses while loading the TTS job; tints
 * accent-colored while playing so the user can tell which message
 * they're listening to in a long chat. */
.read-aloud-btn {
  flex: 0 0 auto;
  align-self: flex-end;
  width: 1.85rem; height: 1.85rem;
  padding: 0; margin-top: .25rem;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 0; border-radius: 4px;
  color: var(--muted);
  opacity: .35;
  cursor: pointer;
  transition: opacity .12s ease, color .12s ease, background .12s ease;
}
.msg.assistant:hover .read-aloud-btn { opacity: .75; }
.read-aloud-btn:hover,
.read-aloud-btn:focus { opacity: 1; color: var(--text); background: #f1f1f1; }
.read-aloud-btn:focus-visible {
  outline: 2px solid var(--accent, #2d72d2);
  outline-offset: 2px;
}
.read-aloud-btn:disabled { cursor: progress; }
.read-aloud-btn.is-loading {
  opacity: .9; color: var(--text);
  animation: read-aloud-pulse 1.1s ease-in-out infinite;
}
.read-aloud-btn.is-playing {
  opacity: 1; color: var(--accent, #2d72d2);
  background: rgba(45, 114, 210, .08);
}
@keyframes read-aloud-pulse {
  0%, 100% { opacity: .5; }
  50%      { opacity: 1; }
}

.stuck-notice {
  margin-top: .5rem; padding: .5rem .75rem; font-size: .85rem;
  background: #fff8e1; color: #8a6500; border: 1px solid #f0d178;
  border-radius: var(--radius);
  display: flex; flex-wrap: wrap; gap: .5rem .75rem; align-items: center;
}
.stuck-notice .stuck-text { flex: 1; min-width: 12rem; }
.stuck-notice .cancel-btn {
  padding: .3rem .65rem; font-size: .85rem; cursor: pointer;
  background: var(--surface); color: #8a6500; border: 1px solid #d3b87a;
  border-radius: var(--radius); width: auto;
}
.stuck-notice .cancel-btn:hover:not(:disabled) { background: #fdf4d6; }
.stuck-notice .cancel-btn:disabled { cursor: not-allowed; color: #999; }

.msg.assistant .bubble.cancelled {
  background: #f4f4f4; color: var(--muted); font-style: italic;
}
.msg.assistant .bubble.cancelled::before { content: "⊘ "; color: var(--muted); }

/* Image viewer / gallery lightbox. Fixed-position overlay above the
 * whole page (z-index 60 > the sidebar's 30). Backdrop is near-opaque
 * black so even a transparent-edged PNG reads cleanly. The viewer is
 * built once at first tap by chat.js and reused for every subsequent
 * open. */
.image-viewer {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0, 0, 0, .92);
  display: flex; align-items: center; justify-content: center;
  user-select: none;
}
.image-viewer[hidden] { display: none; }
.image-viewer .iv-header {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; justify-content: space-between; align-items: center;
  padding: .75rem 1rem;
  background: linear-gradient(to bottom, rgba(0,0,0,.55), rgba(0,0,0,0));
  pointer-events: none;  /* let the gradient pass clicks through to the backdrop */
}
.image-viewer .iv-header > * { pointer-events: auto; }
.image-viewer .iv-counter {
  color: rgba(255, 255, 255, .85); font-size: .85rem;
  letter-spacing: .03em;
}
.image-viewer .iv-actions {
  display: flex; gap: .25rem; align-items: center;
}
.image-viewer .iv-action {
  background: transparent; color: #fff; border: 0;
  padding: .35rem .7rem; border-radius: 4px; cursor: pointer;
  font-size: .9rem; font-family: inherit; line-height: 1.3;
  text-decoration: none;
}
.image-viewer .iv-action:hover,
.image-viewer .iv-action:focus { background: rgba(255, 255, 255, .15); outline: 0; }
.image-viewer .iv-close { font-size: 1.5rem; line-height: 1; padding: .15rem .55rem; }
.image-viewer .iv-image {
  max-width: 92vw; max-height: 86vh;
  box-shadow: 0 8px 32px rgba(0, 0, 0, .55);
  border-radius: 6px; cursor: default;
}
.image-viewer .iv-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(0, 0, 0, .35); color: #fff; border: 0;
  width: 2.75rem; height: 2.75rem; border-radius: 50%;
  font-size: 1.8rem; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.image-viewer .iv-nav:hover,
.image-viewer .iv-nav:focus { background: rgba(0, 0, 0, .6); outline: 0; }
.image-viewer .iv-prev { left: .75rem; }
.image-viewer .iv-next { right: .75rem; }
.image-viewer .iv-nav[hidden] { display: none; }

/* On phones the swipe gesture is the primary nav — buttons crowd
 * the image and steal taps near the screen edges, so hide them.
 * Counter + Save + Close stay visible. */
@media (max-width: 640px) {
  .image-viewer .iv-nav { display: none; }
}

@media (min-width: 640px) {
  .sidebar {
    position: static; transform: none; box-shadow: none;
    width: 260px; max-width: 260px;
  }
  .hamburger { display: none; }
  .chat-pane { padding: 1.5rem; gap: 1rem; }
  .msg { flex-direction: row; gap: .75rem; max-width: 46rem; align-self: center; }
  .msg .role { flex: 0 0 4rem; padding-top: .35rem; }
  .msg .bubble { flex: 1; }
  .composer { padding: 1rem 1.5rem; }
  .composer-inner { max-width: 48rem; margin: 0 auto; }
}
