:root {
  --bg: #f6f7fb; --panel: #ffffff; --text: #1b1f27; --muted: #6b7280; --line: #e5e7eb;
  --accent: #1f6feb; --accent-text: #ffffff; --user-bubble: #dbe8ff; --asst-bubble: #ffffff;
  --danger: #d33; --ok: #1a7f37; --code-bg: #f1f3f7; --shadow: 0 1px 2px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218; --panel: #171b23; --text: #e6e8ee; --muted: #9aa3b2; --line: #2a303b;
    --accent: #3b82f6; --user-bubble: #1e3a6a; --asst-bubble: #1c212b; --code-bg: #0f1218; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--text); background: var(--bg); -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin-top: 10px; font-size: 14px; }
.ok { color: var(--ok); margin-top: 10px; font-size: 14px; }

/* ---------- auth screens ---------- */
.view.auth { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px; box-shadow: var(--shadow); }
.logo { font-weight: 700; font-size: 26px; letter-spacing: .3px; color: var(--accent); }
.card h2 { margin: 18px 0 6px; font-size: 20px; }
label { display: block; margin: 14px 0 6px; font-size: 14px; color: var(--muted); }
input[type=email], input[type=password], input[type=text], select, textarea {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
input.pin { letter-spacing: 10px; font-size: 24px; text-align: center; }
button.primary { background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 12px 18px; font-weight: 600; }
button.primary:disabled { opacity: .6; cursor: default; }
button.wide { width: 100%; margin-top: 16px; }
button.link { background: none; border: 0; color: var(--accent); padding: 6px; }
button.ghost { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 13px; color: var(--text); }
button.icon { background: none; border: 0; border-radius: 10px; width: 40px; height: 40px; font-size: 20px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; color: var(--text); }
button.icon:hover { background: var(--line); }
.who { font-size: 14px; color: var(--muted); margin-top: 4px; }

/* ---------- app layout ---------- */
.view.app { height: 100%; height: 100dvh; display: grid; grid-template-columns: 290px 1fr; }
#sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding: 12px; gap: 10px; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 0; }
.brand { font-weight: 700; color: var(--accent); font-size: 18px; }
#btn-close-sidebar { display: none; }
#btn-new-chat { margin-top: 0; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-item { display: flex; align-items: center; gap: 6px; padding: 10px 10px; border-radius: 10px; cursor: pointer; border: 0; background: none; text-align: left; width: 100%; color: var(--text); }
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: var(--user-bubble); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .d { font-size: 12px; color: var(--muted); }
.sidebar-foot { border-top: 1px solid var(--line); padding-top: 10px; }
.me { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-bottom: 8px; }
.foot-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.scrim { display: none; }

#main { display: flex; flex-direction: column; min-height: 0; height: 100%; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
#btn-open-sidebar { display: none; }
.chat-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 12px; scroll-behavior: smooth; }
.empty-hint { margin: auto; text-align: center; color: var(--muted); }
.empty-hint .big { font-size: 48px; }
.msg { max-width: min(780px, 92%); padding: 10px 14px; border-radius: 16px; box-shadow: var(--shadow); position: relative; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--asst-bubble); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.error { align-self: center; background: transparent; border: 1px dashed var(--danger); color: var(--danger); font-size: 14px; }
.msg .text { white-space: pre-wrap; }
.msg.assistant .text { white-space: normal; }
.msg .time { font-size: 11px; color: var(--muted); margin-top: 4px; text-align: right; }
.msg img.photo { display: block; max-width: 100%; max-height: 360px; border-radius: 10px; margin: 4px 0; cursor: zoom-in; }
.msg audio { display: block; width: 260px; max-width: 100%; margin: 4px 0; }
.msg .transcript { font-size: 14px; color: var(--muted); font-style: italic; margin: 2px 0 4px; }
.msg .transcript::before { content: "🎤 "; font-style: normal; }
.msg.pending .text::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }

/* markdown */
.md p { margin: 0 0 .7em; } .md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: .8em 0 .4em; line-height: 1.25; } .md h1 { font-size: 1.35em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .7em; padding-left: 1.4em; } .md li { margin: .15em 0; }
.md code { background: var(--code-bg); border-radius: 5px; padding: 1px 5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; }
.md pre { background: var(--code-bg); border-radius: 10px; padding: 10px 12px; overflow-x: auto; margin: 0 0 .7em; }
.md pre code { background: none; padding: 0; font-size: .85em; }
.md blockquote { margin: 0 0 .7em; padding: 2px 12px; border-left: 3px solid var(--accent); color: var(--muted); }
.md table { border-collapse: collapse; margin: 0 0 .7em; display: block; overflow-x: auto; max-width: 100%; }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; font-size: .95em; }
.md th { background: var(--code-bg); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: .8em 0; }
.md a { color: var(--accent); }

.status { padding: 4px 16px; font-size: 13px; color: var(--muted); }
.status::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 8px; animation: pulse 1.2s ease-in-out infinite; vertical-align: middle; }
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }

/* composer */
.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea { flex: 1; resize: none; max-height: 180px; padding: 10px 12px; border-radius: 12px; line-height: 1.35; }
button.send { color: var(--accent); }
button.send:disabled { opacity: .4; cursor: default; }
#btn-record.recording { background: var(--danger); color: #fff; }
.rec { font-size: 13px; color: var(--danger); padding: 6px 8px 0; }
.rec .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--danger); margin-right: 6px; animation: pulse 1s infinite; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 8px; }
.chip { position: relative; display: inline-flex; align-items: center; gap: 6px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 4px 28px 4px 4px; font-size: 13px; max-width: 220px; }
.chip img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .rm { position: absolute; right: 2px; top: 50%; transform: translateY(-50%); width: 24px; height: 24px; font-size: 14px; }

/* modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--panel); border-radius: 16px; padding: 18px 20px 22px; width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto; }
.modal-card.narrow { max-width: 380px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; } .modal-head h3 { margin: 0; }
.row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.row input { flex: 1 1 200px; } .row select { flex: 0 1 160px; }
.users { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.user-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; flex-wrap: wrap; }
.user-row .email { flex: 1 1 200px; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--code-bg); color: var(--muted); }
.badge.dev { background: var(--user-bubble); color: var(--text); }
.badge.warn { background: #fff3cd; color: #7a5a00; }
.badge.lock { background: #fde2e2; color: #8a1c1c; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); display: flex; align-items: center; justify-content: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 96vw; max-height: 96vh; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .view.app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 auto 0 0; height: 100dvh; width: min(85vw, 320px); z-index: 40; transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 30px rgba(0,0,0,.3); }
  #sidebar.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .scrim.show { opacity: 1; pointer-events: auto; }
  #btn-open-sidebar, #btn-close-sidebar { display: inline-flex; }
  .msg { max-width: 94%; }
  .messages { padding: 12px 8px; }
}

/* update center */
.upd-summary { margin-top: 10px; line-height: 1.5; }
.upd-summary .cur { color: var(--text); font-size: 15px; }
.upd-summary .line { display: block; }
.upd-summary .warn { color: #b26a00; }
.upd-summary .okc { color: var(--ok); }
.upd-h4 { margin: 18px 0 4px; }
.upd-progress { margin-top: 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--code-bg); font-size: 14px; }
.upd-progress.running::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 8px; animation: pulse 1.2s ease-in-out infinite; vertical-align: middle; }
.upd-progress .plog { margin: 6px 0 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); white-space: pre-wrap; max-height: 140px; overflow-y: auto; }
.versions { display: flex; flex-direction: column; gap: 6px; }
.ver-row { display: grid; grid-template-columns: 76px 1fr auto; gap: 4px 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; }
.ver-row.installed { background: var(--user-bubble); }
.ver-row code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.ver-row .subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ver-row .meta { grid-column: 1 / 3; font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ver-row .act { grid-column: 3; grid-row: 1 / 3; }
@media (max-width: 520px) { .ver-row { grid-template-columns: 1fr auto; } .ver-row code { display: none; } .ver-row .meta { grid-column: 1 / 2; } }

/* security */
.recovery-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 17px; letter-spacing: 1px; line-height: 1.6; text-align: center; padding: 12px; margin-top: 14px; border: 1px dashed var(--accent); border-radius: 10px; background: var(--code-bg); user-select: all; word-break: break-all; }
label.check { display: flex; align-items: center; gap: 8px; margin-top: 14px; color: var(--text); }
label.check input { width: auto; }
.dev-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; flex-wrap: wrap; }
.dev-row .lbl { flex: 1 1 160px; overflow: hidden; text-overflow: ellipsis; }
