:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --ink: #1c2b28;
  --muted: #6b7c78;
  --brand: #0b6b5e;
  --brand-dark: #085247;
  --line: #e2e8e6;
  --accent: #e8f4f1;
  --danger: #b03b3b;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.04);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.5;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---- Top bar ---- */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--brand); color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: #ffd66b; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-email { font-size: 12px; opacity: .9; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* 登出鈕在綠色頂列上要清楚可讀 */
#signoutBtn { background: rgba(255,255,255,.16); color: #fff; border: 1px solid rgba(255,255,255,.55); padding: 6px 12px; font-size: 13px; flex: none; }
#signoutBtn:active { background: rgba(255,255,255,.32); }

/* ---- Views ---- */
.view { padding: 16px; max-width: 900px; margin: 0 auto; }
.center { min-height: 70vh; display: flex; align-items: center; justify-content: center; }

/* ---- Cards / login ---- */
.card { background: var(--card); border-radius: 16px; box-shadow: var(--shadow); padding: 28px 22px; }
.login-card { text-align: center; max-width: 360px; width: 100%; }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.hint { font-size: 13px; color: var(--muted); margin-top: 14px; min-height: 1em; }

/* ---- Buttons ---- */
.btn { font: inherit; border: none; border-radius: 10px; padding: 10px 16px; cursor: pointer; font-weight: 600; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:active { background: var(--brand-dark); }
.btn.big { width: 100%; padding: 14px; font-size: 16px; margin-top: 18px; }
.btn.ghost { background: transparent; color: var(--brand); border: 1px solid var(--line); }
.btn.small { padding: 6px 12px; font-size: 14px; }

/* ---- File list ---- */
.toolbar { display: flex; justify-content: flex-end; margin-bottom: 10px; }
.file-list { display: flex; flex-direction: column; gap: 10px; }
.group-title { font-size: 13px; color: var(--muted); font-weight: 700; margin: 12px 2px 2px; }
.file-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border-radius: 12px; padding: 14px 16px;
  box-shadow: var(--shadow); cursor: pointer; border: 1px solid transparent;
}
.file-item:active { border-color: var(--brand); }
.file-item .ficon { font-size: 22px; }
.file-item .fmeta { flex: 1; min-width: 0; }
.file-item .fname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item .fsub { font-size: 12px; color: var(--muted); }
.file-item .chev { color: var(--muted); }

/* ---- Sheet view ---- */
.sheet-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.sheet-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.controls { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.search {
  width: 100%; padding: 12px 14px; font-size: 16px; border-radius: 10px;
  border: 1px solid var(--line); background: #fff;
}
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.tab { flex: none; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: #fff; font-size: 14px; cursor: pointer; }
.tab.active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ---- Table ---- */
.table-wrap { overflow: auto; border-radius: 12px; box-shadow: var(--shadow); background: #fff; max-height: calc(100vh - 220px); }
table.grid { border-collapse: collapse; width: max-content; min-width: 100%; font-size: 14px; }
table.grid th, table.grid td {
  border: 1px solid var(--line); padding: 6px 10px; text-align: left;
  vertical-align: middle; white-space: nowrap;
}
table.grid thead th { position: sticky; top: 0; background: var(--accent); z-index: 2; font-weight: 700; }
table.grid tbody tr:nth-child(even) td { background: #fafcfb; }
table.grid tbody tr.hit td { background: #fff6d9; }
.rowcount { font-size: 12px; color: var(--muted); padding: 8px 2px; }

/* ---- 統計入口按鈕 ---- */
.stats-entry {
  width: 100%; text-align: left; border: none; cursor: pointer;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
  color: #fff; border-radius: 14px; padding: 16px 18px; margin-bottom: 14px;
  font-size: 17px; font-weight: 700; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px;
}
.stats-entry-sub { font-size: 12px; font-weight: 400; opacity: .85; }

/* ---- 統計頁 ---- */
#statsBody { padding-bottom: 30px; }
.stat-cards { display: flex; gap: 10px; margin-bottom: 14px; }
.stat-card { flex: 1; background: var(--card); border-radius: 12px; padding: 14px 8px; text-align: center; box-shadow: var(--shadow); }
.big-num { font-size: 28px; font-weight: 800; color: var(--brand); line-height: 1.1; }
.card-label { font-size: 12px; color: var(--muted); margin-top: 4px; }
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:active { background: var(--accent); }

.stat-block { background: var(--card); border-radius: 12px; padding: 14px 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.stat-block-title { font-weight: 700; font-size: 14px; margin-bottom: 10px; color: var(--ink); }

.bar-row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.bar-label { flex: 0 0 76px; font-size: 13px; color: var(--muted); }
.bar-track { flex: 1; background: var(--accent); border-radius: 6px; height: 16px; overflow: hidden; }
.bar { height: 100%; background: var(--brand); border-radius: 6px; min-width: 2px; }
.bar-val { flex: 0 0 34px; text-align: right; font-size: 13px; font-weight: 600; }

.diet-grid { display: flex; gap: 10px; }
.diet-card { flex: 1; background: var(--accent); border-radius: 10px; padding: 12px 6px; text-align: center; }
.diet-num { font-size: 22px; font-weight: 800; color: var(--brand-dark); }
.diet-label { font-size: 12px; color: var(--muted); margin-top: 2px; }
.diet-note { font-size: 12px; color: var(--muted); margin-top: 10px; line-height: 1.5; }

/* 可點擊的長條列 / 飲食卡 */
.bar-row.clickable { cursor: pointer; border-radius: 6px; padding: 2px 4px; margin-left: -4px; margin-right: -4px; }
.bar-row.clickable:active { background: var(--accent); }
.bar-row.clickable .bar-label { color: var(--brand-dark); text-decoration: underline; text-underline-offset: 2px; }
.diet-card.clickable { cursor: pointer; }
.diet-card.clickable:active { background: #d9ece7; }
.raw-link { margin-top: 6px; width: 100%; }

.grp-list { display: flex; flex-direction: column; gap: 8px; }
.grp-item { display: flex; align-items: center; justify-content: space-between; background: var(--bg); border-radius: 10px; padding: 12px 14px; cursor: pointer; border: 1px solid var(--line); }
.grp-item:active { border-color: var(--brand); }
.grp-left { display: flex; flex-direction: column; }
.grp-name { font-weight: 700; }
.grp-sub { font-size: 12px; color: var(--muted); }
.grp-right { display: flex; align-items: center; gap: 8px; }
.grp-count { font-size: 20px; font-weight: 800; color: var(--brand); }

.mem-table { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 4px; }
.mem-table th, .mem-table td { border-bottom: 1px solid var(--line); padding: 7px 6px; text-align: left; }
.mem-table th { color: var(--muted); font-weight: 700; font-size: 12px; }

/* ---- Toast / loading ---- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #1c2b28; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 50; box-shadow: var(--shadow);
}
.loading {
  position: fixed; inset: 0; background: rgba(255,255,255,.7);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; z-index: 60;
}
.spinner {
  width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--brand);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
