:root {
  color-scheme: light;
  --bg: #ffffff;
  --fg: #1a1d21;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #3b82f6;
  --accent-soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --ok: #2e9e44;
  --bad: #d23;
  --dirty: color-mix(in srgb, #e0a000 18%, transparent);
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1318;
  --fg: #e6e9ee;
  --muted: #8b95a3;
  --line: #2a313b;
  --accent: #60a5fa;
  --accent-soft: color-mix(in srgb, var(--accent) 20%, transparent);
  --ok: #4ade80;
  --bad: #f87171;
  --dirty: color-mix(in srgb, #e0a000 24%, transparent);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.5 -apple-system, system-ui, "Segoe UI", sans-serif;
  color: var(--fg); background: var(--bg);
  display: grid; grid-template-columns: 220px 1fr; grid-template-rows: 100%;
  transition: grid-template-columns .15s ease;
}

/* ---- sidebar ---- */
aside {
  border-right: 1px solid var(--line);
  padding: 1rem .75rem; display: flex; flex-direction: column; gap: .25rem;
  position: sticky; top: 0; height: 100vh;
}
aside .brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.05rem; padding: .25rem .5rem 1rem; }
aside .brand-logo { height: 42px; width: auto; max-width: 56px; object-fit: contain; flex-shrink: 0; border-radius: 6px; }
aside .brand-text { display: flex; flex-direction: column; min-width: 0; }
aside .brand small { display: block; font-weight: 400; font-size: .72rem; color: var(--muted); }
nav a {
  display: flex; align-items: center; gap: .55rem;
  padding: .5rem .6rem; border-radius: 8px; text-decoration: none; color: var(--fg);
  cursor: pointer; font-weight: 500;
}
nav a:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
nav a.active { background: var(--accent-soft); color: var(--accent); }
nav a .ic { width: 1.1rem; text-align: center; opacity: .8; }
aside .navgroup { font-size: .62rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); padding: 1rem .6rem .25rem; opacity: .7; }
aside .theme-toggle {
  margin-top: auto; padding: .4rem .55rem; font-size: .78rem; text-align: left;
  background: transparent; border: 1px solid var(--line); border-radius: 8px;
  color: var(--muted); cursor: pointer;
  display: flex; align-items: center; gap: .5rem;
}
aside .theme-toggle:hover { color: var(--fg); background: color-mix(in srgb, var(--fg) 6%, transparent); }
aside .health { font-size: .75rem; color: var(--muted); padding: .5rem; }
.dot { display: inline-block; width: .55rem; height: .55rem; border-radius: 50%; background: #aaa; margin-right: .35rem; }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--bad); }

/* ---- collapsible sidebar ---- */
.nav-toggle {
  margin-left: auto; background: none; border: 0; color: var(--muted);
  cursor: pointer; font-size: 1.15rem; line-height: 1; padding: .2rem .35rem; border-radius: 6px;
}
.nav-toggle:hover { color: var(--fg); background: color-mix(in srgb, var(--fg) 6%, transparent); }
:root[data-nav="collapsed"] body { grid-template-columns: 60px 1fr; }
:root[data-nav="collapsed"] aside { padding: 1rem .35rem; }
:root[data-nav="collapsed"] aside .brand { justify-content: center; padding: .25rem 0 .75rem; }
:root[data-nav="collapsed"] aside .brand-logo,
:root[data-nav="collapsed"] aside .brand-text { display: none; }
:root[data-nav="collapsed"] .nav-toggle { margin: 0; }
:root[data-nav="collapsed"] nav a { justify-content: center; gap: 0; padding: .5rem; }
:root[data-nav="collapsed"] nav a .lbl { display: none; }
:root[data-nav="collapsed"] aside .navgroup {
  font-size: 0; padding: .3rem 0; margin: .25rem .35rem 0; border-top: 1px solid var(--line);
}
:root[data-nav="collapsed"] aside .theme-toggle { justify-content: center; padding: .4rem; }
:root[data-nav="collapsed"] aside .theme-toggle .lbl { display: none; }
:root[data-nav="collapsed"] aside .health { font-size: 0; text-align: center; padding: .5rem 0; }
:root[data-nav="collapsed"] aside .health .dot { margin-right: 0; }

/* ---- main ---- */
/* No top padding on main so a sticky .toolbar can sit flush at the top; the
   former top spacing now lives on h1's margin. */
main { padding: 0 2rem; overflow-y: auto; height: 100vh; max-width: 1100px; }
h1 { font-size: 1.3rem; margin: 1.5rem 0 .25rem; }
.pagesub { color: var(--muted); margin: 0 0 1.25rem; }
/* Sticky header: search / refresh / count stay pinned while the list scrolls
   under them. Bleeds into main's horizontal padding so the opaque background
   covers the full width; the border separates it from scrolling content. */
.toolbar {
  display: flex; gap: .75rem; align-items: center;
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  margin: 0 -2rem 1rem; padding: .85rem 2rem;
  border-bottom: 1px solid var(--line);
}
.toolbar input[type=search] {
  flex: 1; max-width: 360px; padding: .45rem .6rem; border: 1px solid var(--line);
  border-radius: 8px; background: var(--bg); color: var(--fg); font: inherit;
}
.count { color: var(--muted); font-size: .8rem; }

/* ---- settings table ---- */
.section { margin: 0; }
.cat {
  font-weight: 600; margin: 0; padding: .7rem .5rem; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em; font-size: .72rem;
  display: flex; align-items: center; gap: .55rem; cursor: pointer; user-select: none;
  border-bottom: 1px solid var(--line);
}
.cat:hover { color: var(--fg); }
.cat .chev { display: inline-block; transition: transform .12s ease; opacity: .7; font-size: .9em; }
.section:not(.collapsed) .cat .chev { transform: rotate(90deg); }
.cat .catcount { margin-left: auto; font-weight: 400; opacity: .65; text-transform: none; letter-spacing: 0; }
.section.collapsed .secbody { display: none; }
.row {
  display: grid; grid-template-columns: minmax(220px, 1fr) 260px auto;
  gap: .5rem 1rem; align-items: start; padding: .55rem .5rem; border-radius: 8px;
  border-bottom: 1px solid var(--line);
}
.row.dirty { background: var(--dirty); }
.row .meta .k { font-family: ui-monospace, monospace; font-size: .82rem; }
.row .meta .s { color: var(--muted); font-size: .78rem; }
.row .ctl { display: flex; flex-direction: column; gap: .3rem; }
.row .ctl input, .row .ctl select {
  width: 100%; padding: .35rem .5rem; border: 1px solid var(--line);
  border-radius: 6px; background: var(--bg); color: var(--fg); font: inherit;
  font-family: ui-monospace, monospace; font-size: .82rem;
}
.row .ctl .err { color: var(--bad); font-size: .75rem; }
.row .side { display: flex; align-items: center; gap: .5rem; justify-content: flex-end; min-height: 1.8rem; }
.badge { font-size: .68rem; padding: .1rem .45rem; border-radius: 1rem; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.live { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.restart { color: #c80; border-color: color-mix(in srgb, #c80 40%, transparent); }
button {
  font: inherit; padding: .35rem .7rem; border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg); color: var(--fg); cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: white; }
button:disabled { opacity: .4; cursor: default; }
.saved { color: var(--ok); font-size: .8rem; }
.savedmsg { color: var(--ok); font-size: .75rem; }
.toggle { display: inline-flex; align-items: center; gap: .3rem; font-size: .8rem; color: var(--muted); cursor: pointer; user-select: none; }
.toggle input { cursor: pointer; }
/* principle editor form */
.fieldgrid { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem .9rem; margin: .7rem 0; }
.field { display: flex; flex-direction: column; gap: .25rem; font-size: .8rem; }
.field-k { color: var(--muted); }
.field select, .field input[type=number] {
  padding: .35rem .5rem; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--fg); font: inherit;
}
.field input[type=checkbox] { width: 1.05rem; height: 1.05rem; align-self: flex-start; }

/* ---- prompts (master-detail) ---- */
.master-detail { display: flex; gap: 1.5rem; align-items: flex-start; }
.plist { flex: 1; min-width: 0; }
.plist .row { grid-template-columns: 1fr; }
.plist .row.prow { cursor: pointer; }
.plist .row.prow:hover { background: color-mix(in srgb, var(--fg) 5%, transparent); }
.plist .row.prow.selected { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.preview {
  /* stick BELOW the sticky toolbar (~4rem tall) so the pane's top isn't hidden
     behind it while the list scrolls; height fits the remaining viewport. */
  width: 440px; flex-shrink: 0; position: sticky; top: 4.5rem;
  max-height: calc(100vh - 5.5rem); overflow: auto;
  border: 1px solid var(--line); border-radius: 10px; padding: 1rem;
}
/* ---- login ---- */
body.locked { grid-template-columns: 1fr; }
body.locked aside { display: none; }
/* main is normally capped at 1100px and left-aligned (sidebar fills the rest);
   on the login screen there's no sidebar, so let it span full width and the
   centered .login block lands in the middle of the actual viewport. */
body.locked #content { max-width: none; }
/* keep the login full-width even when the (now-hidden) sidebar is collapsed —
   otherwise the collapsed-nav rule's `60px 1fr` wins on specificity and the
   single remaining column squishes #content to 60px. */
:root[data-nav="collapsed"] body.locked { grid-template-columns: 1fr; }
.login { max-width: 360px; margin: 12vh auto 0; display: flex; flex-direction: column; align-items: center; gap: .9rem; text-align: center; }
.login-logo { width: 72px; height: 72px; object-fit: contain; border-radius: 12px; }
.login h1 { font-size: 1.2rem; margin: 0; }
.login .primary { padding: .55rem 1rem; }
.login-code { display: flex; gap: .5rem; }
.login-code input { width: 8rem; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--fg); font: inherit; letter-spacing: .2em; text-align: center; }
.login-status { color: var(--muted); font-size: .85rem; min-height: 1.2rem; }

/* ---- chat ---- */
.chatlog { display: flex; flex-direction: column; gap: .55rem; padding: .25rem 0 1rem; }
.msg { display: flex; }
.msg.me { justify-content: flex-end; }
.msg .bubble {
  max-width: 78%; padding: .55rem .8rem; border-radius: 13px;
  line-height: 1.45; font-size: .92rem;
}
.msg .btext { white-space: pre-wrap; word-break: break-word; }
.msg .chan {
  display: block; margin-top: .3rem; font-size: .6rem; text-transform: uppercase;
  letter-spacing: .05em; opacity: .65;
}
.msg.me .chan { color: rgba(255, 255, 255, .85); opacity: .9; }
.msg.me .bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.alfred .bubble { background: color-mix(in srgb, var(--fg) 8%, transparent); border-bottom-left-radius: 4px; }
/* Portal (admin channel) bubbles nudged toward teal/green vs Telegram:
   a clear green shift over the blue (sent), a shade of teal over the grey (Alfred). */
.msg.me.portal .bubble { background: color-mix(in srgb, var(--accent) 55%, #10b981); }
.msg.alfred.portal .bubble { background: color-mix(in srgb, #10b981 22%, color-mix(in srgb, var(--fg) 8%, transparent)); }
/* dark mode: the light base makes the teal pop harder — ease it off */
:root[data-theme="dark"] .msg.alfred.portal .bubble { background: color-mix(in srgb, #10b981 12%, color-mix(in srgb, var(--fg) 8%, transparent)); }
.msg.thinking .bubble { background: transparent; color: var(--muted); font-style: italic; }
.msg.err .bubble { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); }
.chatbar {
  position: sticky; bottom: 0; z-index: 5; background: var(--bg);
  display: flex; gap: .5rem; align-items: flex-end;
  padding: .6rem 0; margin: 0 -2rem; padding-left: 2rem; padding-right: 2rem;
  border-top: 1px solid var(--line);
}
.chatinput {
  flex: 1; resize: vertical; min-height: 2.4rem; max-height: 40vh;
  padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--fg); font: inherit;
}
.backbtn { font-size: .75rem; padding: .2rem .55rem; color: var(--muted); flex-shrink: 0; }
.backbtn:hover { color: var(--fg); }
.preview-empty { color: var(--muted); padding: 1rem .25rem; }
.preview-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .25rem; }
.preview-body {
  white-space: pre-wrap; word-break: break-word; margin: .75rem 0 0;
  font-family: ui-monospace, monospace; font-size: .8rem; line-height: 1.5;
  background: color-mix(in srgb, var(--fg) 4%, transparent);
  padding: .75rem; border-radius: 8px;
}
.preview .kv { display: flex; gap: .6rem; font-size: .8rem; padding: .18rem 0; align-items: baseline; }
.preview .kv-k { color: var(--muted); min-width: 110px; flex-shrink: 0; }
.preview .kv-v { font-family: ui-monospace, monospace; word-break: break-word; }
.preview textarea { width: 100%; min-height: 300px; font-family: ui-monospace, monospace;
  font-size: .8rem; border: 1px solid var(--line); border-radius: 8px; padding: .6rem;
  background: var(--bg); color: var(--fg); resize: vertical; margin-top: .75rem; }
.actions { margin-top: .75rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.editor { margin-top: 1rem; border: 1px solid var(--line); border-radius: 10px; padding: 1rem; }
.editor textarea {
  width: 100%; min-height: 320px; font-family: ui-monospace, monospace; font-size: .82rem;
  border: 1px solid var(--line); border-radius: 8px; padding: .6rem; background: var(--bg); color: var(--fg);
  resize: vertical;
}
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin: .5rem 0; }
.chip { font-size: .72rem; font-family: ui-monospace, monospace; padding: .12rem .5rem;
  border-radius: 1rem; background: var(--accent-soft); color: var(--accent); }
.editor .actions { margin-top: .75rem; display: flex; gap: .5rem; align-items: center; }
.err-banner { color: var(--bad); font-size: .8rem; margin-top: .5rem; }
.loading { color: var(--muted); padding: 2rem 0; }

/* ---- status / containers ---- */
.badge.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.badge.warn { color: #c80; border-color: color-mix(in srgb, #c80 40%, transparent); }
.slist .row { grid-template-columns: 210px 1fr auto; align-items: center; }
.slist .row .cname { font-family: ui-monospace, monospace; font-weight: 600; font-size: .85rem; }
.slist .row .cstatus { color: var(--muted); font-size: .82rem; }
.slist .side { gap: .6rem; }
.restart-inline { display: inline-flex; gap: .5rem; align-items: center; margin-left: .5rem; }

/* ---- data tables (content viewers) ---- */
.dtable { width: 100%; border-collapse: collapse; font-size: .82rem; }
.dtable th {
  text-align: left; font-weight: 600; color: var(--muted); text-transform: uppercase;
  letter-spacing: .03em; font-size: .68rem; padding: .5rem .6rem; border-bottom: 2px solid var(--line);
  position: sticky; top: 0; background: var(--bg);
}
.dtable td { padding: .45rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.dtable td.mono { font-family: ui-monospace, monospace; }
.dtable td.num { text-align: right; font-variant-numeric: tabular-nums; }
.dtable tr:hover td { background: color-mix(in srgb, var(--fg) 4%, transparent); }
.dtable td.wrap { max-width: 560px; white-space: pre-wrap; word-break: break-word; }
.flag { font-size: .66rem; padding: .05rem .35rem; border-radius: 1rem; margin-right: .25rem;
  background: var(--accent-soft); color: var(--accent); }
.diff .o { color: var(--bad); text-decoration: line-through; opacity: .8; }
.diff .n { color: var(--ok); }

/* Filing cabinet — image + Mermaid diagram previews */
.cabinet-img { margin-top: .75rem; }
.cabinet-img img {
  max-width: 100%; height: auto; border-radius: 8px;
  border: 1px solid color-mix(in srgb, var(--fg) 12%, transparent);
  background: color-mix(in srgb, var(--fg) 4%, transparent);
}
.cabinet-mermaid { margin-top: .75rem; }
.mermaid-host { margin-bottom: .75rem; }
.mermaid-diagram {
  background: #ffffff; border-radius: 8px; padding: .75rem; overflow-x: auto;
  border: 1px solid color-mix(in srgb, var(--fg) 12%, transparent);
}
.mermaid-diagram svg { max-width: 100%; height: auto; }
.cabinet-src { margin-top: .5rem; }
.cabinet-src > summary { cursor: pointer; color: var(--muted); font-size: .8rem; }
.cabinet-src .preview-body { margin-top: .5rem; }

/* Filing cabinet — drawer tree */
.drawer { margin: 0; }
.drawer > summary {
  cursor: pointer; user-select: none; list-style: none;
  display: flex; align-items: center; gap: .3rem;
  padding: .4rem .5rem; border-radius: 8px;
}
.drawer > summary::-webkit-details-marker { display: none; }
.drawer > summary::before { content: '▸'; color: var(--muted); font-size: .7rem; width: .7rem; flex-shrink: 0; }
.drawer[open] > summary::before { content: '▾'; }
.drawer > summary:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
.drawer .dname { font-weight: 500; }
.drawer .dcount { color: var(--muted); font-size: .8rem; }
/* monochrome file/folder icons — grey, take the text colour */
.ficon { display: inline-flex; align-items: center; color: var(--muted); flex-shrink: 0; }
.ficon svg { display: block; }
.cabname { display: flex; align-items: center; gap: .4rem; }
.drawer-body {
  margin-left: .9rem; padding-left: .55rem;
  border-left: 1px solid color-mix(in srgb, var(--fg) 10%, transparent);
}

/* Tasks — weekday toggles in the schedule builder */
.dowrow { display: flex; flex-wrap: wrap; gap: .35rem .6rem; }
.dowtog { display: inline-flex; align-items: center; gap: .2rem; font-size: .82rem; color: var(--fg); cursor: pointer; }

.cronpreview { font-family: ui-monospace, monospace; font-size: .8rem; color: var(--muted); padding-top: .15rem; }
