:root {
  --bg: #f5f2ec;
  --surface: #ffffff;
  --border: #e6ded2;
  --text: #26221c;
  --text-muted: #6e6559;
  --accent: #b45f2e;
  --accent-hover: #954e24;
  --accent-contrast: #ffffff;
  --accent-bg: #f7ece0;

  /* Fixed, theme-independent header chrome — deliberately not the brand
     color. A big block of saturated color reads as an older mobile-app
     pattern; keeping the header a neutral near-black in both themes (like
     Vercel/Linear/Raycast) and saving the accent for the logo mark, links,
     and buttons reads as more current and lets the accent mean something
     when it appears. */
  --header-bg: #211d18;
  --header-fg: #f5f1ea;

  --positive: #2f7a4f;
  --positive-bg: #e6f3ea;
  --negative: #b8422f;
  --negative-bg: #fbeae6;
  --neutral: #6b6459;
  --neutral-bg: #efece6;
  --mixed: #8a5cb0;
  --mixed-bg: #f2e9f7;

  --chip-bg: rgba(0, 0, 0, 0.055);
  --chip-border: rgba(0, 0, 0, 0.13);
  --chip-text: var(--text-muted);

  --radius: 18px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(30, 25, 15, 0.06), 0 4px 14px rgba(30, 25, 15, 0.05);
  --shadow-md: 0 2px 6px rgba(30, 25, 15, 0.08), 0 10px 28px rgba(30, 25, 15, 0.07);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17140f;
    --surface: #262019;
    --border: #3d362b;
    --text: #ece7de;
    --text-muted: #ab9f8c;
    --accent: #e0895a;
    --accent-hover: #f0a074;
    --accent-contrast: #1a1815;
    --accent-bg: #33251a;

    --positive: #7fce9f;
    --positive-bg: #1e2f24;
    --negative: #e4917f;
    --negative-bg: #332019;
    --neutral: #ab9f8c;
    --neutral-bg: #2b2620;
    --mixed: #c6a2e0;
    --mixed-bg: #2c2333;

    --chip-bg: rgba(0, 0, 0, 0.25);
    --chip-border: rgba(255, 255, 255, 0.13);
    --chip-text: var(--text);

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 2px 6px rgba(0, 0, 0, 0.35), 0 12px 30px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  overflow: hidden; /* the feed scrolls internally, not the page */
}

a { color: var(--accent); text-decoration-color: color-mix(in srgb, var(--accent) 50%, transparent); }
a:hover { color: var(--accent-hover); }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

code {
  background: var(--neutral-bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

/* ---------------------------------------------------------------------- */
/* App shell — branded header band, scrolling feed, fixed composer        */
/* ---------------------------------------------------------------------- */

.app-shell {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.chat-topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--header-bg);
  color: var(--header-fg);
}

/* Everything below the header: a persistent sidebar + main column on wide
   screens (a "website" shell), collapsing to a single full-width column
   with the sidebar as a slide-in drawer on narrower ones (a "chat app"
   shell). See the min-width:880px override further down. */
.app-body {
  flex: 1 1 auto;
  display: flex;
  min-height: 0; /* let children scroll internally instead of growing the row */
  overflow: hidden;
}

.main-area {
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

.chat-column {
  width: 100%;
  max-width: 900px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: #fff;
  flex-shrink: 0;
}
.brand-mark svg { width: 19px; height: 19px; }

.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.brand-title {
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.01em;
  color: var(--header-fg);
}
.brand-sub {
  font-size: 0.76rem;
  color: rgba(245, 241, 234, 0.65);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}

.topbar-actions { display: flex; gap: 2px; flex-shrink: 0; }

.icon-btn {
  width: 40px;
  height: 40px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: rgba(245, 241, 234, 0.75);
  cursor: pointer;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, transform 0.08s ease;
}
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn:hover { background: rgba(255, 255, 255, 0.1); color: var(--header-fg); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.copied { background: rgba(255, 255, 255, 0.18); color: var(--header-fg); }

/* Settings drawer's own close button sits on a plain surface, not the
   header bar, so it needs the neutral (non-white) icon-btn treatment. */
.drawer-header .icon-btn {
  color: var(--text-muted);
}
.drawer-header .icon-btn:hover { background: var(--neutral-bg); color: var(--text); }

/* ---------------------------------------------------------------------- */
/* Feed                                                                    */
/* ---------------------------------------------------------------------- */

.chat-feed {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.feed-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-muted);
  padding: 20px;
  gap: 2px;
}
.feed-empty.hidden { display: none; }
.feed-empty-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent-bg);
  color: var(--accent);
  margin-bottom: 10px;
}
.feed-empty-icon svg { width: 26px; height: 26px; }
.feed-empty p { margin: 4px 0; max-width: 340px; }
.feed-empty-hint { font-size: 0.82rem; }

/* ---------------------------------------------------------------------- */
/* Turn cards — one card per review, holding both the review and the      */
/* reply as two sections of the same unit (not separate chat bubbles).     */
/* ---------------------------------------------------------------------- */

.turn-card {
  flex-shrink: 0; /* flex items in a column shrink below content height by default — that clipped these cards against overflow:hidden */
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.turn-section-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 5px;
}

.turn-review {
  padding: 14px 16px 13px;
}
.turn-review .turn-section-label { color: var(--text-muted); }

.turn-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
}

.turn-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.tag {
  display: inline-flex;
  align-items: center;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  text-transform: capitalize;
}
.tag.positive { color: var(--positive); background: var(--positive-bg); }
.tag.negative { color: var(--negative); background: var(--negative-bg); }
.tag.neutral { color: var(--neutral); background: var(--neutral-bg); }
.tag.mixed { color: var(--mixed); background: var(--mixed-bg); }
.tag.lang { color: var(--text-muted); background: var(--neutral-bg); text-transform: none; }

.turn-response {
  padding: 13px 16px 14px;
  background: var(--accent-bg);
  border-top: 1px solid var(--border);
}
.turn-response.has-error {
  background: var(--negative-bg);
}

.turn-response-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.turn-response .turn-section-label { color: var(--accent); margin-bottom: 0; }
.turn-response.has-error .turn-section-label { color: var(--negative); }

.turn-toolbar {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.turn-icon-btn {
  width: 30px;
  height: 30px;
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, transform 0.08s ease;
}
.turn-icon-btn svg { width: 15px; height: 15px; }
.turn-icon-btn:hover { background: var(--chip-bg); color: var(--text); }
.turn-icon-btn:active { transform: scale(0.9); }
.turn-icon-btn.copy-icon { color: var(--accent); }
.turn-icon-btn.copy-icon svg { width: 16px; height: 16px; }
.turn-icon-btn.copy-icon.copied { background: var(--accent); color: #fff; }

.turn-response-text {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 0;
  resize: none;
  overflow: hidden;
  min-height: 1.4em;
}
.turn-response-text:focus { outline: none; }
.turn-response-text::placeholder { color: var(--text-muted); }

.turn-controls {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-top: 10px;
  padding-top: 9px;
  border-top: 1px solid var(--chip-border);
}

.chip-select {
  font-size: 0.76rem;
  color: var(--chip-text);
  background: var(--chip-bg);
  border: 1px solid var(--chip-border);
  border-radius: 999px;
  padding: 3px 8px;
  min-height: 26px;
  max-width: 100%;
}

.turn-status {
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-top: 6px;
}
.turn-status.err { color: var(--negative); font-weight: 600; }

.typing-dots {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  padding: 4px 0;
}
.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

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

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer-input {
  flex: 1;
  font: inherit;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 11px 16px;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  line-height: 1.4;
}
.composer-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.send-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  min-height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: var(--accent-contrast);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: filter 0.15s ease, opacity 0.15s ease, transform 0.08s ease;
}
.send-btn svg { width: 19px; height: 19px; margin-left: -2px; }
.send-btn:hover { filter: brightness(1.08); }
.send-btn:active { transform: scale(0.93); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.single-mode-toggle {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 0.76rem;
  color: var(--text-muted);
  padding: 0 16px 10px;
  background: var(--surface);
  cursor: pointer;
}
.single-mode-toggle input { margin: 0; }

/* ---------------------------------------------------------------------- */
/* Settings drawer                                                        */
/* ---------------------------------------------------------------------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 14, 0.45);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.drawer-backdrop.hidden { display: none; }
.drawer-backdrop.visible { opacity: 1; }

.settings-drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  height: 100dvh;
  width: min(400px, 92vw);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  z-index: 41;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s ease;
}
.settings-drawer.open { transform: translateX(0); }

.drawer-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.drawer-header h2 { margin: 0; font-size: 1.1rem; }

.drawer-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 18px;
}

.drawer-footer {
  flex: 0 0 auto;
  padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-muted);
}
.drawer-footer p { margin: 4px 0; }

/* ---------------------------------------------------------------------- */
/* Form controls (inside the drawer)                                       */
/* ---------------------------------------------------------------------- */

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}

input[type="text"],
input[type="password"],
select {
  width: 100%;
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  min-height: 44px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.field-row { margin-bottom: 20px; }

.key-input-group { display: flex; gap: 8px; flex-wrap: wrap; }
.key-input-group input { flex: 1 1 200px; }

.field-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-bottom: 20px;
}
.field-grid.hidden { display: none; }

.audience-toggle {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
}

.audience-btn {
  flex: 1;
  padding: 8px 12px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.audience-btn:hover { color: var(--text); }
.audience-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: #fff;
}

.hint { font-size: 0.82rem; color: var(--text-muted); margin: 8px 0 0; }

.status-line { font-size: 0.82rem; margin: 6px 0 0; min-height: 1.2em; }
.status-line.ok { color: var(--positive); }
.status-line.err { color: var(--negative); }

.button-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }

button { font: inherit; }

.btn-secondary {
  background: var(--neutral-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  min-height: 44px;
  font-weight: 600;
  cursor: pointer;
}
.btn-secondary:hover { background: var(--border); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  min-height: 44px;
  font-weight: 500;
  cursor: pointer;
}
.btn-ghost:hover { color: var(--text); text-decoration: underline; }

/* ---------------------------------------------------------------------- */
/* Toast                                                                   */
/* ---------------------------------------------------------------------- */

.toast {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 11px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  box-shadow: var(--shadow-md);
  z-index: 50;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.hidden { display: none; }

/* ---------------------------------------------------------------------- */
/* Responsive                                                              */
/* ---------------------------------------------------------------------- */

@media (min-width: 640px) {
  .field-grid { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
}

/* Wide screens get a "website" shell: Setup sits permanently in a left
   sidebar instead of behind a gear-icon drawer, and the conversation
   column is centered in the remaining space (capped at 900px so lines of
   text don't stretch edge-to-edge) rather than leaving the rest of a wide
   viewport empty. Narrower than this, it's the drawer-based single column. */
@media (min-width: 880px) {
  #open-settings { display: none; }
  #close-settings { display: none; }
  .drawer-backdrop { display: none !important; }

  .settings-drawer {
    position: static;
    width: 320px;
    min-width: 320px;
    max-width: 320px;
    height: auto;
    transform: none !important;
    box-shadow: none;
    border-right: 1px solid var(--border);
  }

  .main-area { padding: 0 24px; }
}

@media (max-width: 480px) {
  .brand-sub { max-width: 150px; }
}
