:root {
  color-scheme: dark;
  --bg: #17181c;
  --surface: #20222a;
  --surface-2: #2a2d37;
  --border: #353945;
  --text: #e8e9ee;
  --muted: #a0a4b3;
  --accent: #f59e42;
  --accent-text: #1c1408;
  --link: #ffb469;
  --ok: #4ad295;
  --ok-bg: #15352a;
  --bad: #ff7b7b;
  --bad-bg: #3d1f23;
  --warn-bg: #3a2f17;
  --warn: #f3c969;
  --admin: #a78bfa;
  --admin-bg: #2b2447;
  --admin-text: #160f2e;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f5f4f1;
    --surface: #ffffff;
    --surface-2: #f0eeea;
    --border: #dcd9d2;
    --text: #1f2026;
    --muted: #62656f;
    --accent: #e07f15;
    --accent-text: #ffffff;
    --link: #b35c00;
    --ok: #12804f;
    --ok-bg: #dff5ea;
    --bad: #c0303a;
    --bad-bg: #fbe4e5;
    --warn-bg: #fdf1d3;
    --warn: #7a5600;
    --admin: #6d4fd6;
    --admin-bg: #ece7fd;
    --admin-text: #ffffff;
  }
}

/* Global Admin uses its own accent so it never looks like a server page. */
body.is-admin {
  --accent: var(--admin);
  --accent-text: var(--admin-text);
  --link: var(--admin);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}

a { color: var(--link); }
h1 { font-size: 1.6rem; margin: 0 0 .25rem; line-height: 1.2; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.page > h2 { margin-top: 1.75rem; }
p { margin: 0 0 .75rem; }
code, .mono { font-family: var(--mono); font-size: .9em; }
code { background: var(--surface-2); padding: .1em .35em; border-radius: 4px; overflow-wrap: anywhere; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .88rem; }
.grow { flex: 1 1 auto; min-width: 0; }
[hidden] { display: none !important; }

/* ----------------------------------------------------------- top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: .55rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: .55rem; color: var(--text); text-decoration: none; font-weight: 700; font-size: 1.05rem; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: .9rem; flex-wrap: wrap; }
.me { display: flex; align-items: center; gap: .45rem; }
.avatar { border-radius: 50%; width: 24px; height: 24px; background: var(--surface-2); flex: none; }
.avatar-large { width: 36px; height: 36px; }
.activity-face { width: 20px; height: 20px; vertical-align: -4px; margin-right: .15rem; }

.status { font-size: .85rem; padding: .2rem .65rem; border-radius: 999px; white-space: nowrap; }
.status::before { content: ""; display: inline-block; width: .5rem; height: .5rem; border-radius: 50%; margin-right: .4rem; background: currentColor; }
.status-ok { color: var(--ok); background: var(--ok-bg); }
.status-bad { color: var(--bad); background: var(--bad-bg); }

.badge { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: .1rem .45rem; border-radius: 4px; background: var(--accent); color: var(--accent-text); white-space: nowrap; }
.badge-admin { background: var(--admin); color: var(--admin-text); }

/* Server switcher */
.switcher { position: relative; }
.switcher > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; gap: .5rem;
  padding: .3rem .7rem; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
  min-width: 12rem; max-width: 20rem;
}
.switcher > summary::-webkit-details-marker { display: none; }
.switcher[open] > summary, .switcher > summary:hover { border-color: var(--accent); }
.switcher-name { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.switcher-caret { color: var(--muted); }
.menu {
  position: absolute; top: calc(100% + .35rem); left: 0; z-index: 30; min-width: 100%; width: max-content; max-width: min(22rem, 90vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .35rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .35); max-height: 70vh; overflow-y: auto;
}
.menu a { display: flex; align-items: center; gap: .6rem; padding: .45rem .6rem; border-radius: 6px; color: var(--text); text-decoration: none; }
.menu a:hover, .menu a.active { background: var(--surface-2); }
.menu-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: .35rem .6rem .2rem; }
.menu-empty { padding: .4rem .6rem; }
.menu-sep { height: 1px; background: var(--border); margin: .35rem 0; }
.menu a.menu-admin { color: var(--admin); font-weight: 600; }

/* Global Admin banner */
.perspective-bar {
  display: flex; gap: .75rem; align-items: baseline; flex-wrap: wrap;
  padding: .5rem 1.25rem; background: var(--admin-bg); color: var(--text);
  border-bottom: 2px solid var(--admin); font-size: .9rem;
}
.perspective-bar strong { color: var(--admin); text-transform: uppercase; letter-spacing: .05em; font-size: .8rem; }

/* ------------------------------------------------------ shell + sidebar */
.shell { max-width: 1280px; margin: 0 auto; }
.shell.has-sidebar { display: grid; grid-template-columns: 240px minmax(0, 1fr); align-items: start; }
.page { padding: 1.5rem 1.5rem 4rem; min-width: 0; }
.shell:not(.has-sidebar) .page { max-width: 960px; margin: 0 auto; }

.sidebar { padding: 1.25rem .75rem 2rem 1.25rem; position: sticky; top: 0; }
.side-head { display: flex; align-items: center; gap: .7rem; padding: 0 .5rem 1rem; }
.side-head-text { display: flex; flex-direction: column; min-width: 0; }
.side-head-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-nav { display: flex; flex-direction: column; gap: .15rem; }
.side-nav a { color: var(--muted); text-decoration: none; padding: .42rem .7rem; border-radius: 7px; border-left: 3px solid transparent; }
.side-nav a:hover { color: var(--text); background: var(--surface); }
.side-nav a.active { color: var(--text); background: var(--surface); border-left-color: var(--accent); font-weight: 600; }
.side-nav a.side-admin-link { color: var(--admin); }
.side-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 1rem .7rem .25rem; }
.side-empty { padding: .3rem .7rem; font-size: .9rem; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .75rem; flex-wrap: wrap; }
.crumbs { color: var(--muted); font-size: .9rem; margin-bottom: 1rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.25rem; }
.card-danger { border-color: var(--bad); }
.table-card { padding: .5rem 1.25rem; overflow-x: auto; }

.flash, .notice { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1.25rem; overflow-wrap: anywhere; }
.flash-ok { background: var(--ok-bg); color: var(--ok); }
.flash-error, .notice-error { background: var(--bad-bg); color: var(--bad); }
.notice-warn { background: var(--warn-bg); color: var(--warn); }
.notice a { color: inherit; }

.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.empty h1, .empty h2 { color: var(--text); }
.error-code { font-size: 3rem; font-weight: 800; margin: 0; color: var(--accent); }

/* ----------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  font: inherit; font-weight: 600; text-decoration: none; white-space: nowrap;
  padding: .45rem .95rem; border-radius: 7px; cursor: pointer;
  color: var(--text); background: var(--surface-2); border: 1px solid var(--border);
}
.btn:hover { filter: brightness(1.1); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-danger { background: transparent; border-color: var(--bad); color: var(--bad); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--muted); }
.btn-quiet:hover { color: var(--text); background: var(--surface-2); }
.btn-large { padding: .7rem 1.6rem; font-size: 1.05rem; }
.btn-small { padding: .2rem .6rem; font-size: .85rem; }

/* ------------------------------------------------------------- forms */
.field { display: block; border: 0; padding: 0; margin: 0 0 1rem; min-width: 0; }
.field > span, .field > legend { display: block; font-weight: 600; margin-bottom: .3rem; padding: 0; }
input[type="text"], input[type="url"], input[type="number"], input[type="datetime-local"], select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 7px; padding: .5rem .7rem;
}
textarea { resize: vertical; min-height: 4.5rem; }
.check { display: flex; align-items: center; gap: .5rem; padding: .2rem 0; }
.check input { width: 1rem; height: 1rem; accent-color: var(--accent); flex: none; }
.check-indent { padding-left: 1.5rem; }
.inline-form { display: flex; align-items: flex-end; gap: .75rem; flex-wrap: wrap; }
.inline-form .field { margin-bottom: 0; }
.inline-form + .hint { margin: .6rem 0 0; }
.actions { display: flex; gap: .75rem; flex-wrap: wrap; align-items: center; }
.role-row { display: flex; align-items: center; gap: .75rem; padding: .3rem 0; }
.role-row select { width: auto; min-width: 11rem; }
.add-admin { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1.25rem; margin: 0 0 1rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }

/* ----------------------------------------------------------- servers */
.server-grid, .feature-grid { list-style: none; margin: 0 0 1.25rem; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.server-card { display: flex; align-items: center; gap: .9rem; padding: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); text-decoration: none; }
.server-card:hover { border-color: var(--accent); }
.server-card-text { display: flex; flex-direction: column; min-width: 0; }
.server-card-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.server-card-text small { color: var(--muted); }
.server-icon { width: 48px; height: 48px; border-radius: 30%; flex: none; background: var(--surface-2); }
.server-icon-large { width: 72px; height: 72px; }
.server-icon-small { width: 26px; height: 26px; font-size: .7rem; }
.sidebar .server-icon { width: 40px; height: 40px; }
.server-icon-fallback { display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }
.server-head { display: flex; align-items: center; gap: 1rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
.server-head p { margin: 0; }

.feature-card { display: flex; flex-direction: column; align-items: flex-start; gap: .4rem; padding: 1.1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.feature-card p { flex: 1 1 auto; margin: 0 0 .4rem; }

.admin-card {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 2rem; padding: 1rem 1.25rem;
  background: var(--admin-bg); border: 1px solid var(--admin); border-radius: var(--radius); color: var(--text); text-decoration: none;
}
.admin-card strong { color: var(--admin); display: block; }
.admin-card small { color: var(--muted); }

.feature-list, .user-list { list-style: none; margin: 0; padding: 0; }
.feature { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; padding: .9rem 0; border-top: 1px solid var(--border); }
.feature-text p { margin: .2rem 0 0; }
.pill { display: inline-block; font-size: .72rem; font-weight: 700; padding: .1rem .5rem; border-radius: 999px; vertical-align: middle; white-space: nowrap; }
.feature-text .pill { margin-left: .4rem; }
.pill-on { color: var(--ok); background: var(--ok-bg); }
.pill-off { color: var(--muted); background: var(--surface-2); }
.pill-admin { color: var(--admin); background: var(--admin-bg); }

/* ------------------------------------------------------------- users */
.user-list > li { border-top: 1px solid var(--border); }
.list-empty { padding: .7rem 0; }
.user-row { display: flex; align-items: center; gap: .8rem; padding: .7rem 0; flex-wrap: wrap; }
.user-details > summary { cursor: pointer; list-style: none; }
.user-details > summary::-webkit-details-marker { display: none; }
.user-form { padding: .5rem 0 .5rem; }
.user-details .user-form { padding-left: calc(36px + .8rem); padding-bottom: 1.2rem; }

/* ------------------------------------------------------------- login */
.login { max-width: 460px; margin: 3rem auto; text-align: center; }
.login h1 { margin: .5rem 0 1rem; }
.setup { text-align: left; }
.setup ol { margin: .6rem 0 0; padding-left: 1.2rem; }
.setup li { margin-bottom: .35rem; }

/* ------------------------------------------------------------ tables */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; vertical-align: top; padding: .65rem .8rem .65rem 0; border-top: 1px solid var(--border); }
.table th { color: var(--muted); font-weight: 600; font-size: .85rem; border-top: 0; }
.table td.wrap { overflow-wrap: anywhere; white-space: pre-wrap; }
.nowrap { white-space: nowrap; }
.cell-server { display: flex; align-items: center; gap: .6rem; }
.cell-actions { text-align: right; }
.cell-actions .btn { margin-left: .35rem; }

/* ---------------------------------------------------- bot profile */
.profile { display: flex; gap: 1.25rem; align-items: flex-start; flex-wrap: wrap; }
.profile-avatar { width: 96px; height: 96px; border-radius: 50%; object-fit: cover; background: var(--surface-2); flex: none; }
.profile-avatar-blank { display: inline-block; }
.avatar-form { margin-top: 1.25rem; }
input[type="color"] { width: 3.2rem; height: 2.2rem; padding: .15rem; border: 1px solid var(--border); border-radius: 7px; background: var(--bg); cursor: pointer; }
input[type="file"] { width: 100%; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 7px; padding: .4rem .6rem; }

/* ------------------------------------------------- automation pages */
.pill-bad { color: var(--bad); background: var(--bad-bg); }
.warn-text { color: var(--warn); }
.rule-list { list-style: none; margin: 0; padding: 0; }
.rule-list > li { border-top: 1px solid var(--border); }
.rule > summary { cursor: pointer; list-style: none; }
.rule > summary::-webkit-details-marker { display: none; }
.rule-head { display: flex; align-items: center; gap: .8rem; padding: .8rem 0; }
.rule-head .pill { margin-left: .4rem; }
.rule-form { padding: .5rem 0 1rem; }
.rule .rule-form { padding-left: 1rem; border-left: 3px solid var(--border); margin: 0 0 1rem .25rem; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 1rem; }
.grid .span-2 { grid-column: 1 / -1; }
.pair { display: flex; align-items: center; gap: .5rem; }
.pair input[type="number"] { width: 7rem; }
.pair select { width: auto; }
.channel-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 0 1rem; max-height: 16rem; overflow-y: auto; padding: .4rem .6rem; border: 1px solid var(--border); border-radius: 7px; background: var(--bg); margin-bottom: .4rem; }

.picker { position: relative; }
.chips { display: flex; flex-wrap: wrap; gap: .35rem; }
.chips:not(:empty) { margin-bottom: .45rem; }
.chip { display: inline-flex; align-items: center; gap: .35rem; padding: .15rem .25rem .15rem .65rem; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: .9rem; }
.chip button { font: inherit; line-height: 1; width: 1.4rem; height: 1.4rem; border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer; }
.chip button:hover { background: var(--bad-bg); color: var(--bad); }
.picker-results { position: absolute; z-index: 10; left: 0; right: 0; top: 100%; margin: .25rem 0 0; padding: .3rem; list-style: none; max-height: 16rem; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 12px 32px rgba(0, 0, 0, .35); }
.picker-results li { display: flex; align-items: center; gap: .5rem; padding: .35rem .5rem; border-radius: 5px; cursor: pointer; }
.picker-results li.active, .picker-results li[data-id]:hover { background: var(--surface-2); }
.picker-results li.picker-note { cursor: default; color: var(--muted); }

/* ----------------------------------------------------- role selection */
.publish { display: flex; gap: 1.25rem; align-items: flex-start; flex-wrap: wrap; }
.publish-pending { border-color: var(--accent); }
.publish .pill { margin-right: .4rem; }
.publish-actions { display: flex; flex-direction: column; gap: .5rem; align-items: stretch; }
.publish-actions .btn { width: 100%; }
.problems { margin: .5rem 0 0; padding: .6rem .9rem .6rem 1.9rem; border-radius: 8px; background: var(--warn-bg); color: var(--warn); font-size: .92rem; }
.option-rows { display: grid; gap: .4rem; margin-bottom: .5rem; }
.option-row { display: grid; grid-template-columns: minmax(0, 2.2fr) minmax(0, 1.6fr) minmax(0, .8fr) minmax(0, 1.8fr); gap: .5rem; align-items: center; }
.option-head { font-size: .82rem; color: var(--muted); font-weight: 600; }
.inline-number { width: 4.5rem !important; margin: 0 .3rem; padding: .2rem .4rem !important; }

/* ------------------------------------------------------ activity log */
.filters { display: flex; flex-wrap: wrap; gap: 0 1rem; align-items: flex-end; }
.filters .field { flex: 0 1 11rem; }
.filters .field.grow { flex: 1 1 14rem; }
.filters .actions { margin-bottom: 1rem; }
input[type="number"] { width: 100%; }
.activity td { font-size: .93rem; }
.activity tr.activity-error td:first-child { box-shadow: inset 3px 0 0 var(--bad); padding-left: .6rem; }
.table-card + .actions { margin-bottom: 1.25rem; }

@media (max-width: 760px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .option-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .option-head { display: none; }
  .shell.has-sidebar { display: block; }
  .sidebar { position: static; padding: 1rem 1.25rem 0; }
  .side-head { padding-bottom: .5rem; }
  .side-nav { flex-direction: row; flex-wrap: wrap; align-items: center; }
  .side-label { padding: .42rem .3rem; width: 100%; }
  .topbar-right { margin-left: 0; }
  .me-name { display: none; }
  .user-details .user-form { padding-left: 0; }
  .page { padding: 1rem 1.25rem 3rem; }
}
