:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #1d2433;
  --muted: #6b7486;
  --line: #dde2ec;
  --brand: #2f6fde;
  --brand-ink: #ffffff;
  --err: #d23b3b;
  --warn-bg: #fff6e0;
  --mutual: #19a974;

  --k-early: #ffe58f;
  --k-late_short: #bae0ff;
  --k-late_long: #4c7bd9;
  --k-on_early: #eadcff;
  --k-on_late: #8f6fd1;
  --k-standby: #ffd6a5;
  --k-off: #ffffff;
  --k-al: #d9ede3;
  --k-rdo: #d9ede3;
  --k-unknown: #ffccc7;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--brand); color: var(--brand-ink);
}
.top h1 { font-size: 18px; margin: 0; }
.who { font-size: 13px; opacity: .9; text-align: right; }

.tabs { display: flex; background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.tabs button { flex: 1; padding: 12px 4px; border: 0; background: none; font: inherit; font-size: 15px; color: var(--muted); border-bottom: 3px solid transparent; }
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }

main { max-width: 640px; margin: 0 auto; padding: 16px; }
.card { background: var(--card); border-radius: 14px; padding: 16px; margin-bottom: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.card h3, h3 { margin: 0 0 10px; font-size: 16px; }
.center { text-align: center; }
label { display: block; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 11px 12px;
  border: 1px solid var(--line); border-radius: 10px; font: inherit; color: var(--ink); background: #fff;
}
textarea { resize: vertical; font-size: 14px; }
.check { display: flex; align-items: center; gap: 10px; color: var(--ink); font-size: 15px; }
.check input { width: 20px; height: 20px; margin: 0; flex: none; }
.row { display: flex; gap: 8px; }
.row input:first-child { flex: 0 0 34%; }

.actions { display: flex; flex-direction: column; gap: 10px; margin: 14px 0; }
.btn {
  display: block; width: 100%; padding: 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); font: inherit; font-weight: 600; color: var(--ink); text-align: center; cursor: pointer;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn.small { display: inline-block; width: auto; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; }

.err { color: var(--err); font-size: 14px; margin: 6px 0; min-height: 0; }
.err:empty { display: none; }
.warn { background: var(--warn-bg); padding: 10px 12px; border-radius: 10px; margin-top: 0; }
.muted { color: var(--muted); font-size: 14px; }
.notice { background: var(--warn-bg); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; }
.help { font-size: 14px; color: var(--muted); }
.help summary { cursor: pointer; }
.steps { padding-left: 20px; }
.steps li { margin-bottom: 8px; }

.spinner { width: 42px; height: 42px; margin: 10px auto; border: 4px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#preview { max-width: 100%; max-height: 260px; margin-top: 12px; border-radius: 8px; opacity: .7; }

/* 月曆 */
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; font-size: 12px; }
.legend span { padding: 2px 8px; border-radius: 6px; border: 1px solid var(--line); }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 12px; }
.cal .wd { text-align: center; font-size: 12px; color: var(--muted); padding: 2px 0; }
.cal .wd.we { color: var(--err); }
.cell {
  position: relative; min-height: 70px; padding: 4px 2px 18px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--k-off); font: inherit; text-align: center; color: var(--ink); cursor: pointer;
}
.cell .d { display: block; font-size: 11px; color: var(--muted); }
.cell .c { display: block; font-size: 14px; font-weight: 700; word-break: break-all; }
.cell .w { position: absolute; left: 2px; right: 2px; bottom: 2px; font-size: 10px; background: var(--err); color: #fff; border-radius: 4px; }
.cell.empty { visibility: hidden; }
.cell.sel { outline: 3px solid var(--brand); }
.cell .n { display: inline-block; margin-top: 4px; min-width: 20px; padding: 0 5px; border-radius: 10px; background: var(--brand); color: #fff; font-size: 12px; }
.cell .n.m { background: var(--mutual); }
.k-early { background: var(--k-early); }
.k-late_short { background: var(--k-late_short); }
.k-late_long { background: var(--k-late_long); color: #fff; }
.k-late_long .d { color: #e6ecff; }
.k-on_early { background: var(--k-on_early); }
.k-on_late { background: var(--k-on_late); color: #fff; }
.k-on_late .d { color: #efe8ff; }
.k-standby { background: var(--k-standby); }
.k-al, .k-rdo { background: var(--k-al); }
.k-unknown { background: var(--k-unknown); border-color: var(--err); }

/* 配對 */
.seg { display: flex; background: var(--line); border-radius: 10px; padding: 3px; margin-bottom: 10px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; font: inherit; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--ink); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chips button { border: 1px solid var(--line); background: var(--card); border-radius: 18px; padding: 6px 12px; font: inherit; font-size: 14px; color: var(--ink); }
.chips button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.group-title { font-size: 15px; margin: 18px 0 8px; }
.group-title small { color: var(--muted); font-weight: 400; }
.match { background: var(--card); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; border-left: 4px solid var(--brand); box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.match.mutual { border-left-color: var(--mutual); }
.match .head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.match .name { font-weight: 700; }
.match .meta { font-size: 12px; color: var(--muted); }
.match .type { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--bg); color: var(--muted); white-space: nowrap; }
.match ul { margin: 8px 0 6px; padding-left: 18px; font-size: 15px; }
.match .why { font-size: 13px; color: var(--muted); }
.match .why b { color: var(--mutual); font-weight: 600; }
.more { margin-bottom: 10px; }
.empty-state { text-align: center; color: var(--muted); padding: 30px 10px; }

/* 班號表 */
.ftable { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; font-size: 15px; }
.ftable td, .ftable th { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; }
.ftable th { font-size: 13px; color: var(--muted); font-weight: 600; }
.ftable tr[data-code] { cursor: pointer; }
.ftable .tag { font-size: 12px; padding: 2px 8px; border-radius: 6px; }

/* 底部面板 */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--card);
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(12px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto; max-height: 85vh; overflow-y: auto;
}
.sheet h3 { margin-top: 0; }
