/* 10+3+1 — 手機優先，淺色／深色自動切換 */
:root {
  --bg: #faf7f2;
  --card: #ffffff;
  --ink: #2b2520;
  --muted: #7a6f64;
  --line: #e9e2d8;
  --brand: #a8691c;
  --brand-ink: #ffffff;
  --brand-soft: #f6ead8;
  --ok: #2f7d4f;
  --ok-soft: #e3f1e8;
  --warn: #b4541a;
  --warn-soft: #fbe8dc;
  --danger: #b42318;
  --s0: #8a8178; --s0-bg: #efebe6;   /* 未分享健管 */
  --s1: #2563a8; --s1-bg: #e2ecf8;   /* 分享過健管 */
  --s2: #2f7d4f; --s2-bg: #e3f1e8;   /* 做過健管 */
  --radius: 14px;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 2px 8px rgba(60, 40, 20, .05);
  --tabs-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171411;
    --card: #221e1a;
    --ink: #f1ebe3;
    --muted: #a89c8e;
    --line: #3a332c;
    --brand: #e0a050;
    --brand-ink: #1b140c;
    --brand-soft: #3a2a17;
    --ok: #6cc48f; --ok-soft: #1f3327;
    --warn: #f0a070; --warn-soft: #3c2619;
    --danger: #ff8a80;
    --s0: #b3a99e; --s0-bg: #2e2924;
    --s1: #8cb8ec; --s1-bg: #1d2a3a;
    --s2: #6cc48f; --s2-bg: #1f3327;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
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: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
}
main { max-width: 640px; margin: 0 auto; padding: 12px 16px 0; }
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; padding: 48px 0; }
.row { display: flex; align-items: center; gap: 8px; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 12px; }
.hidden, [hidden] { display: none !important; }

/* ---------- 按鈕、輸入 ---------- */
button, .btn {
  font: inherit; border: 0; cursor: pointer; border-radius: 10px;
  padding: 10px 14px; background: var(--brand-soft); color: var(--brand); font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  -webkit-tap-highlight-color: transparent; min-height: 44px;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.danger { background: transparent; color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 6px 10px; font-size: .9rem; }
button:disabled { opacity: .5; cursor: default; }
.icon-btn { background: transparent; color: var(--muted); padding: 6px; min-height: 40px; min-width: 40px; font-size: 1.2rem; }

input, select, textarea {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
textarea { resize: none; min-height: 44px; overflow: hidden; }
label.f { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; font-weight: 600; }

/* ---------- 卡片 ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }

/* ---------- 頁首 ---------- */
.head { display: flex; align-items: center; gap: 8px; margin: 4px 0 12px; }
.datebar { display: flex; align-items: center; justify-content: space-between; gap: 4px; margin-bottom: 12px; }
.datebar .title { position: relative; text-align: center; flex: 1; }
.datebar .title input[type=date] { position: absolute; inset: 0; opacity: 0; width: 100%; cursor: pointer; }
.datebar .sub { font-size: .8rem; color: var(--muted); }

/* ---------- 今日進度 ---------- */
.progress { display: flex; align-items: center; gap: 12px; }
.bar { flex: 1; height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 99px; transition: width .3s; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; }
.chip.s0 { color: var(--s0); background: var(--s0-bg); }
.chip.s1 { color: var(--s1); background: var(--s1-bg); }
.chip.s2 { color: var(--s2); background: var(--s2-bg); }
.chip.warn { color: var(--warn); background: var(--warn-soft); }
.chip.ok { color: var(--ok); background: var(--ok-soft); }
.chip.brand { color: var(--brand); background: var(--brand-soft); }

/* ---------- 互動卡 ---------- */
.ix { display: flex; gap: 12px; align-items: flex-start; }
.ix.done { opacity: .78; }
.ix .who { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ix .who b { font-size: 1.05rem; }
.ix .meta { font-size: .8rem; color: var(--muted); margin: 2px 0 8px; }
.ix textarea { font-size: .95rem; background: var(--bg); border-color: transparent; }
.ix textarea:focus { background: var(--card); }
.check {
  flex: none; width: 34px; height: 34px; min-height: 34px; padding: 0; border-radius: 50%;
  border: 2px solid var(--line); background: var(--card); color: transparent; font-size: 1.1rem;
  margin-top: 2px;
}
.check.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.saved { font-size: .75rem; color: var(--muted); min-height: 1em; transition: opacity .4s; }

/* ---------- 名單 ---------- */
.avatar {
  flex: none; width: 44px; height: 44px; border-radius: 50%; object-fit: cover;
  background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700;
}
.avatar.lg { width: 88px; height: 88px; font-size: 2rem; }
.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--line); }
.list .item { display: flex; align-items: center; gap: 12px; padding: 10px 4px; color: inherit; cursor: pointer; }
.list .name { font-weight: 600; }
.list .sub { font-size: .8rem; color: var(--muted); }
.filters { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.filters button { flex: none; min-height: 34px; padding: 4px 12px; border-radius: 99px; background: var(--card); color: var(--muted); border: 1px solid var(--line); font-weight: 500; }
.filters button.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.sticky { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 8px 0; }
.fab {
  position: fixed; right: max(16px, calc(50% - 304px)); bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 16px);
  width: 56px; height: 56px; border-radius: 50%; font-size: 1.8rem; background: var(--brand); color: var(--brand-ink);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .2); z-index: 20;
}

/* ---------- 健管階段（分段按鈕） ---------- */
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; min-height: 38px; background: transparent; color: var(--muted); font-weight: 500; font-size: .9rem; padding: 6px 4px; }
.seg button.on { background: var(--card); color: var(--ink); font-weight: 700; box-shadow: var(--shadow); }
.quick { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }

/* ---------- 時間軸 ---------- */
.tl { list-style: none; margin: 0; padding: 0 0 0 14px; border-left: 2px solid var(--line); }
.tl li { position: relative; padding: 0 0 14px 10px; }
.tl li::before { content: ""; position: absolute; left: -21px; top: 6px; width: 12px; height: 12px; border-radius: 50%; background: var(--line); }
.tl li.done::before { background: var(--ok); }
.tl .d { font-size: .8rem; color: var(--muted); }

/* ---------- 週曆 ---------- */
.wk { display: flex; gap: 12px; padding: 12px 4px; color: inherit; }
.wk + .wk { border-top: 1px solid var(--line); }
.wk .dd { flex: none; width: 52px; text-align: center; }
.wk .dd b { display: block; font-size: 1.3rem; line-height: 1.1; }
.wk.today .dd b { color: var(--brand); }
.wk .names { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

/* ---------- 統計 ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.5rem; line-height: 1.2; }
.stat span { font-size: .75rem; color: var(--muted); }
.spark { display: flex; align-items: flex-end; gap: 2px; height: 56px; }
.spark i { flex: 1; background: var(--ok); border-radius: 2px 2px 0 0; min-height: 2px; opacity: .85; }
.spark i.zero { background: var(--line); }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper b { min-width: 2.5em; text-align: center; font-size: 1.2rem; }

/* ---------- 底部分頁 ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: .75rem; color: var(--muted); gap: 2px; }
.tabs a .ti { font-size: 1.25rem; line-height: 1; }
.tabs a.on { color: var(--brand); font-weight: 700; }

/* ---------- 底部面板、對話框 ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--bg); width: 100%; max-width: 640px; max-height: 88vh; border-radius: 18px 18px 0 0;
  display: flex; flex-direction: column; padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
.sheet .body { overflow-y: auto; flex: 1; margin: 8px -4px; padding: 0 4px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.overlay.mid { align-items: center; padding: 16px; }
.dialog { background: var(--card); border-radius: 16px; padding: 20px; width: 100%; max-width: 360px; }
.dialog .row { justify-content: flex-end; margin-top: 16px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: .92rem;
  max-width: calc(100% - 32px); box-shadow: 0 4px 14px rgba(0, 0, 0, .2);
}
.empty { text-align: center; padding: 28px 12px; color: var(--muted); }

/* ---------- 照片：大頭照按鈕、裁切框、連續新增提示 ---------- */
.photo-btn { border: 0; padding: 0; background: none; cursor: pointer; display: inline-block; border-radius: 50%; }
.photo-btn .avatar.lg { font-size: 1.8rem; }
.dialog.crop { max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.dialog.crop p { margin: 0; text-align: center; }
.dialog.crop .row { margin-top: 0; }
.dialog.crop input[type=range] { width: 100%; accent-color: var(--brand); }
.cropbox { position: relative; width: 100%; aspect-ratio: 1; overflow: hidden; border-radius: 12px; touch-action: none; cursor: grab; background: #000; }
.cropbox canvas { width: 100%; height: 100%; display: block; }
.cropmask { position: absolute; inset: 0; border-radius: 50%; box-shadow: 0 0 0 999px rgba(0, 0, 0, .5); pointer-events: none; border: 2px solid rgba(255, 255, 255, .8); }
.card.added { margin-bottom: 12px; border: 1px solid var(--ok); }
.avatar.sm { width: 28px; height: 28px; font-size: .8rem; }
.ix .who-link { color: inherit; display: inline-flex; align-items: center; gap: 8px; }

/* ---------- 名單照片牆 ---------- */
.view-seg { flex: none; }
.view-seg button { min-width: 40px; padding: 4px 10px; }
.wall { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
.wall .tile { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; color: inherit; text-align: center; min-width: 0; }
.wall .tile img, .wall .tile .ph { width: 100%; aspect-ratio: 1; border-radius: 14px; object-fit: cover; }
.wall .tile .ph { display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-size: 2rem; font-weight: 700; }
.wall .tile .nm { font-weight: 600; font-size: .92rem; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 4px; }
.wall .tile .sb { font-size: .75rem; color: var(--muted); }
.wall .tile .dot { position: absolute; top: 6px; right: 6px; width: 12px; height: 12px; border-radius: 50%; background: var(--warn, #d97706); border: 2px solid var(--card); }
.wall > .empty { grid-column: 1 / -1; }

/* ---------- 用照片批次新增 ---------- */
.bgrid { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.bcard { position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.bcard.err { border-color: var(--danger); }
.bcard.saved { opacity: .7; }
.bcard .bimg { border: 0; padding: 0; background: var(--brand-soft); border-radius: 10px; aspect-ratio: 1; overflow: hidden; cursor: pointer; display: grid; place-items: center; }
.bcard .bimg img { width: 100%; height: 100%; object-fit: cover; display: block; }
.bcard .bimg .ph { color: var(--muted); }
.bcard .bx { position: absolute; top: 12px; right: 12px; background: rgba(0, 0, 0, .55); color: #fff; width: 28px; height: 28px; min-height: 0; min-width: 0; padding: 0; font-size: .85rem; border-radius: 50%; }
.bcard .bok { position: absolute; top: 12px; right: 12px; background: var(--ok); color: #fff; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; }
.bcard .bname { padding: 8px 10px; }

/* ---------- 今日、朋友詳細：手機一欄，電腦兩欄（見下方 @media） ---------- */
.side-brand { display: none; }
.dgrid > * + *, .mgrid > .m-form + *, .mgrid > .m-tl + * { margin-top: 12px; }
.dgrid > .d-note { margin-top: 16px; }

/* =====================================================================
   電腦版（寬 900px 以上）：左側選單、內容放寬、兩欄、面板改成置中視窗
   手機不受影響
   ===================================================================== */
@media (min-width: 900px) {
  :root { --side-w: 220px; --main-w: 1040px; }
  body { padding: 0 0 32px var(--side-w); }
  main { max-width: var(--main-w); padding: 24px 32px 0; }
  /* 設定、統計類的頁面內容不多，不要拉太寬 */
  main[data-page="me"], main[data-page="admin"], main[data-page="settings"],
  main[data-page="week"], main[data-page="week/id"], main[data-page="m/new"], main[data-page="m/batch"] { max-width: 760px; }

  /* 底部分頁 → 左側選單 */
  .tabs {
    top: 0; right: auto; width: var(--side-w); height: auto; flex-direction: column; gap: 4px;
    border-top: 0; border-right: 1px solid var(--line); padding: 20px 12px;
  }
  .side-brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 20px; font-size: 1.15rem; }
  .side-brand img { width: 34px; height: 34px; border-radius: 9px; }
  .tabs a { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; font-size: 1rem; padding: 10px 14px; border-radius: 10px; color: var(--ink); }
  .tabs a .ti { width: 1.4em; text-align: center; color: var(--muted); }
  .tabs a:hover { background: var(--brand-soft); }
  .tabs a.on { background: var(--brand-soft); color: var(--brand); }
  .tabs a.on .ti { color: var(--brand); }

  .toast { bottom: 28px; left: calc(50% + var(--side-w) / 2); }
  .fab { bottom: 32px; right: max(32px, calc((100vw - var(--side-w) - var(--main-w)) / 2 + 32px)); }

  /* 底部面板 → 畫面中間的視窗 */
  .overlay { align-items: center; padding: 24px; }
  .sheet { max-width: 560px; max-height: 80vh; border-radius: 18px; padding: 18px 20px; }

  /* 今日：左邊名單，右邊進度＋心得 */
  .dgrid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; grid-template-rows: auto 1fr; gap: 16px 24px; align-items: start; }
  .dgrid > * + *, .dgrid > .d-note { margin-top: 0; }
  .dgrid .d-items { grid-column: 1; grid-row: 1 / span 2; }
  .dgrid .d-prog { grid-column: 2; grid-row: 1; }
  .dgrid .d-note { grid-column: 2; grid-row: 2; position: sticky; top: 24px; }
  .dgrid .d-note textarea { min-height: 160px; }

  /* 朋友詳細：左邊照片＋資料，右邊互動紀錄 */
  .mgrid:not(.new) { display: grid; grid-template-columns: 380px minmax(0, 1fr); grid-template-rows: auto auto 1fr; gap: 16px 24px; align-items: start; }
  .mgrid:not(.new) > * + * { margin-top: 0; }
  .mgrid .m-photo { grid-column: 1; grid-row: 1; }
  .mgrid .m-form { grid-column: 1; grid-row: 2; }
  .mgrid .m-act { grid-column: 1; grid-row: 3; }
  .mgrid .m-tl { grid-column: 2; grid-row: 1 / span 3; }

  /* 名單：列表兩欄、照片牆格子大一點 */
  .mlist { display: grid; grid-template-columns: 1fr 1fr; column-gap: 24px; }
  .mlist > li + li { border-top: 0; }
  .mlist > li:nth-child(n+3) { border-top: 1px solid var(--line); }
  .mlist > .empty { grid-column: 1 / -1; }
  .wall { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 16px; }
  .bgrid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }

  /* 滑鼠移上去有反應 */
  .list .item, .wk, a.card { transition: background .15s; border-radius: 10px; }
  .list .item:hover, .wk:hover { background: var(--bg); }
  a.card:hover { border-color: var(--brand); }
  .wall .tile:hover img, .wall .tile:hover .ph { outline: 3px solid var(--brand-soft); }
  .btn:hover:not(:disabled), button:hover:not(:disabled) { filter: brightness(.97); }
}
