:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --border: #dde1e8;
  --primary: #2f5bea;
  --primary-text: #ffffff;
  --danger: #c62828;
  --ok: #1b7f3b;
  --warn: #a15c00;
  --bad: #c62828;
  --ok-bg: #e3f4e8;
  --warn-bg: #fdf0d9;
  --bad-bg: #fbe4e4;
  --muted-bg: #eceff3;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #12151b;
    --surface: #1b2029;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --border: #2e3542;
    --primary: #6d8cff;
    --primary-text: #0b1020;
    --danger: #ff7a7a;
    --ok: #66d28a;
    --warn: #f2b45a;
    --bad: #ff7a7a;
    --ok-bg: #16301f;
    --warn-bg: #3a2d14;
    --bad-bg: #3a1c1c;
    --muted-bg: #262c37;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 16px; }
.muted, .sub, .hint { color: var(--muted); }
.sub, .hint { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.stack { display: flex; flex-direction: column; gap: 12px; }

/* controls */
input[type='text'], input[type='password'], input[type='search'], input[type='number'], select, textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
header input[type='search'] { width: 280px; flex: none; }

button {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.small { padding: 3px 8px; font-size: 12px; }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
button.danger { color: var(--danger); border-color: var(--danger); }
button.danger[type='submit'] { background: var(--danger); color: var(--primary-text); }
button.icon { border: 0; background: none; font-size: 22px; line-height: 1; padding: 0 6px; }

/* layout */
header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
nav { display: flex; gap: 4px; flex: 1; }
.tab { border-color: transparent; background: none; }
.tab.active { background: var(--muted-bg); font-weight: 600; }
.count { margin-left: 6px; padding: 0 6px; border-radius: 10px; background: var(--muted-bg); color: var(--muted); font-size: 12px; }
main { padding: 16px; }
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

/* table */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; white-space: nowrap; }
.check-col { width: 32px; }
.actions-col { text-align: right; }
.actions-col button { margin: 0 0 4px 4px; }

/* badges, chips */
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; background: var(--muted-bg); color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge + .badge { margin-left: 4px; }
.chips { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.chip { padding: 0 7px; border-radius: 4px; background: var(--muted-bg); font-size: 12px; font-family: ui-monospace, Menlo, Consolas, monospace; }

/* banners, toasts */
.banner { padding: 8px 12px; border-radius: var(--radius); }
.banner.error { background: var(--bad-bg); color: var(--bad); margin-bottom: 12px; }
.banner[hidden] { display: none; }
.bulkbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; padding: 8px 12px; background: var(--muted-bg); border-radius: var(--radius); }
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: min(420px, calc(100vw - 32px)); }
.toast { padding: 10px 14px; border-radius: var(--radius); background: var(--text); color: var(--bg); cursor: pointer; box-shadow: 0 4px 16px rgb(0 0 0 / 25%); }
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

/* forms and dialogs */
.field { display: flex; flex-direction: column; gap: 4px; }
.lbl { font-weight: 500; }
.req { color: var(--danger); }
.check { display: flex; align-items: center; gap: 8px; }
.check input { flex: none; }
.checks { display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow: auto; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius); }
.checks fieldset, fieldset.bare { margin: 0; padding: 0; border: 0; min-width: 0; }
.checks legend { padding: 0; font-weight: 600; font-size: 12px; color: var(--muted); }
.optin { display: flex; flex-direction: column; gap: 4px; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius); }
.optin fieldset:disabled { opacity: 0.45; }
details { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 10px; }
summary { cursor: pointer; font-weight: 500; margin-bottom: 4px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; }

dialog {
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
dialog.wide { width: min(720px, calc(100vw - 24px)); }
dialog::backdrop { background: rgb(0 0 0 / 50%); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.dialog-body { padding: 16px; }

.login { width: min(340px, calc(100vw - 32px)); margin: 18vh auto 0; padding: 24px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }

@media (max-width: 640px) {
  header { padding: 8px 12px; }
  nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  header input[type='search'] { width: auto; flex: 1; min-width: 0; }
  main { padding: 12px; }
}
