:root {
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-light: #ede9fe;
  --secondary: #7c3aed;
  --bg: #f8fafc;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --row-hover: #f1f5f9;
  --paid-bg: #dcfce7;
  --paid-border: #86efac;
  --unpaid-bg: #fef3c7;
  --unpaid-border: #fcd34d;
  --danger: #dc2626;
  --shadow: 0 4px 12px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }
body {
  margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5;
}

/* ── 登入頁 ────────────────────────────────────────────── */
.login-body { background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); margin: 0; }
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  background: white; border-radius: 16px; padding: 40px 36px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  width: 100%; max-width: 420px;
}
.login-logo-wrap { text-align: center; margin-bottom: 18px; }
.login-logo {
  display: inline-flex; align-items: center; justify-content: center;
  width: 64px; height: 64px; border-radius: 16px;
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  color: white; font-size: 28px; font-weight: 700;
  box-shadow: 0 8px 24px rgba(99,102,241,0.3);
}
.login-title { text-align: center; font-size: 22px; font-weight: 600; margin: 0 0 4px; }
.login-subtitle { text-align: center; font-size: 13px; color: var(--muted); margin: 0 0 28px; }
.login-field { margin-bottom: 14px; }
.login-field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.login-field input {
  width: 100%; padding: 12px 14px; font-size: 15px;
  border: 1px solid var(--border); border-radius: 8px;
  font-family: inherit; box-sizing: border-box;
}
.login-field input:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}
.login-btn {
  width: 100%; padding: 13px; font-size: 15px; font-weight: 500;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  color: white; border: none;
  border-radius: 8px; cursor: pointer; margin-top: 8px;
  font-family: inherit;
  transition: transform 0.1s, box-shadow 0.1s;
}
.login-btn:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(99,102,241,0.3); }
.login-btn:disabled { opacity: 0.6; cursor: wait; transform: none; }
.login-error {
  background: #fef2f2; border: 1px solid #fecaca; color: #991b1b;
  padding: 10px 12px; border-radius: 7px; font-size: 13px;
  margin-bottom: 14px; display: none;
}
.login-error.show { display: block; }
.login-hint {
  text-align: center; font-size: 12px; color: var(--muted);
  margin-top: 20px; line-height: 1.7;
}

/* ── 主頁 ──────────────────────────────────────────────── */
header {
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 14px 28px; display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  color: white; display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700;
  box-shadow: 0 4px 12px rgba(99,102,241,0.25);
}
.title { font-size: 17px; font-weight: 600; }
.version-tag {
  font-size: 11px; font-weight: 500;
  color: var(--primary); background: var(--primary-light);
  border: 1px solid #c7d2fe; padding: 1px 8px; border-radius: 10px;
  margin-left: 6px; vertical-align: middle;
}
.subtitle { font-size: 12px; color: var(--muted); }

nav { display: flex; gap: 4px; }
.nav-btn {
  background: transparent; border: none; padding: 8px 16px;
  font-size: 14px; cursor: pointer; border-radius: 8px; color: var(--muted);
  font-family: inherit;
}
.nav-btn:hover { background: var(--row-hover); color: var(--text); }
.nav-btn.active { background: var(--primary); color: white; }
.nav-btn.admin-only { display: none; }
body.is-admin .nav-btn.admin-only { display: inline-block; }

.user-info { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.user-info #user-name { font-weight: 500; color: var(--text); }
.user-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: linear-gradient(135deg, #fde68a, #fbbf24);
  color: #78350f; font-weight: 500;
}
.btn-logout {
  background: white; border: 1px solid var(--border); color: var(--text);
  padding: 6px 12px; border-radius: 7px; font-size: 13px; cursor: pointer;
  font-family: inherit;
}
.btn-logout:hover { background: var(--row-hover); color: var(--danger); border-color: var(--danger); }

main { max-width: 1280px; margin: 0 auto; padding: 32px 28px; }
.tab { display: none; }
.tab.active { display: block; }

/* ── Hero 歡迎區 ─────────────────────────────────────── */
.hero { margin-bottom: 32px; }
.hero h2 { font-size: 26px; font-weight: 600; margin: 0 0 4px; }
.hero p { margin: 0; font-size: 14px; }

/* ── 系統卡片 ────────────────────────────────────────── */
.systems-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}
.system-card {
  --card-color: var(--primary);
  display: block; text-decoration: none; color: var(--text);
  background: white; padding: 24px; border-radius: 14px;
  border: 1px solid var(--border);
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}
.system-card::before {
  content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--card-color);
}
.system-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 28px rgba(0,0,0,0.1);
  border-color: var(--card-color);
}
.card-icon {
  width: 56px; height: 56px; border-radius: 12px;
  background: color-mix(in srgb, var(--card-color) 12%, white);
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; margin-bottom: 14px;
}
.card-name { font-size: 17px; font-weight: 600; margin-bottom: 6px; }
.card-desc { font-size: 13px; color: var(--muted); min-height: 20px; }
.card-arrow {
  position: absolute; right: 20px; top: 24px;
  font-size: 18px; color: var(--card-color);
  opacity: 0; transition: all 0.2s;
}
.system-card:hover .card-arrow { opacity: 1; right: 16px; }

.empty-state {
  text-align: center; padding: 80px 20px;
  background: white; border-radius: 14px; border: 1px dashed var(--border);
}
.empty-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state h3 { font-size: 17px; margin: 0 0 8px; }
.empty-state p { color: var(--muted); margin: 0; }

/* ── 工具列、表格 ────────────────────────────────────── */
.toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.muted { color: var(--muted); }

.table-wrap {
  background: white; border: 1px solid var(--border); border-radius: 12px;
  overflow: auto;
}
table { width: 100%; border-collapse: collapse; }
th, td {
  padding: 12px 14px; text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
th { background: #f8fafc; font-weight: 500; color: var(--muted); font-size: 13px; }
tbody tr:hover { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: none; }
code {
  background: var(--row-hover); padding: 2px 6px; border-radius: 4px;
  font-size: 12px; font-family: monospace;
}

/* ── 按鈕 ──────────────────────────────────────────── */
.btn-primary {
  background: var(--primary); color: white; border: none;
  padding: 9px 16px; border-radius: 7px; cursor: pointer;
  font-size: 14px; font-family: inherit;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
  background: white; border: 1px solid var(--border); color: var(--text);
  padding: 9px 16px; border-radius: 7px; cursor: pointer;
  font-size: 14px; font-family: inherit;
}
.btn-ghost:hover { background: var(--row-hover); }
.btn-mini {
  background: white; border: 1px solid var(--border); color: var(--text);
  padding: 5px 10px; border-radius: 6px; cursor: pointer;
  font-size: 12px; font-family: inherit; margin-right: 4px;
}
.btn-mini:hover { background: var(--primary); color: white; border-color: var(--primary); }
.btn-mini.btn-danger:hover { background: var(--danger); border-color: var(--danger); }

/* ── 標籤 ──────────────────────────────────────────── */
.tag {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 11px; font-weight: 500;
}
.tag-paid { background: var(--paid-bg); color: #065f46; border: 1px solid var(--paid-border); }
.tag-unpaid { background: var(--unpaid-bg); color: #92400e; border: 1px solid var(--unpaid-border); }

/* ── Modal ─────────────────────────────────────────── */
.modal[hidden] { display: none !important; }
.modal {
  position: fixed; inset: 0; background: rgba(15,23,42,.55);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 20px;
}
.modal-card {
  background: white; border-radius: 14px; box-shadow: var(--shadow);
  max-height: 92vh; display: flex; flex-direction: column;
  width: 480px; overflow: hidden;
}
.modal-head {
  padding: 18px 22px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.modal-body { padding: 20px 22px; overflow-y: auto; }
.modal-body label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--text); }
.modal-body label input[type=text],
.modal-body label input[type=number] {
  display: block; width: 100%; padding: 9px 12px; margin-top: 4px;
  border: 1px solid var(--border); border-radius: 7px;
  font-size: 14px; font-family: inherit; box-sizing: border-box;
}
.modal-body label input[type=color] {
  display: block; width: 100%; height: 40px; padding: 2px;
  border: 1px solid var(--border); border-radius: 7px; margin-top: 4px;
  cursor: pointer;
}
.checkbox-label { display: flex !important; align-items: center; gap: 8px; cursor: pointer; }
.checkbox-label input { width: auto !important; margin: 0 !important; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-foot {
  padding: 14px 22px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}
.btn-close {
  background: transparent; border: none; font-size: 22px; cursor: pointer;
  color: var(--muted); padding: 0; width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
}
.btn-close:hover { color: var(--text); }

/* ── Toast ────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: white; padding: 10px 18px;
  border-radius: 8px; font-size: 13px; z-index: 200;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  max-width: calc(100vw - 32px);
}

/* ── 權限管理 dirty + save bar (v1.8.0)───────────────── */
tr.row-dirty td {
  background: #fffbeb;  /* 淡黃色提示 */
}
tr.row-dirty td:first-child {
  border-left: 3px solid #fbbf24;
  padding-left: 11px;  /* 補回 border 佔的空間 */
}
.perm-save-bar {
  position: sticky; bottom: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: 0 0 12px 12px;
  border-top: none;
  margin-top: -1px;
  padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.05);
  z-index: 10;
}
.perm-save-bar[hidden] { display: none !important; }
#perm-dirty-count { font-weight: 500; color: #b45309; }

/* ── RWD 手機優化 (v1.7.1)──────────────────────────── */
/* 主 breakpoint:平板與手機 */
@media (max-width: 768px) {
  /* Header:三段(brand / nav / user-info)會在窄螢幕 wrap */
  header {
    padding: 10px 14px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .brand { gap: 8px; min-width: 0; flex-shrink: 1; }
  .logo { width: 34px; height: 34px; font-size: 16px; border-radius: 8px; }
  .title { font-size: 15px; }
  .subtitle { display: none; }
  .version-tag { font-size: 10px; padding: 1px 6px; margin-left: 4px; }

  /* Nav:橫向滾動(避開漢堡選單,5 個按鈕直接滑) */
  nav {
    order: 3;
    flex-basis: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    margin-top: 4px;
    padding-bottom: 4px;
  }
  nav::-webkit-scrollbar { height: 2px; }
  nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
  .nav-btn {
    flex: 0 0 auto;
    padding: 7px 12px;
    font-size: 13px;
    white-space: nowrap;
  }

  /* user-info */
  .user-info { gap: 6px; font-size: 12px; min-width: 0; }
  .user-info #user-name {
    max-width: 80px; overflow: hidden;
    text-overflow: ellipsis; white-space: nowrap;
  }
  .user-badge { font-size: 10px; padding: 1px 6px; }
  .btn-logout { padding: 5px 9px; font-size: 12px; }

  /* Main */
  main { padding: 20px 14px; max-width: 100%; }

  /* Hero */
  .hero { margin-bottom: 22px; }
  .hero h2 { font-size: 22px; }
  .hero p { font-size: 13px; }

  /* Systems grid:單列卡片 */
  .systems-grid {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  .system-card { padding: 18px; }
  .card-icon { width: 48px; height: 48px; font-size: 24px; margin-bottom: 10px; border-radius: 10px; }
  .card-name { font-size: 16px; }
  .card-arrow { right: 14px; top: 18px; font-size: 16px; opacity: 1; }
  .system-card:hover { transform: none; }  /* 手機 hover 沒意義 */

  /* Empty state */
  .empty-state { padding: 50px 16px; }
  .empty-icon { font-size: 40px; }

  /* Toolbar 改成直立 */
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .toolbar h2 { font-size: 18px; }
  .toolbar > button.btn-primary { align-self: flex-start; }

  /* Tables:容器 horizontal scroll(.table-wrap 已 overflow:auto)+ 縮 padding */
  th, td { padding: 8px 10px; font-size: 13px; }
  .btn-mini { padding: 4px 8px; font-size: 11px; margin-right: 2px; }

  /* 提示卡(.hint)*/
  .hint { font-size: 12px !important; padding: 10px !important; }

  /* Modal */
  .modal { padding: 10px; }
  .modal-card {
    width: 100%; max-width: 480px;
    max-height: calc(100vh - 20px);
  }
  .modal-head { padding: 14px 16px; }
  .modal-head h2 { font-size: 16px; }
  .modal-body { padding: 16px; }
  .modal-body label { margin-bottom: 10px; font-size: 13px; }
  .modal-body label input[type=text],
  .modal-body label input[type=number] {
    padding: 10px 12px; font-size: 16px;  /* 16px 避免 iOS Safari 自動 zoom */
  }
  .grid-2 { grid-template-columns: 1fr; gap: 8px; }
  .modal-foot {
    padding: 12px 16px;
    flex-wrap: wrap;
  }
  .modal-foot button { flex: 1; min-width: 100px; }

  /* 登入頁 */
  .login-wrap { padding: 12px; }
  .login-card { padding: 28px 22px; max-width: 100%; }
  .login-logo { width: 56px; height: 56px; font-size: 24px; }
  .login-title { font-size: 20px; }
  .login-subtitle { margin-bottom: 22px; }
  .login-field input { padding: 12px 14px; font-size: 16px; }  /* 16px 避免 iOS auto-zoom */
  .login-btn { padding: 13px; font-size: 16px; }
}

/* 極窄螢幕(< 380px,e.g. iPhone SE 1st gen)*/
@media (max-width: 380px) {
  .title { font-size: 14px; }
  .version-tag { display: none; }  /* 太擠隱藏 */
  main { padding: 16px 12px; }
  th, td { padding: 6px 8px; font-size: 12px; }
  .nav-btn { padding: 6px 10px; font-size: 12px; }
  .login-card { padding: 22px 18px; }
}
