/* ULTRA — premium dark design tokens */
:root {
  --bg0: #0a0c11;
  --bg1: #10131b;
  --bg2: #161a25;
  --bg3: #1d2230;
  --line: #262c3d;
  --line-soft: #1e2331;
  --text: #e8ebf4;
  --text-dim: #9aa3b8;
  --text-faint: #626c84;
  --accent: #7c5cff;
  --accent2: #4f8cff;
  --grad: linear-gradient(135deg, #7c5cff, #4f8cff);
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --shadow: 0 8px 32px rgba(0,0,0,.45);
  --font: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, sans-serif;
  --mono: 'SF Mono', ui-monospace, Menlo, monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: var(--font); background: var(--bg0); color: var(--text); font-size: 14px; line-height: 1.55; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent2); }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

.hidden { display: none !important; }

/* ---------- buttons / inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px;
  background: var(--bg3); border: 1px solid var(--line); border-radius: var(--r-sm);
  color: var(--text); cursor: pointer; transition: all .15s; font-size: 13px; white-space: nowrap;
}
.btn:hover { background: #232a3d; border-color: #364060; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--grad); border: none; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.12); }
.btn.danger { color: var(--err); border-color: #4d2626; }
.btn.danger:hover { background: #2a1717; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { background: var(--bg2); color: var(--text); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.btn.icon { padding: 7px 9px; }

.inp, select.inp, textarea.inp {
  width: 100%; padding: 9px 12px; background: var(--bg1); border: 1px solid var(--line);
  border-radius: var(--r-sm); color: var(--text); transition: border .15s;
}
.inp:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124,92,255,.15); }
.inp::placeholder { color: var(--text-faint); }
label.lbl { display: block; font-size: 12px; color: var(--text-dim); margin: 10px 0 4px; }

.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 20px; font-size: 11px; background: var(--bg3); color: var(--text-dim); border: 1px solid var(--line); }
.badge.ok { color: var(--ok); border-color: #1d4436; }
.badge.err { color: var(--err); border-color: #4d2626; }
.badge.accent { color: #b7a5ff; border-color: #3b3070; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); display: inline-block; }
.dot.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; box-shadow: 0 0 0 0 rgba(52,211,153,.5);} 50% { opacity: .6; box-shadow: 0 0 0 5px rgba(52,211,153,0);} }

.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- login ---------- */
#view-login { display: flex; align-items: center; justify-content: center; height: 100vh; background: radial-gradient(1200px 700px at 70% -10%, #1a1440 0%, var(--bg0) 55%); }
.login-card { width: 380px; max-width: calc(100vw - 32px); background: var(--bg1); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 36px; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: .12em; font-size: 18px; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; background: var(--grad); display: flex; align-items: center; justify-content: center; font-size: 17px; letter-spacing: 0; }
.login-card h1 { font-size: 20px; margin: 22px 0 4px; }
.login-card .sub { color: var(--text-dim); font-size: 13px; margin-bottom: 18px; }
.form-err { color: var(--err); font-size: 12px; margin-top: 10px; min-height: 16px; }

/* ---------- app shell ---------- */
#view-app { display: flex; height: 100vh; }
.sidebar { width: 264px; min-width: 264px; background: var(--bg1); border-right: 1px solid var(--line-soft); display: flex; flex-direction: column; }
.sb-head { padding: 14px 14px 10px; display: flex; align-items: center; justify-content: space-between; }
.sb-actions { padding: 0 12px 10px; display: flex; flex-direction: column; gap: 8px; }
.sb-search { position: relative; }
.sb-search .inp { padding-left: 30px; background: var(--bg2); }
.sb-search::before { content: '⌕'; position: absolute; left: 10px; top: 7px; color: var(--text-faint); font-size: 15px; }
.sb-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 8px; }
.sb-sec { font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--text-faint); padding: 12px 8px 6px; text-transform: uppercase; display: flex; justify-content: space-between; align-items: center; }
.sb-item { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: var(--r-sm); cursor: pointer; color: var(--text-dim); transition: background .12s; font-size: 13px; }
.sb-item:hover { background: var(--bg2); color: var(--text); }
.sb-item.active { background: var(--bg3); color: var(--text); }
.sb-item .count { margin-left: auto; font-size: 11px; color: var(--text-faint); }
.sb-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.sb-foot { border-top: 1px solid var(--line-soft); padding: 8px; display: flex; gap: 4px; align-items: center; }
.sb-foot .btn { position: relative; }
.notif-badge { position: absolute; top: -3px; right: -3px; background: var(--err); color: #fff; font-size: 10px; min-width: 15px; height: 15px; border-radius: 8px; display: flex; align-items: center; justify-content: center; padding: 0 3px; }

/* ---------- main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 52px; border-bottom: 1px solid var(--line-soft); display: flex; align-items: center; gap: 14px; padding: 0 16px; background: var(--bg1); }
.crumbs { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-dim); min-width: 0; }
.crumbs b { color: var(--text); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.tb-meta { display: flex; align-items: center; gap: 10px; margin-left: auto; font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.tabs { display: flex; gap: 2px; }
.tab { padding: 6px 12px; border-radius: var(--r-sm); cursor: pointer; font-size: 13px; color: var(--text-dim); border: 1px solid transparent; }
.tab:hover { background: var(--bg2); color: var(--text); }
.tab.active { background: var(--bg3); color: var(--text); border-color: var(--line); }

.content { flex: 1; display: flex; min-height: 0; }
.chat-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* ---------- chat ---------- */
.msgs { flex: 1; overflow-y: auto; padding: 24px 0 12px; }
.msgs-inner { max-width: 820px; margin: 0 auto; padding: 0 20px; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; gap: 12px; }
.msg .ava { width: 30px; height: 30px; min-width: 30px; border-radius: 9px; display: flex; align-items: center; justify-content: center; font-size: 14px; background: var(--bg3); border: 1px solid var(--line); }
.msg.user .ava { background: var(--grad); border: none; }
.msg .body { min-width: 0; flex: 1; }
.msg .who { font-size: 12px; color: var(--text-faint); margin-bottom: 3px; display: flex; gap: 8px; align-items: baseline; }
.msg .md { font-size: 14px; }
.md p { margin: 0 0 10px; } .md p:last-child { margin: 0; }
.md h1, .md h2, .md h3 { margin: 14px 0 8px; line-height: 1.3; }
.md ul, .md ol { margin: 0 0 10px 20px; }
.md pre { background: var(--bg1); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 12px; overflow-x: auto; margin: 0 0 10px; font-family: var(--mono); font-size: 12.5px; }
.md code { font-family: var(--mono); font-size: .92em; background: var(--bg3); padding: 1px 5px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; margin: 0 0 10px; width: 100%; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; font-size: 13px; }
.md th { background: var(--bg2); }
.md blockquote { border-left: 3px solid var(--accent); padding-left: 12px; color: var(--text-dim); margin: 0 0 10px; }
.think-block { background: var(--bg1); border: 1px dashed var(--line); border-radius: var(--r-sm); margin-bottom: 8px; font-size: 12.5px; color: var(--text-dim); }
.think-block summary { cursor: pointer; padding: 8px 12px; color: var(--text-faint); user-select: none; }
.think-block .tb-body { padding: 0 12px 10px; white-space: pre-wrap; max-height: 240px; overflow-y: auto; }
.msg-empty { text-align: center; color: var(--text-faint); padding: 60px 20px; }
.msg-empty .big { font-size: 40px; margin-bottom: 12px; }
.msg-empty h2 { color: var(--text-dim); font-size: 18px; margin-bottom: 6px; }

/* composer */
.composer { padding: 10px 20px 16px; }
.composer-inner { max-width: 820px; margin: 0 auto; position: relative; }
.comp-box { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 10px 12px; transition: border .15s; }
.comp-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124,92,255,.12); }
.comp-box textarea { width: 100%; background: none; border: none; resize: none; outline: none; max-height: 200px; min-height: 24px; line-height: 1.5; }
.comp-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.comp-row select { background: var(--bg3); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 5px 8px; font-size: 12px; color: var(--text-dim); max-width: 210px; }
.comp-hints { display: flex; gap: 14px; justify-content: center; margin-top: 8px; font-size: 11px; color: var(--text-faint); }
.comp-hints kbd { background: var(--bg3); border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-family: var(--mono); font-size: 10px; }
.slash-menu { position: absolute; bottom: calc(100% + 8px); left: 0; width: 380px; max-height: 320px; overflow-y: auto; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-md); box-shadow: var(--shadow); z-index: 30; }
.slash-item { padding: 9px 14px; cursor: pointer; display: flex; gap: 10px; align-items: baseline; }
.slash-item:hover, .slash-item.sel { background: var(--bg3); }
.slash-item .cmd { font-family: var(--mono); font-size: 12.5px; color: #b7a5ff; white-space: nowrap; }
.slash-item .desc { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- right panel (tabs) ---------- */
.rpanel { width: 46%; min-width: 380px; max-width: 720px; border-left: 1px solid var(--line-soft); background: var(--bg1); display: flex; flex-direction: column; }
.rpanel.full { position: fixed; inset: 0; width: auto; max-width: none; z-index: 60; }
.rp-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line-soft); }
.rp-head b { font-size: 13px; }
.rp-body { flex: 1; overflow: auto; padding: 14px; }
.rp-body textarea.notes { width: 100%; height: 100%; min-height: 300px; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-md); padding: 14px; resize: none; outline: none; }

/* file tree */
.ftree { font-size: 13px; user-select: none; }
.fnode { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; cursor: pointer; color: var(--text-dim); }
.fnode:hover { background: var(--bg2); color: var(--text); }
.fnode.sel { background: var(--bg3); color: var(--text); }
.fnode .sz { margin-left: auto; font-size: 11px; color: var(--text-faint); }
.fkids { margin-left: 16px; border-left: 1px solid var(--line-soft); padding-left: 4px; }

/* viewer */
.viewer { position: fixed; inset: 0; background: rgba(6,8,12,.82); z-index: 80; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(4px); }
.viewer-card { width: min(1040px, 94vw); height: min(88vh, 900px); background: var(--bg1); border: 1px solid var(--line); border-radius: var(--r-lg); display: flex; flex-direction: column; box-shadow: var(--shadow); }
.viewer-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line-soft); }
.viewer-head b { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-body { flex: 1; overflow: auto; display: flex; flex-direction: column; align-items: center; padding: 16px; }
.viewer-body canvas { box-shadow: 0 4px 24px rgba(0,0,0,.5); margin-bottom: 12px; background: #fff; }
.viewer-body .docx-wrap { background: #fff; color: #111; padding: 0; border-radius: 4px; max-width: 100%; }
.viewer-body table.xlsx { border-collapse: collapse; background: var(--bg2); font-size: 13px; }
.viewer-body table.xlsx td, .viewer-body table.xlsx th { border: 1px solid var(--line); padding: 5px 10px; min-width: 60px; }
.viewer-body table.xlsx tr:first-child td { background: var(--bg3); font-weight: 600; }
.viewer-body textarea.txt { width: 100%; height: 100%; background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 14px; font-family: var(--mono); font-size: 12.5px; resize: none; outline: none; }
.viewer-body img { max-width: 100%; }

/* ---------- full views (dashboard/admin/settings/routines/agents) ---------- */
.page { flex: 1; overflow-y: auto; padding: 24px; }
.page-inner { max-width: 1080px; margin: 0 auto; }
.page h1 { font-size: 20px; margin-bottom: 4px; }
.page .psub { color: var(--text-dim); font-size: 13px; margin-bottom: 20px; }
.grid { display: grid; gap: 14px; }
.grid.c2 { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.grid.c4 { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-md); padding: 16px; }
.card h3 { font-size: 14px; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.kpi { font-size: 24px; font-weight: 700; }
.kpi-sub { font-size: 12px; color: var(--text-dim); }
.card .runline { font-size: 12px; color: var(--text-dim); padding: 6px 0; border-top: 1px solid var(--line-soft); }
.card .runline:first-of-type { border-top: none; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; color: var(--text-faint); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 9px 10px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
.tbl tr:hover td { background: rgba(255,255,255,.015); }

.seg { display: inline-flex; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-sm); overflow: hidden; margin-bottom: 18px; }
.seg button { padding: 7px 16px; background: none; border: none; cursor: pointer; color: var(--text-dim); font-size: 13px; }
.seg button.active { background: var(--bg3); color: var(--text); }

/* modal */
.modal-layer { position: fixed; inset: 0; background: rgba(6,8,12,.7); z-index: 70; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(3px); }
.modal { width: min(480px, 92vw); max-height: 86vh; overflow-y: auto; background: var(--bg1); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 22px; box-shadow: var(--shadow); }
.modal h2 { font-size: 16px; margin-bottom: 14px; }
.modal .mrow { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* toasts */
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--bg2); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: var(--r-sm); padding: 12px 16px; width: 320px; box-shadow: var(--shadow); animation: slidein .25s ease; font-size: 13px; }
.toast b { display: block; margin-bottom: 2px; font-size: 13px; }
.toast .t-body { color: var(--text-dim); font-size: 12px; }
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--err); }
@keyframes slidein { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }

/* notifications dropdown */
.notif-drop { position: fixed; left: 270px; bottom: 56px; width: 360px; max-height: 60vh; overflow-y: auto; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--r-md); box-shadow: var(--shadow); z-index: 90; }
.notif-item { padding: 10px 14px; border-bottom: 1px solid var(--line-soft); font-size: 13px; }
.notif-item .nb { color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.notif-item .nt { color: var(--text-faint); font-size: 11px; margin-top: 3px; }
.notif-item.unread { background: rgba(124,92,255,.06); }

/* mobile */
@media (max-width: 860px) {
  .sidebar { position: fixed; z-index: 50; height: 100%; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .rpanel { position: fixed; right: 0; top: 52px; bottom: 0; width: 100%; min-width: 0; max-width: none; z-index: 40; }
  .tabs { overflow-x: auto; }
  .burger { display: inline-flex !important; }
  .tb-meta .tokens { display: none; }
}
.burger { display: none; }

/* project actions (export/clone) + chat branching */
.sb-item .pact { display: none; margin-left: auto; gap: 2px; }
.sb-item:hover .pact { display: inline-flex; }
.sb-item:hover .count { display: none; }
.pbtn { background: none; border: 0; color: var(--text-dim); cursor: pointer; font-size: 13px; padding: 1px 5px; border-radius: 6px; transition: background .12s; }
.pbtn:hover { background: var(--bg3); color: var(--text); }
.mbranch { background: none; border: 0; color: var(--text-faint); cursor: pointer; font-size: 11px; padding: 0 6px; border-radius: 6px; opacity: 0; transition: opacity .15s, background .12s; }
.msg:hover .mbranch { opacity: 1; }
.mbranch:hover { color: var(--text); background: var(--bg3); }
