/* ── 設計代幣 ─────────────────────────────────────────────────────────── */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --surface-3: #e3eaf3;
  --border: #d4dde8;
  --border-strong: #b9c6d6;
  --text: #16202c;
  --text-dim: #5b6a7d;
  --text-faint: #8695a7;
  --brand: #1e4b8f;
  --brand-ink: #ffffff;
  --brand-soft: #dbe7f8;
  --ok: #117a4a; --ok-soft: #d8f0e3;
  --warn: #9a6400; --warn-soft: #fdeecb;
  --bad: #b3261e; --bad-soft: #fbe0de;
  --neutral: #5b6a7d; --neutral-soft: #e6ebf1;
  --info: #1c5f9e; --info-soft: #dceaf7;
  --rose: #9c3368; --rose-soft: #fadfeb;
  /* 序數藍階：聚會情況是有序的，用單一色相四階（已過 validate_palette --ordinal） */
  --ord-1: #0d366b; --ord-2: #256abf; --ord-3: #3987e5; --ord-4: #6da7ec;
  --nodata: #c9c8c2;
  --seq-0: #f0f4f9; --seq-1: #cde2fb; --seq-2: #9ec5f4; --seq-3: #6da7ec;
  --seq-4: #3987e5; --seq-5: #256abf; --seq-6: #184f95;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16,32,52,.06), 0 6px 18px rgba(16,32,52,.08);
  --row-h: 38px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1420; --surface: #141d2b; --surface-2: #1b2635; --surface-3: #223047;
    --border: #2a3a51; --border-strong: #3a4e6b;
    --text: #e8eef6; --text-dim: #9fb1c7; --text-faint: #7589a1;
    --brand: #4a86d8; --brand-ink: #081220; --brand-soft: #1d3153;
    --ok: #4ddb9b; --ok-soft: #10392a;
    --warn: #ffc95c; --warn-soft: #3d2f0d;
    --bad: #ff8a80; --bad-soft: #43201e;
    --neutral: #9fb1c7; --neutral-soft: #242f41;
    --info: #7bb6ef; --info-soft: #16304c;
    --rose: #f5a0c6; --rose-soft: #3e1c2d;
    --ord-1: #cde2fb; --ord-2: #86b6ef; --ord-3: #3987e5; --ord-4: #1c5cab;
    --nodata: #3a3a36;
    --seq-0: #1f1f1e; --seq-1: #1c3350; --seq-2: #1c5cab; --seq-3: #2a78d6;
    --seq-4: #5598e7; --seq-5: #86b6ef; --seq-6: #cde2fb;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(84px + var(--safe-b));
}
h1, h2, h3 { margin: 0; font-weight: 700; }
button, input, select, textarea { font: inherit; color: inherit; }

.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 100;
  background: var(--brand); color: var(--brand-ink); padding: 8px 14px; border-radius: 0 0 var(--radius) 0;
}
.skip-link:focus { left: 0; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 4px; }

/* ── 頂部 ─────────────────────────────────────────────────────────────── */
.appbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px clamp(10px, 2vw, 20px) 8px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
}
.appbar__row { display: flex; align-items: flex-start; gap: 12px; }
.appbar__title { min-width: 0; flex: 1; }
.appbar__title h1 {
  font-size: clamp(16px, 2.4vw, 20px); letter-spacing: .3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.appbar__sub { margin: 2px 0 0; font-size: 12px; color: var(--text-dim); }
.appbar__actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

/* ── 按鈕 ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.btn--primary:hover { filter: brightness(1.08); background: var(--brand); }
.btn--ghost { background: transparent; }
.btn--danger { background: var(--bad); border-color: var(--bad); color: #fff; font-weight: 600; }
@media (prefers-color-scheme: dark) { .btn--danger { color: #2a0d0b; } }
.btn--danger-ghost { background: transparent; border-color: transparent; color: var(--bad); }
.btn--danger-ghost:hover { background: var(--bad-soft); }
.icon-btn {
  border: 0; background: transparent; cursor: pointer; font-size: 18px;
  width: 34px; height: 34px; border-radius: var(--radius-sm); color: var(--text-dim);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ── 選單 ─────────────────────────────────────────────────────────────── */
.menu { position: relative; }
.menu > summary { list-style: none; min-width: 40px; }
.menu > summary::-webkit-details-marker { display: none; }
.menu__panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 40;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 6px; min-width: 190px;
}
.menu__panel hr { border: 0; border-top: 1px solid var(--border); margin: 6px 4px; }
.menu__item {
  display: block; width: 100%; text-align: start; padding: 9px 10px;
  border: 0; border-radius: var(--radius-sm); background: transparent; cursor: pointer; font-size: 14px;
}
.menu__item:hover { background: var(--surface-2); }

/* ── 統計列 ───────────────────────────────────────────────────────────── */
.stats {
  display: flex; gap: 8px; margin-top: 10px;
  overflow-x: auto; scrollbar-width: none; padding-bottom: 2px;
}
.stats::-webkit-scrollbar { display: none; }
.stat {
  flex: 0 0 auto; min-width: 104px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface-2); padding: 7px 11px;
  cursor: pointer; text-align: start;
}
.stat[aria-pressed="true"] { border-color: var(--brand); background: var(--brand-soft); }
.stat__label { font-size: 11px; color: var(--text-dim); display: block; }
.stat__value { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat__meta { font-size: 11px; color: var(--text-dim); margin-inline-start: 6px; font-weight: 500; }

/* ── 工具列 ───────────────────────────────────────────────────────────── */
.toolbar { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.field { position: relative; flex: 1 1 230px; min-width: 170px; }
.field__icon { position: absolute; inset-inline-start: 10px; top: 50%; transform: translateY(-50%); font-size: 13px; opacity: .6; }
.field input { width: 100%; padding-inline-start: 32px; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 8px 10px; min-height: 36px; width: 100%;
}
select { padding-inline-end: 26px; }
textarea { min-height: 62px; resize: vertical; }
.toolbar select { flex: 0 1 auto; width: auto; min-width: 118px; }

/* ── 橫幅 / 狀態 ──────────────────────────────────────────────────────── */
.banner {
  margin: 10px clamp(10px, 2vw, 20px) 0; padding: 9px 12px;
  border-radius: var(--radius); font-size: 13px;
  background: var(--warn-soft); color: var(--warn); border: 1px solid currentColor;
}
.state { padding: 28px clamp(10px, 2vw, 20px); text-align: center; color: var(--text-dim); }
.state__title { font-weight: 700; color: var(--text); margin: 0 0 4px; }
.state__text { margin: 0 0 14px; font-size: 14px; }
.state--error .state__title { color: var(--bad); }
.skeleton {
  height: 56px; border-radius: var(--radius); margin-bottom: 10px;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%; animation: shimmer 1.3s ease infinite;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } * { transition: none !important; } }

/* ── 分組 ─────────────────────────────────────────────────────────────── */
.group { margin: 14px clamp(10px, 2vw, 20px) 0; }
.group__head {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
  padding: 0 2px 6px;
}
.group__name { font-size: 15px; font-weight: 700; }
.group__counts { font-size: 12px; color: var(--text-dim); display: flex; gap: 8px; flex-wrap: wrap; }
.group__counts b { font-variant-numeric: tabular-nums; color: var(--text); }

/* ── 表格（桌機） ─────────────────────────────────────────────────────── */
.table-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow);
}
table.roster { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 14px; }
.roster th, .roster td {
  padding: 0 10px; height: var(--row-h);
  border-bottom: 1px solid var(--border); text-align: start; white-space: nowrap;
}
.roster thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--surface-3); color: var(--text-dim);
  font-size: 12px; font-weight: 600; letter-spacing: .4px;
  border-bottom: 1px solid var(--border-strong);
}
.roster tbody tr:hover { background: var(--surface-2); }
.roster tbody tr:last-child td { border-bottom: 0; }
.col-seq { width: 46px; color: var(--text-faint); font-variant-numeric: tabular-nums; text-align: center; }
.col-name { font-weight: 600; }
.col-act { width: 84px; text-align: end; }
.cell-dim { color: var(--text-faint); }
.cell-pending { color: var(--text-faint); font-style: italic; }
.roster .row-act { opacity: 0; transition: opacity .12s; }
.roster tr:hover .row-act, .roster tr:focus-within .row-act { opacity: 1; }
@media (hover: none) { .roster .row-act { opacity: 1; } }

/* ── 標籤 ─────────────────────────────────────────────────────────────── */
.tag {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; line-height: 19px; white-space: nowrap;
}
.tag--ok { background: var(--ok-soft); color: var(--ok); }
.tag--warn { background: var(--warn-soft); color: var(--warn); }
.tag--bad { background: var(--bad-soft); color: var(--bad); }
.tag--neutral { background: var(--neutral-soft); color: var(--neutral); }
.tag--info { background: var(--info-soft); color: var(--info); }
.tag--rose { background: var(--rose-soft); color: var(--rose); }
.tag--outline { background: transparent; border: 1px dashed var(--border-strong); color: var(--text-dim); font-weight: 500; }

/* ── 卡片（手機） ─────────────────────────────────────────────────────── */
.cards { display: grid; gap: 8px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 11px 12px; box-shadow: var(--shadow); cursor: pointer;
  display: grid; gap: 7px;
}
.card:active { background: var(--surface-2); }
.card__top { display: flex; align-items: center; gap: 8px; }
.card__seq { color: var(--text-faint); font-size: 12px; font-variant-numeric: tabular-nums; min-width: 20px; }
.card__name { font-weight: 700; font-size: 16px; }
.card__tags { margin-inline-start: auto; display: flex; gap: 5px; }
.card__meta { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--text-dim); }
.card__meta span b { color: var(--text); font-weight: 600; }

/* 斷點：≥900px 用表格，其他用卡片 */
.cards { display: grid; }
.table-wrap { display: none; }
@media (min-width: 900px) {
  .cards { display: none; }
  .table-wrap { display: block; }
}

/* ── 對話框 ───────────────────────────────────────────────────────────── */
.dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(680px, 100%);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
  max-height: 92dvh;
}
.dialog--sm { width: min(420px, 100%); }
.dialog::backdrop { background: rgba(8, 16, 28, .5); backdrop-filter: blur(2px); }
.dialog .form { display: flex; flex-direction: column; max-height: 92dvh; }
.dialog__head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.dialog__head h2 { font-size: 16px; flex: 1; }
.dialog__body { padding: 16px; overflow: auto; }
.dialog__foot {
  display: flex; gap: 8px; align-items: center;
  padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface-2);
  padding-bottom: calc(12px + var(--safe-b));
}
.spacer { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 620px) { .grid2 { grid-template-columns: 1fr; } }
.lbl { display: grid; gap: 5px; font-size: 13px; color: var(--text-dim); font-weight: 600; }
.lbl--full { grid-column: 1 / -1; }
.lbl--row { display: flex; align-items: center; gap: 8px; }
.lbl--row input { width: auto; min-height: auto; }
.lbl input, .lbl select, .lbl textarea { color: var(--text); font-weight: 400; }
fieldset.lbl { border: 0; padding: 0; margin: 0; }
fieldset.lbl legend { padding: 0; font-size: 13px; font-weight: 600; color: var(--text-dim); }
.req { color: var(--bad); }
.hint { font-size: 12px; color: var(--text-faint); font-weight: 400; margin: 4px 0 0; }
.form__error {
  grid-column: 1 / -1; margin: 0; padding: 9px 11px; border-radius: var(--radius-sm);
  background: var(--bad-soft); color: var(--bad); font-size: 13px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.chip {
  padding: 6px 13px; border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--surface); cursor: pointer; font-size: 13px;
}
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.api-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.api-table td { padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.api-table td:first-child { width: 58px; }
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .92em;
  background: var(--surface-2); padding: 1px 5px; border-radius: 4px;
}

/* ── FAB / Toast ──────────────────────────────────────────────────────── */
.fab-wrap { position: fixed; inset-inline-end: 16px; bottom: calc(16px + var(--safe-b)); z-index: 25; }
.fab {
  width: 56px; height: 56px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--brand); color: var(--brand-ink); font-size: 28px; line-height: 1;
  box-shadow: 0 6px 20px rgba(16, 40, 80, .35);
}
@media (min-width: 900px) { .fab-wrap { display: none; } }
.toasts {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(20px + var(--safe-b)); z-index: 60;
  display: grid; gap: 8px; width: min(92vw, 420px);
}
.toast {
  padding: 11px 14px; border-radius: var(--radius); font-size: 14px;
  background: #16202c; color: #f2f6fb; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 10px;
  animation: rise .18s ease;
}
.toast--ok { background: #0f5f3c; }
.toast--bad { background: #8c1d18; }
.toast button { background: transparent; border: 0; color: inherit; font-weight: 700; cursor: pointer; margin-inline-start: auto; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

@media print {
  .appbar__actions, .toolbar, .fab-wrap, .toasts, .col-act, .banner { display: none !important; }
  body { background: #fff; padding: 0; }
  .cards { display: none; } .table-wrap { display: block; box-shadow: none; }
}

/* ── 牧養名單：加寬表格與固定欄 ───────────────────────────────────────── */
.tag--amber { background: var(--warn-soft); color: var(--warn); }
.hint--full { grid-column: 1 / -1; margin: -4px 0 2px; }

.col-hall { width: 52px; text-align: center; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.col-time { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.col-note { color: var(--text-dim); max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.col-name { min-width: 76px; }

/* 橫向捲動時固定序號／會所／姓名 */
.roster .col-seq, .roster .col-hall, .roster .col-name { position: sticky; z-index: 1; background: var(--surface); }
.roster .col-seq { inset-inline-start: 0; }
.roster .col-hall { inset-inline-start: 46px; }
.roster .col-name { inset-inline-start: 98px; box-shadow: 1px 0 0 var(--border); }
.roster thead .col-seq, .roster thead .col-hall, .roster thead .col-name { z-index: 3; background: var(--surface-3); }
.roster tbody tr:hover .col-seq,
.roster tbody tr:hover .col-hall,
.roster tbody tr:hover .col-name { background: var(--surface-2); }


/* ── 照片 ─────────────────────────────────────────────────────────────── */
.avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--surface-3) center/cover no-repeat;
  display: inline-grid; place-items: center; flex: 0 0 auto;
  font-size: 11px; font-weight: 700; color: var(--text-dim);
  border: 1px solid var(--border);
  overflow: hidden; vertical-align: middle;
}
.avatar--lg { width: 96px; height: 96px; font-size: 30px; }
.avatar--card { width: 40px; height: 40px; font-size: 15px; }
.roster .col-name { display: flex; align-items: center; gap: 8px; }
.photo-row {
  grid-column: 1 / -1; display: flex; gap: 14px; align-items: center;
  padding-bottom: 12px; border-bottom: 1px solid var(--border);
}
.photo-row__actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.photo-row__actions .hint { width: 100%; margin: 0; }


/* ── 數據分析 ─────────────────────────────────────────────────────────── */
.dialog--wide { width: min(1180px, 97vw); }
.ana { display: grid; gap: 14px; }
.ana__row { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap: 14px; }
@media (max-width: 860px) { .ana__row { grid-template-columns: 1fr; } }
.panel {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px 14px;
}
.panel__h { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 9px; }
.panel__t { font-size: 13px; font-weight: 700; margin: 0; }
.panel__s { font-size: 11.5px; color: var(--text-faint); font-weight: 400; }
.ana__foot { font-size: 11.5px; color: var(--text-faint); flex: 1; line-height: 1.45; }

.ana-kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 8px; }
.ana-kpi__c { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px 10px; }
.ana-kpi__v { font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.ana-kpi__l { font-size: 11px; color: var(--text-dim); }
.ana-kpi--alert .ana-kpi__v { color: var(--bad); }

.ana-seg { display: flex; flex-wrap: wrap; gap: 5px; }
.ana-seg button {
  font: inherit; font-size: 12px; padding: 4px 9px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text-dim); border-radius: 999px;
}
.ana-seg button[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }

.ana-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 8px; }
.ana-legend__i { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.ana-sw { width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(0,0,0,.15); }

.ugroup { margin-bottom: 11px; }
.ugroup__h { display: flex; align-items: baseline; gap: 7px; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.ugroup__n { font-weight: 700; color: var(--text); font-size: 12.5px; }
.cells { display: flex; flex-wrap: wrap; gap: 3px; }
.cell {
  width: 25px; height: 25px; border-radius: 4px; border: 0; padding: 0; cursor: pointer;
  display: grid; place-items: center; font-size: 12px; font-weight: 600;
  font-family: inherit; color: #fff;
}
.cell[data-ink="dark"] { color: #0b0b0b; }
.cell[data-nodata="1"] { background-image: repeating-linear-gradient(135deg, rgba(0,0,0,.17) 0 2px, transparent 2px 5px); }
.cell:hover { outline: 2px solid var(--text); outline-offset: 1px; }

.abars { display: grid; gap: 4px; }
.abar { display: grid; grid-template-columns: 88px 1fr 32px; gap: 8px; align-items: center; font-size: 12.5px; }
.abar__l { text-align: right; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.abar__t { display: block; background: var(--surface-3); border-radius: 0 4px 4px 0; height: 15px; }
.abar__f { display: block; height: 100%; background: var(--brand); border-radius: 0 4px 4px 0; min-width: 2px; }
.abar__n { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-dim); }
.abar--gap .abar__f { background: var(--bad); }
.abar--gap .abar__l { color: var(--bad); font-weight: 700; }

table.heat { border-collapse: separate; border-spacing: 2px; font-size: 12px; }
.heat th { font-weight: 600; color: var(--text-dim); font-size: 11.5px; padding: 2px 5px; }
.heat th[scope="row"] { text-align: right; }
.heat td { width: 42px; height: 28px; text-align: center; border-radius: 4px; font-variant-numeric: tabular-nums; }

table.atbl { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.atbl th { text-align: start; font-size: 11px; letter-spacing: .05em; color: var(--text-faint); padding: 5px 7px; border-bottom: 1px solid var(--border); }
.atbl td { padding: 5px 7px; border-bottom: 1px solid var(--border); }
.atbl tbody tr { cursor: pointer; }
.atbl tbody tr:hover { background: var(--surface-3); }
.scrolly { max-height: 260px; overflow: auto; }
