/* Agent Chat — one token system, dark by default, light optional.
   Breakpoints: phone ≤699, compact 700–899, tablet 900–1199, desktop ≥1200. */

:root {
  color-scheme: dark;
  --bg: #0e0f12;
  --surface: #16181d;
  --surface-2: #1e2128;
  --surface-3: #272b34;
  --text: #f3f4f6;
  --text-secondary: #b4bac6;
  --text-tertiary: #8d93a1;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --accent: #6c8cff;
  --accent-press: #4f6ef7;
  --on-accent: #ffffff;
  --mine: #2a3a64;
  --theirs: #1e2128;
  --danger: #f07178;
  --warning: #e0a14a;
  --success: #3ecf8e;
  --focus: #b9cbff;
  --code-bg: #0c0d11;
  --code-inline: rgba(255, 255, 255, 0.1);
  --shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
  --meta-mine: #c5d0f0;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --font: "Segoe UI", system-ui, -apple-system, "PingFang SC", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Sarasa Mono SC", Menlo, Consolas, monospace;
  --sidebar: 320px;
  --hit: 40px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --surface-3: #e6e8ee;
  --text: #1a1c20;
  --text-secondary: #3e4553;
  --text-tertiary: #5c6472;
  --border: rgba(20, 24, 36, 0.12);
  --border-strong: rgba(20, 24, 36, 0.22);
  --accent: #3a4ec4;
  --accent-press: #2d3ea3;
  --on-accent: #ffffff;
  --mine: #e4e9fb;
  --theirs: #eef0f4;
  --danger: #b42334;
  --warning: #8a5a10;
  --success: #0f7a4a;
  --focus: #2d3ea3;
  --code-bg: #eef0f4;
  --code-inline: rgba(20, 24, 36, 0.08);
  --shadow: 0 16px 40px rgba(20, 24, 36, 0.12);
  --meta-mine: #3d4e7a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
button, input, textarea { font: inherit; color: inherit; touch-action: manipulation; }
button { cursor: pointer; }
input, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  width: 100%;
  font-size: 16px;
}
textarea { line-height: 1.45; }
input:focus, textarea:focus { border-color: var(--accent); outline: none; }
input:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.primary, .ghost, .icon-btn, .tab, .user-row, .swatch, .avatar-btn {
  min-height: var(--hit);
}
.primary {
  background: var(--accent-press);
  color: var(--on-accent);
  border: 0;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-weight: 650;
}
.primary:hover { background: var(--accent); }
.primary:disabled { opacity: 0.6; cursor: default; }
.ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}
.ghost:hover { color: var(--text); border-color: var(--accent); }
.icon-btn { width: var(--hit); padding: 0; }
.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.hint { color: var(--text-secondary); font-size: 13px; margin: 0; }
.error { color: var(--danger); font-size: 14px; margin: 4px 0; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Auth */
.auth-wrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  overflow: auto;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 22px 24px;
  margin: 12px 0;
}
.auth-card h1 { margin: 0 0 6px; font-size: 28px; letter-spacing: -0.03em; font-weight: 650; }
.tagline { color: var(--text-secondary); font-size: 15px; margin: 0 0 20px; }
.lang-row, .foot-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.lang-row { margin-bottom: 8px; }
.tabs { display: flex; gap: 4px; margin-bottom: 18px; padding: 3px; background: var(--surface-2); border-radius: 10px; }
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 8px;
  font-weight: 650;
}
.tab.active { color: var(--on-accent); background: var(--accent-press); }
#auth-form label, #profile-form label, .color-field, .segment {
  display: block;
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--text-secondary);
}
#auth-form input, #profile-form input, #profile-form textarea { margin-top: 6px; }
#auth-submit, #profile-save { width: 100%; margin-top: 4px; }
.count { display: block; margin-top: 4px; text-align: right; }

/* App shell */
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}
.sidebar {
  width: var(--sidebar);
  min-width: var(--sidebar);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head { padding: 12px 12px 4px; }
.me-card {
  display: flex;
  gap: 12px;
  align-items: center;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 8px;
  color: inherit;
  min-height: 64px;
}
.me-card:hover { background: var(--surface-2); }
.me-copy { min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.me-name, .name, #thread-peer {
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.me-name { font-size: 16px; }
.my-handle, .me-status, .sub { font-size: 13px; color: var(--text-secondary); }
.me-status, .sub {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.me-status[data-status="idle"], .sub[data-status="idle"] { color: var(--text-tertiary); }
.me-status[data-status="away"], .sub[data-status="away"], .thread-note.is-away { color: var(--warning); }
.me-status[data-status="busy"], .sub[data-status="busy"], .thread-note.is-busy { color: var(--danger); }
.sidebar-search, .admin-search { padding: 8px 12px 0; }
.admin-search { width: 100%; margin: 12px 0; }
.sidebar-label {
  padding: 14px 16px 6px;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
}
.label-row { display: flex; align-items: center; padding-right: 8px; }
.label-row .sidebar-label { flex: 1; }
.tiny { width: 32px; min-height: 32px; padding: 0; }
.group-list {
  list-style: none;
  margin: 0;
  padding: 0 8px;
  overflow-y: auto;
  flex: 0 1 auto;
  max-height: 34%;
}
.stale { padding: 0 16px 6px; }
.user-list { list-style: none; margin: 0; padding: 0 8px 8px; overflow-y: auto; flex: 1; min-height: 0; }
.user-list li { margin: 0 0 2px; }
.user-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: inherit;
  text-align: left;
}
.user-row:hover { background: var(--surface-2); }
.user-row.active { background: var(--surface-3); }
.user-row.unread .name { font-weight: 700; }
.user-row.unread .sub { color: var(--text); }
.who { min-width: 0; }
.name, .sub { display: block; }
.badge {
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  border-radius: 999px;
  min-width: 20px;
  text-align: center;
  padding: 2px 6px;
}
.list-note { padding: 12px; }
.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.nav-btn { width: 100%; text-align: left; }
.admin-nav { display: flex; flex-direction: column; gap: 8px; }
.admin-nav .sidebar-label { padding: 4px 4px 0; }
.foot-row { justify-content: flex-start; }
.foot-row .ghost { flex: 1; }

/* Avatars */
.avatar {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--av, var(--accent-press));
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 650;
  flex: none;
  overflow: visible;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; }
.avatar.lg { width: 72px; height: 72px; font-size: 24px; }
.avatar.group { border-radius: 12px; }
.avatar.group::after { display: none; }
.avatar::after {
  content: "";
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 2px var(--surface);
}
.avatar[data-status="idle"]::after { background: var(--text-tertiary); }
.avatar[data-status="away"]::after { background: var(--warning); }
.avatar[data-status="busy"]::after { background: var(--danger); }
.avatar.lg::after { width: 14px; height: 14px; }

/* Chat */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px 24px;
  text-align: center;
}
.empty-state p { max-width: 36rem; margin: 0; }
.thread { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-head {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 68px;
}
.thread-id { min-width: 0; flex: 1; }
.name-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
#thread-peer { font-size: 16px; }
.presence {
  flex: none;
  font-size: 12px;
  font-weight: 650;
  color: var(--success);
}
.presence[data-status="idle"] { color: var(--text-tertiary); }
.presence[data-status="away"] { color: var(--warning); }
.presence[data-status="busy"] { color: var(--danger); }
.presence::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 5px;
  border-radius: 50%;
  background: currentColor;
}
.thread-note {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.back-btn { display: none; flex: none; }
.thread-body { position: relative; flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 64px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}
.thread-empty, .pane-error {
  margin: auto;
  text-align: center;
  color: var(--text-secondary);
  max-width: 28rem;
}
.msg {
  max-width: min(78%, 38rem);
  padding: 10px 14px;
  border-radius: 16px;
  word-break: break-word;
}
.msg.theirs { background: var(--theirs); border: 1px solid var(--border); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.mine { background: var(--mine); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.unseen { box-shadow: inset 3px 0 0 var(--accent); }
.msg .meta { display: block; font-size: 12px; color: var(--text-tertiary); margin-top: 6px; }
.msg.mine .meta { color: var(--meta-mine); }
.msg-actions, .missed-actions { display: flex; flex-wrap: wrap; gap: 2px 12px; align-items: center; }
.msg-actions { margin-top: 2px; }
.text-btn {
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 2px 0;
  min-height: 32px;
  font-size: 13px;
  font-weight: 650;
}
.text-btn:disabled { color: var(--text-tertiary); cursor: default; }
.assist-result {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 14px;
}
.assist-result p { margin: 0; white-space: pre-wrap; }
.assist-result.is-error { color: var(--danger); }
.assist-label {
  display: block;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.03em;
  color: var(--text-tertiary);
  margin-bottom: 2px;
}
.missed-banner {
  align-self: stretch;
  margin: 4px 0 8px;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
}
.missed-line { display: flex; gap: 8px 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.missed-title { font-weight: 650; }
.jump-latest {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 2;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  min-height: 36px;
  font-size: 13px;
  font-weight: 650;
  box-shadow: var(--shadow);
}
.composer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  align-items: stretch;
  background: var(--surface);
}
.composer-row { display: flex; gap: 10px; align-items: flex-end; }
.composer textarea { resize: none; flex: 1; min-width: 0; min-height: 48px; max-height: 160px; }
.composer .primary { flex: none; min-width: 76px; }
.composer-preview { display: flex; align-items: flex-start; gap: 8px; }
.composer-preview img { width: 72px; height: 72px; object-fit: cover; border-radius: 8px; background: var(--surface-2); }
.msg-stack { display: flex; flex-direction: column; align-items: flex-start; align-self: flex-start; gap: 4px; max-width: min(78%, 38rem); }
.msg-stack .msg { max-width: 100%; }
.msg-from { font-size: 12px; font-weight: 650; color: var(--text-secondary); margin: 0 2px; }
.image-card { margin-bottom: 8px; }
.msg-image { display: block; max-width: 100%; max-height: 280px; border-radius: 10px; background: var(--surface-2); }
.image-extract { margin: 8px 0 0; font-size: 13px; white-space: pre-wrap; }
.image-extract-label { display: block; font-size: 11px; font-weight: 650; letter-spacing: 0.03em; color: var(--text-tertiary); margin-bottom: 2px; }
.image-ttl, .image-expired { margin: 6px 0 0; font-size: 12px; color: var(--text-tertiary); }
.picker { max-height: 220px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; }
.pick-row, .member-line { display: flex; gap: 10px; align-items: center; width: 100%; }
.member-line .who, .pick-row .who { flex: 1; min-width: 0; }
.group-actions { display: flex; gap: 8px; justify-content: space-between; align-items: center; }
.group-actions .primary { margin-left: auto; }

/* Profile */
.profile-photo { display: flex; gap: 14px; align-items: center; margin: 8px 0 16px; }
.avatar-btn, .swatch, .swatch-auto { border-radius: 999px; }
.avatar-btn { border: 0; background: transparent; padding: 0; width: 72px; height: 72px; }
.photo-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.color-field, .segment { border: 0; margin: 0 0 14px; padding: 0; min-inline-size: 0; }
.color-field legend, .segment legend { padding: 0; margin-bottom: 8px; }
.swatch-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 28px;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border: 2px solid transparent;
}
.swatch[aria-pressed="true"], .swatch-auto[aria-pressed="true"] {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}
.segment-row {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 10px;
}
.segment-row label {
  flex: 1;
  margin: 0;
  text-align: center;
  font-size: 14px;
  font-weight: 650;
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 8px 4px;
  cursor: pointer;
  min-height: var(--hit);
  display: flex;
  align-items: center;
  justify-content: center;
}
.segment-row input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segment-row label:has(input:checked) {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
}
.segment-row label:has(input:focus-visible) { outline: 2px solid var(--focus); }
#status-field.is-emphasis input { border-color: var(--warning); }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 9, 12, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  z-index: 10;
}
.modal {
  width: 100%;
  max-width: 520px;
  max-height: min(90dvh, 900px);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-wide { max-width: 760px; }
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 16px 16px 12px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; }
.backup-file { display: flex; flex-direction: column; gap: 6px; margin: 16px 0 8px; }
#backup-download { margin-top: 12px; }
#backup-restore { margin-top: 8px; }
.key-create { display: flex; gap: 8px; flex-wrap: wrap; margin: 14px 0; align-items: center; }
.key-create input { flex: 1 1 180px; width: auto; }
.new-key {
  background: var(--surface-2);
  border: 1px solid var(--success);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 16px;
}
.new-key code { display: block; word-break: break-all; margin: 8px 0; color: var(--success); font-size: 13px; font-family: var(--mono); }
.key-list { list-style: none; margin: 0; padding: 0; }
.key-list li {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.roster li { flex-direction: column; align-items: stretch; }
.roster-main { display: flex; gap: 10px; align-items: center; min-width: 0; }
.roster-copy { min-width: 0; }
.roster-copy .hint { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.key-list .revoked { color: var(--text-tertiary); text-decoration: line-through; }
.user-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.invite-message {
  margin: 10px 0;
  padding: 12px;
  max-height: 240px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--code-bg);
}

/* Markdown */
.msg-body > :first-child { margin-top: 0; }
.msg-body > :last-child { margin-bottom: 0; }
.msg-body .md-p { margin: 0 0 8px; }
.msg-body .md-p:last-child { margin-bottom: 0; }
.msg-body .md-head { font-weight: 650; margin: 10px 0 6px; line-height: 1.3; }
.msg-body pre {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--code-bg);
  overflow-x: auto;
}
.msg-body pre code { font-size: 13px; line-height: 1.45; white-space: pre; }
.msg-body code { font-family: var(--mono); }
.msg-body :not(pre) > code {
  background: var(--code-inline);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.92em;
}
.msg-body a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.msg-body ul, .msg-body ol { margin: 6px 0; padding-left: 20px; }
.msg-body li { margin: 2px 0; }
.msg-body blockquote {
  margin: 8px 0;
  padding-left: 10px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-secondary);
}
.msg-body hr { border: 0; border-top: 1px solid var(--border); margin: 10px 0; }
.msg-body table { border-collapse: collapse; margin: 8px 0; font-size: 13px; display: block; overflow-x: auto; }
.msg-body th, .msg-body td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.msg-body th { background: var(--surface-3); }
.msg-body .code-block { position: relative; margin: 8px 0; }
.msg-body .code-block pre { margin: 0; padding-right: 72px; }
.msg-body .code-copy {
  position: absolute;
  top: 6px;
  right: 6px;
  min-height: 28px;
  padding: 3px 8px;
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.msg-body .code-copy.copied { color: var(--success); border-color: var(--success); }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(92vw, 420px);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  z-index: 20;
  box-shadow: var(--shadow);
}

/* Phone: one task at a time */
@media (max-width: 699px) {
  :root { --sidebar: 100%; --hit: 44px; }
  .auth-card { padding: 20px 16px; border-radius: var(--radius-md); }
  .app { flex-direction: column; }
  .sidebar { width: 100%; min-width: 0; flex: 1; border-right: 0; }
  .chat { flex: 1; }
  .app:not(.thread-open) .chat { display: none; }
  .app.thread-open .sidebar { display: none; }
  .app.thread-open .back-btn { display: inline-flex; align-items: center; }
  .thread-head { padding-left: 8px; }
  .messages { padding: 12px 12px 64px; }
  .text-btn, .jump-latest { min-height: 44px; }
  .msg, .msg-stack { max-width: 92%; }
  .composer { padding: 10px 12px max(10px, env(safe-area-inset-bottom)); }
  .admin-nav { flex-direction: row; flex-wrap: wrap; }
  .admin-nav .sidebar-label { flex: 1 0 100%; }
  .admin-nav .nav-btn { width: auto; flex: 1 1 calc(50% - 8px); }
  .user-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .user-actions .danger, .user-actions .key-create { grid-column: 1 / -1; }
  .modal-backdrop { padding: 0; align-items: stretch; }
  .modal, .modal-wide {
    max-width: none;
    max-height: none;
    height: 100%;
    border: 0;
    border-radius: 0;
  }
  .modal-head { padding-top: max(16px, env(safe-area-inset-top)); }
  .modal-body { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  #profile-save {
    position: sticky;
    bottom: 0;
    z-index: 1;
    margin-top: 8px;
    box-shadow: 0 -12px 16px var(--surface);
  }
  .key-list li { flex-wrap: wrap; }
  .thread-note { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
}

/* Tablet, including portrait tablets and large-phone landscape */
@media (min-width: 700px) and (max-width: 1199px) {
  :root { --sidebar: 280px; }
  .auth-card { padding: 28px 24px; }
  .messages { padding: 18px 20px; }
  .msg { max-width: min(86%, 34rem); }
  .foot-row .ghost { font-size: 13px; padding: 8px 6px; }
}

/* Desktop */
@media (min-width: 1200px) {
  :root { --sidebar: 340px; }
  .auth-card { padding: 36px 32px; }
  .messages { padding: 28px 6vw 24px; }
  .thread-head, .composer { padding-left: 28px; padding-right: 28px; }
}

/* Short landscape: keep chrome reachable */
@media (max-height: 520px) {
  .auth-card { padding: 14px 16px; }
  .auth-card h1 { font-size: 22px; }
  .tagline { margin-bottom: 10px; }
  .sidebar-head { padding-top: 6px; }
  .thread-head { min-height: 52px; padding-top: 6px; padding-bottom: 6px; }
  .avatar.lg { width: 56px; height: 56px; }
  .empty-state { padding: 12px; }
}

@media (pointer: coarse) {
  :root { --hit: 44px; }
  .user-row { min-height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* Connect an agent panel.
   The closed control follows the app theme. The open list is forced light:
   on Windows the native menu stays white, and color-scheme: dark was painting
   the option text white on top of it. */
select {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  color-scheme: light;
}
select option,
select optgroup {
  background: #ffffff;
  color: #1a1c20;
}
select:focus { outline: 2px solid var(--accent); border-color: transparent; }
.agent-tool { display: block; margin: 0 0 14px; font-size: 13px; color: var(--text-secondary); }
.agent-tool select { margin-top: 6px; }
#agent-prompt { max-height: 44vh; }

/* Info tooltip ("?" next to a section title) */
.info-tip {
  width: 18px;
  height: 18px;
  min-width: 18px;
  padding: 0;
  margin-left: 8px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: middle;
}
.info-tip:hover, .info-tip:focus-visible { color: var(--text); border-color: var(--accent); }
.modal-head h2 { display: flex; align-items: center; gap: 0; }
.tooltip {
  position: fixed;
  z-index: 999;
  max-width: min(320px, 80vw);
  padding: 8px 10px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 12px;
  line-height: 1.5;
  pointer-events: none;
}
.docs-link { display: inline-block; margin: -2px 0 12px; text-decoration: underline; text-underline-offset: 2px; }

/* Sidebar docs link (an anchor styled like the nav buttons) */
.sidebar-foot a.nav-btn { display: block; text-decoration: none; line-height: 1.4; }
