:root {
  --bg: #f7f7f5; --panel: #ffffff; --ink: #1c1c1c; --muted: #6b6b6b; --line: #e4e4e0;
  --side: #0f2a28; --side-ink: #d9e6e4; --side-muted: #8aa6a2; --side-hover: #173f3b;
  --accent: #0f766e; --accent-ink: #ffffff; --accent-soft: #e6f4f2; --error: #b91c1c; --error-bg: #fef2f2;
  --user-bubble: #e8efee; --radius: 12px; --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); color: var(--ink); background: var(--bg); font-size: 15px; line-height: 1.5; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--error); background: var(--error-bg); border: 1px solid #fca5a5; padding: .5rem .7rem; border-radius: 8px; margin: .3rem 0; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; }

/* ---------- screens (login etc.) ---------- */
.screen { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 2rem; width: 100%; max-width: 400px; display: grid; gap: .8rem; box-shadow: 0 10px 30px rgba(0,0,0,.06); }
.card h2 { margin: 0; font-size: 1.2rem; }
.card label { display: grid; gap: .3rem; font-size: 13px; color: var(--muted); }
.card input { padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; color: var(--ink); }
.card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.brand { display: flex; align-items: center; gap: .6rem; }
.logo { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 9px; background: var(--accent); color: #fff; font-weight: 700; }
.brand-name { font-weight: 700; font-size: 1.15rem; letter-spacing: .2px; }
.primary { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 9px; padding: .65rem 1rem; font-weight: 600; }
.primary:disabled { opacity: .6; cursor: default; }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; }
.ghost { background: transparent; color: var(--side-ink); border: 1px solid #2b4f4b; border-radius: 8px; padding: .4rem .7rem; }
.ghost:hover { background: var(--side-hover); }

/* ---------- app layout ---------- */
.app { display: grid; grid-template-columns: 270px 1fr; height: 100vh; height: 100dvh; }
#sidebar { background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; min-height: 0; }
.side-top { padding: 1rem 1rem .6rem; display: grid; gap: .8rem; }
.side-top .brand-name { color: #fff; }
.newchat { background: #fff; color: var(--side); border: 0; border-radius: 9px; padding: .55rem .8rem; font-weight: 600; text-align: left; }
.newchat:hover { background: #e9f1ef; }
.conv-list { flex: 1; overflow-y: auto; padding: .2rem .6rem; display: flex; flex-direction: column; gap: 2px; }
.conv { display: flex; align-items: center; gap: .4rem; border-radius: 8px; padding: .45rem .6rem; color: var(--side-ink); cursor: pointer; font-size: 14px; }
.conv:hover, .conv.active { background: var(--side-hover); }
.conv .title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv .del { opacity: 0; background: none; border: 0; color: var(--side-muted); padding: 0 .2rem; }
.conv:hover .del { opacity: 1; }
.side-bottom { padding: .8rem 1rem 1rem; border-top: 1px solid #1f3f3b; display: grid; gap: .7rem; }
.quota-row { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--side-muted); }
.bar { height: 6px; background: #1f3f3b; border-radius: 4px; overflow: hidden; margin-top: 4px; }
.bar > div { height: 100%; width: 0; background: #34d399; transition: width .3s; }
.bar > div.warn { background: #fbbf24; } .bar > div.over { background: #f87171; }
.me-name { font-weight: 600; color: #fff; }
.me-role { font-size: 12.5px; color: var(--side-muted); }
.side-actions { display: flex; gap: .5rem; }

#main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.topbar { display: flex; align-items: center; gap: .8rem; padding: .6rem 1rem; border-bottom: 1px solid var(--line); background: var(--panel); }
.topbar-title { font-weight: 600; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.icon { background: none; border: 0; font-size: 1.2rem; display: none; }

/* ---------- chat ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 1.5rem 1rem 1rem; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; padding-top: 10vh; }
.empty h1 { font-size: 1.6rem; margin-bottom: .3rem; }
.suggest { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; margin-top: 1.2rem; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: .45rem .9rem; font-size: 13.5px; color: var(--ink); }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.msg { display: flex; gap: .8rem; margin: 0 auto 1.2rem; }
.msg.user { justify-content: flex-end; }
.msg .bubble { max-width: 100%; }
.msg.user .bubble { background: var(--user-bubble); border-radius: 16px 16px 4px 16px; padding: .7rem 1rem; white-space: pre-wrap; max-width: 85%; }
.msg.assistant .bubble { padding: .2rem 0; width: 100%; }
.msg .avatar { flex: 0 0 30px; width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 13px; margin-top: 4px; }
.tools { display: flex; flex-wrap: wrap; gap: .4rem; margin: .3rem 0 .6rem; }
.tool { font-size: 12px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: .15rem .6rem; display: inline-flex; align-items: center; gap: .3rem; }
.tool.running::before { content: ""; width: 8px; height: 8px; border-radius: 50%; border: 2px solid var(--accent); border-top-color: transparent; animation: spin .8s linear infinite; }
.tool.failed { background: var(--error-bg); color: var(--error); }
@keyframes spin { to { transform: rotate(360deg); } }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.md p { margin: .4rem 0 .8rem; } .md p:first-child { margin-top: 0; }
.md h1, .md h2, .md h3 { margin: 1rem 0 .4rem; font-size: 1.05rem; }
.md ul, .md ol { padding-left: 1.4rem; }
.md pre { background: #0f172a; color: #e2e8f0; padding: .8rem 1rem; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.md code { background: #eef0ee; padding: .1rem .3rem; border-radius: 4px; font-size: 13px; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; margin: .6rem 0; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: .35rem .6rem; }
.md th { background: #f0f2f0; }
.md a { color: var(--accent); }
.md blockquote { border-left: 3px solid var(--line); margin: .5rem 0; padding-left: .8rem; color: var(--muted); }
.meta { font-size: 12px; color: var(--muted); margin-top: .3rem; }
.note { background: var(--error-bg); border: 1px solid #fca5a5; color: var(--error); border-radius: 10px; padding: .6rem .9rem; margin: 0 auto 1rem; }

.composer { padding: .6rem 1rem 1rem; background: linear-gradient(transparent, var(--bg) 30%); }
.composer-box { max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: .5rem; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: .5rem .5rem .5rem .9rem; box-shadow: 0 4px 16px rgba(0,0,0,.05); }
.composer-box:focus-within { border-color: var(--accent); }
#input { flex: 1; border: 0; resize: none; outline: none; max-height: 200px; padding: .4rem 0; background: transparent; color: var(--ink); }
.send { width: 38px; height: 38px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; font-size: 16px; }
.send:disabled { opacity: .4; cursor: default; }
.composer-hint { text-align: center; margin-top: .5rem; }

/* ---------- admin ---------- */
.admin { flex: 1; overflow-y: auto; padding: 1.2rem 1.5rem 3rem; }
.admin h2 { margin: .4rem 0 .6rem; font-size: 1.15rem; }
.admin h3 { margin: 1.4rem 0 .5rem; font-size: 1rem; }
.admin-tabs { display: flex; gap: .3rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; }
.tab { background: none; border: 0; padding: .6rem .9rem; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.table { width: 100%; border-collapse: collapse; background: var(--panel); font-size: 13.5px; }
.table th, .table td { text-align: left; padding: .45rem .6rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { background: #f0f2f0; font-weight: 600; }
.table select, .table input { padding: .25rem .4rem; border: 1px solid var(--line); border-radius: 6px; }
.table input.cap { width: 80px; }
.table .mini { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: .25rem .55rem; font-size: 12.5px; margin-right: .2rem; }
.table .mini:hover { border-color: var(--accent); color: var(--accent); }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: 12px; background: #dcfce7; color: #166534; }
.badge.off { background: #fee2e2; color: #991b1b; }
.inline-form { display: flex; flex-wrap: wrap; gap: .5rem; }
.inline-form input, .inline-form select { padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; }
.reveal { margin-top: .8rem; padding: .8rem 1rem; background: #fff8e1; border: 1px solid #f1d27a; border-radius: 10px; font-family: ui-monospace, monospace; }
.settings { display: grid; gap: 1rem; max-width: 720px; }
.settings fieldset { border: 1px solid var(--line); border-radius: 10px; padding: .8rem 1rem; display: grid; gap: .6rem; background: var(--panel); }
.settings legend { padding: 0 .4rem; color: var(--muted); font-size: 13px; }
.settings label { display: grid; gap: .25rem; font-size: 13px; color: var(--muted); }
.settings input, .settings select { padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 8px; color: var(--ink); }
.toast { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); background: #1c1c1c; color: #fff; padding: .6rem 1rem; border-radius: 10px; font-size: 14px; z-index: 50; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 25% 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  #sidebar.open { transform: none; }
  .icon { display: inline-block; }
  .messages { padding: 1rem .7rem; }
  .msg.user .bubble { max-width: 95%; }
  .admin { padding: 1rem .8rem 3rem; }
  .table { font-size: 12.5px; display: block; overflow-x: auto; }
}
