:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e3e6ea;
  --border-strong: #cdd3da;
  --text: #14181d;
  --muted: #667085;
  --accent: #5b5bd6;
  --accent-hover: #4a4ac4;
  --accent-soft: #eef0fd;
  --on-accent: #ffffff;
  --danger: #c02b1d;
  --danger-soft: #fdf0ef;
  --ok: #067647;
  --warn: #b45309;
  --warn-soft: #fef6ec;
  --warn-border: #f3c98b;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a1f;
    --surface-2: #1c2027;
    --border: #282d35;
    --border-strong: #39404a;
    --text: #e6e8eb;
    --muted: #939ba7;
    --accent: #8b8bf0;
    --accent-hover: #9d9df5;
    --accent-soft: #23243a;
    --on-accent: #10121a;
    --danger: #ff9a8f;
    --danger-soft: #2a1917;
    --ok: #56d68c;
    --warn: #f0b76b;
    --warn-soft: #241c10;
    --warn-border: #5c4415;
    --shadow: none;
  }
}

*, *::before, *::after { box-sizing: border-box; }

/* hidden 屬性必須贏過任何 class 的 display，否則登入畫面關不掉 */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 15px; }
p { margin: 0; }

/* ==========================================================================
   版面
   ========================================================================== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar strong { font-size: 15px; font-weight: 650; }
.topbar-right { display: flex; align-items: center; gap: 14px; font-size: 13px; }

.nav {
  position: sticky;
  top: 56px;
  z-index: 19;
  display: flex;
  gap: 2px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}

.main {
  padding: 24px 24px 64px;
  max-width: 1040px;
  margin: 0 auto;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  margin-bottom: 18px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.note { color: var(--muted); font-size: 13px; line-height: 1.6; max-width: 68ch; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; text-align: center; margin-top: 16px; }
.error { color: var(--danger); font-size: 13px; }

.warn {
  margin-top: 16px;
  padding: 11px 14px;
  background: var(--danger-soft);
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  border-radius: 9px;
  font-size: 13px;
  line-height: 1.6;
}

/* ==========================================================================
   按鈕
   ========================================================================== */
button {
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: inherit;
  padding: 7px 14px;
  transition: background .12s, border-color .12s, color .12s;
}

button:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .5; cursor: default; }

.primary {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
  font-weight: 600;
}

.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

.ghost {
  border-color: transparent;
  background: transparent;
  padding: 5px 9px;
  font-size: 13px;
  color: var(--muted);
}

.ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.ghost.danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }

.tab {
  border: none;
  background: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 12px 14px;
  color: var(--muted);
  white-space: nowrap;
  font-size: 14px;
}

.tab:hover { background: none; color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ==========================================================================
   測試模式
   ========================================================================== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid transparent;
}

.badge-test { background: var(--warn-soft); border-color: var(--warn-border); color: var(--warn); }
.badge-live { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }

.mode-panel {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  margin: 18px 0 4px;
  padding: 16px 18px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.mode-panel.is-live { background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.mode-panel.is-test { background: var(--warn-soft); border-color: var(--warn-border); }

.mode-text { flex: 1; min-width: 220px; }
.mode-title { font-weight: 650; font-size: 15px; margin-bottom: 2px; }
.mode-sub { color: var(--muted); font-size: 13px; }

/* 開關 */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  border: none;
  background: none;
  padding: 4px;
}

.toggle:hover { background: none; }

.toggle-track {
  position: relative;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: var(--border-strong);
  transition: background .16s;
  flex: none;
}

.toggle[aria-checked="true"] .toggle-track { background: var(--warn); }

.toggle-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .16s;
}

.toggle[aria-checked="true"] .toggle-knob { transform: translateX(20px); }
.toggle-caption { font-size: 14px; font-weight: 600; white-space: nowrap; }

/* ==========================================================================
   表單
   ========================================================================== */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { color: var(--muted); font-size: 13px; font-weight: 500; }

.field-inline {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 0;
}

.field-inline > span { min-width: 92px; }

input[type="text"], input[type="email"], input[type="password"],
input[type="date"], input[type="datetime-local"], textarea, select {
  font: inherit;
  font-size: 14px;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 8px 11px;
  width: 100%;
  max-width: 420px;
  transition: border-color .12s, box-shadow .12s;
}

.field-inline input { max-width: 280px; width: auto; flex: 1; }

textarea { resize: vertical; min-height: 72px; line-height: 1.6; }
select { max-width: 220px; }

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input:disabled { background: var(--surface-2); color: var(--muted); }
input:not(:placeholder-shown):invalid { border-color: var(--danger); }

input[type="checkbox"] {
  width: 18px; height: 18px;
  accent-color: var(--accent);
  margin: 3px 0;
}

.form { background: var(--surface-2); }
.form-actions { display: flex; gap: 8px; margin-top: 20px; }

/* ==========================================================================
   表格
   ========================================================================== */
.grid-wrap {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}

.grid { width: 100%; border-collapse: collapse; font-size: 14px; }

.grid th {
  text-align: left;
  padding: 9px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  letter-spacing: .02em;
  white-space: nowrap;
}

.grid td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.grid tbody tr:last-child td { border-bottom: none; }
.grid tbody tr:hover td { background: var(--surface-2); }
.grid td.empty { text-align: center; padding: 32px; color: var(--muted); }

.actions-col { white-space: nowrap; width: 1%; text-align: right; }

.grid pre {
  margin: 8px 0 0;
  padding: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 12px;
  line-height: 1.5;
  max-width: 460px;
  overflow-x: auto;
}

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
details summary { cursor: pointer; color: var(--muted); }
details[open] summary { color: var(--text); }

/* ==========================================================================
   登入
   ========================================================================== */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  margin: 0;
  padding: 32px 28px;
}

.login-card h1 { text-align: center; margin-bottom: 24px; }
.login-card .field { margin-bottom: 16px; }
.login-card button { width: 100%; padding: 10px; margin-top: 8px; }
.login-card input { max-width: none; }
.login-card .error { margin-top: 14px; text-align: center; }

/* ==========================================================================
   toast
   ========================================================================== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  padding: 11px 20px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--ok);
  box-shadow: 0 8px 28px rgba(16, 24, 40, .16);
  font-size: 14px;
  max-width: min(92vw, 540px);
  z-index: 50;
}

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

@media (max-width: 640px) {
  .topbar { padding: 0 14px; }
  .main { padding: 16px 14px 48px; }
  .card { padding: 16px; }
  .topbar-right .email { display: none; }
}
