/* ---------------------------------------------------------------
   localchat — design tokens
   Light is the base. Dark is declared twice on purpose: once for
   the OS preference (unless the user pinned light) and once for an
   explicit pin, so the manual toggle wins in both directions.
   --------------------------------------------------------------- */

:root {
  --bg: #FAF9F7;
  --surface: #FFFFFF;
  --surface-2: #F1EFEA;
  --text: #1A1917;
  --text-muted: #7A756C;
  --border: #E8E4DD;
  --border-strong: #D8D2C8;
  --accent: #1A1917;
  --accent-contrast: #FAF9F7;
  --code-bg: #F5F3EF;
  --danger: #A6432F;
  --shimmer: rgba(26, 25, 23, .12);
  --shadow: 0 1px 2px rgba(26, 25, 23, .04), 0 8px 24px -12px rgba(26, 25, 23, .10);
  --shadow-lift: 0 1px 2px rgba(26, 25, 23, .05), 0 14px 36px -14px rgba(26, 25, 23, .16);

  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  --col: 46rem;
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151513;
    --surface: #1E1E1B;
    --surface-2: #262622;
    --text: #EDEBE6;
    --text-muted: #9A948A;
    --border: #2E2D29;
    --border-strong: #3C3B36;
    --accent: #EDEBE6;
    --accent-contrast: #151513;
    --code-bg: #191916;
    --danger: #E08D77;
    --shimmer: rgba(237, 235, 230, .16);
    --shadow: 0 1px 2px rgba(0, 0, 0, .30), 0 8px 24px -12px rgba(0, 0, 0, .55);
    --shadow-lift: 0 1px 2px rgba(0, 0, 0, .35), 0 14px 36px -14px rgba(0, 0, 0, .65);
  }
}

:root[data-theme="dark"] {
  --bg: #151513;
  --surface: #1E1E1B;
  --surface-2: #262622;
  --text: #EDEBE6;
  --text-muted: #9A948A;
  --border: #2E2D29;
  --border-strong: #3C3B36;
  --accent: #EDEBE6;
  --accent-contrast: #151513;
  --code-bg: #191916;
  --danger: #E08D77;
  --shimmer: rgba(237, 235, 230, .16);
  --shadow: 0 1px 2px rgba(0, 0, 0, .30), 0 8px 24px -12px rgba(0, 0, 0, .55);
  --shadow-lift: 0 1px 2px rgba(0, 0, 0, .35), 0 14px 36px -14px rgba(0, 0, 0, .65);
}

/* --------------------------------------------------------------- */

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

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---- header ---------------------------------------------------- */

.header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  max-width: calc(var(--col) + 4rem);
  width: 100%;
  margin: 0 auto;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  font-size: .9375rem;
  font-weight: 500;
  letter-spacing: -.01em;
}

.wordmark-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--accent);
}

.header-brand {
  display: inline-flex;
  align-items: center;
  gap: .625rem;
  min-width: 0;
}

/* Deliberately muted, never --danger: losing the connection is not a fault
   state here, it's the app doing what it says on the tin. */
.conn-chip {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .1875rem .5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: .75rem;
  line-height: 1.4;
  white-space: nowrap;
}

.conn-chip::before {
  content: "";
  width: 7px; height: 7px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
}

/* An author `display` beats the UA's [hidden] rule, so say it explicitly. */
.conn-chip[hidden] { display: none; }

.header-actions { display: flex; align-items: center; gap: .25rem; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 34px; height: 34px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.icon-btn svg {
  width: 17px; height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn .fillable { fill: currentColor; stroke: none; }

/* Only the glyph matching the current preference is shown. */
.icon-sun, .icon-moon, .icon-auto { display: none; }
body[data-theme-pref="light"]  .icon-sun  { display: block; }
body[data-theme-pref="dark"]   .icon-moon { display: block; }
body[data-theme-pref="system"] .icon-auto { display: block; }

.text-btn {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: .875rem;
  padding: .375rem .625rem;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.text-btn:hover { background: var(--surface-2); color: var(--text); }
.text-btn[data-confirming="true"] { color: var(--danger); background: var(--surface-2); }

/* ---- layout ---------------------------------------------------- */

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

.thread-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable both-edges;
}

.thread {
  max-width: var(--col);
  margin: 0 auto;
  padding: 1rem 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
}

.stage {
  flex: none;
  width: 100%;
  max-width: var(--col);
  margin: 0 auto;
  padding: 0 1.25rem 1.25rem;
}

/* Empty state: everything floats in the middle of the page. */
.main[data-state="empty"] .thread-wrap { display: none; }
.main[data-state="empty"] .stage { margin: auto; padding-bottom: 12vh; }

/* Active state: thread scrolls, composer rests at the bottom. */
.main[data-state="active"] .greeting,
.main[data-state="active"] .chips { display: none; }
.main[data-state="active"] .stage { padding-top: .5rem; }

.greeting { text-align: left; margin-bottom: 1.5rem; }

.greeting-title {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(2rem, 5.5vw, 2.9rem);
  line-height: 1.12;
  letter-spacing: -.015em;
  margin: 0 0 .35rem;
}

.greeting-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 1.0625rem;
}

/* ---- composer -------------------------------------------------- */

.composer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .875rem 1rem .75rem;
  box-shadow: var(--shadow);
  transition: border-color .18s ease, box-shadow .18s ease;
}
.composer:focus-within {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-lift);
}
.composer[data-disabled="true"] { background: transparent; box-shadow: none; }

.composer-input {
  display: block;
  width: 100%;
  border: 0;
  padding: 0;
  margin: 0 0 .5rem;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.55;
  resize: none;
  max-height: 40vh;
  overflow-y: auto;
}
.composer-input:focus { outline: none; }
.composer-input::placeholder { color: var(--text-muted); }
.composer-input:disabled { cursor: not-allowed; }

.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.composer-hint {
  font-size: .8125rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.send-btn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 34px; height: 34px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  cursor: pointer;
  transition: opacity .15s ease, transform .15s ease;
}
.send-btn:disabled { opacity: .28; cursor: default; }
.send-btn:not(:disabled):hover { transform: translateY(-1px); }

.send-btn svg {
  width: 17px; height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.send-btn .icon-stop { fill: currentColor; stroke: none; }
.send-btn .icon-stop { display: none; }
.send-btn[data-mode="stop"] .icon-send { display: none; }
.send-btn[data-mode="stop"] .icon-stop { display: block; }
.send-btn[data-mode="stop"] { opacity: 1; cursor: pointer; }

/* ---- download progress ----------------------------------------- */

.progress { margin-top: .875rem; }

.progress-track {
  position: relative;
  height: 3px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

/* A sweep while waiting on the next event, so the long silences between
   Chrome's coarse progress steps read as working rather than stalled. */
.progress-track::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, var(--shimmer), transparent);
  transform: translateX(-100%);
  opacity: 0;
  transition: opacity .3s ease;
}
.progress[data-waiting="true"] .progress-track::after {
  opacity: 1;
  animation: sweep 1.8s ease-in-out infinite;
}

/* Width is eased frame by frame in app.js, so no CSS transition here. */
.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 3px;
  background: var(--accent);
}

@keyframes sweep {
  to { transform: translateX(100%); }
}

.progress-label {
  margin: .5rem 0 0;
  font-size: .8125rem;
  color: var(--text-muted);
}

/* ---- notice ---------------------------------------------------- */

.notice {
  margin-top: 1rem;
  padding: .875rem 1rem;
  border-left: 2px solid var(--border-strong);
  background: var(--surface-2);
  border-radius: 0 10px 10px 0;
  font-size: .9375rem;
}
.notice p { margin: 0; }
.notice p + p { margin-top: .375rem; color: var(--text-muted); font-size: .875rem; }

.notice-code {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  margin-top: .625rem;
  padding: .25rem .3rem .25rem .55rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--font-mono);
  font-size: .8125rem;
  color: var(--text);
}

/* ---- update pill ------------------------------------------------ */

/* A new version is cached and waiting. It never interrupts — the page keeps
   running the old version until this is accepted. */
.update-pill {
  position: fixed;
  left: 50%;
  top: 4.25rem;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .5rem .5rem .5rem .875rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow-lift);
  font-size: .875rem;
  animation: pill-in .28s ease-out both;
}

.update-pill[hidden] { display: none; }

.update-pill button { margin: 0; }

@keyframes pill-in {
  from { opacity: 0; transform: translate(-50%, -8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

.mini-btn {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-family: var(--font-ui);
  font-size: .75rem;
  padding: .15rem .4rem;
  border-radius: 5px;
  cursor: pointer;
}
.mini-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---- suggestion chips ------------------------------------------ */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  margin-top: 1.125rem;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font: inherit;
  font-size: .8125rem;
  padding: .4rem .8rem;
  border-radius: 999px;
  cursor: pointer;
  transition: color .15s ease, border-color .15s ease, transform .15s ease;
}
.chip:hover { color: var(--text); border-color: var(--border-strong); transform: translateY(-1px); }
.chips[data-disabled="true"] { opacity: .45; pointer-events: none; }

/* ---- messages -------------------------------------------------- */

.msg { display: flex; flex-direction: column; }
.msg-user { align-items: flex-end; }

.msg-user .msg-body {
  max-width: 80%;
  background: var(--surface-2);
  padding: .625rem 1rem;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Assistant turns are unbubbled prose so long answers read as a document. */
.msg-assistant .msg-body { max-width: 100%; overflow-wrap: anywhere; }

.msg-body > *:first-child { margin-top: 0; }
.msg-body > *:last-child { margin-bottom: 0; }

.msg-body p { margin: 0 0 .875rem; }
.msg-body h1, .msg-body h2, .msg-body h3,
.msg-body h4, .msg-body h5, .msg-body h6 {
  font-family: var(--font-display);
  font-weight: 400;
  letter-spacing: -.01em;
  line-height: 1.25;
  margin: 1.5rem 0 .625rem;
}
.msg-body h1 { font-size: 1.5rem; }
.msg-body h2 { font-size: 1.3125rem; }
.msg-body h3 { font-size: 1.125rem; }
.msg-body h4, .msg-body h5, .msg-body h6 { font-size: 1rem; font-family: var(--font-ui); font-weight: 600; }

.msg-body ul, .msg-body ol { margin: 0 0 .875rem; padding-left: 1.35rem; }
.msg-body li { margin-bottom: .3rem; }
.msg-body li::marker { color: var(--text-muted); }

.msg-body blockquote {
  margin: 0 0 .875rem;
  padding-left: .9rem;
  border-left: 2px solid var(--border-strong);
  color: var(--text-muted);
}

.msg-body hr { border: 0; border-top: 1px solid var(--border); margin: 1.5rem 0; }

.msg-body a { color: var(--text); text-decoration: underline; text-underline-offset: 2px; text-decoration-color: var(--border-strong); }
.msg-body a:hover { text-decoration-color: var(--text); }

.msg-body code {
  font-family: var(--font-mono);
  font-size: .875em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: .1em .35em;
}

.msg-body pre {
  position: relative;
  margin: 0 0 .875rem;
  padding: .875rem 1rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-x: auto;
}
.msg-body pre code {
  background: none;
  border: 0;
  padding: 0;
  font-size: .8125rem;
  line-height: 1.6;
}

.code-copy {
  position: absolute;
  top: .5rem; right: .5rem;
  opacity: 0;
  transition: opacity .15s ease;
}
.msg-body pre:hover .code-copy,
.code-copy:focus-visible { opacity: 1; }

/* ---- message actions ------------------------------------------- */

.msg-actions {
  display: flex;
  gap: .25rem;
  margin-top: .5rem;
  opacity: 0;
  transition: opacity .15s ease;
}
.msg:hover .msg-actions,
.msg-actions:focus-within { opacity: 1; }

.msg-flag {
  margin-top: .5rem;
  font-size: .8125rem;
  color: var(--text-muted);
  font-style: italic;
}

.msg-error .msg-body { color: var(--danger); }

/* ---- thinking indicator ---------------------------------------- */

.dots { display: inline-flex; gap: 5px; padding: .5rem 0; }
.dots span {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: pulse 1.3s ease-in-out infinite;
}
.dots span:nth-child(2) { animation-delay: .18s; }
.dots span:nth-child(3) { animation-delay: .36s; }

@keyframes pulse {
  0%, 60%, 100% { opacity: .25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

/* ---- cursor ----------------------------------------------------- */

/* A custom arrow with a warm glow. Pointer devices only — a touch
   device has no cursor to replace, and a coarse pointer shouldn't pay
   for the image. */
@media (hover: hover) and (pointer: fine) {
  body {
    cursor: url("data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20width='44'%20height='44'%20viewBox='0%200%2044%2044'><defs><filter%20id='g'%20x='-70%'%20y='-70%'%20width='260%'%20height='260%'><feGaussianBlur%20in='SourceAlpha'%20stdDeviation='2.6'%20result='b'/><feFlood%20flood-color='%23FF7A45'%20flood-opacity='0.85'/><feComposite%20in2='b'%20operator='in'%20result='glow'/><feMerge><feMergeNode%20in='glow'/><feMergeNode%20in='glow'/><feMergeNode%20in='SourceGraphic'/></feMerge></filter></defs><path%20d='M11%208%20L11%2030%20L16.6%2024.6%20L20.3%2032.6%20L24.1%2030.8%20L20.5%2023%20L28.2%2022.4%20Z'%20fill='%23FFFFFF'%20stroke='%232A1A12'%20stroke-width='1.1'%20stroke-linejoin='round'%20filter='url(%23g)'/></svg>") 11 8, auto;
  }

  a, button, .chip, summary, [role="button"] {
    cursor: url("data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20width='44'%20height='44'%20viewBox='0%200%2044%2044'><defs><filter%20id='g'%20x='-70%'%20y='-70%'%20width='260%'%20height='260%'><feGaussianBlur%20in='SourceAlpha'%20stdDeviation='2.6'%20result='b'/><feFlood%20flood-color='%23FF7A45'%20flood-opacity='0.85'/><feComposite%20in2='b'%20operator='in'%20result='glow'/><feMerge><feMergeNode%20in='glow'/><feMergeNode%20in='glow'/><feMergeNode%20in='SourceGraphic'/></feMerge></filter></defs><path%20d='M11%208%20L11%2030%20L16.6%2024.6%20L20.3%2032.6%20L24.1%2030.8%20L20.5%2023%20L28.2%2022.4%20Z'%20fill='%23FFFFFF'%20stroke='%232A1A12'%20stroke-width='1.1'%20stroke-linejoin='round'%20filter='url(%23g)'/></svg>") 11 8, pointer;
  }

  .composer-input, .msg-body, .notice-code {
    cursor: text;
  }

  .composer-input:disabled {
    cursor: url("data:image/svg+xml,<svg%20xmlns='http://www.w3.org/2000/svg'%20width='44'%20height='44'%20viewBox='0%200%2044%2044'><defs><filter%20id='g'%20x='-70%'%20y='-70%'%20width='260%'%20height='260%'><feGaussianBlur%20in='SourceAlpha'%20stdDeviation='2.6'%20result='b'/><feFlood%20flood-color='%23FF7A45'%20flood-opacity='0.85'/><feComposite%20in2='b'%20operator='in'%20result='glow'/><feMerge><feMergeNode%20in='glow'/><feMergeNode%20in='glow'/><feMergeNode%20in='SourceGraphic'/></feMerge></filter></defs><path%20d='M11%208%20L11%2030%20L16.6%2024.6%20L20.3%2032.6%20L24.1%2030.8%20L20.5%2023%20L28.2%2022.4%20Z'%20fill='%23FFFFFF'%20stroke='%232A1A12'%20stroke-width='1.1'%20stroke-linejoin='round'%20filter='url(%23g)'/></svg>") 11 8, not-allowed;
  }
}

/* ---- responsive ------------------------------------------------ */

@media (max-width: 480px) {
  body { font-size: 15px; }
  .header { padding: .75rem 1rem; }
  .thread { padding: .75rem 1rem 1.25rem; gap: 1.5rem; }
  .stage { padding: 0 1rem 1rem; }
  .main[data-state="empty"] .stage { padding-bottom: 8vh; }
  .msg-user .msg-body { max-width: 92%; }
  .msg-actions, .code-copy { opacity: 1; }
  .conn-chip { display: none; }
  .update-pill { left: 1rem; right: 1rem; top: 3.75rem; transform: none; justify-content: center; }
  @keyframes pill-in {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
  .dots span { opacity: .5; }
}
