:root {
  --bg: #f4f1ea; --card: #ffffff; --ink: #24302a; --muted: #6b766f; --line: #e3ded3;
  --brand: #4f6f5f; --brand-ink: #ffffff; --brand-soft: #e4ece6;
  --ok: #2f7d4f; --ok-soft: #e1f2e7; --bad: #b4412f; --bad-soft: #fbe6e1; --warn: #a86a12; --warn-soft: #fbefd9;
  --s0: #4f6f5f; --s1: #c27b45;
  --radius: 16px; --shadow: 0 1px 2px rgba(30,40,35,.06), 0 4px 14px rgba(30,40,35,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161b18; --card: #20271f; --ink: #e8ece8; --muted: #9aa69e; --line: #333d36;
    --brand: #8db39e; --brand-ink: #10160f; --brand-soft: #2a3a31;
    --ok: #7fcf9c; --ok-soft: #1f3a29; --bad: #f08e7c; --bad-soft: #43231e; --warn: #f0b860; --warn-soft: #3d2f17;
    --s0: #8db39e; --s1: #e5a06f; --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: 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
body.noscroll { overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.05rem; margin-bottom: 10px; }
h3 { font-size: 1rem; margin-bottom: 6px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--bg); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 1.1rem; }
.brand small { color: var(--muted); font-size: .8rem; white-space: nowrap; }
.logo { font-size: 1.6rem; }
.logo.big { font-size: 3rem; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.langs { display: inline-flex; background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 2px; }
.langs.center { margin: 10px auto 18px; }
.langs button { border: 0; background: none; padding: 5px 10px; border-radius: 999px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.langs button.on { background: var(--brand); color: var(--brand-ink); }
.icon { border: 1px solid var(--line); background: var(--card); width: 38px; height: 38px; border-radius: 50%; font-size: 1rem; display: inline-grid; place-items: center; }
.icon:disabled { opacity: .35; }
.banner { display: block; width: calc(100% - 32px); margin: 10px 16px 0; padding: 10px 14px; border-radius: 12px; border: 0; text-align: left; font-weight: 600; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.err { background: var(--bad-soft); color: var(--bad); }

.wrap { max-width: 640px; margin: 0 auto; padding: 14px 16px 24px; }
.family .wrap { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; border: 1px solid var(--line); }

/* ---------- tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.tile {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 104px; padding: 12px 6px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--card); box-shadow: var(--shadow); font-weight: 650; font-size: .95rem; text-align: center;
}
.tile .ic { font-size: 2.1rem; line-height: 1; }
.tile:active { transform: scale(.97); }
.tile-incident { background: var(--bad-soft); border-color: transparent; color: var(--bad); }
.add .tiles { margin: 12px 0 0; }
.add summary { font-weight: 650; cursor: pointer; list-style: none; }
.add summary::-webkit-details-marker { display: none; }

/* ---------- med list ---------- */
.medlist { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.mname { min-width: 0; overflow-wrap: anywhere; }
.med {
  width: 100%; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; text-align: left;
  padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg);
}
.med:disabled { cursor: default; color: inherit; }
.slot { font-variant-numeric: tabular-nums; font-weight: 700; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; }
.slot.big { font-size: 1.3rem; }
.mname small { color: var(--muted); }
.mnote { display: block; }
.mst { font-size: .85rem; font-weight: 700; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.med.ok { background: var(--ok-soft); border-color: transparent; } .med.ok .mst { color: var(--ok); }
.med.bad { background: var(--bad-soft); border-color: transparent; } .med.bad .mst { color: var(--bad); }
.med.late { border-color: var(--bad); } .med.late .mst { background: var(--bad); color: #fff; }
.med.wait .mst { color: var(--muted); background: var(--card); }
.med-head { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }

/* ---------- timeline ---------- */
.timeline { list-style: none; margin: 0; padding: 0; }
.ev { display: grid; grid-template-columns: 46px 28px 1fr auto; gap: 6px; align-items: start; padding: 10px 0; border-top: 1px solid var(--line); }
.ev:first-child { border-top: 0; }
.ev-time { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .9rem; padding-top: 1px; }
.ev-ic { font-size: 1.15rem; }
.ev-body b { font-weight: 650; }
.ev-memo { color: var(--muted); font-size: .92rem; margin-top: 2px; white-space: pre-wrap; }
.ev-meta { color: var(--muted); font-size: .78rem; margin-top: 2px; }
.ev.alert { background: var(--bad-soft); border-radius: 10px; padding-left: 8px; padding-right: 4px; border-top-color: transparent; }
.alert-txt { color: var(--bad); font-weight: 600; }
.del { border: 0; background: none; color: var(--muted); font-size: 1.3rem; width: 34px; height: 34px; border-radius: 50%; }

/* ---------- family ---------- */
.alerts { background: var(--bad-soft); border-color: transparent; }
.alerts h2 { color: var(--bad); }
.alerts ul { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 6px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--bg); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat span { color: var(--muted); font-size: .82rem; }
.stat b { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.stat b small { font-size: .85rem; color: var(--muted); font-weight: 500; }
.stat b.txt { font-size: .95rem; font-weight: 600; }
.vitals-row { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 12px; font-size: .95rem; }
.vitals-row small { color: var(--muted); }
.datebar { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.datebar input { flex: 1; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); text-align: center; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabbar button { border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 1px; font-size: .78rem; color: var(--muted); padding: 4px; border-radius: 10px; }
.tabbar button span { font-size: 1.3rem; }
.tabbar button.on { color: var(--brand); font-weight: 700; background: var(--brand-soft); }
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .ax { fill: var(--muted); font-size: 10px; }
.chart .ln { fill: none; stroke-width: 2; stroke-linejoin: round; }
.chart .ln.s0, .chart .dot.s0 { stroke: var(--s0); } .chart .dot.s0 { fill: var(--s0); }
.chart .ln.s1, .chart .dot.s1 { stroke: var(--s1); } .chart .dot.s1 { fill: var(--s1); }
.legend { display: flex; gap: 14px; font-size: .82rem; color: var(--muted); margin-bottom: 4px; }
.lg::before { content: ''; display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.lg.s0::before { background: var(--s0); } .lg.s1::before { background: var(--s1); }
.medadmin { list-style: none; margin: 0; padding: 0; }
.medadmin li { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.medadmin li:first-child { border-top: 0; }
.times { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0; }
.pill { background: var(--brand-soft); color: var(--brand); font-weight: 700; font-size: .85rem; border-radius: 999px; padding: 2px 9px; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* ---------- buttons & forms ---------- */
.btn { border: 1px solid var(--line); background: var(--card); padding: 10px 16px; border-radius: 12px; font-weight: 650; }
.btn.sm { padding: 6px 12px; font-size: .9rem; }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; padding: 14px; }
.btn:disabled { opacity: .5; }
.fld { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; font-size: .9rem; color: var(--muted); }
.fld input, .fld textarea, .fld select {
  width: 100%; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font-size: 1.05rem;
}
.fld input:focus, .fld textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; }
.grp { border: 0; padding: 0; margin: 0 0 14px; min-width: 0; }
.grp legend { font-weight: 650; margin-bottom: 8px; padding: 0; font-size: .95rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span { display: inline-block; padding: 10px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); font-weight: 600; }
.chip input:checked + span { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
.chip input:focus-visible + span { outline: 2px solid var(--brand); }
.chips.big .chip span { padding: 13px 18px; font-size: 1.05rem; }
.quick { display: flex; gap: 6px; flex-wrap: wrap; margin: -4px 0 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; font-weight: 600; }
.check input { width: 22px; height: 22px; accent-color: var(--brand); }
.hint.danger { background: var(--bad-soft); color: var(--bad); padding: 10px 12px; border-radius: 12px; font-weight: 600; margin: 0 0 12px; }
.form-err { color: var(--bad); font-weight: 650; }

/* ---------- sheet ---------- */
#sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(15,20,17,.45); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; max-height: 92dvh; display: flex; flex-direction: column;
  background: var(--card); border-radius: 20px 20px 0 0; max-width: 640px; margin: 0 auto; animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.sheet-body { padding: 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-body .medlist { margin-bottom: 4px; }
.sheet-foot { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.sheet-foot .btn { padding: 14px; }

/* ---------- login & toast ---------- */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; background: var(--card); border-radius: 22px; padding: 28px 22px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.login-card .fld { text-align: left; }
.login-card input { text-align: center; font-size: 1.5rem; letter-spacing: .3em; }
.login-err { color: var(--bad); font-weight: 650; }
#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 30;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-weight: 600; max-width: 90vw; text-align: center;
}
@media (min-width: 560px) { .tiles { grid-template-columns: repeat(6, 1fr); } }
@media (max-width: 360px) { .brand small { display: none; } .grid3 { grid-template-columns: 1fr 1fr; } }

/* ---------- photos & translation ---------- */
.thumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.thumbs:empty { display: none; }
.thumb { position: relative; width: 64px; height: 64px; padding: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb img:not([src]) { opacity: 0; }
.thumb.broken::after { content: '📷'; position: absolute; inset: 0; display: grid; place-items: center; opacity: .4; }
#photo-prev .thumb { width: 76px; height: 76px; }
.thumb .rm { position: absolute; top: 3px; right: 3px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 1rem; line-height: 1; padding: 0; }
.photo-add { align-self: flex-start; color: var(--brand); margin-top: 4px; }
.photo-add[hidden] { display: none; }
.photo-full { width: 100%; height: auto; border-radius: 12px; display: block; min-height: 120px; background: var(--bg); }
.ev-tr { margin-top: 4px; padding: 6px 10px; border-left: 3px solid var(--brand); background: var(--brand-soft); border-radius: 0 8px 8px 0; font-size: .92rem; white-space: pre-wrap; }
.tr-tag { display: inline-block; font-size: .7rem; font-weight: 700; color: var(--brand); border: 1px solid var(--brand); border-radius: 4px; padding: 0 4px; margin-right: 6px; vertical-align: 1px; }
.ev.alert .ev-tr { background: rgba(255,255,255,.35); }
