:root {
  --bg: #F5F6F8;
  --card: #FFFFFF;
  --text: #111827;
  --muted: #4B5563;
  --line: #E5E7EB;
  --line-soft: #F0F1F3;
  --accent: #0E7490;
  --accent-soft: #E0F2FE;
  --post: #C2410C;
  --post-soft: #FFEDD5;
  --ok-fg: #166534; --ok-bg: #DCFCE7;
  --hi-fg: #9A3412; --hi-bg: #FFEDD5;
  --lo-fg: #1E40AF; --lo-bg: #DBEAFE;
  --na-fg: #4B5563; --na-bg: #F3F4F6;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[hidden] { display: none !important; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", Inter, Roboto, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { display: block; }

#app { max-width: 560px; margin: 0 auto; min-height: 100%; }
#view {
  padding: calc(var(--safe-top) + 20px) 20px calc(var(--safe-bottom) + 100px);
  display: flex; flex-direction: column; gap: 16px;
}

/* Headings */
.head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.eyebrow { font-size: 13px; font-weight: 500; color: var(--muted); }
h1 { margin: 2px 0 0; font-size: 32px; font-weight: 700; letter-spacing: -0.5px; }
h2 { margin: 8px 0 0; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; }

/* Cards */
.card { background: var(--card); border-radius: 20px; box-shadow: 0 1px 2px rgba(17,24,39,.06); }
.card.pad { padding: 16px 18px; display: flex; flex-direction: column; gap: 10px; }
.reading-card { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.rc-top { display: flex; justify-content: space-between; align-items: center; }
.rc-title { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; }
.icon-chip { width: 32px; height: 32px; border-radius: 10px; display: flex; align-items: center; justify-content: center; }
.icon-chip svg { width: 18px; height: 18px; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-chip.f { background: var(--accent-soft); } .icon-chip.f svg { stroke: var(--accent); }
.icon-chip.p { background: var(--post-soft); } .icon-chip.p svg { stroke: var(--post); }
.big { display: flex; align-items: baseline; gap: 6px; }
.big b { font-size: 48px; font-weight: 700; letter-spacing: -1.5px; line-height: 1; }
.rc-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; } .xsmall { font-size: 12px; }

.pill { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.pill.ok { color: var(--ok-fg); background: var(--ok-bg); }
.pill.hi { color: var(--hi-fg); background: var(--hi-bg); }
.pill.lo { color: var(--lo-fg); background: var(--lo-bg); }
.pill.na { color: var(--na-fg); background: var(--na-bg); }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.stats.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stat { background: var(--card); border-radius: 16px; padding: 14px 12px; display: flex; flex-direction: column; gap: 4px; }
.stat .k { font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.stat .v { font-size: 22px; font-weight: 700; }
.stat .s { font-size: 13px; color: var(--muted); }

/* Buttons */
.btn { height: 54px; width: 100%; border-radius: 16px; border: none; font-size: 17px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 8px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.outline-post { height: 44px; border-radius: 12px; border: 1.5px solid var(--post); background: transparent; color: var(--post); font-size: 15px; }
.btn.outline-f { height: 44px; border-radius: 12px; border: 1.5px solid var(--accent); background: transparent; color: var(--accent); font-size: 15px; }
.btn.secondary { background: var(--card); color: var(--accent); font-size: 16px; height: 50px; }
.btn.danger { background: transparent; color: #B91C1C; font-size: 16px; height: 48px; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }
.link { background: none; border: none; color: var(--accent); font-size: 17px; height: 44px; padding: 0 8px; }
.link.strong { font-weight: 600; }
.pillbtn { height: 36px; padding: 0 14px; border-radius: 18px; border: 1px solid var(--line); background: var(--card); font-size: 13px; font-weight: 500; }
.pillbtn[aria-pressed="true"] { background: var(--text); color: #fff; border-color: var(--text); font-weight: 600; }
.ranges { display: flex; gap: 8px; flex-wrap: wrap; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  background: rgba(255,255,255,.96); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 6px 8px calc(var(--safe-bottom) + 6px);
}
.tab { background: none; border: none; min-height: 50px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); font-size: 11px; font-weight: 500; }
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Log */
.day { display: flex; flex-direction: column; gap: 8px; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 4px; }
.day-head b { font-size: 15px; }
.entry { width: 100%; min-height: 60px; padding: 10px 16px; background: none; border: none; border-bottom: 1px solid var(--line-soft); display: flex; align-items: center; gap: 12px; text-align: left; }
.entry:last-child { border-bottom: none; }
.entry .meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.entry .meta b { font-size: 15px; font-weight: 600; }
.entry .meta span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .val { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.entry .val b { font-size: 17px; font-weight: 600; }
.entry .pill { font-size: 11px; padding: 2px 8px; }
.empty { padding: 32px 20px; text-align: center; color: var(--muted); font-size: 15px; line-height: 1.5; }

/* Trends chart */
.chart-card { padding: 16px 12px 12px; display: flex; flex-direction: column; gap: 10px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; padding: 0 6px; font-size: 12px; font-weight: 500; }
.legend span { display: flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 5px; display: inline-block; }
.chart { width: 100%; height: auto; }
.chart text { font-size: 10px; fill: var(--muted); font-family: inherit; }
.note-card { padding: 14px 16px; font-size: 14px; line-height: 1.45; }

/* Settings */
.list { display: flex; flex-direction: column; }
.row { min-height: 52px; padding: 8px 18px; display: flex; justify-content: space-between; align-items: center; gap: 12px; border-bottom: 1px solid var(--line-soft); font-size: 15px; }
.row:last-child { border-bottom: none; }
.row.col { flex-direction: column; align-items: stretch; gap: 6px; }
.row input[type="number"] { width: 84px; height: 40px; border: 1px solid var(--line); border-radius: 10px; padding: 0 10px; text-align: right; background: #fff; }
.row input[type="datetime-local"] { border: none; background: none; color: var(--accent); font-weight: 500; text-align: right; min-height: 40px; }
.row textarea { border: none; resize: none; padding: 0; background: transparent; font-size: 15px; }
.row .seg-sm { display: flex; background: var(--line); border-radius: 10px; padding: 2px; gap: 2px; }
.row .seg-sm button { border: none; background: transparent; height: 36px; padding: 0 12px; border-radius: 8px; font-size: 14px; color: var(--muted); }
.row .seg-sm button[aria-pressed="true"] { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(17,24,39,.12); }
.foot { font-size: 12px; color: var(--muted); line-height: 1.5; padding: 0 4px; }

/* Sheet */
.sheet { position: fixed; inset: 0; z-index: 10; background: var(--bg); overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet[hidden] { display: none; }
.sheet-inner { max-width: 560px; margin: 0 auto; padding: calc(var(--safe-top) + 8px) 0 calc(var(--safe-bottom) + 24px); }
.sheet-nav { display: flex; justify-content: space-between; align-items: center; padding: 4px 12px; position: sticky; top: 0; background: var(--bg); z-index: 1; }
.sheet-title { font-size: 17px; font-weight: 600; }
.sheet-body { padding: 8px 20px 0; display: flex; flex-direction: column; gap: 16px; }
.segmented { background: var(--line); border-radius: 12px; padding: 3px; display: grid; grid-template-columns: 1fr 1fr; gap: 3px; }
.segmented button { height: 40px; border: none; border-radius: 10px; background: transparent; color: var(--muted); font-size: 15px; font-weight: 500; }
.segmented button[aria-pressed="true"] { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(17,24,39,.12); }
.value-card { padding: 22px 18px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.value-row { display: flex; align-items: baseline; justify-content: center; gap: 8px; }
.value-row input { width: 190px; border: none; background: transparent; font-size: 64px; font-weight: 700; letter-spacing: -2px; text-align: right; padding: 0; }
.value-row input::-webkit-outer-spin-button, .value-row input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.value-row input { -moz-appearance: textfield; }
.value-row input:focus, .value-row input:focus-visible { outline: none; }
.row > span:first-child { white-space: nowrap; }
.unit { font-size: 17px; font-weight: 500; color: var(--muted); }
.field-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.chips { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; margin-bottom: 4px; }
.chips button { height: 44px; border-radius: 12px; border: 1px solid var(--line); background: #fff; font-size: 14px; font-weight: 500; padding: 0 4px; }
.chips button[aria-pressed="true"] { border: 1.5px solid var(--post); background: #FFF7ED; color: var(--hi-fg); font-weight: 600; }
.error { margin: 0; min-height: 0; color: #B91C1C; font-size: 14px; text-align: center; }
.error:empty { display: none; }

input:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 84px); transform: translate(-50%, 20px);
  background: var(--text); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Print report */
.report { display: none; }
@media print {
  #app { display: none !important; }
  .report { display: block; font-size: 12pt; color: #000; padding: 0; }
  .report h1 { font-size: 20pt; margin: 0 0 4pt; }
  .report p { margin: 0 0 10pt; }
  .report table { width: 100%; border-collapse: collapse; }
  .report th, .report td { border-bottom: 1px solid #ccc; padding: 5pt 6pt; text-align: left; }
  .report th { font-size: 11pt; }
}
