:root {
  --navy: #1a1a2e;
  --red: #c0392b;
  --red-dark: #a93226;
  --bg: #f0f2f5;
  --card: #ffffff;
  --text: #2c3e50;
  --muted: #7f8c8d;
  --border: #e0e0e0;
  --green: #27ae60;
  --yellow: #f39c12;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Noto Sans TC', 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.6;
}

/* ── Nav ── */
nav {
  background: var(--navy);
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 0;
  height: 56px;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}

.nav-brand {
  color: white;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: .5px;
  margin-right: 32px;
  text-decoration: none;
  white-space: nowrap;
}

.nav-brand span { color: #e74c3c; }

nav a {
  color: rgba(255,255,255,.65);
  text-decoration: none;
  font-size: 14px;
  padding: 0 16px;
  height: 56px;
  display: flex;
  align-items: center;
  border-bottom: 3px solid transparent;
  transition: all .15s;
}

nav a:hover { color: white; }
nav a.active { color: white; border-bottom-color: var(--red); }

/* ── Layout ── */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px;
  display: grid;
  gap: 20px;
}

.card {
  background: var(--card);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}

.card-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1.2px;
  margin-bottom: 16px;
}

/* ── Form Elements ── */
label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  display: block;
  margin-bottom: 6px;
}

input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color .15s;
  background: white;
  color: var(--text);
}
input[type="checkbox"], input[type="radio"] {
  width: auto;
  flex-shrink: 0;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--red);
}

textarea {
  resize: vertical;
  line-height: 1.7;
}

/* ── Buttons ── */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.btn-primary { background: var(--red); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--red-dark); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary { background: #ecf0f1; color: var(--text); }
.btn-secondary:hover { background: #dce1e3; }

.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }

.btn-green { background: var(--green); color: white; }
.btn-green:hover:not(:disabled) { background: #219a52; }
.btn-green:disabled { opacity: .5; cursor: not-allowed; }

.btn-sm { padding: 6px 14px; font-size: 13px; }

/* ── Rows ── */
.row {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.row .field { flex: 1; min-width: 140px; }

.action-row {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

/* ── Status ── */
.status {
  font-size: 13px;
  color: var(--muted);
  margin-top: 8px;
}
.status.error { color: var(--red); }
.status.success { color: var(--green); }
.status.loading { color: #2980b9; }

/* ── Spinner ── */
.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Badge ── */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}
.badge-red { background: #fdecea; color: var(--red); }
.badge-green { background: #e8f8ef; color: var(--green); }
.badge-blue { background: #e8f0fe; color: #1a73e8; }
.badge-yellow { background: #fef9e7; color: var(--yellow); }

/* ── Table ── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.data-table th {
  background: var(--navy);
  color: white;
  padding: 11px 14px;
  text-align: left;
  font-weight: 600;
}
.data-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #fafafa; }
.data-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ── Markdown output ── */
.md-output {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  background: #fafafa;
  font-size: 14px;
  line-height: 1.8;
}

.md-output h1 {
  font-size: 20px;
  color: var(--navy);
  border-bottom: 2px solid var(--red);
  padding-bottom: 8px;
  margin: 24px 0 14px;
}
.md-output h1:first-child { margin-top: 0; }

.md-output h2 {
  font-size: 15px;
  color: var(--navy);
  margin: 18px 0 8px;
  padding-left: 10px;
  border-left: 3px solid var(--red);
}

.md-output h3 {
  font-size: 14px;
  color: #555;
  margin: 12px 0 6px;
}

.md-output p { margin: 6px 0; }

.md-output table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.md-output table th {
  background: var(--navy);
  color: white;
  padding: 8px 12px;
  text-align: left;
}
.md-output table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.md-output table tr:nth-child(even) td { background: #f5f5f5; }

.md-output ul, .md-output ol { margin: 6px 0 6px 22px; }
.md-output li { margin: 3px 0; }
.md-output strong { color: var(--navy); }
.md-output hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* ── Person cards (save section) ── */
.person-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.person-card .name { font-weight: 700; font-size: 15px; min-width: 80px; }
.person-card .fields { display: flex; gap: 12px; flex: 1; flex-wrap: wrap; }
.person-card .fields .field { flex: 1; min-width: 100px; }
.person-card .fields label { font-size: 11px; }
.person-card .fields input { font-size: 13px; padding: 7px 10px; }

/* ── Stats grid ── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}
.stat-card {
  background: var(--card);
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}
.stat-card .val {
  font-size: 28px;
  font-weight: 700;
  color: var(--navy);
  line-height: 1.2;
}
.stat-card .lbl {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  z-index: 1000;
  overflow-y: auto;
}
.modal {
  background: white;
  border-radius: 14px;
  width: 100%;
  max-width: 800px;
  padding: 28px;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}

/* ── Filter bar ── */
.filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
}
.filter-bar .field { min-width: 140px; }
.filter-bar .field select,
.filter-bar .field input { font-size: 13px; }

/* ── Empty state ── */
.empty {
  text-align: center;
  padding: 48px 20px;
  color: var(--muted);
}
.empty p { margin-top: 8px; font-size: 14px; }

.hidden { display: none !important; }

/* ── Mobile ──────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* 表格：橫向捲動 */
  .data-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .data-table th,
  .data-table td { white-space: nowrap; }
}

@media (max-width: 640px) {
  /* Nav：隱藏文字連結（底部列取代），只保留品牌 + 使用者 block */
  nav a:not(.nav-brand):not(#nav-user-block a) { display: none; }
  nav { padding: 0 12px; flex-wrap: nowrap; justify-content: space-between; }

  /* Layout */
  main { padding: 10px; gap: 10px; }
  .card { padding: 14px; }

  /* 統計卡：2欄 */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-card { padding: 14px; }
  .stat-card .val { font-size: 22px; }

  /* 表單欄位：全寬 + 最小高度 44px */
  .field { width: 100%; min-width: unset !important; }
  .field input, .field select, .field textarea {
    width: 100%;
    min-height: 44px;
    font-size: 16px; /* 防止 iOS 自動縮放 */
    box-sizing: border-box;
  }
  .row { flex-direction: column; gap: 10px; }

  /* 按鈕：最小高度 44px，確保好按 */
  .btn { min-height: 44px; font-size: 14px; }
  .btn-sm { min-height: 38px; }

  /* Modal：從底部滑出 */
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal {
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 92vh;
    overflow-y: auto;
    padding: 20px 14px;
  }
  .ov .eb {
    max-width: 100% !important;
    width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 88vh;
    overflow-y: auto;
  }

  /* Filter bar：垂直堆疊 */
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar .field { min-width: unset; width: 100%; }

  /* 自檢清單每週表格：橫向捲動 */
  .weekly-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .weekly-header,
  .weekly-row { min-width: 480px; }

  /* Tabs：橫向捲動 */
  .tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .tab { white-space: nowrap; flex-shrink: 0; padding: 10px 14px; }

  /* Checkbox label：最小觸控高度 44px */
  label { min-height: 44px; display: flex; align-items: center; }
}
