/* === Tokens === */
:root {
  color-scheme: light dark;
  --bg-app: #f2f3f5;
  --bg-sidebar: #ffffff;
  --bg-card: #ffffff;
  --bg-card-inset: #f7f8f9;
  --bg-input: #ffffff;
  --bg-hover: #f0f1f3;
  --bg-active: #e9eaf5;
  --fg: #060607;
  --fg-muted: #5c5e66;
  --fg-faint: #80838e;
  --fg-on-accent: #ffffff;
  --border: #e3e5e8;
  --border-strong: #d1d3d8;
  --accent: #5865f2;
  /* Accent used as foreground/text (e.g. active nav item) rather than a button background —
     kept separate from --accent because the two need different contrast targets: --accent is
     tuned so white text reads on it, --accent-fg is tuned so it reads on --bg-*. Same value in
     light mode (both already pass); dark mode gives --accent-fg a lighter tint. */
  --accent-fg: var(--accent);
  --accent-hover: #4752c4;
  --accent-active: #3c45a5;
  --accent-tint: #e8e9fd;
  --success: #1f7a30;
  --success-tint: #e3f6e8;
  --danger: #c62828;
  --danger-hover: #a11f1f;
  --danger-tint: #fcebec;
  --neutral-pill-bg: #e3e5e8;
  --neutral-pill-fg: #4e5058;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --sidebar-w: 240px;
  --font-sans: "gg sans", "Noto Sans", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto,
    Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-app: #1e1f22;
    --bg-sidebar: #2b2d31;
    --bg-card: #313338;
    --bg-card-inset: #2b2d31;
    --bg-input: #1e1f22;
    --bg-hover: #35373c;
    --bg-active: #3c3f8a55;
    --fg: #f2f3f5;
    --fg-muted: #b5bac1;
    --fg-faint: #949ba4;
    --border: #3f4147;
    --border-strong: #4e5058;
    --accent-fg: #949cf7;
    --accent-hover: #5a66e0;
    --accent-tint: #3c3f8a40;
    --success: #4fcf94;
    --success-tint: #2b8a3e26;
    --danger: #f23f42;
    --danger-hover: #f66;
    --danger-tint: #f2373526;
    --neutral-pill-bg: #3f4147;
    --neutral-pill-fg: #b5bac1;
  }
}

/* === Reset / base === */
* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  color: var(--fg);
  background: var(--bg-app);
}

a { color: var(--accent-fg); text-decoration: none; }
a:hover { text-decoration: underline; }
a:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent-fg);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

h1 { font-size: 1.3rem; margin: 0; }
h2 { font-size: 1rem; margin: 0 0 var(--space-3); }

/* === Layout shell === */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: var(--space-4);
  gap: var(--space-4);
}

main {
  padding: var(--space-6);
  max-width: 1080px;
  width: 100%;
}

main.login {
  max-width: 360px;
  margin: 0 auto;
  padding-top: 4rem;
  text-align: center;
}

.login-copy { margin: var(--space-3) 0 var(--space-4); }

/* === Sidebar components === */
.brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: inherit;
  text-decoration: none;
  font-weight: 700;
  font-size: 1rem;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--fg-on-accent);
  font-size: 0.8rem;
  font-weight: 700;
}

.guild-switcher {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--border);
}

.guild-chip {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  background: var(--bg-card-inset);
  overflow: hidden;
}

.guild-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--accent-tint);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.8rem;
  text-transform: uppercase;
}

.guild-chip-name {
  font-size: 0.85rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.switch-server {
  font-size: 0.8rem;
  color: var(--fg-muted);
  text-decoration: none;
}
.switch-server:hover { color: var(--accent-fg); text-decoration: underline; }

.section-nav {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.nav-item {
  display: block;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--fg-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
}
.nav-item:hover { background: var(--bg-hover); color: var(--fg); text-decoration: none; }

/* .shell (not body) carries the page's active-nav class — hx-boost only swaps body's
   *inner* content, not body's own attributes, so a class on body itself would get stuck
   after the first boosted navigation. */
.shell.page-rules .nav-item[href$="/rules"],
.shell.page-scopes .nav-item[href$="/scopes"],
.shell.page-admin-roles .nav-item[href$="/admin-roles"],
.shell.page-features .nav-item[href$="/features"],
.shell.page-commands .nav-item[href$="/commands"],
.shell.page-operator .nav-item[href$="/operator/features"] {
  background: var(--bg-active);
  color: var(--accent-fg);
}

.sidebar-spacer { flex: 1; }

.sidebar-user {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding-top: var(--space-4);
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
}
.sidebar-user .user { color: var(--fg-muted); }
.sidebar-user a { color: var(--fg-muted); text-decoration: none; }
.sidebar-user a:hover { color: var(--danger); text-decoration: underline; }
.sidebar-user button.link-button {
  color: var(--fg-muted);
  text-decoration: none;
  font: inherit;
  align-self: flex-start;
}
.sidebar-user button.link-button:hover { color: var(--danger); text-decoration: underline; }

/* === Card / page header === */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  /* A wide table (or anything else) scrolls inside the card rather than pushing the page
     itself wider — the page must never gain a horizontal scrollbar. */
  overflow-x: auto;
}
.card + .card { margin-top: var(--space-5); }
.card + h2 { margin-top: var(--space-6); }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
}

/* === Table === */
table { width: 100%; border-collapse: collapse; }

thead th {
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--fg-muted);
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

tbody td {
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
tbody tr:hover td { background: var(--bg-hover); }
tbody tr:last-child td { border-bottom: none; }

/* td.actions itself stays a plain table-cell (display:flex directly on a <td> knocks it out of
   the table's own row-height/border layout, desyncing its border from sibling cells) — the flex
   layout lives on this inner wrapper instead. */
td.actions { text-align: right; }
.actions-inner {
  display: inline-flex;
  gap: var(--space-3);
  align-items: center;
  justify-content: flex-end;
}
.actions-inner form { margin: 0; }

/* === Pill / badge === */
.pill {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  background: var(--neutral-pill-bg);
  color: var(--neutral-pill-fg);
  white-space: nowrap;
}
.pill-success { background: var(--success-tint); color: var(--success); }

/* === Buttons === */
.btn, button, .button {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.875rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  width: fit-content;
  /* Selects and buttons render at slightly different heights for the same padding (native
     control rendering differs by element type) — pin both to the same height explicitly
     rather than relying on padding/font-size alone to match up in an inline-form row. */
  min-height: 2.5rem;
}

.btn-primary, button:not(.link-button):not(.btn-secondary):not(.btn-danger), .button {
  background: var(--accent);
  color: var(--fg-on-accent);
  border-color: var(--accent);
}
.btn-primary:hover, button:not(.link-button):not(.btn-secondary):not(.btn-danger):hover,
.button:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:active, .button:active { background: var(--accent-active); border-color: var(--accent-active); }

.btn-secondary {
  background: var(--bg-card-inset);
  color: var(--fg);
  border-color: var(--border-strong);
}
.btn-secondary:hover { background: var(--bg-hover); }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger-tint); }

button.link-button {
  background: none;
  border: none;
  color: var(--danger);
  padding: 0;
  text-decoration: underline;
  width: auto;
  font-size: 0.85rem;
}
button.link-button:hover { color: var(--danger-hover); }

.error { color: var(--danger); }

/* === Forms / inputs === */
form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  max-width: 480px;
}

form.inline-form {
  flex-direction: row;
  /* Top-align: every item here is a label-line + gap + control (see .btn-spacer below, which
     gives bare buttons a same-sized invisible label so they're structurally identical to real
     fields) — top-aligning means every control starts at the same y regardless of its own
     height, which is the one strategy that stays correct whether sibling controls are the same
     height (select next to a button) or not (select next to a 2-row textarea). */
  align-items: flex-start;
  flex-wrap: wrap;
  max-width: none;
  gap: var(--space-3);
  padding-top: var(--space-4);
  margin-top: var(--space-4);
  border-top: 1px solid var(--border);
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.9rem;
}

/* Invisible label placeholder for a bare submit button in an .inline-form, so its layout box
   matches a real label+control field exactly (same text line-height pushing the control down)
   instead of needing a manually-guessed margin to line up with sibling fields. */
.btn-spacer { visibility: hidden; }

label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
}

input[type="text"], select, textarea {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.9rem;
}
input[type="text"], select {
  /* Matches the .btn/button min-height so a select+button pair in an inline-form row lines
     up exactly instead of differing by the browser's native per-element-type rendering. */
  min-height: 2.5rem;
}
input[type="text"]:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-tint);
}

.field-group { display: flex; flex-direction: column; gap: 0.3rem; }
.field-group[hidden] { display: none; }

small { opacity: 0.75; font-weight: normal; color: var(--fg-muted); }

/* === List with trailing action === */
.item-list {
  list-style: none;
  padding: 0;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.item-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  padding: 0.6rem 0.9rem;
  background: var(--bg-card-inset);
  font-size: 0.88rem;
}
.item-list li + li { border-top: 1px solid var(--border); }
.item-list form { margin: 0; }

/* === Notices === */
.notice {
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
  font-size: 0.88rem;
}
.notice ul { margin: var(--space-1) 0 0; padding-left: 1.2rem; }
.notice-error { border-left-color: var(--danger); background: var(--danger-tint); }
small.warning { color: var(--danger); }

/* === Command editor === */
.editor-node > summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: var(--space-3);
}
.editor-node .children { display: flex; flex-direction: column; gap: var(--space-3); }
.editor-node .editor-node { margin: 0; }
.editor-node h4 { margin: var(--space-4) 0 var(--space-2); font-size: 0.85rem; }
.editor-list { list-style: none; margin: 0 0 var(--space-3); padding: 0; display: flex; flex-direction: column; gap: var(--space-2); }
.editor-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
}
.editor-item-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-2); }
.row-buttons { display: inline-flex; gap: var(--space-2); align-items: center; }
.row-buttons .danger { color: var(--danger); }
.inline-form label.wide { flex: 1 1 100%; }
.inline-form label.wide textarea { width: 100%; }
small.hint { flex: 1 1 100%; color: var(--fg-muted); }
.editor-footer { margin-top: var(--space-4); }

/* === Responsive === */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    flex-direction: row;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
  }
  .guild-switcher { border-bottom: none; padding-bottom: 0; }
  .section-nav {
    flex-direction: row;
    overflow-x: auto;
    max-width: 100%;
  }
  .sidebar-spacer { display: none; }
  .sidebar-user { border-top: none; padding-top: 0; }
  main { padding: var(--space-4); }
}

.access { border-top: 1px dashed var(--border); margin-top: var(--space-3); padding-top: var(--space-2); }
.condition-group { flex: 1 1 100%; border-left: 3px solid var(--border); padding-left: var(--space-3); }
fieldset.checklist { flex: 1 1 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); padding: var(--space-2) var(--space-3); }
fieldset.checklist legend { font-size: 0.8rem; color: var(--fg-muted); }
details.nested { flex: 1 1 100%; border-left: 3px solid var(--border); padding: var(--space-1) 0 var(--space-1) var(--space-3); margin: var(--space-2) 0; }
details.nested > summary { cursor: pointer; font-weight: 600; font-size: 0.85rem; color: var(--fg-muted); }
.action-list { flex: 1 1 100%; }
input[type=color] { width: 3rem; padding: 0; height: 2.25rem; }
