:root {
  --primary: #9b2c22;          /* 廟宇朱紅 */
  --primary-dark: #75201a;
  --primary-light: #fbeceb;
  --accent: #c08a2e;           /* 金 */
  --bg: #f7f4f0;
  --card: #ffffff;
  --text: #2a2320;
  --muted: #7d7168;
  --border: #e6ded6;
  --danger: #c0392b;
  --danger-bg: #fdecea;
  --ok: #1e8e3e;
  --ok-bg: #e8f5ea;
  --warn: #b7791f;
  --warn-bg: #fdf3e0;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(60, 40, 30, 0.09);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.55;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); text-decoration: none; }

/* ---- 版面 ---- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; background: #3d1d17; color: #e8d9cf; flex-shrink: 0;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .brand {
  padding: 18px 16px 14px; color: #fff; font-size: 17px; font-weight: 700;
  border-bottom: 1px solid rgba(255,255,255,0.12);
}
.sidebar .brand small { display: block; font-size: 11px; font-weight: 400; color: #d3ab7e; margin-top: 2px; }
.nav { flex: 1; padding: 8px 0; }
.nav a { display: block; padding: 10px 18px; color: #e8d9cf; font-size: 14px; border-left: 3px solid transparent; }
.nav a:hover { background: rgba(255,255,255,0.06); }
.nav a.active { background: rgba(255,255,255,0.1); color: #fff; border-left-color: var(--accent); }
.nav .nav-group { padding: 12px 18px 4px; font-size: 11px; color: #b99b7d; letter-spacing: 1px; }
.sidebar .user-box { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,0.12); font-size: 13px; }
.sidebar .user-box .name { color: #fff; font-weight: 600; }
.sidebar .user-box button {
  margin-top: 8px; width: 100%; padding: 6px; background: transparent; color: #e8d9cf;
  border: 1px solid rgba(255,255,255,0.3); border-radius: 6px; cursor: pointer; font-size: 13px;
}
.sidebar .user-box button:hover { background: rgba(255,255,255,0.08); }

.main { flex: 1; min-width: 0; padding: 20px 24px 60px; }
.page-title { font-size: 21px; font-weight: 700; margin-bottom: 4px; }
.page-sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }

/* 手機版頂欄 */
.topbar { display: none; }
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { position: fixed; left: -240px; z-index: 60; transition: left 0.2s; height: 100vh; }
  .sidebar.open { left: 0; box-shadow: 4px 0 20px rgba(0,0,0,0.3); }
  .topbar {
    display: flex; align-items: center; gap: 12px; background: #3d1d17; color: #fff;
    padding: 12px 16px; position: sticky; top: 0; z-index: 50;
  }
  .topbar .menu-btn { background: none; border: 1px solid rgba(255,255,255,0.4); color: #fff; border-radius: 6px; padding: 4px 10px; cursor: pointer; }
  .main { padding: 16px 14px 60px; }
  .backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 55; }
  .backdrop.show { display: block; }
}

/* ---- 卡片、統計 ---- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 16px; }
.card h3 { font-size: 15px; margin-bottom: 12px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.stat .num { font-size: 26px; font-weight: 700; color: var(--primary-dark); }
.stat .num.warn { color: var(--warn); }
.stat .num.danger { color: var(--danger); }
.stat .num.money { font-size: 21px; }
.stat .label { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.clickable { cursor: pointer; }
.stat.clickable:hover { outline: 2px solid var(--primary-light); }

/* ---- 表格 ---- */
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--card); border-radius: var(--radius); }
table.list th, table.list td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.list td.wrap { white-space: normal; min-width: 160px; }
table.list td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.list th { background: #f4efe9; color: var(--muted); font-weight: 600; font-size: 12.5px; position: sticky; top: 0; }
table.list tr:hover td { background: #fdfaf7; }

/* ---- 按鈕、標籤 ---- */
.btn {
  display: inline-block; padding: 7px 14px; border-radius: 7px; border: 1px solid transparent;
  background: var(--primary); color: #fff; cursor: pointer; font-size: 14px; line-height: 1.4;
}
.btn:hover { background: var(--primary-dark); }
.btn.secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: #f4efe9; }
.btn.danger { background: var(--danger); }
.btn.warn { background: var(--accent); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.tiny { padding: 2px 8px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.tag { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; background: #f0eae4; color: var(--muted); }
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.danger { background: var(--danger-bg); color: var(--danger); }
.tag.primary { background: var(--primary-light); color: var(--primary-dark); }
.tag.gold { background: #fbf1dd; color: #8a6220; }

/* ---- 工具列 ---- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.export-group { display: inline-flex; gap: 6px; }
.rp-field { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.rp-field input, .rp-field select { font-size: 13.5px; }
.toolbar input, .toolbar select {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; font-size: 14px;
}

/* ---- 表單 ---- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.form-row { display: flex; flex-direction: column; gap: 4px; }
.form-row.full { grid-column: 1 / -1; }
.form-row label { font-size: 12.5px; color: var(--muted); }
.form-row input, .form-row select, .form-row textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; width: 100%;
}
.form-row textarea { min-height: 72px; resize: vertical; }
.form-row input:focus, .form-row select:focus, .form-row textarea:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }
.form-hint { font-size: 12px; color: var(--muted); }
.checkline { display: flex; align-items: center; gap: 6px; font-size: 13.5px; }
.checkline input { width: auto; }

/* ---- Modal ---- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(40,25,20,0.5); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 5vh 14px; overflow-y: auto;
}
.modal { background: #fff; border-radius: 12px; width: 640px; max-width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,0.25); }
.modal.wide { width: 880px; }
.modal-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 16px; flex: 1; }
.modal-head .close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px; max-height: 72vh; overflow-y: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* ---- Toast ---- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: #2a2320; color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 14px;
  opacity: 0; transition: opacity 0.25s; pointer-events: none; max-width: 90vw;
}
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); }

/* ---- 登入頁 ---- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(150deg, #9b2c22, #3d1d17); padding: 16px; }
.login-card { background: #fff; border-radius: 14px; padding: 34px 30px; width: 380px; max-width: 100%; box-shadow: 0 16px 50px rgba(0,0,0,0.3); }
.login-card h1 { font-size: 22px; margin-bottom: 2px; color: var(--primary-dark); }
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.login-card .form-row { margin-bottom: 14px; }
.login-card .btn { width: 100%; padding: 10px; font-size: 15px; margin-top: 6px; }
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }

/* ---- 燈位圖 ---- */
.seat-legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; flex-wrap: wrap; }
.seat-legend i { display: inline-block; width: 11px; height: 11px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.seat-rows { display: flex; flex-direction: column; gap: 5px; overflow-x: auto; padding-bottom: 6px; }
.seat-row { display: flex; gap: 5px; align-items: center; }
.seat-row .rowno { width: 30px; flex-shrink: 0; font-size: 11.5px; color: var(--muted); text-align: right; }
.seat {
  width: 62px; height: 40px; border-radius: 6px; border: 1px solid var(--border); background: #fff;
  font-size: 11px; line-height: 1.25; padding: 3px 2px; cursor: pointer; text-align: center;
  overflow: hidden; flex-shrink: 0;
}
.seat .sc { color: var(--muted); font-size: 9.5px; }
.seat.used { background: var(--primary-light); border-color: #e6b3ae; color: var(--primary-dark); font-weight: 600; }
.seat.used.unpaid { background: var(--warn-bg); border-color: #e8cf9c; color: #8a6220; }
.seat.blocked { background: #ede8e3; color: #a89c92; cursor: not-allowed; text-decoration: line-through; }
.seat.pick { outline: 2px solid var(--primary); }

/* ---- 牌位清單 ---- */
.tablet-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.tablet-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: #fff; }
.tablet-item .tn { font-weight: 700; font-size: 15px; }
.tablet-item .tm { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tablet-item.memorial { border-left: 4px solid #6b6b6b; }
.tablet-item.bless { border-left: 4px solid var(--primary); }

/* ---- 行事曆 ---- */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-head { margin-bottom: 4px; }
.cal-head div { text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0; font-weight: 700; }
.cal-cell { min-height: 96px; border: 1px solid var(--border); border-radius: 7px; padding: 4px 5px; background: #fff; overflow: hidden; }
.cal-cell.cal-out { background: #faf7f4; border-style: dashed; min-height: 0; }
.cal-cell.cal-today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cal-day { font-size: 12px; color: var(--muted); font-weight: 700; margin-bottom: 3px; display: flex; justify-content: space-between; }
.cal-day .lu { font-weight: 400; color: #a2968c; }
.cal-day .lu.mark { color: var(--primary); font-weight: 700; }
.cal-today .cal-day { color: var(--primary-dark); }
.cal-ev {
  font-size: 11.5px; line-height: 1.35; padding: 2px 4px; margin-bottom: 2px; cursor: pointer;
  background: #f6f1ec; border-radius: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cal-ev.feast { background: #fbf1dd; color: #8a6220; }
.cal-ev.ceremony { background: var(--primary-light); color: var(--primary-dark); }
@media (max-width: 780px) {
  .cal-cell { min-height: 68px; }
  .cal-ev { font-size: 10px; }
}

/* ---- 其他 ---- */
.empty { text-align: center; color: var(--muted); padding: 36px 0; font-size: 14px; }
.section-tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 2px solid var(--border); flex-wrap: wrap; }
.section-tabs button {
  background: none; border: none; padding: 9px 16px; cursor: pointer; font-size: 14.5px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -2px;
}
.section-tabs button.active { color: var(--primary-dark); border-bottom-color: var(--primary); font-weight: 600; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px 18px; font-size: 14px; }
.detail-grid .dg-label { color: var(--muted); font-size: 12px; }
.bar-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13px; }
.bar-row .bl { width: 92px; flex-shrink: 0; color: var(--muted); }
.bar-row .bar { height: 14px; border-radius: 7px; background: var(--primary); min-width: 2px; }
.bar-row .bar.expense { background: var(--accent); }
.bar-row .bv { font-variant-numeric: tabular-nums; }

@media print {
  .sidebar, .topbar, .toolbar, .btn, .section-tabs { display: none !important; }
  .main { padding: 0; }
}
