:root {
  --bg: #ffffff;
  --bg-side: #f7f7f7;
  --bg-soft: #f3f3f3;
  --bg-hover: #ececec;
  --bg-active: #e6e6e6;
  --text: #0d0d0d;
  --muted: #6b6b6b;
  --faint: #9a9a9a;
  --border: #e3e3e3;
  --border-strong: #d0d0d0;
  --user-bubble: #f1f1f1;
  --accent: #0d0d0d;
  --accent-text: #ffffff;
  --think-on: #2563eb;
  --think-on-bg: #eaf1ff;
  --code-bg: #f6f6f6;
  --ok: #16a34a;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.06);
  --radius: 22px;
  --font: ui-sans-serif, -apple-system, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171717;
    --bg-side: #111111;
    --bg-soft: #232323;
    --bg-hover: #222222;
    --bg-active: #2b2b2b;
    --text: #ececec;
    --muted: #a3a3a3;
    --faint: #737373;
    --border: #2c2c2c;
    --border-strong: #3a3a3a;
    --user-bubble: #262626;
    --accent: #ececec;
    --accent-text: #111111;
    --think-on: #7aa5ff;
    --think-on-bg: #1c2840;
    --code-bg: #1f1f1f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.6 var(--font); color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 10px; background: transparent; color: var(--muted); }
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

/* ---------- login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); padding: 24px; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; align-items: stretch; text-align: center; }
.login-logo { width: 56px; height: 56px; margin: 0 auto; }
.login-card h1 { margin: 0 0 12px; font-size: 22px; font-weight: 600; letter-spacing: -0.01em; }
.login-card input { padding: 12px 16px; border: 1px solid var(--border-strong); border-radius: 14px; background: var(--bg); outline: none; }
.login-card input:focus { border-color: var(--text); }
.login-card button { padding: 12px; border: 0; border-radius: 14px; background: var(--accent); color: var(--accent-text); font-weight: 500; }
.login-error { min-height: 1.4em; margin: 0; color: var(--bad); font-size: 13px; }

/* ---------- layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar { width: 264px; flex: none; display: flex; flex-direction: column; background: var(--bg-side); border-right: 1px solid var(--border); }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.only-mobile { display: none; }
.scrim { display: none; }

.sidebar-top { display: flex; align-items: center; gap: 6px; padding: 12px 10px 6px; }
.new-chat { flex: 1; display: flex; align-items: center; gap: 8px; padding: 9px 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); font-weight: 500; font-size: 14px; }
.new-chat:hover { background: var(--bg-hover); }
.search { margin: 4px 10px 8px; padding: 8px 12px; border: 1px solid transparent; border-radius: 10px; background: var(--bg-soft); outline: none; font-size: 13.5px; }
.search:focus { border-color: var(--border-strong); background: var(--bg); }

.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-group { margin: 14px 8px 4px; font-size: 11.5px; font-weight: 600; color: var(--faint); text-transform: uppercase; letter-spacing: .04em; }
.chat-item { display: flex; align-items: center; gap: 4px; padding: 7px 8px 7px 10px; border-radius: 10px; cursor: pointer; font-size: 14px; color: var(--text); }
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); }
.chat-item .t { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .t input { width: 100%; border: 1px solid var(--border-strong); border-radius: 6px; padding: 1px 6px; background: var(--bg); outline: none; }
.chat-item .act { display: none; width: 24px; height: 24px; border: 0; background: transparent; border-radius: 6px; color: var(--muted); place-items: center; }
.chat-item:hover .act, .chat-item.active .act { display: inline-grid; }
.chat-item .act:hover { background: var(--bg-active); color: var(--text); }
.chat-item .spin { width: 8px; height: 8px; border-radius: 50%; background: var(--think-on); animation: pulse 1s infinite ease-in-out; }

.sidebar-bottom { display: flex; align-items: center; gap: 6px; padding: 10px; border-top: 1px solid var(--border); }
.worker-status { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.worker-status .label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.worker-status .dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--faint); }
.worker-status.on .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.worker-status.off .dot { background: var(--bad); }
.worker-status.busy .dot { background: var(--think-on); animation: pulse 1.2s infinite ease-in-out; }
.sidebar-actions { display: flex; gap: 2px; }

/* ---------- top bar ---------- */
.topbar { height: 52px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 14px; }
.chat-title { flex: 1; min-width: 0; text-align: center; font-size: 14px; font-weight: 500; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- messages ---------- */
.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; scroll-behavior: auto; }
.messages { max-width: 780px; margin: 0 auto; padding: 8px 24px 32px; }
.empty { height: 100%; display: grid; place-items: center; padding-bottom: 8vh; }
.empty-logo { width: 64px; height: 64px; opacity: .9; }
.has-messages .empty { display: none; }

.msg { margin: 0 0 26px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { max-width: 82%; padding: 10px 16px; border-radius: 20px; background: var(--user-bubble); white-space: pre-wrap; word-wrap: break-word; }
.msg.user .edit-box { width: 100%; }
.msg.user .edit-box textarea { width: 100%; min-height: 80px; padding: 12px 14px; border: 1px solid var(--border-strong); border-radius: 16px; background: var(--bg); outline: none; resize: vertical; }
.msg.user .edit-box .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.msg-actions { display: flex; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions, .msg:focus-within .msg-actions, .msg.last .msg-actions { opacity: 1; }
.msg-actions button { display: inline-grid; place-items: center; height: 28px; min-width: 28px; padding: 0 6px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: 12px; }
.msg-actions button:hover { background: var(--bg-hover); color: var(--text); }
.msg-actions .stats { align-self: center; min-width: 0; padding: 0 6px; font-size: 12px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.thinking { margin: 0 0 12px; }
.thinking summary { list-style: none; display: inline-flex; align-items: center; gap: 6px; cursor: pointer; color: var(--muted); font-size: 13.5px; user-select: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary .chev { transition: transform .15s; }
.thinking[open] summary .chev { transform: rotate(90deg); }
.thinking.live summary .lbl { background: linear-gradient(90deg, var(--muted) 30%, var(--text) 50%, var(--muted) 70%); background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: shimmer 1.6s linear infinite; }
.thinking .think-body { margin-top: 8px; padding: 2px 0 2px 14px; border-left: 2px solid var(--border); color: var(--muted); font-size: 14px; white-space: pre-wrap; max-height: 360px; overflow-y: auto; }

.content { word-wrap: break-word; }
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content p { margin: 0 0 12px; }
.content h1, .content h2, .content h3, .content h4 { margin: 20px 0 8px; line-height: 1.3; font-weight: 600; }
.content h1 { font-size: 1.45em; } .content h2 { font-size: 1.25em; } .content h3 { font-size: 1.1em; }
.content ul, .content ol { margin: 0 0 12px; padding-left: 24px; }
.content li { margin: 3px 0; }
.content a { color: var(--think-on); }
.content blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--muted); }
.content code { font-family: var(--mono); font-size: .88em; padding: 1px 5px; border-radius: 5px; background: var(--code-bg); }
.content pre { position: relative; margin: 0 0 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--code-bg); overflow: hidden; }
.content pre code { display: block; padding: 14px 16px; overflow-x: auto; background: none; font-size: 13px; line-height: 1.55; }
.content pre .code-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--border); font: 12px var(--font); color: var(--muted); }
.content pre .code-head button { border: 0; background: transparent; color: var(--muted); padding: 2px 8px; border-radius: 6px; font-size: 12px; }
.content pre .code-head button:hover { background: var(--bg-hover); color: var(--text); }
.content .table-wrap { width: fit-content; max-width: 100%; margin: 0 0 14px; overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: 10px; }
.content table { border-collapse: collapse; width: max-content; }
.content th, .content td { border-bottom: 1px solid var(--border); padding: 7px 12px; text-align: left; vertical-align: top; max-width: 22em; }
.content tr:last-child td { border-bottom: 0; }
.content th + th, .content td + td { border-left: 1px solid var(--border); }
.content th { background: var(--bg-soft); font-weight: 600; }
.content hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
.content img { max-width: 100%; border-radius: 10px; }

.status-line { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
.cursor::after { content: ""; display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%; background: var(--text); vertical-align: middle; animation: pulse 1s infinite ease-in-out; }
.error-box { padding: 10px 14px; border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent); border-radius: 12px; background: color-mix(in srgb, var(--bad) 7%, transparent); color: var(--bad); font-size: 14px; }

.to-bottom { position: absolute; left: 50%; bottom: 150px; transform: translateX(-50%); width: 34px; height: 34px; display: grid; place-items: center; border: 1px solid var(--border); border-radius: 50%; background: var(--bg); color: var(--text); box-shadow: var(--shadow); }

/* ---------- composer ---------- */
.composer { width: calc(100% - 32px); max-width: 780px; margin: 0 auto; padding: 10px 10px 8px 16px; border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--bg); box-shadow: var(--shadow); }
.composer:focus-within { border-color: color-mix(in srgb, var(--text) 35%, var(--border-strong)); }
.composer textarea { display: block; width: 100%; max-height: 40vh; min-height: 26px; padding: 4px 4px 2px 0; border: 0; outline: none; background: transparent; resize: none; line-height: 1.5; }
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.composer-left, .composer-right { display: flex; align-items: center; gap: 6px; min-width: 0; }
.pill { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border: 1px solid var(--border); border-radius: 999px; background: transparent; color: var(--muted); font-size: 13px; }
.pill:hover { background: var(--bg-hover); }
.pill.on { border-color: transparent; background: var(--think-on-bg); color: var(--think-on); }
.pill[disabled] { opacity: .45; cursor: not-allowed; }
.model-picker select { max-width: 280px; height: 32px; padding: 0 28px 0 12px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; appearance: none; text-overflow: ellipsis;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'%3E%3Cpath d='M6 9l6 6 6-6' stroke='%23888' stroke-width='2.4' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; }
.model-picker select:hover { background-color: var(--bg-hover); color: var(--text); }
.model-picker select option { color: var(--text); background: var(--bg); }
.send { display: grid; place-items: center; width: 34px; height: 34px; flex: none; border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-text); }
.send:disabled { opacity: .3; cursor: default; }
.send .i-stop { display: none; }
.busy .send .i-send { display: none; }
.busy .send .i-stop { display: block; }
.footnote { margin: 6px 0 10px; text-align: center; font-size: 11.5px; color: var(--faint); }

/* ---------- settings ---------- */
.settings { width: min(520px, calc(100% - 32px)); padding: 0; border: 1px solid var(--border); border-radius: 18px; background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
.settings::backdrop { background: rgba(0,0,0,.35); }
.settings form { display: flex; flex-direction: column; gap: 14px; padding: 22px; }
.settings h2 { margin: 0; font-size: 18px; font-weight: 600; }
.settings label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--muted); }
.settings textarea, .settings input, .settings select { padding: 9px 12px; border: 1px solid var(--border-strong); border-radius: 10px; background: var(--bg); color: var(--text); outline: none; font-weight: 400; font-size: 14px; }
.settings textarea { resize: vertical; }
.settings .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.settings .hint { margin: -6px 0 0; font-size: 12px; color: var(--faint); }
.settings .danger { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.settings .actions { display: flex; justify-content: flex-end; gap: 8px; }
.ghost, .primary { padding: 8px 14px; border-radius: 10px; font-size: 13.5px; font-weight: 500; }
.ghost { border: 1px solid var(--border); background: transparent; color: var(--text); }
.ghost:hover { background: var(--bg-hover); }
.ghost.file { flex-direction: row; display: inline-flex; color: var(--text); cursor: pointer; }
.ghost.red { color: var(--bad); }
.primary { border: 0; background: var(--accent); color: var(--accent-text); }

@keyframes pulse { 0%, 100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* ---------- mobile ---------- */
/* app height follows the visible viewport (keyboard open/closed) — set from JS as --app-h */
.app { height: var(--app-h, 100dvh); }
.topbar { padding-top: env(safe-area-inset-top); height: calc(52px + env(safe-area-inset-top)); padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
.sidebar-top { padding-top: calc(12px + env(safe-area-inset-top)); }
.sidebar-bottom { padding-bottom: max(10px, env(safe-area-inset-bottom)); }

@media (max-width: 820px), (max-height: 500px) and (pointer: coarse) {
  .only-mobile { display: inline-grid; }
  .sidebar { position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: 84%; max-width: 320px; transform: translateX(-100%); transition: transform .22s ease; padding-left: env(safe-area-inset-left); }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .sidebar.dragging { transition: none; }
  .scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .22s; display: block; }
  .sidebar.open + .scrim { opacity: 1; pointer-events: auto; }

  /* bigger touch targets in the sidebar */
  .chat-item { padding: 10px 8px 10px 12px; font-size: 15px; }
  .chat-item .act { width: 32px; height: 32px; }
  .icon-btn { width: 40px; height: 40px; }

  .topbar { border-bottom: 1px solid transparent; transition: border-color .15s; }
  .scrolled .topbar { border-bottom-color: var(--border); }
  .chat-title { font-size: 15px; color: var(--text); }

  .messages { padding: 8px max(14px, env(safe-area-inset-right)) 20px max(14px, env(safe-area-inset-left)); }
  .msg { margin-bottom: 20px; }
  .msg.user .bubble { max-width: 88%; padding: 9px 14px; border-radius: 18px; }
  .content { font-size: 15.5px; }
  .content pre code { font-size: 12.5px; padding: 12px; }
  .content ul, .content ol { padding-left: 20px; }
  .thinking .think-body { max-height: 40vh; }

  /* assistant actions always shown (no hover on touch); user actions only for the tapped bubble */
  .msg-actions { opacity: 1; }
  .msg.user .msg-actions { display: none; }
  .msg.user.show-actions .msg-actions { display: flex; }
  .msg-actions button { height: 34px; min-width: 34px; }

  .composer { width: calc(100% - 16px - env(safe-area-inset-left) - env(safe-area-inset-right)); max-width: 780px; margin: 0 auto; padding: 8px 8px 8px 14px; border-radius: 20px; }
  .composer textarea { font-size: 16px; max-height: 30vh; } /* 16px: iOS doesn't zoom on focus */
  .composer-bar { margin-top: 4px; }
  .pill { height: 32px; padding: 0 10px; }
  .model-picker select { max-width: 46vw; font-size: 13px; }
  .send { width: 36px; height: 36px; }
  .footnote { display: none; }
  .main { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .keyboard .main { padding-bottom: 6px; }
  .to-bottom { bottom: calc(110px + env(safe-area-inset-bottom)); }

  .search, .login-card input, .settings textarea, .settings input, .settings select, .msg.user .edit-box textarea, .chat-item .t input { font-size: 16px; }
  .empty-logo { width: 52px; height: 52px; }

  /* settings as a bottom sheet */
  .settings { width: 100%; max-width: none; max-height: 92dvh; margin: auto 0 0; border-radius: 18px 18px 0 0; border-bottom: 0; }
  .settings form { padding: 20px 18px max(18px, env(safe-area-inset-bottom)); overflow-y: auto; max-height: 92dvh; }
}
@media (max-width: 380px) {
  .pill span { display: none; }                 /* Think → icon only */
  .pill { width: 34px; padding: 0; justify-content: center; }
  .model-picker select { max-width: 58vw; }
  .settings .row { grid-template-columns: 1fr; }
}
/* phone landscape: every pixel of height counts */
@media (max-height: 500px) and (max-width: 1000px) {
  .topbar { height: calc(42px + env(safe-area-inset-top)); }
  .composer textarea { max-height: 22vh; }
  .composer-bar { margin-top: 2px; }
  .pill, .model-picker select { height: 28px; }
  .send { width: 32px; height: 32px; }
  .msg { margin-bottom: 14px; }
}
@media (hover: none) {
  .icon-btn:hover, .pill:hover, .new-chat:hover, .msg-actions button:hover { background: transparent; }
  .chat-item:hover:not(.active) { background: transparent; }
  .chat-item:active, .icon-btn:active, .msg-actions button:active, .pill:active { background: var(--bg-hover); }
  .pill.on:hover, .pill.on:active { background: var(--think-on-bg); }
}
