:root {
  --bg: #f4f5f9;
  --panel: #ffffff;
  --panel-2: #f8f9fc;
  --border: #e3e6ef;
  --text: #1c2033;
  --muted: #6b7288;
  --accent: #5b5bd6;
  --accent-soft: #ececfd;
  --ok: #138a52;
  --ok-soft: #e3f6ec;
  --warn: #b26a00;
  --warn-soft: #fdf1dc;
  --bad: #c9303f;
  --bad-soft: #fde8ea;
  --side: #161a2b;
  --side-text: #c4c8da;
  --side-active: #262b45;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1220;
    --panel: #171b2d;
    --panel-2: #1c2136;
    --border: #2a3050;
    --text: #e4e7f2;
    --muted: #9399b3;
    --accent: #8b8bf5;
    --accent-soft: #262a4d;
    --ok: #45c486;
    --ok-soft: #173326;
    --warn: #f0b04a;
    --warn-soft: #3a2c13;
    --bad: #f0707c;
    --bad-soft: #3d1a20;
    --side: #0b0e19;
    --side-active: #1d2238;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------- layout: top bar + context sidebar + content ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; grid-template-rows: 52px 1fr; min-height: 100vh; }
.topbar {
  grid-column: 1 / -1; position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 10px; padding: 0 14px;
  background: var(--side); color: #fff; border-bottom: 1px solid #262b45;
}
.topbar .crumb { display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar .sep { color: #4b5175; font-size: 18px; }
.topbar .crumb-title { color: var(--side-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar select { width: auto; max-width: 260px; background: var(--side-active); color: #fff; border: 1px solid #333a5c; padding: 5px 8px; }
#menu-toggle { display: none; color: #fff; font-size: 20px; padding: 2px 8px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: #fff; text-decoration: none; }
.brand-logo { width: 28px; height: 28px; border-radius: 7px; background: #6366f1; display: grid; place-items: center; font-size: 14px; color: #fff; }
.sidebar {
  background: var(--side); color: var(--side-text);
  padding: 12px; display: flex; flex-direction: column; gap: 2px;
  position: sticky; top: 52px; height: calc(100vh - 52px); overflow-y: auto;
}
.side-head { padding: 4px 8px 10px; border-bottom: 1px solid #262b45; margin-bottom: 4px; }
.side-head .back { color: #8f96b8; text-decoration: none; font-size: 12.5px; }
.side-head .back:hover { color: #fff; }
.side-title { color: #fff; font-weight: 700; font-size: 15px; margin-top: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-sub { color: #8f96b8; font-size: 12px; font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-section { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: #6c7393; padding: 12px 10px 4px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  color: var(--side-text); text-decoration: none;
  padding: 7px 10px; border-radius: 7px;
}
.nav a:hover { background: var(--side-active); color: #fff; }
.nav a.active { background: var(--side-active); color: #fff; font-weight: 600; }
.nav .ico { width: 18px; text-align: center; opacity: .85; }

/* account menu */
.usermenu { position: relative; }
.usermenu > button { background: transparent; border: 1px solid transparent; color: #fff; padding: 4px 8px; }
.usermenu > button:hover { border-color: #333a5c; }
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 12px; text-transform: uppercase; }
.usermenu .who { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.15; }
.usermenu .who small { color: #8f96b8; font-size: 11px; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; z-index: 45;
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,.25); padding: 6px;
}
.menu a { display: flex; gap: 10px; padding: 8px 10px; border-radius: 7px; color: var(--text); text-decoration: none; }
.menu a:hover { background: var(--panel-2); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }
.menu .menu-head { padding: 6px 10px 8px; font-size: 12px; color: var(--muted); }

/* permissions matrix */
.matrix th.perm, .matrix td.perm { text-align: center; white-space: nowrap; }
.matrix th.perm { font-size: 11.5px; }
.matrix td.perm input { width: 16px; height: 16px; }
.matrix tr.dirty td { background: var(--warn-soft); }
.main { padding: 24px 28px 48px; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  #menu-toggle { display: inline-flex; }
  .brand-name, .usermenu .who { display: none; }
  .topbar select { max-width: 150px; }
  .sidebar { display: none; z-index: 39; }
  .layout.menu-open .sidebar { display: flex; }
  .layout.menu-open .main { display: none; }
  .main { padding: 16px; }
  .page-head h1 { font-size: 19px; }
  .modal-backdrop { padding: 8px; place-items: end center; }
  .modal { max-height: 94vh; }
  .terminal-box { height: calc(100vh - 190px); }
  td.actions { white-space: normal; }
  td.actions button, td.actions .btn { margin: 2px 0; }
  /* 16px inputs stop iOS Safari from zooming in on focus */
  input, select, textarea { font-size: 16px; }
}

/* ---------- components ---------- */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-head h2 { margin: 0; }
.stat .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 24px; font-weight: 700; margin: 4px 0; }
.bar { height: 8px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .4s; }
.bar.warn > span { background: var(--warn); }
.bar.bad > span { background: var(--bad); }

button, .btn {
  font: inherit; cursor: pointer;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 6px 12px; border-radius: 7px; display: inline-flex; align-items: center; gap: 6px;
  text-decoration: none; white-space: nowrap;
}
button:hover, .btn:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { color: var(--bad); }
.btn-danger:hover { border-color: var(--bad); }
.btn-sm { padding: 3px 8px; font-size: 12.5px; }
.btn-ghost { border-color: transparent; background: transparent; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 7px; padding: 7px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
input[type=checkbox], input[type=radio] { width: auto; }
textarea { font-family: var(--mono); font-size: 12.5px; resize: vertical; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-weight: 600; font-size: 12.5px; margin-bottom: 4px; }
label.field > small { display: block; color: var(--muted); margin-top: 3px; font-size: 12px; }
label.check { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--panel-2); position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--panel-2); }
td.actions { text-align: right; white-space: nowrap; }
.table-wrap { overflow: auto; max-height: 70vh; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.notice { padding: 10px 14px; border-radius: 8px; background: var(--accent-soft); margin-bottom: 16px; }
.notice.warn { background: var(--warn-soft); }
.notice.bad { background: var(--bad-soft); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 8px 14px; color: var(--muted); }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 24px; }

pre.log {
  background: #0b0e19; color: #d6dbef; padding: 12px 14px; border-radius: 8px;
  max-height: 60vh; overflow: auto; white-space: pre-wrap; word-break: break-word; margin: 0;
}

.app-card { display: flex; flex-direction: column; gap: 8px; }
.app-card .desc { color: var(--muted); font-size: 13px; flex: 1; }
.app-card .top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }

.breadcrumb { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; font-family: var(--mono); font-size: 13px; }
.breadcrumb a { text-decoration: none; padding: 2px 4px; border-radius: 4px; }
.breadcrumb a:hover { background: var(--accent-soft); }
.file-name { cursor: pointer; display: inline-flex; align-items: center; gap: 8px; }
.file-name:hover { color: var(--accent); }
.drop-hint { outline: 2px dashed var(--accent); outline-offset: -6px; }

/* ---------- modal & toasts ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(8, 10, 20, .55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; width: min(560px, 100%); max-height: 92vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.modal.wide { width: min(1000px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 18px; overflow: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: 420px; }
.toast { background: var(--panel); border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 8px; padding: 10px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.18); white-space: pre-wrap; }
.toast.error { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--ok); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: min(380px, 100%); }
.login .brand { color: var(--text); justify-content: center; font-size: 22px; padding-bottom: 20px; }

/* ---------- terminal ---------- */
.terminal-box { background: #0b0e19; border-radius: var(--radius); padding: 8px; height: calc(100vh - 150px); min-height: 320px; }
.terminal-box .xterm { height: 100%; }
.btn-danger-fill { background: var(--bad); border-color: var(--bad); color: #fff; }

/* Shown until app.js replaces #app; the hint fades in only if loading stalls (no inline JS needed). */
.boot { min-height: 100vh; display: grid; place-content: center; text-align: center; color: var(--muted); padding: 16px; }
.boot-hint { opacity: 0; animation: boot-hint 0s 8s forwards; max-width: 360px; }
@keyframes boot-hint { to { opacity: 1; } }

/* ---------- terminal + command book ---------- */
.term-layout { display: grid; grid-template-columns: 1fr; gap: 12px; }
.term-layout.with-book { grid-template-columns: minmax(0, 1fr) 380px; }
.term-layout:not(.with-book) .cmdbook { display: none; }
.cmdbook { display: flex; flex-direction: column; height: calc(100vh - 150px); min-height: 320px; }
.cb-search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.cb-list { overflow-y: auto; flex: 1; }
.cb-foot { padding: 8px 12px; border-top: 1px solid var(--border); }
.cb-section { border-bottom: 1px solid var(--border); }
.cb-section > summary { cursor: pointer; padding: 9px 12px; font-weight: 600; display: flex; gap: 8px; align-items: center; list-style: none; }
.cb-section > summary::-webkit-details-marker { display: none; }
.cb-section > summary::after { content: '▸'; margin-left: auto; color: var(--muted); }
.cb-section[open] > summary::after { content: '▾'; }
.cb-section > summary:hover { background: var(--panel-2); }
.cb-ico { width: 18px; text-align: center; }
.cb-item { padding: 8px 12px 10px 38px; border-top: 1px dashed var(--border); }
.cb-title { font-weight: 600; font-size: 13px; }
.cb-cmd { display: block; margin: 5px 0; padding: 6px 8px; background: #0b0e19; color: #d6dbef; border-radius: 6px; white-space: pre-wrap; word-break: break-all; font-size: 12px; }
.cb-cmd mark { background: #3b3f7a; color: #fff; border-radius: 3px; padding: 0 3px; }
.cb-desc { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.cb-actions { gap: 4px; }
@media (max-width: 1100px) {
  .term-layout.with-book { grid-template-columns: 1fr; }
  .cmdbook { height: 60vh; }
}
.notice mark { background: var(--accent); color: #fff; border-radius: 3px; padding: 0 3px; }

/* ---------- dashboard health card ---------- */
.hc-problems { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.hc-problem { display: flex; gap: 10px; align-items: center; padding: 8px 12px; border-radius: 8px; background: var(--warn-soft); }
.hc-problem.bad { background: var(--bad-soft); }
.hc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 8px; }
.hc-tile { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: var(--panel-2); }
.hc-tile.bad { border-color: var(--bad); }
.hc-tile.warn { border-color: var(--warn); }
.hc-tile.off { opacity: .65; }
.hc-actions { gap: 2px; margin-top: 4px; }
.hc-actions .btn-sm { padding: 1px 6px; }
details > summary { cursor: pointer; margin: 10px 0 6px; }
.badge.method-get { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.method-post { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.method-patch, .badge.method-put { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.method-delete { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.access-block { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
.access-list { max-height: 240px; overflow-y: auto; }
.access-list td { padding: 5px 8px; }
