:root {
  --bg: #ffffff;
  --panel: #f6f7f9;
  --panel-2: #eceef2;
  --sidebar: #f1f3f6;
  --text: #1a1c20;
  --muted: #6b7280;
  --faint: #9aa2b1;
  --border: #dfe2e8;
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.12);
  --ok: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --user-bubble: #4f46e5;
  --assistant-bubble: #f0f1f4;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --panel: #171a21;
    --panel-2: #1e222c;
    --sidebar: #12141a;
    --text: #e7e9ee;
    --muted: #9aa2b1;
    --faint: #6b7280;
    --border: #262b36;
    --accent: #7c83ff;
    --accent-soft: rgba(124, 131, 255, 0.16);
    --ok: #34d399;
    --warn: #fbbf24;
    --danger: #f87171;
    --user-bubble: #4f46e5;
    --assistant-bubble: #1e222c;
  }
}
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14.5px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  height: 100dvh;
  overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
a { color: var(--accent); }

/* ---------- Login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 50; padding: 20px; }
.login-card { width: min(360px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 26px; display: flex; flex-direction: column; gap: 12px; }
.login-brand { font-size: 22px; font-weight: 800; }
.login-sub { margin: 0; color: var(--muted); font-size: 13px; }
.login-card input { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 11px 13px; }
.login-card button { background: var(--accent); color: #fff; border: none; border-radius: 10px; padding: 11px; font-weight: 700; }
.login-card form { display: flex; flex-direction: column; gap: 10px; }
.ms-btn { display: flex; align-items: center; justify-content: center; gap: 10px; background: #fff; color: #1a1a1a; border: 1px solid #d0d3d9; border-radius: 10px; padding: 11px; font-weight: 600; text-decoration: none; }
.ms-btn:hover { background: #f3f4f6; }
.linkbtn { background: none !important; border: none !important; color: var(--muted) !important; font-weight: 500 !important; font-size: 13px; padding: 2px !important; text-decoration: underline; }
.login-err { color: var(--danger); font-size: 13px; min-height: 16px; }

/* ---------- App shell ---------- */
.app { display: grid; grid-template-columns: 232px 1fr; height: 100dvh; }
.nav-toggle { display: none; position: fixed; top: 10px; left: 10px; z-index: 30; background: var(--panel); border: 1px solid var(--border); color: var(--text); border-radius: 9px; width: 38px; height: 38px; font-size: 18px; }

.sidebar { background: var(--sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 12px; gap: 6px; }
.side-brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 17px; padding: 6px 8px 14px; }
.side-brand .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px var(--accent-soft); }
.nav { display: flex; flex-direction: column; gap: 3px; }
.nav-item { display: flex; align-items: center; gap: 11px; background: transparent; border: none; color: var(--muted); border-radius: 10px; padding: 10px 11px; text-align: left; font-weight: 600; }
.nav-item:hover { background: var(--panel-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item .ico { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linejoin: round; stroke-linecap: round; }
.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 8px; }
.host-line { color: var(--faint); font-size: 11.5px; font-family: var(--mono); word-break: break-all; }
.logout { background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); border-radius: 9px; padding: 7px; font-size: 12.5px; }

.main { display: flex; flex-direction: column; min-width: 0; height: 100dvh; }
.main-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.main-head h1 { margin: 0; font-size: 17px; font-weight: 700; }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.view { flex: 1; overflow-y: auto; min-height: 0; }

/* ---------- Generic bits ---------- */
.btn { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 9px; padding: 7px 12px; font-size: 13px; font-weight: 600; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.sel { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.sel select { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; font-size: 13px; max-width: 44vw; }
.sel .modelinput { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 9px; font-size: 13px; min-width: 210px; max-width: 46vw; font-family: var(--mono); }
.sel .modelinput:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.chip { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.chip.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.chip.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.chip.accent { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.sdot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); display: inline-block; }
.sdot.ok { background: var(--ok); } .sdot.bad { background: var(--danger); } .sdot.warn { background: var(--warn); }
.muted { color: var(--muted); } .empty { color: var(--muted); text-align: center; padding: 40px; }

/* ---------- Chat ---------- */
.chat { display: flex; flex-direction: column; height: 100%; }
.thread { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { width: 100%; max-width: 820px; margin: 0 auto; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 85%; padding: 10px 14px; border-radius: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--assistant-bubble); border-bottom-left-radius: 4px; }
.msg.error .bubble { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.chat-empty { margin: auto; color: var(--muted); text-align: center; max-width: 440px; padding: 20px; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 12px; padding: 11px 14px; font: inherit; }
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- Status ---------- */
.pad { padding: 20px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.tile { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.tile .k { font-size: 12px; color: var(--muted); }
.tile .v { font-size: 22px; font-weight: 800; margin-top: 4px; }
.tile .v small { font-size: 13px; font-weight: 600; color: var(--muted); }
.bars { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
@media (max-width: 640px) { .bars { grid-template-columns: 1fr; } }
.meter { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.meter .row { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.track { height: 10px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.track > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.4s; }
.track > span.warn { background: var(--warn); } .track > span.bad { background: var(--danger); }
.svc { margin-top: 20px; display: flex; flex-direction: column; gap: 8px; }
.svc h2, .files h2, .projects h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); margin: 0 0 6px; }
.svc-row { display: flex; align-items: center; gap: 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 11px 14px; }
.svc-row .nm { font-weight: 700; } .svc-row .meta { margin-left: auto; color: var(--muted); font-size: 12.5px; font-family: var(--mono); }

/* ---------- Projects ---------- */
.projects { padding: 20px; }
.pcards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.pcard { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 15px; display: flex; flex-direction: column; gap: 10px; }
.pcard .top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.pcard .nm { font-weight: 800; font-size: 15px; }
.pcard .cmt { font-family: var(--mono); font-size: 11.5px; color: var(--muted); word-break: break-word; }
.pcard .acts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.pcard .acts .btn { padding: 6px 10px; font-size: 12px; }

.files { margin-top: 26px; }
.crumbs { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-family: var(--mono); font-size: 12.5px; margin-bottom: 10px; }
.crumbs a { cursor: pointer; text-decoration: none; }
.crumbs .sep { color: var(--faint); }
.filelist { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.frow { display: flex; align-items: center; gap: 10px; padding: 9px 14px; border-top: 1px solid var(--border); cursor: pointer; }
.frow:first-child { border-top: none; }
.frow:hover { background: var(--panel-2); }
.frow .fico { width: 16px; text-align: center; opacity: 0.8; }
.frow .fnm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.frow .fsz { color: var(--faint); font-size: 12px; font-family: var(--mono); }

/* ---------- Home launcher ---------- */
.applauncher { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 14px; }
.appcard { position: relative; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 22px 16px; text-align: center; cursor: pointer; transition: border-color 0.15s, transform 0.1s, box-shadow 0.15s; }
.appcard:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.12); }
.appicon { font-size: 42px; line-height: 1; margin-bottom: 10px; }
.appname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.appdesc { font-size: 12px; color: var(--muted); margin-top: 4px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.app-edit { position: absolute; top: 8px; right: 8px; background: var(--panel-2); border: 1px solid var(--border); color: var(--muted); border-radius: 7px; width: 26px; height: 26px; opacity: 0; transition: opacity 0.15s; }
.appcard:hover .app-edit { opacity: 1; }

/* ---------- Modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: grid; place-items: center; z-index: 60; padding: 16px; }
.modal { width: min(440px, 94vw); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 13px 16px; border-bottom: 1px solid var(--border); }
.modal-title { font-weight: 700; }
.modal-head .x { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; width: 30px; height: 30px; }
.modal-body { padding: 16px; }
.modal-body .fld { max-width: none; }

/* ---------- Tabbed settings ---------- */
.settings { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.tabbar { display: flex; gap: 4px; padding: 10px 16px 0; border-bottom: 1px solid var(--border); overflow-x: auto; flex: 0 0 auto; }
.tab { background: transparent; border: 1px solid transparent; border-bottom: none; color: var(--muted); border-radius: 10px 10px 0 0; padding: 8px 14px; font-weight: 600; font-size: 13px; white-space: nowrap; margin-bottom: -1px; }
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: var(--text); background: var(--panel); border-color: var(--border); border-bottom-color: var(--panel); }
.tab-add { color: var(--accent); }
.settings .pad { flex: 1; overflow-y: auto; min-height: 0; }

.kvlist { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; max-width: 560px; }
.kvrow { display: flex; gap: 8px; }
.kv-key, .kv-val { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 9px; padding: 8px 10px; font: inherit; }
.kv-key { flex: 0 0 40%; font-family: var(--mono); font-size: 13px; }
.kv-val { flex: 1; }
.kv-del { flex: 0 0 auto; padding: 8px 11px; }
.kv-add { margin-bottom: 12px; }

/* ---------- Settings ---------- */
.seth { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); margin: 20px 0 12px; }
.seth:first-child { margin-top: 0; }
.fld { display: flex; flex-direction: column; gap: 5px; max-width: 540px; margin-bottom: 15px; }
.flabel { font-size: 13px; font-weight: 600; }
.fld input { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 9px; padding: 9px 11px; font: inherit; }
.fld input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.fhint { font-size: 12px; color: var(--muted); }
.setstatus { margin-bottom: 16px; }
.setactions { display: flex; align-items: center; gap: 14px; margin-top: 6px; }
.setmsg { font-size: 13px; }
.setmsg.ok { color: var(--ok); }
.setmsg.err { color: var(--danger); }

/* ---------- Console / drawer ---------- */
.drawer { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: none; align-items: flex-end; z-index: 40; }
.drawer.open { display: flex; }
.drawer-inner { background: var(--panel); border-top: 1px solid var(--border); border-radius: 16px 16px 0 0; width: 100%; max-height: 80vh; display: flex; flex-direction: column; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.drawer-head .t { font-weight: 700; font-family: var(--mono); font-size: 13px; }
.drawer-head .x { margin-left: auto; background: var(--panel-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; width: 30px; height: 30px; }
.console { flex: 1; overflow: auto; margin: 0; padding: 14px 16px; font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; word-break: break-word; color: var(--text); background: var(--bg); }
.console .err { color: var(--danger); }
.console .dim { color: var(--muted); }
.spin { width: 15px; height: 15px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .nav-toggle { display: block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 232px; z-index: 25; transform: translateX(-100%); transition: transform 0.2s; box-shadow: 0 0 40px rgba(0,0,0,0.35); }
  .sidebar.open { transform: none; }
  .main-head { padding-left: 58px; }
}
