:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1f2933;
  --muted: #5b6573;
  --border: #e1e4e8;
  --primary: #0f766e;
  --primary-hover: #0b5f58;
  --on-primary: #ffffff;
  --focus: #2563eb;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --ok: #067647;
  --ok-bg: #e3f6ec;
  --warn: #9a5b00;
  --warn-bg: #fff3d6;
  --orange: #b54708;
  --orange-bg: #ffead5;
  --info: #3538cd;
  --info-bg: #eaecfd;
  --neutral: #4b5563;
  --neutral-bg: #eceef1;
  --bar-track: #e6e8ec;
  --bar-ok: #12a26b;
  --bar-warn: #e3a008;
  --bar-bad: #e5484d;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1e232b;
    --text: #e6e8eb;
    --muted: #a0a8b4;
    --border: #2b313a;
    --primary: #2dd4bf;
    --primary-hover: #5eead4;
    --on-primary: #06201d;
    --focus: #7aa7ff;
    --danger: #ff8a80;
    --danger-bg: #3a1d1b;
    --ok: #6ee7a8;
    --ok-bg: #12301f;
    --warn: #fbd168;
    --warn-bg: #352a10;
    --orange: #fdb07a;
    --orange-bg: #3a2414;
    --info: #a5b4fc;
    --info-bg: #222546;
    --neutral: #c3c9d2;
    --neutral-bg: #2a3038;
    --bar-track: #2b313a;
    --bar-ok: #34c38f;
    --bar-warn: #f0b429;
    --bar-bad: #ff6b6b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", Roboto, sans-serif;
  min-height: 100vh;
}
a { color: var(--primary); }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.noscript { padding: 16px; }
.boot { padding: 48px 16px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* progress bar */
.progress { position: fixed; inset: 0 0 auto 0; height: 3px; z-index: 1000; pointer-events: none; opacity: 0; transition: opacity .15s; }
.progress.on { opacity: 1; }
.progress-bar { height: 100%; width: 30%; background: var(--primary); animation: progress 1.1s ease-in-out infinite; }
@keyframes progress { 0% { transform: translateX(-100%); } 100% { transform: translateX(340%); } }

/* toasts */
.toasts { position: fixed; top: 10px; left: 50%; transform: translateX(-50%); z-index: 1100; display: flex; flex-direction: column; gap: 8px; width: min(440px, calc(100vw - 24px)); pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; border-radius: 8px; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-left: 4px solid var(--primary); box-shadow: var(--shadow-lg); animation: toast-in .15s ease-out; }
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--ok); }
.toast-msg { flex: 1; word-break: break-word; }
.toast-close { border: 0; background: none; color: var(--muted); font-size: 18px; line-height: 1; cursor: pointer; padding: 0 2px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; transition: background .08s, transform .05s, box-shadow .08s; white-space: nowrap; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.97); background: var(--border); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-primary:active { background: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-danger { color: #2a0b08; } }
.btn-danger:hover, .btn-danger:active { background: var(--danger); filter: brightness(.92); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-link-danger { color: var(--danger); }
.btn-sm { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn:disabled { cursor: progress; opacity: .7; transform: none; }
.btn.busy::before { content: ""; width: 12px; height: 12px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* inputs */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label, .label { font-weight: 500; font-size: 13px; }
.input, select.input { width: 100%; min-height: 38px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; }
.input:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
.input[aria-invalid="true"] { border-color: var(--danger); }
.hint { font-size: 12px; color: var(--muted); }
.form-error { color: var(--danger); background: var(--danger-bg); border-radius: 8px; padding: 8px 10px; margin-bottom: 14px; font-size: 13px; }

/* header */
.app-header { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50; }
.header-inner { max-width: 1280px; margin: 0 auto; padding: 0 16px; min-height: 56px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); text-decoration: none; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { padding: 6px 10px; border-radius: 6px; color: var(--muted); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a:active { background: var(--border); }
.nav a[aria-current="page"] { color: var(--primary); background: var(--surface-2); }
.user-box { display: flex; align-items: center; gap: 8px; min-width: 0; }
.user-email { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.role-badge { font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--info-bg); color: var(--info); font-weight: 600; white-space: nowrap; }
.role-badge.member { background: var(--neutral-bg); color: var(--neutral); }

@media (max-width: 640px) {
  .header-inner { gap: 4px 10px; padding: 6px 16px; }
  .brand span { display: none; }
  .nav { flex: 1; }
  .user-box { width: 100%; order: 3; justify-content: flex-end; padding-bottom: 4px; }
  .user-email { flex: 1; max-width: none; }
}

/* layout */
.view { max-width: 1280px; margin: 0 auto; padding: 20px 16px 48px; outline: none; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { font-size: 20px; margin: 0; flex: 1; min-width: 140px; }
.page-meta { color: var(--muted); font-size: 13px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 16px; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty-title { font-size: 15px; color: var(--text); font-weight: 500; margin-bottom: 4px; }
.load-error { text-align: center; padding: 40px 16px; }
.load-error p { margin: 0 0 12px; color: var(--muted); }

/* login */
.login-wrap { min-height: calc(100vh - 80px); display: flex; align-items: center; justify-content: center; }
.login-card { width: 100%; max-width: 380px; padding: 28px 24px; }
.login-card h1 { font-size: 22px; margin: 0 0 4px; }
.login-card .sub { color: var(--muted); margin: 0 0 20px; }
.login-card .btn { width: 100%; min-height: 40px; }
.totp-input { letter-spacing: .4em; font-size: 18px; text-align: center; font-variant-numeric: tabular-nums; }
.link-btn { border: 0; background: none; color: var(--primary); cursor: pointer; font: inherit; padding: 4px 0; margin-top: 10px; }

/* tables */
.table-wrap { overflow-x: auto; }
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; background: var(--surface-2); }
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data th:first-child { border-top-left-radius: var(--radius); }
table.data th:last-child { border-top-right-radius: var(--radius); }
.cell-name { font-weight: 500; word-break: break-all; }
.cell-sub { color: var(--muted); font-size: 12px; }
.mono { font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.row-actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.note-edit { display: flex; gap: 6px; align-items: center; }
.note-edit .input { min-width: 120px; }

/* capacity */
.cap { min-width: 90px; }
.cap-text { font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 3px; background: var(--bar-track); overflow: hidden; }
.bar > span { display: block; height: 100%; width: 0; background: var(--bar-ok); border-radius: 3px; }
.bar.warn > span { background: var(--bar-warn); }
.bar.bad > span { background: var(--bar-bad); }
.cap .bar { margin-top: 4px; width: 90px; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.st-active { background: var(--ok-bg); color: var(--ok); }
.st-error { background: var(--danger-bg); color: var(--danger); }
.st-rate_limited { background: var(--warn-bg); color: var(--warn); }
.st-overloaded { background: var(--orange-bg); color: var(--orange); }
.st-temp_unschedulable { background: var(--info-bg); color: var(--info); }
.st-paused, .st-disabled { background: var(--neutral-bg); color: var(--neutral); }
.status-until { display: block; font-size: 12px; color: var(--muted); margin-top: 3px; white-space: nowrap; }

/* on/off indicator */
.onoff { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }
.onoff .dot { width: 26px; height: 15px; border-radius: 999px; background: var(--bar-track); position: relative; }
.onoff .dot::after { content: ""; position: absolute; top: 2px; left: 2px; width: 11px; height: 11px; border-radius: 50%; background: var(--surface); box-shadow: 0 0 0 1px var(--border); }
.onoff.on .dot { background: var(--primary); }
.onoff.on .dot::after { left: 13px; box-shadow: none; }
.onoff.off { color: var(--muted); }

/* switch (interactive) */
.switch { position: relative; display: inline-flex; width: 40px; height: 24px; border-radius: 999px; border: 0; padding: 0; background: var(--bar-track); cursor: pointer; transition: background .1s; flex-shrink: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: left .1s; }
.switch[aria-checked="true"] { background: var(--primary); }
.switch[aria-checked="true"]::after { left: 19px; }
.switch:active::after { width: 22px; }
.switch[aria-checked="true"]:active::after { left: 15px; }
.switch.pending { opacity: .7; }
.switch-wrap { display: inline-flex; align-items: center; gap: 8px; }

/* usage windows */
.usage { display: flex; flex-direction: column; gap: 4px; min-width: 210px; }
.uw { display: grid; grid-template-columns: 62px 64px 38px 1fr; align-items: center; gap: 6px; font-size: 12px; }
.uw-label { color: var(--muted); white-space: nowrap; }
.uw-pct { text-align: right; font-variant-numeric: tabular-nums; font-weight: 500; }
.uw-reset { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.uw-unknown { color: var(--muted); }

/* toolbar */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar .input { width: auto; flex: 1; min-width: 160px; }
.add-form { display: grid; grid-template-columns: minmax(180px, 2fr) minmax(140px, 2fr) auto; gap: 8px; align-items: end; }
.add-form .field { margin: 0; }
.section-title { font-size: 15px; margin: 0 0 10px; }

/* dialogs */
dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); padding: 0; box-shadow: var(--shadow-lg); max-width: calc(100vw - 24px); }
dialog::backdrop { background: rgba(15, 18, 22, .45); }
.confirm { width: 400px; }
.dlg-body { padding: 20px; }
.dlg-body h2 { font-size: 17px; margin: 0 0 8px; }
.dlg-body p { margin: 0; color: var(--muted); word-break: break-word; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; align-items: center; flex-wrap: wrap; }
.dlg-foot .grow { flex: 1; color: var(--muted); font-size: 13px; }

.assign { width: 760px; height: min(760px, calc(100vh - 32px)); max-height: calc(100vh - 32px); }
.assign[open] { display: flex; flex-direction: column; }
.assign-head { padding: 16px 20px 0; }
.assign-head h2 { font-size: 17px; margin: 0 0 2px; }
.assign-head p { margin: 0 0 12px; color: var(--muted); word-break: break-all; }
.assign-tools { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 20px 10px; border-bottom: 1px solid var(--border); }
.assign-tools .input[type="search"] { flex: 1; min-width: 160px; }
.assign-tools select { width: auto; min-width: 120px; }
.select-all { display: flex; align-items: center; gap: 8px; padding: 10px 20px; border-bottom: 1px solid var(--border); font-size: 13px; cursor: pointer; }
.assign-list { flex: 1; overflow-y: auto; margin: 0; padding: 0; list-style: none; }
.assign-list li { border-bottom: 1px solid var(--border); }
.assign-list label { display: grid; grid-template-columns: 20px 1fr auto; gap: 10px; align-items: center; padding: 9px 20px; cursor: pointer; }
.assign-list label:hover { background: var(--surface-2); }
.assign-list label:active { background: var(--border); }
.assign-list input, .select-all input { width: 16px; height: 16px; accent-color: var(--primary); margin: 0; }
.assign-list .a-name { font-weight: 500; word-break: break-all; }
.assign-list .a-sub { color: var(--muted); font-size: 12px; word-break: break-all; }
.assign-state { padding: 32px 20px; text-align: center; color: var(--muted); }

/* stacked cards on narrow screens */
@media (max-width: 760px) {
  .view { padding: 14px 16px 40px; }
  table.data.stack, table.data.stack tbody, table.data.stack tr, table.data.stack td { display: block; width: 100%; }
  table.data.stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table.data.stack tr { padding: 12px 14px; border-bottom: 1px solid var(--border); }
  table.data.stack tr:last-child { border-bottom: 0; }
  table.data.stack td { border: 0; padding: 4px 0; display: grid; grid-template-columns: 78px 1fr; gap: 8px; align-items: start; }
  table.data.stack td::before { content: attr(data-label); color: var(--muted); font-size: 12px; padding-top: 2px; }
  table.data.stack td.td-title { grid-template-columns: 1fr; padding-bottom: 6px; }
  table.data.stack td.td-title::before { display: none; }
  table.data.stack .row-actions { justify-content: flex-start; }
  .usage { min-width: 0; }
  .uw { grid-template-columns: 58px 1fr 36px auto; }
  .add-form { grid-template-columns: 1fr; }
  .assign { width: 100vw; max-width: 100vw; height: 100%; max-height: 100%; margin: 0; border-radius: 0; }
  .assign .dlg-foot { border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
.add-card { margin-bottom: 16px; }
.add-card .hint { margin: 10px 0 0; }
.bind-tag { display: inline-block; margin-left: 6px; font-size: 11px; font-weight: 600; padding: 0 6px; border-radius: 4px; background: var(--neutral-bg); color: var(--neutral); vertical-align: 1px; white-space: nowrap; cursor: help; }
.bind-tag.bound { background: var(--ok-bg); color: var(--ok); }
@media (max-width: 760px) {
  table.data.stack td.td-title { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
  table.data.stack td.td-title .bind-tag { margin-left: 2px; }
}
.captcha-widget { min-height: 65px; display: flex; justify-content: center; }
.captcha-widget:empty { min-height: 0; }
.captcha-status { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.captcha-msg.error { color: var(--danger); }
/* the Turnstile widget is 300px wide: keep it inside the card down to 360px screens */
@media (max-width: 380px) { .login-card { padding: 24px 12px; } }
