:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4e9;
  --text: #1b1f24;
  --muted: #646d78;
  --accent: #0b5cd5;
  --accent-text: #ffffff;
  --add: #18794e;
  --add-bg: #e3f5ea;
  --drop: #c4302b;
  --drop-bg: #fde8e7;
  --event: #8a5a00;
  --event-bg: #fdf1d8;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242b;
    --border: #2b313a;
    --text: #e6e9ee;
    --muted: #949daa;
    --accent: #4d8ff5;
    --accent-text: #0b0e12;
    --add: #4cc38a;
    --add-bg: #11291d;
    --drop: #f27066;
    --drop-bg: #33171a;
    --event: #e5b454;
    --event-bg: #2e2412;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 16px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 24px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { color: var(--accent); font-size: 22px; }
.sub { color: var(--muted); font-size: 12px; }
.tabs { display: flex; gap: 4px; }
.tab {
  border: 0; background: none; color: var(--muted);
  padding: 8px 12px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--surface-2); color: var(--text); }
.run-box { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.run-status { color: var(--muted); font-size: 12px; text-align: right; }
.run-status .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.dot.ok { background: var(--add); }
.dot.partial { background: var(--event); }
.dot.error { background: var(--drop); }
.dot.running { background: var(--accent); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 12px; border-radius: 8px; font: inherit; font-weight: 500;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: default; }

.banner { margin: 16px 24px 0; padding: 10px 14px; border-radius: var(--radius); background: var(--event-bg); color: var(--event); }
.banner.error { background: var(--drop-bg); color: var(--drop); }

main { padding: 16px 24px 48px; max-width: 1400px; margin: 0 auto; }

/* Filters */
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-bottom: 16px; }
.filters label, .card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
.filters .grow { flex: 1; min-width: 180px; }
select, input {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; min-width: 0;
}
input.narrow { width: 110px; }
body[data-view="events"] .log-only,
body[data-view="log"] .events-only,
body[data-view="settings"] .filters { display: none; }

/* Tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .label { color: var(--muted); font-size: 12px; }
.tile .value { font-size: 24px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .value .add { font-size: 20px; }
.tile .value .drop { font-size: 20px; }

/* Cards & tables */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 18px; }
.table-card { overflow-x: auto; }
.table-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 0; color: var(--muted); }
.table-head h2 { margin: 0; color: var(--text); }
table.data { width: 100%; border-collapse: collapse; }
table.data th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
  padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; user-select: none;
}
table.data th[data-sort] { cursor: pointer; }
table.data th[data-sort]:hover { color: var(--text); }
table.data th.sorted::after { content: " ▾"; }
table.data th.sorted.asc::after { content: " ▴"; }
table.data td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr.clickable:hover { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
th.num { text-align: right; }
.code { color: var(--muted); font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.nowrap { white-space: nowrap; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.more { display: flex; justify-content: center; padding: 12px; }

.add { color: var(--add); font-weight: 600; }
.drop { color: var(--drop); font-weight: 600; }
.zero { color: var(--muted); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.ADDED { background: var(--add-bg); color: var(--add); }
.badge.DROPPED { background: var(--drop-bg); color: var(--drop); }
.badge.EVENT_ADDED, .badge.EVENT_REMOVED { background: var(--event-bg); color: var(--event); }
.badge.status-ok { background: var(--add-bg); color: var(--add); }
.badge.status-partial { background: var(--event-bg); color: var(--event); }
.badge.status-error { background: var(--drop-bg); color: var(--drop); }
.badge.status-running { background: var(--surface-2); color: var(--accent); }

/* Settings */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; margin-bottom: 16px; }
.card form, .card.pad { display: flex; flex-direction: column; gap: 12px; }
.card label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; font-weight: 400; }
.row { display: flex; gap: 8px; }
.msg { margin: 0; min-height: 1em; }
.msg.ok { color: var(--add); }
.msg.err { color: var(--drop); }

/* Drawer */
.drawer { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 10; display: flex; justify-content: flex-end; }
.drawer-panel { position: relative; width: min(640px, 100%); height: 100%; overflow-y: auto; background: var(--surface); padding: 24px; border-left: 1px solid var(--border); }
.drawer .close { position: absolute; top: 14px; right: 16px; border: 0; background: none; color: var(--muted); font-size: 26px; cursor: pointer; }
.drawer h2 { font-size: 18px; margin: 0 32px 4px 0; }
.drawer h3 { font-size: 14px; margin: 24px 0 8px; }
.meta { color: var(--muted); margin-bottom: 8px; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; padding: 12px 16px; gap: 12px; }
  .run-box { margin-left: 0; width: 100%; justify-content: space-between; }
  main { padding: 16px; }
  .run-status { text-align: left; }
}

/* Check-progress popup */
.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 20;
  width: min(380px, calc(100vw - 32px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 28px rgba(16, 24, 40, .18);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 8px;
  animation: toast-in .18s ease-out;
}
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }
.toast-head { display: flex; align-items: center; gap: 8px; }
.toast-head .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex: none; }
.toast-head #pt-count { margin-left: auto; font-variant-numeric: tabular-nums; }
.toast-close { border: 0; background: none; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; padding: 0 0 0 4px; }
.pt-event { min-height: 1.4em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pt-event a { color: var(--text); text-decoration: none; }
.pt-event a:hover { text-decoration: underline; }
.bar { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .4s ease; }
.bar.indeterminate .bar-fill { width: 30% !important; animation: slide 1.1s ease-in-out infinite alternate; }
@keyframes slide { from { margin-left: 0; } to { margin-left: 70%; } }
.toast.done .bar-fill { background: var(--add); }
.toast.failed .bar-fill { background: var(--drop); }

/* Confirm / message dialog */
.dialog {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--border); border-radius: 12px; padding: 20px;
  background: var(--surface); color: var(--text);
  box-shadow: 0 12px 40px rgba(16, 24, 40, .25);
}
.dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialog h2 { font-size: 16px; margin: 0 0 8px; }
.dialog p { margin: 0 0 18px; color: var(--muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Stop controls */
.btn.danger { background: var(--surface); border-color: var(--drop); color: var(--drop); }
.btn.danger:hover { background: var(--drop-bg); }
.btn-sm { padding: 3px 10px; font-size: 12px; }
.pt-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#pt-stop { color: var(--drop); border-color: var(--drop); }
.toast.stopped .bar-fill { background: var(--event); }
.dot.cancelled { background: var(--event); }
.badge.status-cancelled { background: var(--event-bg); color: var(--event); }

/* Focus regions */
.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.field-label .muted { font-weight: 400; }
.region-picker {
  max-height: 210px; overflow-y: auto; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}
.picker-group + .picker-group { margin-top: 8px; }
.picker-head { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 2px; }
.region-picker label.check { font-size: 13px; padding: 2px 0; }
.focus-mark { color: var(--event); }

/* Capacity */
.spots { font-variant-numeric: tabular-nums; }
.spots.low { color: var(--event); font-weight: 600; }
.spots.full { color: var(--drop); font-weight: 600; }

/* FRC Events API (published lists) vs FIRST live data */
.api-behind { color: var(--event); font-weight: 600; }
.source-tag {
  display: inline-block; margin-top: 3px; padding: 0 6px; border-radius: 4px;
  font-size: 11px; font-weight: 600; color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); white-space: nowrap;
}
.source-tag.inline { margin: 0 0 0 4px; vertical-align: 1px; }

/* Sign-in page */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-wrap { width: min(380px, calc(100vw - 32px)); padding: 0; }
.login-brand { justify-content: center; margin-bottom: 16px; }
.login-card { gap: 14px; }

/* Signed-in user / roles */
.user-box { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
body[data-role="viewer"] .admin-only { display: none !important; }
body:not([data-server]) .server-only { display: none !important; }
body[data-server] .local-only { display: none !important; }
.users-table td { vertical-align: middle; }
.add-user { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.add-user label { flex: 1; min-width: 120px; }
