:root {
  color-scheme: dark;
  --bg: #07111d;
  --bg-2: #0c1826;
  --bg-3: #111f2f;
  --panel: rgba(12, 20, 32, 0.82);
  --panel-strong: rgba(18, 28, 44, 0.95);
  --panel-soft: rgba(255, 255, 255, 0.04);
  --border: rgba(214, 191, 143, 0.14);
  --border-strong: rgba(214, 191, 143, 0.26);
  --text: #f5efe3;
  --muted: #b2a892;
  --accent: #d9b26d;
  --accent-2: #7dd3fc;
  --accent-3: #8fb6ff;
  --danger: #ff7489;
  --success: #63d39d;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
  --radius-xl: 28px;
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 12px;
  --sidebar-w: 320px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, Monaco, monospace;
  --sans: "Aptos", "Segoe UI", "Trebuchet MS", sans-serif;
  --display: Georgia, "Times New Roman", serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  color: var(--text);
  font-family: var(--sans);
  background:
    radial-gradient(circle at 18% 10%, rgba(217, 178, 109, 0.18), transparent 28%),
    radial-gradient(circle at 82% 18%, rgba(125, 211, 252, 0.14), transparent 24%),
    radial-gradient(circle at 50% 100%, rgba(143, 182, 255, 0.12), transparent 32%),
    linear-gradient(180deg, #061019 0%, #091521 45%, #07111d 100%);
}

body {
  line-height: 1.55;
}

body.app-body {
  min-height: 100vh;
  overflow-x: hidden;
  overflow-y: auto;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 64px 64px;
  mask-image: linear-gradient(180deg, rgba(0, 0, 0, 0.45), rgba(0, 0, 0, 0));
}

a {
  color: var(--accent-2);
  text-decoration: none;
}

a:hover {
  color: #bfe8ff;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

input,
textarea,
select {
  width: 100%;
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.88rem 1rem;
  outline: none;
  transition: border-color 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

textarea {
  resize: vertical;
  min-height: 112px;
}

input:focus,
textarea:focus,
select:focus {
  border-color: rgba(217, 178, 109, 0.55);
  box-shadow: 0 0 0 3px rgba(217, 178, 109, 0.12);
  background: rgba(255, 255, 255, 0.06);
}

label {
  display: grid;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.94rem;
}

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

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 0.65rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text);
}

.brand-mark::before {
  content: "";
  width: 0.95rem;
  height: 0.95rem;
  border-radius: 999px;
  background:
    radial-gradient(circle at 30% 30%, #fff7d9 0%, #f7ce74 35%, #c88b36 100%);
  box-shadow: 0 0 0 5px rgba(217, 178, 109, 0.12);
}

.btn,
.chip,
.icon-btn,
.segmented-btn,
.thread-item {
  border: 1px solid transparent;
  transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease, box-shadow 0.16s ease;
}

.btn:hover,
.chip:hover,
.icon-btn:hover,
.segmented-btn:hover,
.thread-item:hover {
  transform: translateY(-1px);
}

.btn {
  border-radius: 999px;
  padding: 0.84rem 1rem;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  border-color: var(--border);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.03);
}

.btn-primary {
  background: linear-gradient(135deg, #d9b26d 0%, #f0d7a2 55%, #9fd6ff 150%);
  border-color: rgba(255, 255, 255, 0.12);
  color: #101010;
  font-weight: 800;
  box-shadow: 0 16px 32px rgba(217, 178, 109, 0.18);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.035);
}

.full {
  width: 100%;
}

.chip,
.segmented-btn,
.icon-btn {
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  border-color: var(--border);
  padding: 0.56rem 0.85rem;
}

.chip.is-active,
.segmented-btn.is-active {
  border-color: rgba(217, 178, 109, 0.6);
  background: rgba(217, 178, 109, 0.1);
  box-shadow: inset 0 0 0 1px rgba(217, 178, 109, 0.18);
}

.icon-btn {
  width: 2.35rem;
  height: 2.35rem;
  display: inline-grid;
  place-items: center;
  padding: 0;
}

.auth-body {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 1.25rem;
}

.auth-shell {
  width: min(100%, 540px);
}

.auth-card {
  position: relative;
  overflow: hidden;
  border-radius: 30px;
  padding: 2rem;
  background:
    linear-gradient(180deg, rgba(17, 28, 46, 0.96), rgba(10, 17, 28, 0.94));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow);
}

.auth-card::before {
  content: "";
  position: absolute;
  inset: -22% -10% auto auto;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(217, 178, 109, 0.24), transparent 68%);
  pointer-events: none;
}

.auth-card h1,
.workspace h1,
.workspace h2 {
  margin: 0;
  letter-spacing: -0.03em;
}

.auth-card h1,
.workspace h1 {
  font-family: var(--display);
  font-weight: 700;
}

.stack {
  display: grid;
  gap: 1rem;
  margin-top: 1.25rem;
}

.alert {
  padding: 0.9rem 1rem;
  border-radius: 14px;
  border: 1px solid var(--border);
  margin-top: 1rem;
}

.alert-error {
  background: rgba(255, 116, 137, 0.1);
  border-color: rgba(255, 116, 137, 0.24);
  color: #ffd4de;
}

.app-shell {
  position: relative;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
  background:
    linear-gradient(180deg, rgba(11, 18, 29, 0.98), rgba(8, 13, 22, 0.98));
  border-right: 1px solid rgba(255, 255, 255, 0.07);
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar::after {
  content: "";
  position: absolute;
  inset: auto 0 0 0;
  height: 160px;
  background: linear-gradient(180deg, transparent, rgba(217, 178, 109, 0.06));
  pointer-events: none;
}

.sidebar-brand,
.topbar,
.panel-head,
.composer-toolbar,
.message-header,
.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

.sidebar-brand {
  padding: 0.15rem 0.05rem 0.4rem;
}

.sidebar-toggle {
  display: none;
}

.filter-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.sidebar-section {
  display: grid;
  gap: 0.75rem;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.section-title,
.rail-title,
.eyebrow {
  color: #c9b794;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
}

.thread-list {
  display: grid;
  gap: 0.55rem;
  overflow: auto;
  min-height: 0;
  padding-right: 0.1rem;
}

.thread-item {
  text-align: left;
  padding: 0.92rem 0.95rem;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(255, 255, 255, 0.02));
  border-color: rgba(255, 255, 255, 0.06);
  display: grid;
  gap: 0.25rem;
  color: var(--text);
}

.thread-item.is-active {
  background: linear-gradient(180deg, rgba(217, 178, 109, 0.12), rgba(125, 211, 252, 0.06));
  border-color: rgba(217, 178, 109, 0.36);
  box-shadow: inset 0 0 0 1px rgba(217, 178, 109, 0.08);
}

.thread-title {
  font-weight: 700;
}

.thread-meta,
.message-status,
.status-pill,
.empty-note {
  color: var(--muted);
  font-size: 0.86rem;
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 0.75rem;
  position: relative;
  z-index: 1;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  background: linear-gradient(180deg, rgba(9, 16, 26, 0.96), rgba(9, 16, 26, 0.88));
  backdrop-filter: blur(18px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.sidebar-toggle-top {
  display: none;
  flex-shrink: 0;
}

.topbar h1 {
  font-size: clamp(1.5rem, 2vw, 2.15rem);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 0.6rem;
  min-width: 0;
}

.segmented {
  display: inline-flex;
  gap: 0.35rem;
  padding: 0.28rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
}

.segmented-btn {
  min-width: 4.8rem;
}

.select {
  width: auto;
  min-width: 8.5rem;
  padding: 0.66rem 0.9rem;
  border-radius: 999px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.56rem 0.9rem;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 332px;
  gap: 1rem;
  min-height: 0;
  padding: 1rem 1.2rem;
  align-items: start;
}

.messages-panel,
.rail-card {
  background: linear-gradient(180deg, rgba(17, 27, 43, 0.92), rgba(12, 20, 32, 0.9));
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow);
}

.messages-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

.panel-head {
  padding: 1.05rem 1.05rem 0.55rem;
}

.message-list {
  overflow: auto;
  min-height: 0;
  padding: 0.5rem 1.05rem 1.05rem;
  display: grid;
  gap: 0.9rem;
}

.empty-state {
  display: grid;
  place-items: center;
  text-align: center;
  min-height: 240px;
  padding: 2rem;
  color: var(--muted);
  border: 1px dashed rgba(255, 255, 255, 0.12);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.025);
}

.empty-state h2 {
  font-family: var(--display);
  font-size: 1.5rem;
  margin-bottom: 0.35rem;
}

.message-card {
  position: relative;
  padding: 1rem;
  border-radius: 22px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.025));
  overflow: hidden;
}

.message-card::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: linear-gradient(180deg, rgba(217, 178, 109, 0.8), rgba(125, 211, 252, 0.55));
}

.message-user {
  background: linear-gradient(180deg, rgba(217, 178, 109, 0.08), rgba(255, 255, 255, 0.03));
}

.message-assistant {
  background: linear-gradient(180deg, rgba(125, 211, 252, 0.08), rgba(255, 255, 255, 0.03));
}

.message-header {
  margin-bottom: 0.65rem;
}

.message-actions {
  position: relative;
}

.message-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.35rem);
  min-width: 220px;
  padding: 0.45rem;
  border-radius: 16px;
  background: #0b1320;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow);
  display: none;
  z-index: 20;
}

.message-menu.is-open {
  display: grid;
}

.message-menu button {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 0.72rem 0.75rem;
  border-radius: 11px;
}

.message-menu button:hover {
  background: rgba(255, 255, 255, 0.06);
}

.context-card {
  margin: 0 0 0.75rem;
  padding: 0.68rem 0.85rem;
  border-radius: 14px;
  border: 1px solid rgba(217, 178, 109, 0.22);
  background: rgba(217, 178, 109, 0.08);
}

.context-card summary {
  cursor: pointer;
  color: #f0d7a2;
  margin-bottom: 0.5rem;
}

.message-body {
  min-width: 0;
  overflow-wrap: anywhere;
  color: #f6f2ea;
}

.message-body p {
  margin: 0 0 0.75rem;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4,
.message-body h5,
.message-body h6 {
  margin: 1rem 0 0.55rem;
  font-family: var(--display);
}

.message-body ul,
.message-body ol {
  margin: 0.55rem 0 0.8rem 1.3rem;
}

.message-body blockquote {
  margin: 0.8rem 0;
  padding: 0.1rem 0 0.1rem 0.95rem;
  border-left: 3px solid rgba(217, 178, 109, 0.46);
  color: #f7ead0;
}

.message-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.8rem 0;
  display: block;
  overflow-x: auto;
}

.message-body th,
.message-body td {
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 0.55rem 0.7rem;
  text-align: left;
}

.message-body code {
  font-family: var(--mono);
  font-size: 0.92em;
  padding: 0.12rem 0.38rem;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.message-body pre {
  margin: 0.85rem 0;
  padding: 0.95rem;
  border-radius: 18px;
  background: #050a12;
  border: 1px solid rgba(255, 255, 255, 0.08);
  overflow: auto;
}

.message-body pre code {
  background: transparent;
  padding: 0;
  display: block;
  white-space: pre;
}

.code-block {
  margin: 0.85rem 0;
  border-radius: 18px;
  background: #050a12;
  border: 1px solid rgba(255, 255, 255, 0.08);
  overflow: hidden;
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.52rem 0.9rem;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  gap: 0.5rem;
}

.code-lang {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: lowercase;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.code-copy {
  font-size: 0.75rem;
  padding: 0.26rem 0.62rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.code-copy:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
}

.code-copy.copied {
  color: var(--success);
  border-color: rgba(99, 211, 157, 0.35);
}

.code-block pre {
  margin: 0;
  border-radius: 0;
  background: transparent;
  border: 0;
  padding: 0.95rem;
}

.app-toast {
  position: fixed;
  bottom: 5.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(0.6rem);
  background: rgba(10, 18, 30, 0.97);
  border: 1px solid rgba(255, 255, 255, 0.13);
  color: var(--text);
  padding: 0.7rem 1.2rem;
  border-radius: 999px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 200;
  white-space: nowrap;
  max-width: calc(100vw - 3rem);
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

.app-toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.right-rail {
  display: grid;
  gap: 1rem;
  align-content: start;
  min-width: 0;
}

.rail-card {
  padding: 1rem;
  display: grid;
  gap: 0.75rem;
}

.timeline {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.5rem;
}

.timeline li {
  padding: 0.55rem 0.75rem;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.07);
  color: var(--muted);
}

.timeline li.is-done {
  color: #dfffee;
  border-color: rgba(99, 211, 157, 0.25);
  background: rgba(99, 211, 157, 0.08);
}

.composer-wrap {
  position: sticky;
  bottom: 0;
  padding: 0 1.2rem 1.2rem;
  background: linear-gradient(180deg, rgba(7, 17, 29, 0), rgba(7, 17, 29, 0.8) 18%, rgba(7, 17, 29, 0.98));
}

.composer {
  padding: 1rem;
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(15, 23, 38, 0.96), rgba(10, 17, 28, 0.96));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow);
  display: grid;
  gap: 0.85rem;
}

.composer textarea {
  min-height: 130px;
  border-radius: 22px;
  padding: 1rem 1rem 1.05rem;
  font-size: 1rem;
}

.composer-toolbar {
  flex-wrap: wrap;
}

.composer-left,
.composer-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.composer-left {
  flex: 1 1 auto;
}

.composer-right {
  flex: 0 0 auto;
}

.empty-note {
  padding: 0.85rem 0.9rem;
  background: rgba(255, 255, 255, 0.035);
  border-radius: 12px;
}

/* ─── 1100px: hide right-rail, keep desktop sidebar ───────────── */
@media (max-width: 1100px) {
  .content-grid {
    grid-template-columns: minmax(0, 1fr);
    padding: 0.9rem 1rem;
  }

  .right-rail {
    display: none;
  }
}

/* ─── 960px: off-canvas sidebar, fixed-height layout ──────────── */
@media (max-width: 960px) {
  /* App shell: full width */
  .app-shell {
    grid-template-columns: 1fr;
  }

  /* Sidebar: off-canvas overlay */
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(88vw, var(--sidebar-w));
    height: 100%;
    transform: translateX(-104%);
    transition: transform 0.26s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 50;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .sidebar.is-open {
    transform: translateX(0);
    box-shadow: 6px 0 48px rgba(0, 0, 0, 0.55);
  }

  .sidebar-toggle,
  .sidebar-toggle-top {
    display: inline-grid;
  }

  /* Backdrop with blur */
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.58);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 49;
    -webkit-tap-highlight-color: transparent;
  }

  .sidebar-backdrop.is-visible {
    display: block;
  }

  /* Fixed-height app shell */
  body.app-body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .workspace {
    height: 100vh;
    height: 100dvh;
    min-height: unset;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* Topbar: static flex item, not sticky */
  .topbar {
    position: static;
    flex-shrink: 0;
    padding: 0.8rem 0.9rem 0.75rem;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .topbar-left {
    flex: 1 1 auto;
    min-width: 0;
  }

  /* Controls: single scrollable row */
  .topbar-actions {
    width: 100%;
    flex-wrap: nowrap;
    gap: 0.45rem;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 1px;
    justify-content: flex-start;
  }

  .topbar-actions::-webkit-scrollbar {
    display: none;
  }

  /* Controls wrapper: transparent at this size so children join the row */
  .topbar-controls {
    display: contents;
  }

  .topbar-actions .segmented,
  .topbar-actions .select,
  .topbar-actions .status-pill {
    flex-shrink: 0;
    width: auto;
    min-width: auto;
  }

  .topbar-actions .segmented-btn {
    flex: 0 0 auto;
    min-width: 4rem;
  }

  /* Content area: flex-fill */
  .content-grid {
    flex: 1 1 0;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    grid-template-columns: unset;
    padding: 0.7rem 0.75rem 0;
    gap: 0;
  }

  /* Messages panel: fill remaining space */
  .messages-panel {
    flex: 1 1 0;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .message-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 0.5rem 0.85rem 0.9rem;
    gap: 0.75rem;
  }

  .right-rail {
    display: none;
  }

  /* Composer: part of flex column, not sticky */
  .composer-wrap {
    position: static;
    flex-shrink: 0;
    padding: 0 0.75rem max(0.75rem, env(safe-area-inset-bottom, 0px));
  }

  .composer {
    padding: 0.85rem;
    gap: 0.7rem;
  }

  .composer textarea {
    min-height: 88px;
  }

  /* Toolbar: horizontal scroll instead of wrapping */
  .composer-toolbar {
    flex-wrap: nowrap;
    gap: 0.45rem;
  }

  .composer-left {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    gap: 0.45rem;
    flex: 1 1 0;
  }

  .composer-left::-webkit-scrollbar {
    display: none;
  }

  .composer-left .chip {
    flex-shrink: 0;
    width: auto;
  }

  .composer-right {
    flex-shrink: 0;
    flex-wrap: nowrap;
    gap: 0.45rem;
  }

  .composer-right .btn {
    flex-shrink: 0;
    width: auto;
  }
}

/* ─── 640px: phone — single-row topbar, minimal UI ────────────── */
@media (max-width: 640px) {
  /* Auth */
  .auth-body {
    padding: 0.85rem;
    align-items: flex-start;
    padding-top: max(1.2rem, env(safe-area-inset-top, 1.2rem));
  }

  .auth-card {
    padding: 1.4rem 1.2rem;
    border-radius: 22px;
  }

  /* Topbar: single compact row */
  .topbar {
    padding: 0.6rem 0.7rem;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.4rem;
  }

  .topbar-left {
    flex: 1 1 0;
    min-width: 0;
    overflow: hidden;
  }

  /* Thread title: one line, truncated */
  .topbar-left h1 {
    font-size: 0.96rem;
    font-family: var(--sans);
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: 0;
    line-height: 1.3;
  }

  /* Hide "Signed in as" sub-line */
  .eyebrow {
    display: none;
  }

  /* Actions: only provider switcher visible, no overflow */
  .topbar-actions {
    flex-shrink: 0;
    width: auto;
    overflow: visible;
    padding-bottom: 0;
  }

  /* Hide model/effort/approval/status on phone */
  .topbar-controls {
    display: none;
  }

  .topbar-actions .segmented-btn {
    min-width: 3.2rem;
    padding: 0.4rem 0.55rem;
    font-size: 0.82rem;
  }

  /* Compact content grid */
  .content-grid {
    padding: 0.5rem 0.5rem 0;
  }

  .panel-head {
    padding: 0.65rem 0.75rem 0.35rem;
  }

  .message-list {
    padding: 0.4rem 0.6rem 0.7rem;
    gap: 0.55rem;
  }

  /* Message cards */
  .messages-panel {
    border-radius: 16px;
  }

  .message-card {
    padding: 0.7rem 0.75rem 0.7rem 0.9rem;
    border-radius: 14px;
  }

  .message-card::before {
    width: 3px;
  }

  .message-body {
    font-size: 0.93rem;
    line-height: 1.6;
  }

  .message-body p {
    margin: 0 0 0.55rem;
  }

  .message-body code {
    font-size: 0.87em;
  }

  /* Code blocks */
  .code-block {
    border-radius: 12px;
  }

  .code-block pre {
    padding: 0.7rem 0.8rem;
    font-size: 0.85rem;
  }

  /* Message action menu: prevent off-screen */
  .message-menu {
    min-width: 170px;
    right: 0;
    left: auto;
  }

  .message-menu button {
    padding: 0.6rem 0.65rem;
    font-size: 0.88rem;
  }

  /* Composer: minimal */
  .composer-wrap {
    padding: 0 0.5rem max(0.5rem, env(safe-area-inset-bottom, 0px));
  }

  .composer {
    padding: 0.6rem 0.65rem;
    border-radius: 18px;
    gap: 0.5rem;
  }

  .composer textarea {
    min-height: 44px;
    max-height: 140px;
    padding: 0.6rem 0.75rem;
    font-size: 0.94rem;
    border-radius: 12px;
    resize: none;
  }

  /* Toolbar: scrollable single row */
  .composer-toolbar {
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 1px;
    gap: 0.35rem;
  }

  .composer-toolbar::-webkit-scrollbar {
    display: none;
  }

  .composer-left {
    flex: 0 0 auto;
    gap: 0.35rem;
  }

  .composer-right {
    flex: 0 0 auto;
    margin-left: auto;
    gap: 0.35rem;
  }

  .composer-left .chip {
    padding: 0.4rem 0.6rem;
    font-size: 0.82rem;
    white-space: nowrap;
  }

  .composer-right .btn {
    padding: 0.5rem 0.85rem;
    font-size: 0.88rem;
    width: auto;
  }

  /* Sidebar */
  .sidebar {
    width: min(90vw, 310px);
  }

  .thread-item {
    padding: 0.78rem 0.82rem;
  }
}

/* ─── 380px: small phones ─────────────────────────────────────── */
@media (max-width: 380px) {
  .topbar-left h1 {
    font-size: 0.88rem;
  }

  .topbar-actions .segmented-btn {
    min-width: 2.8rem;
    padding: 0.38rem 0.45rem;
    font-size: 0.78rem;
  }

  .content-grid {
    padding: 0.4rem 0.4rem 0;
  }

  .message-list {
    padding: 0.35rem 0.5rem 0.6rem;
    gap: 0.45rem;
  }

  .message-card {
    padding: 0.6rem 0.65rem 0.6rem 0.82rem;
    border-radius: 12px;
  }

  .composer {
    padding: 0.5rem 0.55rem;
    border-radius: 16px;
  }

  .composer textarea {
    min-height: 40px;
    max-height: 120px;
    padding: 0.52rem 0.65rem;
    font-size: 0.92rem;
    border-radius: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
