:root {
  color-scheme: light;
  --bg: #faf8f5;
  --card: #ffffff;
  --ink: #1f1d1b;
  --muted: #6b6660;
  --line: #e8e3dd;
  --accent: #6c8cff;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

/* The `hidden` HTML attribute should always win, even on elements whose
   class rules set an explicit display (e.g. .files / .chat use display:flex,
   which otherwise overrides the user-agent [hidden]{display:none}). */
[hidden] { display: none !important; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  min-height: 100vh;
}

header.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: var(--card);
}

header.appbar h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
}

main {
  padding: 2rem 1.5rem 4rem;
  max-width: 64rem;
  margin: 0 auto;
}

button {
  font: inherit;
  cursor: pointer;
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 0.75rem;
  padding: 0.75rem 1.25rem;
}

button.secondary {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
}

/* Anchor styled as a small secondary button — used for the "← all projects"
   back link in the project page header. */
.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  text-decoration: none;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.35rem 0.7rem;
  font-size: 0.9rem;
  white-space: nowrap;
}
/* Dual selector — Chrome 86+ honours :focus-visible (keyboard-only
   focus ring); Chrome 85 and older ignore it but read :focus, so the
   focus outline still appears for keyboard users on those devices.
   Trade: mouse-click focus also gets the outline on old browsers,
   which is the pre-2020 default behaviour we'd otherwise lose. */
.btn-back:hover, .btn-back:focus-visible, .btn-back:focus {
  border-color: var(--accent);
  color: var(--accent);
}
.btn-back:active { transform: scale(0.98); }

button:disabled { opacity: 0.5; cursor: not-allowed; }

.profile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  gap: 1.25rem;
  max-width: 48rem;
  margin: 4rem auto 2rem;
}

.profile-tile {
  background: var(--card);
  color: var(--ink);                /* override the global button{color:white} */
  border: 1px solid var(--line);
  border-radius: 1.5rem;
  padding: 1.5rem 1rem 1.25rem;
  text-align: center;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s;
}

.profile-tile:hover { transform: translateY(-2px); box-shadow: 0 10px 24px -12px rgba(0,0,0,0.18); }
/* :focus-visible (Chrome 86+) + :focus fallback for Chrome 85 — see
   the dual-selector note on .btn-back above. */
.profile-tile:focus-visible,
.profile-tile:focus { outline: 3px solid var(--accent); outline-offset: 2px; }

.avatar {
  width: 6rem; height: 6rem;
  border-radius: 50%;
  display: grid; place-items: center;
  margin: 0 auto 0.75rem;
  font-size: 2.5rem;
  font-weight: 600;
  color: white;
  letter-spacing: -0.02em;
}

.profile-name { font-size: 1.1rem; font-weight: 500; }

/* PIN pad modal */
.pin-overlay {
  /* Explicit top/right/bottom/left longhand — `inset: 0` is a
     shorthand only supported from Chrome 87 / Safari 14.1. Devices on
     older Chrome (Android 11 / iOS 14 carriers, some tablet WebViews)
     ignore `inset` and end up with no positioning offsets, leaving
     the overlay placed wherever the document flow happened to put it
     (350+ pixels below the viewport — the exact bug a user hit on a
     Chrome 85 Android tablet). */
  position: fixed; top: 0; right: 0; bottom: 0; left: 0;
  display: none;
  /* The composer's bottom + the pin-card layout-fix need a viewport-
     sized height. CSS cascade order matters here: declarations are
     parsed top-to-bottom; the LAST VALID one wins. Chrome 85 doesn't
     know 100dvh — the second declaration is dropped, the first (100vh)
     applies. Chrome 108+ accepts 100dvh — the second declaration wins.
     If project.js / login.js has set --vv-height (a px value), the
     third declaration's var() resolves cleanly and wins (most accurate
     when the keyboard or URL bar is visible). */
  height: 100vh;
  /* flex-start (not center) so when the card is taller than the
     viewport, its TOP is anchored — kid can scroll the overlay to
     reach the bottom row of keys. With center, both top and bottom
     clip symmetrically (the original bug: only "1 2 3" visible on
     short viewports because the title + dots pushed the keys down
     and the bottom row fell off the bottom edge).
     justify-content: center stays so the card is horizontally
     centered. */
  align-items: flex-start; justify-content: center;
  background: rgba(20, 18, 14, 0.45);
  padding: 1rem;
  z-index: 10;
  /* Allow vertical scroll inside the overlay if the card exceeds the
     visible area. Belt + braces with the pin-card max-height below. */
  overflow-y: auto;
  /* visualViewport-aware height — login.js / project.js sets
     --vv-height to window.visualViewport.height. Overrides the 100vh
     fallback above when JS is loaded. The 100dvh fallback inside the
     var() handles the (rare) case where JS hasn't set --vv-height yet
     but the browser supports dvh (Chrome 108+). */
  height: var(--vv-height, 100dvh);
}
.pin-overlay.open { display: flex; }
.pin-card {
  background: var(--card);
  border-radius: 1.5rem;
  padding: 1.5rem 1.5rem 1rem;
  width: 100%;
  max-width: 22rem;
  text-align: center;
  /* Sticky-top: pad with the overlay's own padding so the card has
     breathing room from the screen edge when the viewport is tall, and
     vertically auto-centers via `margin: auto` ONLY when there's room
     (margin: auto on a flex item collapses to the available free
     space). On short viewports the card pins to the top and overflows
     scrollably. */
  margin: auto;
}
.pin-display {
  display: flex; justify-content: center; gap: 0.6rem;
  font-size: 2rem; margin: 1rem 0;
}
.pin-dot {
  width: 1rem; height: 1rem; border-radius: 50%;
  background: var(--line);
}
.pin-dot.filled { background: var(--ink); }
.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
  margin-top: 0.75rem;
}
.pin-key {
  background: #f4f2ee;
  color: var(--ink);
  border-radius: 1rem;
  padding: 1rem 0;
  font-size: 1.4rem;
  font-weight: 500;
  user-select: none;
}
.pin-key:active { transform: scale(0.97); }
.pin-message {
  min-height: 1.25rem;
  color: #c0392b;
  font-size: 0.95rem;
  margin-top: 0.5rem;
}

/* Home / project list */
.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr));
  gap: 1rem;
  margin-top: 1.5rem;
}
.project-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 1rem;
  padding: 1rem 1.1rem 1.1rem;
  text-decoration: none;
  color: var(--ink);
}
.project-card:hover { border-color: var(--accent); }
.project-card h3 { margin: 0 0 0.25rem; font-size: 1.05rem; }
.project-card .meta { color: var(--muted); font-size: 0.85rem; display: flex; align-items: center; gap: 0.25rem; flex-wrap: wrap; }
.project-card .owner-chip {
  display: inline-grid;
  place-items: center;
  width: 1.1rem; height: 1.1rem;
  border-radius: 50%;
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0;
}
.project-card .owner-name { font-weight: 500; color: var(--ink); }
.empty-state {
  text-align: center; color: var(--muted);
  margin-top: 3rem;
}

/* Family admin section on the home page */
.family-heading { margin: 3rem 0 0.5rem; font-size: 1.05rem; }
.family-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: 0.75rem;
}
.family-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--card);
}
.family-card .who { flex: 1; min-width: 0; }
.family-card .who-name { font-weight: 500; }
.family-card .who-state { font-size: 0.85rem; color: var(--muted); }
.family-card .who-state.locked { color: #b03030; }
.family-card .who-last-active { font-size: 0.8rem; color: var(--muted); opacity: 0.8; }
.project-card .meta.stats { font-size: 0.8rem; opacity: 0.85; }
.project-card .meta.stats.errors { color: #a4373a; opacity: 1; }
.family-card .unlock-btn {
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 0.5rem;
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
}
.family-card .unlock-btn:disabled { opacity: 0.4; cursor: default; }

.row { display: flex; gap: 0.5rem; align-items: center; }
.spacer { flex: 1; }

/* Project / chat page */
/* --vv-height is set by project.js from window.visualViewport.height,
   which is THE measurement that shrinks when the iOS keyboard appears
   (100dvh / 100svh do NOT — they only account for the URL bar). When
   the kid taps the textarea and the keyboard appears, --vv-height
   shrinks, the body shrinks with it, and the composer stays visible
   above the keyboard. Fallback: 100dvh on browsers without
   visualViewport (modern browsers all support it; this is a no-op
   today but harmless). */
/* The global \`body { min-height: 100vh }\` (line 26) was overriding the
   project-body's \`height\` on Chrome 85 — 100vh there counts the
   LAYOUT viewport (605px on the user's tablet) not the visual
   viewport (549px), so the body stretched to 605px and the
   composer's Send button at y=545-593 sat below the visible
   549px-tall viewport. The composer was "hidden by 56 pixels."
   Resetting min-height to 0 on the project-body lets the explicit
   `height: var(--vv-height)` actually take effect.

   100vh fallback for Chrome < 108 (no dvh support); then the var()
   declaration's px value (set by project.js) wins on browsers that
   load the JS, or 100dvh wins on modern browsers without JS loaded
   yet. See .pin-overlay comment for the full cascade rationale. */
/* iOS Safari / Chrome iOS will auto-scroll the documentElement when
   a focused input would be obscured by the on-screen keyboard. With
   only `body { overflow: hidden }`, the document element itself can
   still scroll — the composer ends up above the visible viewport
   while the kid is typing. Locking the html element (overflow + a
   fixed height) prevents iOS from scrolling the document, so the
   body's compressed-by-vv-height layout stays put. Scoped to the
   project page so other pages (home, login) keep their normal
   scroll behaviour. */
html:has(body.project-body) {
  overflow: hidden;
  height: 100%;
}
/* When the soft keyboard opens on a phone (viewport-keyboard.js sets
   body.keyboard-open if vv.height drops > 120px below baseline), hide
   the non-essential chrome — mode-chips and the chat-topbar status
   pill — to give the composer enough vertical room that its Send
   button stays above the keyboard. The chips' selected state is
   preserved (just visually hidden); blur restores everything. */
body.keyboard-open .chat-topbar,
body.keyboard-open .mode-row {
  display: none;
}
body.project-body {
  display: flex; flex-direction: column;
  min-height: 0;
  height: 100vh;
  height: var(--vv-height, 100dvh);
  overflow: hidden;
}
.project-body main.project-main { flex: 1; min-height: 0; max-width: none; padding: 0; margin: 0; display: flex; }

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

.chat-topbar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1.25rem 0.5rem;
}

.open-link {
  font-size: 0.95rem;
  text-decoration: none;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.25rem 0.75rem;
  color: var(--accent);
}
.open-link:hover { border-color: var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.25rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.msg-row { display: flex; flex-direction: column; max-width: 100%; }
.msg-row.user      { align-items: flex-end; }
.msg-row.assistant { align-items: flex-start; }
.msg-row.system    { align-items: flex-start; }
.msg-row.error     { align-items: flex-start; }

.bubble-author {
  font-size: 0.78rem;
  color: var(--accent);
  font-weight: 500;
  margin: 0 0.4rem 0.2rem;
}

/* Per-turn mode tag rendered above a user bubble. Mirrors the
   .bubble-author position; small enough that even both tags together
   stay readable. */
.bubble-mode {
  font-size: 0.78rem;
  font-weight: 500;
  margin: 0 0.4rem 0.2rem;
  display: inline-block;
  padding: 0.05rem 0.45rem;
  border-radius: 0.6rem;
  align-self: flex-end;
}
.bubble-mode.mode-bug     { background: #ffeaea; color: #a4373a; }
.bubble-mode.mode-feature { background: #eef9ec; color: #2f7a35; }
.bubble-mode.mode-plan    { background: #f4f7ff; color: #3b5bdb; }
/* Fallback when the server ships a mode the client doesn't know yet
   (e.g. browser cached project.js predates a deploy that added a new
   mode). Warning-amber so the kid + admin notice something's off. */
.bubble-mode.mode-unknown { background: #fef3c7; color: #92400e; }

/* Decomposition breakdown bubble — full plan shown above a pipeline.
   Each li gets a ○ (pending) → ✓ (done) tick as steps complete. */
.msg-row.breakdown {
  background: #f4f7ff;
  border: 1px solid #d0d9f5;
  border-radius: 0.6rem;
  padding: 0.7rem 0.9rem;
  margin: 0.4rem 0;
  align-self: stretch;
}
.msg-row.breakdown .bubble-title {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.4rem;
  color: #3b5bdb;
}
.breakdown-list {
  margin: 0;
  padding-left: 0;
  list-style: none;
}
.breakdown-list li {
  display: flex;
  gap: 0.5rem;
  padding: 0.2rem 0;
  font-size: 0.92rem;
  line-height: 1.35;
}
.breakdown-list li.done { color: var(--muted); }
.breakdown-list .step-tick {
  flex: 0 0 1.1rem;
  font-weight: 700;
  color: #3b5bdb;
}
.breakdown-list li.done .step-tick { color: #2f7a35; }

/* Step header row — a thin separator above each step's assistant
   bubble + tool lines. Smaller / muted so it doesn't compete with
   the bubble content visually. */
.msg-row.step-header {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
  padding: 0.3rem 0.6rem;
  margin: 0.4rem 0 0.1rem;
  background: transparent;
  border-left: 2px solid #d0d9f5;
}

/* Stop button — replaces Send during a pipeline. Red to signal "this
   interrupts." Matches Send's size + alignment so the swap is clean. */
.stop-btn {
  background: #c0392b;
  color: white;
  border: none;
  border-radius: 0.4rem;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
  font-weight: 600;
}
.stop-btn:hover { background: #a4302a; }
.stop-btn:disabled { background: #d8a098; cursor: not-allowed; }

.viewing-as-admin {
  text-align: center;
  font-size: 0.85rem;
  color: var(--accent);
  background: #f4f7ff;
  border-bottom: 1px solid #d6e0ff;
  padding: 0.3rem 1rem;
}

/* Share panel — a dropdown sheet under the project header. */
.share-panel {
  position: absolute;
  right: 1rem;
  top: 4.5rem;
  width: 22rem;
  max-width: calc(100vw - 2rem);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  padding: 1rem;
  z-index: 30;
}
.share-panel[hidden] { display: none; }
.share-header { display: flex; align-items: center; justify-content: space-between; }
.share-header h2 { margin: 0; font-size: 1rem; }
.share-list { margin: 0.75rem 0; display: grid; gap: 0.5rem; }
.share-row {
  display: flex; align-items: center; gap: 0.5rem;
  border: 1px solid var(--line); border-radius: 0.5rem; padding: 0.4rem 0.6rem;
}
.share-row .avatar {
  width: 2rem; height: 2rem; border-radius: 50%;
  color: white; display: flex; align-items: center; justify-content: center;
  font-weight: 600;
}
.share-row .share-who { flex: 1; min-width: 0; }
.share-row .share-name { font-weight: 500; }
.share-row .share-meta { font-size: 0.8rem; color: var(--muted); }
.share-empty { color: var(--muted); font-size: 0.9rem; padding: 0.25rem; }
.share-add { display: flex; gap: 0.4rem; align-items: center; }
.share-add select { flex: 1; padding: 0.4rem; }
.share-help { color: var(--muted); font-size: 0.8rem; margin: 0.75rem 0 0; }

.shared-chip {
  display: inline-block;
  font-size: 0.7rem;
  background: #fff3cd;
  color: #8a6d3b;
  padding: 0.1rem 0.4rem;
  border-radius: 0.4rem;
  margin-left: 0.25rem;
}

/* Retry button under error bubbles — copies the previous user message
   into the composer. Same shape as the restore button below. */
.bubble-retry {
  margin-top: 0.25rem;
}
.retry-btn {
  font-size: 0.75rem;
  background: transparent;
  color: #a4373a;
  border: 1px solid #f4c5c5;
  border-radius: 0.4rem;
  padding: 0.15rem 0.5rem;
  cursor: pointer;
}
.retry-btn:hover {
  background: #ffeaea;
}

/* Restore-to-checkpoint button under each assistant bubble that committed. */
.bubble-restore {
  margin-top: 0.25rem;
}
.restore-btn {
  font-size: 0.75rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  padding: 0.15rem 0.5rem;
  cursor: pointer;
}
.restore-btn:hover {
  background: #fff3cd;
  color: #8a6d3b;
  border-color: #e9d28a;
}
.restore-btn:disabled { opacity: 0.5; cursor: default; }

.bubble {
  max-width: 38rem;
  border-radius: 1rem;
  padding: 0.75rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-wrap: break-word;
}
.bubble.user { background: #e9efff; border-color: #d6e0ff; }
.bubble.assistant { }
.bubble.system { background: #fff6e0; border-color: #f0e0a0; font-size: 0.95rem; }
.bubble.error { background: #ffeaea; border-color: #f4c5c5; }

.bubble-meta {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0.2rem 0.4rem 0;
  font-variant-numeric: tabular-nums;
}

/* Suggested-next-step chips shown under the latest assistant reply. */
.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.5rem 0.1rem 0;
  max-width: 38rem;
}
.suggestion-chip {
  background: var(--card);
  color: var(--accent);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.suggestion-chip:hover { border-color: var(--accent); background: #f4f7ff; }
.suggestion-chip:active { transform: scale(0.97); }

/* In-progress placeholder bubble. Used to be bouncing dots; now shows
   live text ("Thinking…", "Editing 3 files…", "Almost done…") so the
   kid can see progress at a glance without scrolling to the status
   pill. The text updates synchronously via updateThinkingBubble() in
   project.js. A subtle opacity pulse keeps a sense of activity even
   when the text isn't changing — floor at 0.75 (was 0.6) so the
   muted italic text stays readable through the dim half of the
   cycle. */
.thinking-bubble {
  padding: 0.7rem 0.95rem;
  color: var(--muted);
  font-style: italic;
  animation: thinking-pulse 1.8s infinite ease-in-out;
}
@keyframes thinking-pulse {
  0%, 100% { opacity: 0.75; }
  50%      { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .thinking-bubble { animation: none; opacity: 0.9; }
}

.tool-line {
  align-self: flex-start;
  font-size: 0.85rem;
  color: var(--muted);
  padding: 0.25rem 0.75rem;
  border-left: 3px solid var(--line);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.tool-line.error { color: #c0392b; border-left-color: #c0392b; }

/* Per-turn chat-mode chip row above the composer textarea. Three small
   round pill-buttons; click toggles selection (only one active at a time).
   Selected state is the existing accent colour to match the rest of the
   platform's button-affirmative pattern. */
.mode-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}
.mode-chip {
  border: 1px solid var(--line);
  background: white;
  border-radius: 999px;
  padding: 0.25rem 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
.mode-chip:hover { background: #f5f7fb; }
.mode-chip.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}
.mode-chip .mode-icon { font-size: 0.95rem; }
@media (max-width: 480px) {
  /* On phones, drop the verbose label and show only the icon — keeps the
     row from wrapping to two lines on narrow screens. Tap targets stay
     ≥44×44px (WCAG 2.5.5 / Apple HIG) since a 7-year-old's pointer
     accuracy is worse than an adult's; the icon-only chip's hit area
     would otherwise be ~24px. */
  .mode-chip .mode-label { display: none; }
  .mode-chip {
    padding: 0.6rem;
    min-width: 2.75rem;  /* ≈44px */
    min-height: 2.75rem;
    justify-content: center;
  }
  .mode-chip .mode-icon { font-size: 1.25rem; }
  .mode-row { gap: 0.6rem; }
}

.composer {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem 1.25rem;
  border-top: 1px solid var(--line);
  background: var(--bg);
  /* The composer's children (Send button most importantly) MUST be
     visible when the body shrinks for the soft keyboard. Without
     flex-shrink: 0, the messages flex sibling above can refuse to
     yield and the composer overflows the body's bottom edge —
     `body.project-body { overflow: hidden }` then clips the Send
     button. Pin composer to its natural height; messages takes
     whatever's left. */
  flex-shrink: 0;
}
.composer-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
}
.composer textarea {
  flex: 1;
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  padding: 0.6rem 0.8rem;
  resize: vertical;
  min-height: 2.5rem;
  max-height: 12rem;
  background: var(--card);
}
.composer button { align-self: stretch; min-width: 5rem; }
.composer-controls { display: flex; flex-direction: column; gap: 0.4rem; }
.composer-controls select {
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.35rem 0.5rem;
  background: var(--card);
  color: var(--ink);
}
.composer.drag-over textarea {
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
  background: #f4f7ff;
}

.icon-btn {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.35rem 0.6rem;
  font-size: 1rem;
  min-width: 0;
  align-self: stretch;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }

/* Attachment chips above the textarea, and on user message bubbles. */
.composer-attachments,
.bubble-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0 0.1rem;
}
.composer-attachments { margin-bottom: 0.25rem; }
.bubble-attachments { margin-top: 0.5rem; }

.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.5rem 0.2rem 0.3rem;
  background: #f4f2ee;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  font-size: 0.85rem;
  max-width: 14rem;
}
.att-chip .att-thumb {
  width: 1.5rem; height: 1.5rem;
  border-radius: 0.25rem;
  object-fit: cover;
  background: var(--bg);
}
.att-chip .att-icon {
  width: 1.5rem; height: 1.5rem;
  display: grid; place-items: center;
  font-size: 0.95rem;
  color: var(--muted);
}
.att-chip .att-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.att-chip .att-remove {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.1rem;
  min-width: 0;
}
.att-chip .att-remove:hover { color: #c0392b; }

/* In bubble: bigger image previews so the kid can see what they shared. */
.bubble-attachments .att-image {
  display: block;
  max-width: 14rem;
  max-height: 12rem;
  border-radius: 0.5rem;
  border: 1px solid var(--line);
}
.composer-error { padding: 0.5rem 1.25rem; color: #c0392b; font-size: 0.95rem; }

.status-pill {
  font-size: 0.85rem;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  background: var(--line);
  color: var(--muted);
}
.status-pill.ready { background: #d9f7e0; color: #2e7d3a; }
.status-pill.scaffolding { background: #fff3d6; color: #8a6900; }
.status-pill.failed { background: #fcd6d6; color: #b03030; }
.status-pill.working { background: #d6e7fd; color: #2a5fb0; }

.btn-danger {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.35rem 0.55rem;
  font-size: 0.95rem;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.btn-danger:hover, .btn-danger:focus-visible, .btn-danger:focus {
  border-color: #c0392b;
  color: #c0392b;
  background: #fff5f5;
}
.btn-danger:disabled { opacity: 0.5; cursor: not-allowed; }

/* Tab bar in the project header */
.tab-bar { display: flex; gap: 0.25rem; }
.tab {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.9rem;
}
.tab.active { background: var(--card); border-color: var(--accent); color: var(--accent); }

/* Files pane: tree on left, content on right */
.files {
  flex: 1;
  display: flex;
  min-height: 0;
  max-width: 64rem;
  width: 100%;
  margin: 0 auto;
}
.files-tree {
  width: 16rem;
  min-width: 12rem;
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 0.75rem 0.5rem;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.9rem;
}
.files-tree .tree-row {
  cursor: pointer;
  padding: 0.15rem 0.5rem;
  border-radius: 0.3rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.files-tree .tree-row:hover { background: #f4f2ee; }
.files-tree .tree-row.dir { color: var(--muted); cursor: default; }
.files-tree .tree-row.dir:hover { background: transparent; }
.files-tree .tree-row.active { background: #e9efff; color: var(--accent); }

/* Chat + Files layout — user-chosen view via the always-visible tab bar.
   #project-main[data-view] is set by project.js: "chat" / "files" show one pane;
   "both" shows them side-by-side (a resizable split) in EITHER host mode. */
#project-main[data-view="chat"] > #files-pane { display: none; }
#project-main[data-view="files"] > #chat-pane { display: none; }

/* Split view: chat is a fixed (draggable) width, files takes the rest. The
   single-pane defaults (.chat/.files max-width + margin:0 auto) MUST be reset
   here or the panes center/clamp and the split breaks. --chat-width is a % the
   resizer drives (default 40%); specificity [data-view] .chat (0,2,0) beats the
   bare .chat (0,1,0) — no !important needed since these never set display:none. */
#project-main[data-view="both"] .chat {
  flex: 0 0 var(--chat-width, 40%); min-width: 16rem; max-width: none; margin: 0;
}
#project-main[data-view="both"] .files {
  flex: 1; min-width: 12rem; max-width: none; margin: 0; border-left: 1px solid var(--line);
}
#project-main[data-view="both"] .files .files-tree { width: 12rem; min-width: 10rem; }

/* The drag handle between the panes — only present in the split view. */
.pane-resizer { display: none; }
#project-main[data-view="both"] .pane-resizer {
  display: block;
  flex: 0 0 6px;
  cursor: col-resize;
  background: var(--line);
  touch-action: none;
}
#project-main[data-view="both"] .pane-resizer:hover { background: var(--accent); }
body.resizing { cursor: col-resize; user-select: none; }

/* The "Both" split isn't usable on phones too narrow to fit two panes — hide
   the tab there (project.js renders chat in that case, keeping the stored pref). */
@media (max-width: 767px) {
  #tab-both { display: none; }
}

/* Uploads section at the top of the files tree (user-attached files). */
.uploads-group {
  padding: 0.5rem 0.5rem 0.25rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.upload-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.2rem 0.5rem;
  margin-bottom: 0.5rem;
  border-radius: 0.3rem;
  text-decoration: none;
  color: var(--ink);
  cursor: pointer;
}
.upload-row:hover { background: #f4f2ee; }
.upload-row:last-of-type { margin-bottom: 0.75rem; }
.upload-thumb {
  width: 1.75rem;
  height: 1.75rem;
  object-fit: cover;
  border-radius: 0.25rem;
  border: 1px solid var(--line);
  flex: 0 0 auto;
}
.upload-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.files-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.files-view-header {
  padding: 0.6rem 1rem;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  background: var(--card);
}
#files-view-content {
  flex: 1;
  overflow: auto;
  margin: 0;
  padding: 1rem;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.85rem;
  line-height: 1.45;
  white-space: pre;
  background: #fafafa;
}

.bubble-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  margin-top: 0.55rem;
}

/* "Send error to Claude" inside system error bubbles */
.bubble.system .btn-send-error,
.bubble.error .btn-send-error {
  display: inline-block;
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 0.5rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.bubble.system .btn-send-error.btn-refresh,
.bubble.error .btn-send-error.btn-refresh {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
}

/* =========================================================================
   Media queries and feature queries — placed at the very end so they always
   win the cascade against the base rules above (regardless of selector order).
   ========================================================================= */

/* ----- Mobile (narrow screens) -----
   Phones in portrait. Tablets in portrait (~768px) already render acceptably
   with the desktop layout, so we keep the breakpoint at 640px. */
@media (max-width: 640px) {
  body { font-size: 16px; }

  header.appbar { padding: 0.75rem 1rem; gap: 0.5rem; flex-wrap: wrap; }
  header.appbar h1 { font-size: 1.05rem; }

  main { padding: 1rem 1rem 3rem; }

  /* Login: 2-column tile grid, smaller avatars */
  .profile-grid { margin: 1.5rem auto 1rem; gap: 0.85rem; grid-template-columns: repeat(2, 1fr); }
  .profile-tile { padding: 1rem 0.5rem 0.85rem; border-radius: 1.25rem; }
  .avatar { width: 4.5rem; height: 4.5rem; font-size: 2rem; }
  .profile-name { font-size: 1rem; }

  /* Home: single-column project tiles */
  .project-grid { grid-template-columns: 1fr; }

  /* Composer: textarea above, controls in a horizontal row below. Add a
     safe-area bottom inset so the iOS home indicator doesn't sit on Send. */
  .composer {
    padding: 0.75rem 1rem;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom, 0.75rem));
    gap: 0.5rem;
  }
  /* Cap the textarea against the actual visible viewport height so
     it can't bloat past the keyboard-shrunk area on iOS. We CAN'T
     use `dvh` here: iOS Safari/WebKit treats the on-screen keyboard
     as an overlay rather than a viewport shrink, so `dvh` stays at
     the full pre-keyboard height. Use `--vv-height` instead, which
     viewport-keyboard.js maintains from `window.visualViewport.height`
     and DOES shrink when the keyboard opens.
     calc(var(--vv-height) - 220px) reserves ~220px for the body's
     header (appbar ~50 + chat-topbar ~40) and the composer chrome
     (mode chips ~50 + controls row ~50 + paddings ~30); whatever's
     left becomes the textarea's max-height. min(...) clamps it to a
     reasonable upper bound when the viewport is generous; max(60px, ...)
     keeps a usable typing area even on a tiny screen. */
  .composer textarea {
    max-height: min(12rem, max(60px, calc(var(--vv-height, 100dvh) - 220px)));
  }
  .composer-row    { flex-direction: column; }
  .composer-controls { flex-direction: row; gap: 0.5rem; align-items: stretch; }
  .composer-controls select { flex: 1; }
  .composer-controls button { flex: 1; min-width: 0; align-self: auto; }
  .icon-btn { flex: 0 0 auto; }   /* paperclip stays compact, doesn't grow */

  /* Files tab: stack tree above viewer */
  .files { flex-direction: column; }
  .files-tree { width: 100%; min-width: 0; max-height: 40vh; border-right: 0; border-bottom: 1px solid var(--line); }

  /* Family cards a touch tighter */
  .family-card { padding: 0.5rem 0.75rem; gap: 0.5rem; }

  /* Bubbles fill the conversation column */
  .bubble { max-width: 100%; }

  /* Make sure the chat scroll area can shrink so its overflow:auto kicks in. */
  .chat     { min-height: 0; }
  .messages { min-height: 0; flex: 1 1 0; }
}

/* ----- iOS / Safari guards -----
   Prevent the on-focus zoom on form fields (only triggers when font-size < 16px). */
@supports (-webkit-touch-callout: none) {
  .composer textarea,
  input[type="text"],
  input[type="number"],
  select { font-size: 16px; }
}

/* ----- Co-work mode (co.kandc.net) -----
   The chatbox is shared single-source; these are the only mode-specific
   divergences, gated on the server-injected <body data-mode="cowork">. */

/* No running app: hide the "open your app" link + the bug/feature/plan chips.
   Qualified to body[...] so it can't cross-match the chat-mode chips, which
   also use data-mode (data-mode="bug|feature|plan"). */
body[data-mode="cowork"] #open-app { display: none !important; }
body[data-mode="cowork"] .mode-row { display: none; }

/* Files tab: rendered markdown view (instead of the raw-text <pre>). */
#files-view-content.rendered-md {
  white-space: normal;
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.6;
  background: var(--card);
  color: var(--ink);
}
#files-view-content.rendered-md pre {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: #f4f1ec;
  padding: 0.75rem;
  border-radius: 0.4rem;
  overflow: auto;
  white-space: pre;
}
#files-view-content.rendered-md code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: #f4f1ec;
  padding: 0.1em 0.3em;
  border-radius: 0.3em;
}
#files-view-content.rendered-md pre code { background: none; padding: 0; }
#files-view-content.rendered-md blockquote {
  border-left: 3px solid var(--line);
  margin: 0.5rem 0;
  padding: 0.2rem 1rem;
  color: var(--muted);
}
#files-view-content.rendered-md table {
  border-collapse: collapse;
  margin: 0.75rem 0;
  font-size: 0.9rem;
  display: block;
  overflow-x: auto;
  max-width: 100%;
}
#files-view-content.rendered-md th,
#files-view-content.rendered-md td {
  border: 1px solid var(--line);
  padding: 0.35rem 0.6rem;
  text-align: left;
  vertical-align: top;
}
#files-view-content.rendered-md th { background: #f4f1ec; font-weight: 600; }
#files-view-content.rendered-md tbody tr:nth-child(even) { background: #faf8f5; }
.files-view-header { display: flex; align-items: center; gap: 0.75rem; }
.files-view-header .file-download {
  margin-left: auto;
  font-family: system-ui, sans-serif;
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
}
.files-view-header .file-download:hover { text-decoration: underline; }

/* Shared-password gate on the login page. */
.cowork-gate {
  max-width: 22rem;
  margin: 4rem auto 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.5rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.75rem;
}
.cowork-gate h2 { margin: 0; }
.cowork-gate .cowork-label { font-size: 0.95rem; color: var(--muted); }
.cowork-gate input {
  padding: 0.6rem 0.7rem;
  font-size: 16px;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
}
