:root, [data-theme="dark"] {
  color-scheme: dark;
  --bg: #0c1222;
  --sidebar: #080e1a;
  --card: #141c2e;
  --card-2: #1c2740;
  --ink: #f6f1e7;
  --muted: #9aa4b8;
  --line: rgba(246, 241, 231, 0.12);
  --accent: #e8a15a;
  --accent-ink: #2a1606;
  --accent-soft: rgba(232, 161, 90, 0.16);
  --ice: #6ee0c5;
  --ice-soft: rgba(110, 224, 197, 0.16);
  --amber: #f0c14b;
  --amber-soft: rgba(240, 193, 75, 0.16);
  --red: #ff7d72;
  --red-soft: rgba(255, 125, 114, 0.16);
  --danger-ink: #2a100e;
  --input: #0d1424;
  --code: #0e1526;
  --shadow: 0 30px 70px rgba(0, 0, 0, 0.45);
  --grid: rgba(246, 241, 231, 0.045);
  --glow: rgba(232, 161, 90, 0.22);
  --mark: linear-gradient(145deg, #f0c27a, #c46a2c);
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f3eadc;
  --sidebar: #faf4ea;
  --card: #fffaf3;
  --card-2: #f4ebe0;
  --ink: #1c1612;
  --muted: #6f655b;
  --line: rgba(28, 22, 18, 0.12);
  --accent: #c45d1a;
  --accent-ink: #fff8f2;
  --accent-soft: rgba(196, 93, 26, 0.12);
  --ice: #0d7a66;
  --ice-soft: rgba(13, 122, 102, 0.12);
  --amber: #8a5a00;
  --amber-soft: rgba(138, 90, 0, 0.12);
  --red: #b42318;
  --red-soft: rgba(180, 35, 24, 0.1);
  --danger-ink: #fff;
  --input: #fff;
  --code: #f7f1e8;
  --shadow: 0 22px 50px rgba(70, 42, 16, 0.1);
  --grid: rgba(28, 22, 18, 0.05);
  --glow: rgba(196, 93, 26, 0.18);
  --mark: linear-gradient(145deg, #e8893a, #a34712);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: "Avenir Next", "Segoe UI", ui-sans-serif, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background:
    radial-gradient(900px 520px at 110% -10%, var(--glow), transparent 55%),
    radial-gradient(700px 420px at -15% 110%, rgba(110, 224, 197, 0.08), transparent 50%),
    var(--bg);
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 56px 56px;
  mask-image: radial-gradient(circle at 15% 0%, #000 0%, transparent 72%);
}
.login, .shell, .toasts { position: relative; z-index: 1; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
code, .mono, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
h1, h2, .stat strong, .card h3, dialog h2 {
  font-family: inherit;
  font-weight: 650;
  letter-spacing: -0.03em;
}
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}
.hidden { display: none !important; }
.lede { margin: 8px 0 0; color: var(--muted); max-width: 46rem; }
.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 750;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px 20px;
}
.login-card {
  width: min(340px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.login-card button { width: 100%; margin-top: 10px; }

.brand { display: flex; align-items: center; gap: 12px; }
.brand strong { font-size: 1.02rem; letter-spacing: -0.02em; }
.mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--mark);
  box-shadow: 0 8px 20px rgba(196, 106, 44, 0.35);
  flex: none;
  position: relative;
}
.mark::after {
  content: "";
  position: absolute;
  inset: 11px 9px 11px 13px;
  border-left: 2px solid rgba(42, 22, 6, 0.75);
  border-bottom: 2px solid rgba(42, 22, 6, 0.75);
  transform: skewX(-18deg);
}

label { display: block; margin: 18px 0 8px; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--input);
  border-radius: 12px;
  padding: 12px 13px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-color: transparent;
}
button {
  border: 0;
  border-radius: 12px;
  padding: 11px 16px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 750;
}
button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  font-weight: 650;
}
button.danger { background: var(--red); color: var(--danger-ink); }
.form-error { min-height: 1.25em; margin: 10px 0 0; color: var(--red); font-size: 0.9rem; }

.shell {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  min-height: 100vh;
}
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 24px 16px 18px;
  background: color-mix(in srgb, var(--sidebar) 92%, transparent);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(16px);
}
.nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 28px;
}
.nav button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: transparent;
  color: var(--muted);
  border-radius: 12px;
  padding: 12px 12px 12px 14px;
  font-weight: 700;
  box-shadow: inset 0 0 0 0 transparent;
}
.nav button.active {
  color: var(--ink);
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}
.nav em {
  font-style: normal;
  font-variant-numeric: tabular-nums;
  font-size: 0.75rem;
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
}
.sidebar-foot {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 16px;
}
.sidebar-foot button { width: 100%; }

.workspace { padding: 32px 36px 72px; }
.top {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 26px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}
.top h1 { margin: 0; font-size: clamp(2rem, 3vw, 2.8rem); line-height: 0.95; }
.status-pill {
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border-radius: 999px;
  padding: 7px 12px 7px 10px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 0.82rem;
  font-weight: 750;
  white-space: nowrap;
}
.status-pill::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.status-pill.live { color: var(--ice); background: var(--ice-soft); border-color: transparent; }
.status-pill.live::before { background: var(--ice); box-shadow: 0 0 0 4px rgba(110, 224, 197, 0.15); }
.status-pill.offline { color: var(--amber); background: var(--amber-soft); border-color: transparent; }
.status-pill.offline::before { background: var(--amber); }

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0;
  margin-bottom: 22px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  overflow: hidden;
}
.stat {
  padding: 18px 20px 16px;
  border-right: 1px solid var(--line);
  border-top: 3px solid var(--muted);
  background: transparent;
}
.stat:last-child { border-right: 0; }
.stat.all { border-top-color: var(--accent); }
.stat.replied { border-top-color: var(--ice); }
.stat.silent { border-top-color: var(--amber); }
.stat.error { border-top-color: var(--red); }
.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 750;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.stat strong {
  display: block;
  margin-top: 10px;
  font-size: 2.4rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.stat.replied strong { color: var(--ice); }
.stat.silent strong { color: var(--amber); }
.stat.error strong { color: var(--red); }

.toolbar, .toolbar-actions, dialog header, dialog footer, .card-actions, .card-top {
  display: flex;
  align-items: center;
  gap: 10px;
}
.toolbar { justify-content: space-between; margin-bottom: 10px; flex-wrap: wrap; }
.toolbar-actions { flex-wrap: wrap; }
.search { position: relative; flex: 1; min-width: 220px; margin: 0; }
.filters { display: flex; gap: 4px; flex-wrap: wrap; }
.filters button {
  background: transparent;
  color: var(--muted);
  padding: 8px 2px;
  margin-right: 14px;
  border-radius: 0;
  border-bottom: 2px solid transparent;
  font-weight: 700;
}
.filters button.active {
  background: transparent;
  color: var(--ink);
  border-bottom-color: var(--accent);
}
.freshness { margin: 0 0 12px; color: var(--muted); font-size: 0.78rem; letter-spacing: 0.04em; text-transform: uppercase; min-height: 1.2em; }

.table-wrap, .empty {
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--card);
}
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td {
  text-align: left;
  padding: 15px 16px;
  vertical-align: top;
  border-bottom: 1px solid var(--line);
}
th {
  font-size: 0.7rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--card-2);
}
td {
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}
tr:last-child td { border-bottom: 0; }
tbody tr { cursor: pointer; }
tbody td:first-child { box-shadow: inset 3px 0 0 transparent; }
tbody tr.replied td:first-child { box-shadow: inset 3px 0 0 var(--ice); }
tbody tr.silent td:first-child { box-shadow: inset 3px 0 0 var(--amber); }
tbody tr.error td:first-child { box-shadow: inset 3px 0 0 var(--red); }
tbody tr:hover td { background: var(--card-2); }
.clip { max-width: 280px; }
.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--card-2);
  color: var(--muted);
}
.badge.replied { background: var(--ice-soft); color: var(--ice); }
.badge.error { background: var(--red-soft); color: var(--red); }
.badge.silent { background: var(--amber-soft); color: var(--amber); }
.empty { margin: 0; padding: 36px 20px; color: var(--muted); text-align: center; }
.older { margin-top: 16px; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}
.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px 18px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 230px;
  overflow: hidden;
}
.card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--muted);
}
.card.is-on::before { background: linear-gradient(var(--ice), var(--accent)); }
.card.is-off::before { background: var(--line); }
.card h3 { margin: 0; font-size: 1.45rem; line-height: 1.05; }
.card p { margin: 0; color: var(--muted); }
.card .desc { flex: 1; }
.file { color: var(--muted); font-size: 0.78rem; overflow-wrap: anywhere; }
.chip {
  align-self: flex-start;
  border-radius: 999px;
  padding: 4px 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
}
.card-actions { margin-top: 4px; }
.card-actions .ghost { margin-left: auto; }
.load-error { color: var(--red); font-size: 0.85rem; }

.switch {
  position: relative;
  width: 44px;
  height: 26px;
  margin: 0;
  flex: none;
}
.switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.switch i {
  display: block;
  height: 26px;
  border-radius: 999px;
  background: #314056;
  transition: background 0.15s ease;
}
[data-theme="light"] .switch i { background: #d7cbbd; }
.switch i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff8f0;
  transition: transform 0.15s ease;
}
.switch input:checked + i { background: var(--ice); }
.switch input:checked + i::after { transform: translateX(18px); }
.switch input:disabled + i { opacity: 0.45; }

dialog {
  border: 0;
  padding: 0;
  color: inherit;
  background: transparent;
  max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgba(6, 10, 18, 0.72); }
dialog form {
  width: 100%;
  max-height: calc(100vh - 32px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
}
#editor { width: min(960px, calc(100vw - 32px)); }
#detail { width: min(640px, calc(100vw - 32px)); }
#confirm { width: min(440px, calc(100vw - 32px)); }
dialog header, dialog footer {
  justify-content: space-between;
  flex: none;
  background: var(--card);
}
dialog header { padding: 22px 24px 12px; }
dialog footer { padding: 14px 24px 20px; border-top: 1px solid var(--line); }
dialog h2 { margin: 4px 0 0; font-size: 1.7rem; }
.editor-body, .detail-body { overflow: auto; padding: 8px 24px 20px; }
.editor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.editor-grid label { margin-top: 8px; }
.field-block { margin-top: 22px; }
.field-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px 16px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.field-head h3 { margin: 0; font-size: 1rem; font-family: inherit; letter-spacing: 0; }
.field-head p { margin: 0; color: var(--muted); font-size: 0.84rem; }
.editor-host {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--code);
}
.editor-host .cm-editor { min-height: 112px; }
.editor-code { margin-top: 16px; }
.editor-code .cm-editor { min-height: min(62vh, 640px); }
.editor-code .cm-scroller { min-height: min(62vh, 640px); }
.fn-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin-top: 12px;
  background: var(--card-2);
}
.detail-body h3 { margin: 18px 0 8px; font-size: 0.95rem; font-family: inherit; }
.meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  margin: 8px 0 4px;
}
.meta div { min-width: 0; }
.meta dt { color: var(--muted); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.1em; text-transform: uppercase; }
.meta dd { margin: 4px 0 0; overflow-wrap: anywhere; }
pre {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background: var(--code);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 0.84rem;
  line-height: 1.55;
}
.confirm-form { padding: 26px; gap: 8px; }
.confirm-form h2 { margin: 0; }
.confirm-form .lede { margin: 10px 0 18px; }
.confirm-form footer { padding: 0; border: 0; background: transparent; }

.toasts {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
  width: min(320px, calc(100vw - 32px));
}
.toast {
  margin: 0;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--accent);
  box-shadow: var(--shadow);
}
.toast.error { border-color: var(--red); color: var(--red); }

@media (max-width: 980px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat:nth-child(2) { border-right: 0; }
  .stat:nth-child(-n + 2) { border-bottom: 1px solid var(--line); }
  .editor-grid, .meta { grid-template-columns: 1fr; }
  .field-head { flex-direction: column; align-items: flex-start; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 5;
    height: auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    padding: 12px 14px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .brand { flex: 1 1 auto; min-width: 0; }
  .brand strong { white-space: nowrap; }
  .nav { flex-direction: row; flex: 1 1 100%; margin: 0; }
  .nav button { width: auto; white-space: nowrap; box-shadow: none; }
  .nav button.active { box-shadow: inset 0 -2px 0 var(--accent); }
  .sidebar-foot { flex-direction: row; margin: 0 0 0 auto; padding: 0; }
  .sidebar-foot button { width: auto; white-space: nowrap; padding: 8px 10px; }
  .workspace { padding: 20px 16px 48px; }
  .top, .toolbar { align-items: stretch; flex-direction: column; }
  th:last-child, td:last-child {
    position: sticky;
    right: 0;
    background: var(--card);
    border-left: 1px solid var(--line);
  }
  tbody tr:hover td:last-child { background: var(--card-2); }
}
