:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1a1d21; --muted: #5f6672; --border: #e2e5ea;
  --accent: #29b5e8; --accent-dark: #1d8fb8; --user: #e8f6fc; --error: #b42318; --error-bg: #fdecea;
  --ok: #1a7f37; --ok-bg: #e6f4ea; --warn: #9a6700; --warn-bg: #fff8c5; --code: #f1f3f5; --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1216; --panel: #171b21; --text: #e7eaee; --muted: #9aa3ad; --border: #2a3039; --user: #143647;
    --error-bg: #3a1a17; --error: #ff8a80; --ok-bg: #12301c; --ok: #6fdc8c; --warn-bg: #3a2f0b; --warn: #f2cc60; --code: #0f1216; }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.5; }
.app { display: flex; flex-direction: column; height: 100%; max-width: 900px; margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); background: var(--panel); position: sticky; top: 0; z-index: 2; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 17px; margin: 0; }
.brand p { margin: 0; font-size: 13px; }
.logo { font-size: 26px; }
.actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--bg); color: var(--muted); white-space: nowrap; }
.pill.ok { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.pill.bad { color: var(--error); background: var(--error-bg); border-color: transparent; }
.btn { font: inherit; border-radius: 10px; padding: 8px 14px; cursor: pointer; border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.btn.ghost:hover { background: var(--bg); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #062b3a; font-weight: 600; }
.btn.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); color: white; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.chat { flex: 1; overflow-y: auto; padding: 22px 18px 12px; display: flex; flex-direction: column; gap: 16px; }
.welcome { margin: 4vh auto 0; max-width: 640px; text-align: center; }
.welcome h2 { font-size: 22px; margin: 0 0 8px; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 16px 0; }
.chip { font: inherit; font-size: 13px; padding: 8px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--panel); color: var(--text); cursor: pointer; text-align: left; }
.chip:hover { border-color: var(--accent); }
.msg { display: flex; flex-direction: column; gap: 6px; max-width: 92%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.bubble { padding: 12px 16px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--border); overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); border-color: transparent; white-space: pre-wrap; }
.bubble p { margin: 0 0 10px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 6px 0 10px; padding-left: 22px; }
.bubble h1, .bubble h2, .bubble h3 { font-size: 15px; margin: 10px 0 6px; }
.bubble code { font-family: var(--mono); font-size: 13px; background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble pre { background: var(--code); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 10px; text-align: left; }
.bubble th { background: var(--bg); }
.status { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; padding-left: 4px; }
.status::before { content: ""; width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--accent); border-top-color: transparent; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error { padding: 10px 14px; border-radius: 10px; background: var(--error-bg); color: var(--error); font-size: 14px; }
.meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 12px; color: var(--muted); padding-left: 4px; }
.badge { padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
details.queries { font-size: 12px; }
details.queries summary { cursor: pointer; color: var(--muted); }
details.queries pre { margin: 6px 0; font-size: 12px; white-space: pre-wrap; background: var(--code); padding: 8px 10px; border-radius: 8px; }
details.queries .q { margin: 6px 0 10px; }
details.queries .q .qmeta { color: var(--muted); }
details.queries .q.bad .qmeta { color: var(--error); }
.composer { padding: 10px 18px 14px; border-top: 1px solid var(--border); background: var(--panel); }
.composer form { display: flex; gap: 10px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; font: inherit; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); color: var(--text); max-height: 160px; }
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
#footnote { margin: 8px 0 0; }
@media (max-width: 600px) { .brand p { display: none; } .msg { max-width: 100%; } .topbar { padding: 10px 12px; } }

/* ---- v2: layout with sidebar, workflow timeline, settings, memory drawer ---- */
.layout { display: flex; height: 100%; }
.sidebar { width: 260px; flex-shrink: 0; border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; }
.sidebar[hidden] { display: none; }
.sidebar-head { padding: 12px; }
.side-label { margin: 4px 12px; }
.chat-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
.chat-list li { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-radius: 10px; cursor: pointer; font-size: 13px; }
.chat-list li:hover { background: var(--bg); }
.chat-list li.active { background: var(--user); }
.chat-list li .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list li .del { opacity: 0; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 14px; }
.chat-list li:hover .del { opacity: 1; }
.sidebar-foot { padding: 10px 12px; border-top: 1px solid var(--border); }
.app { max-width: none; flex: 1; min-width: 0; }
.wide { width: 100%; }
.small-btn { font-size: 12px; padding: 6px 10px; }
.btn.icon { padding: 6px 9px; }
.actions .btn { font-size: 13px; }
.timeline { margin: 4px 0 0; padding: 0; list-style: none; font-size: 12px; color: var(--muted); }
.timeline li { display: flex; flex-direction: column; gap: 2px; padding: 4px 0; border-left: 2px solid var(--border); padding-left: 10px; margin-left: 6px; }
.timeline li.running { border-left-color: var(--accent); }
.timeline li.done { border-left-color: var(--ok); }
.timeline li.error { border-left-color: var(--error); }
.timeline .head { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.timeline .head .name { color: var(--text); font-weight: 600; }
.timeline .head .dur { margin-left: auto; }
.timeline .body { display: none; }
.timeline li.open .body { display: block; }
.timeline pre { margin: 4px 0; white-space: pre-wrap; background: var(--code); padding: 6px 8px; border-radius: 8px; max-height: 260px; overflow: auto; font-size: 11.5px; }
.timeline .tag { padding: 1px 6px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); }
.drawer { position: fixed; top: 0; right: 0; height: 100%; width: min(440px, 100%); background: var(--panel); border-left: 1px solid var(--border); z-index: 20; display: flex; flex-direction: column; box-shadow: -8px 0 24px rgba(0,0,0,.15); }
.drawer[hidden] { display: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.drawer-body { padding: 12px 16px; overflow-y: auto; font-size: 13px; }
.drawer-body h4 { margin: 12px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.drawer-body table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.drawer-body td { border-bottom: 1px solid var(--border); padding: 4px 6px; vertical-align: top; }
.drawer-body td:first-child { color: var(--muted); white-space: nowrap; }
.drawer-body pre { white-space: pre-wrap; background: var(--code); padding: 6px 8px; border-radius: 8px; font-size: 11.5px; max-height: 200px; overflow: auto; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; width: min(560px, 100%); }
.settings-form { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.settings-form input, .settings-form select { font: inherit; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.settings-form .row { display: flex; align-items: center; gap: 10px; }
.spacer { flex: 1; }
.pill.warn { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
@media (max-width: 800px) { .sidebar { position: fixed; z-index: 15; height: 100%; } }
.settings-form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.btn.on { border-color: var(--accent); color: var(--accent-dark); }
.timeline .section { margin-top: 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.timeline .section.input { color: var(--warn); }
.timeline pre.input { border-left: 3px solid var(--warn); max-height: 420px; }

/* ---- auth pages & admin portal ---- */
.auth-page { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card { width: min(440px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 22px 22px 18px; display: flex; flex-direction: column; gap: 14px; }
.auth-card .brand h1 { font-size: 20px; }
.auth-alt { display: flex; flex-direction: column; gap: 8px; align-items: stretch; text-align: center; }
.auth-alt summary { cursor: pointer; }
.btn.wide { display: block; text-align: center; text-decoration: none; }
.admin-page { max-width: 1200px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
.admin-main { padding: 16px 18px 40px; display: flex; flex-direction: column; gap: 28px; }
.admin-main section h2 { font-size: 18px; margin: 0 0 10px; }
.admin-main h3 { font-size: 14px; margin: 14px 0 6px; }
.admin-main h4 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 10px 0 4px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.tile { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.tile-v { font-size: 22px; font-weight: 700; }
.tile-l { font-size: 12px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 800px) { .grid2 { grid-template-columns: 1fr; } }
.table-wrap { overflow-x: auto; }
.admin-main table { border-collapse: collapse; width: 100%; font-size: 13px; background: var(--panel); }
.admin-main th, .admin-main td { border-bottom: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: middle; }
.admin-main th { color: var(--muted); font-weight: 600; font-size: 12px; }
.row.wrap { flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.row.wrap input, .row.wrap select { font: inherit; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.btn.danger { color: var(--error); }
.token-box { background: var(--ok-bg); color: var(--ok); padding: 10px 12px; border-radius: 10px; font-size: 13px; overflow-wrap: anywhere; }
.admin-main select { font: inherit; padding: 4px 6px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.sidebar-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.actions a.btn { text-decoration: none; }
.btn.google { background: #fff; color: #1f1f1f; border-color: #dadce0; }
.btn.google:hover { background: #f3f4f6; color: #1f1f1f; border-color: #c6c9ce; }
.btn.disabled { opacity: .5; cursor: not-allowed; }

/* static documents (privacy, terms) */
body.doc { padding: 24px 16px; }
.doc-body { max-width: 760px; margin: 0 auto; line-height: 1.55; }
.doc-body h1 { margin-top: 0; }
