:root {
  --bg: #f4f7f6;
  --card: #ffffff;
  --text: #13201e;
  --muted: #5b6b68;
  --border: #dde5e3;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #ccfbf1;
  --danger: #b91c1c;
  --warn-bg: #fff7ed;
  --warn-border: #fdba74;
  --sys: #dc2626;
  --dia: #2563eb;
  --pul: #9ca3af;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1211;
    --card: #141e1d;
    --text: #e6efed;
    --muted: #93a5a1;
    --border: #24312f;
    --primary: #2dd4bf;
    --primary-ink: #04201c;
    --primary-soft: #123c37;
    --danger: #f87171;
    --warn-bg: #2a1c0e;
    --warn-border: #9a5b1c;
    --sys: #f87171;
    --dia: #60a5fa;
    --pul: #6b7280;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Hiragino Sans", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.row { display: flex; align-items: center; }
.row.between { justify-content: space-between; }
.row.gap { gap: .75rem; flex-wrap: wrap; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: .6rem; }

/* ---------- boot / spinner ---------- */
.boot { position: fixed; inset: 0; display: grid; place-items: center; }
.spinner {
  width: 36px; height: 36px; margin: 1rem auto;
  border: 4px solid var(--primary-soft); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- forms & buttons ---------- */
label { display: block; font-size: .9rem; color: var(--muted); }
input[type=email], input[type=number], input[type=text], input:not([type]), input[type=datetime-local] {
  display: block; width: 100%; margin-top: .3rem;
  font: inherit; font-size: 1.05rem; color: var(--text);
  padding: .75rem .85rem; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg);
  min-height: 48px;
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; border: 0; cursor: pointer;
  padding: .8rem 1.1rem; min-height: 48px; border-radius: 12px;
  width: 100%; margin-top: .6rem; text-decoration: none;
}
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.secondary { background: var(--primary-soft); color: var(--text); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn.link { background: none; color: var(--primary); width: auto; padding: .5rem; min-height: 40px; font-weight: 500; }
.btn.big { min-height: 56px; font-size: 1.05rem; }
.btn:disabled { opacity: .6; cursor: progress; }
.row .btn { width: auto; }
.row .btn.grow { flex: 1; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.msg { min-height: 1.2em; color: var(--danger); font-size: .9rem; margin: .6rem 0 0; }
.msg.ok { color: var(--primary); }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 1.25rem; }
.login-card { width: 100%; max-width: 380px; background: var(--card); padding: 1.75rem 1.4rem; border-radius: 22px; box-shadow: var(--shadow); text-align: center; }
.login-card form, .login-card .msg { text-align: left; }
.login-logo { display: block; margin: 0 auto .4rem; }
.login-card h1 { margin: .2rem 0; font-size: 1.6rem; color: var(--primary); }
.code-input { text-align: center; letter-spacing: .5em; font-size: 1.5rem !important; font-variant-numeric: tabular-nums; }

/* ---------- shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  padding: calc(env(safe-area-inset-top, 0px) + .7rem) 1rem .7rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { display: inline-flex; align-items: center; gap: .5rem; font-weight: 700; color: var(--primary); font-size: 1.1rem; }
.view { padding: .25rem 1rem calc(var(--tabbar-h) + var(--safe-b) + 1.5rem); max-width: 640px; margin: 0 auto; }

.card { background: var(--card); border-radius: var(--radius); padding: 1.1rem; margin-bottom: 1rem; box-shadow: var(--shadow); border: 1px solid var(--border); }
.card-title { font-weight: 700; font-size: 1rem; margin-bottom: .5rem; }
.empty { text-align: center; color: var(--muted); }

.bp-big { font-size: 3.2rem; font-weight: 800; line-height: 1.05; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.bp-big .slash { color: var(--muted); font-weight: 300; margin: 0 .1em; }
.bp-big .unit { font-size: .95rem; font-weight: 500; color: var(--muted); margin-left: .4rem; letter-spacing: 0; }
.badge { display: inline-block; padding: .2rem .65rem; border-radius: 999px; font-size: .82rem; font-weight: 700; color: #fff; }
.badge.normal { background: #16a34a; }
.badge.elevated { background: #ca8a04; }
.badge.stage1 { background: #ea580c; }
.badge.stage2 { background: #dc2626; }
.badge.crisis { background: #7f1d1d; }
#latest-time { margin-top: .4rem; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.stat-label { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: .8rem; color: var(--muted); }

.seg { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 3px; border: 1px solid var(--border); }
.seg button { font: inherit; font-size: .8rem; font-weight: 600; border: 0; background: none; color: var(--muted); padding: .35rem .55rem; border-radius: 8px; min-height: 32px; cursor: pointer; }
.seg button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.1); }
.chart-wrap { position: relative; height: 240px; margin-top: .75rem; }
.legend { display: flex; gap: 1rem; justify-content: center; font-size: .82rem; color: var(--muted); margin-top: .5rem; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: .35rem; vertical-align: middle; }
.dot.sys { background: var(--sys); } .dot.dia { background: var(--dia); } .dot.pul { background: var(--pul); }
.disclaimer { font-size: .78rem; color: var(--muted); text-align: center; padding: 0 1rem; }

/* ---------- add flow ---------- */
.preview { display: block; width: 100%; max-height: 240px; object-fit: contain; border-radius: 12px; background: #000; margin-bottom: .8rem; }
.alert { border-radius: 12px; padding: .85rem 1rem; margin-bottom: .8rem; }
.alert p { margin: .3rem 0 0; }
.alert.warn { background: var(--warn-bg); border: 1px solid var(--warn-border); }
.bp-inputs { display: grid; grid-template-columns: 1fr auto 1fr; gap: .5rem .6rem; align-items: end; margin: .4rem 0 1rem; }
.bp-inputs .slash { font-size: 2rem; color: var(--muted); padding-bottom: .4rem; }
.bp-inputs input { font-size: 1.8rem !important; font-weight: 800; text-align: center; font-variant-numeric: tabular-nums; }
.bp-inputs .pulse { grid-column: 1 / -1; }
.bp-inputs .pulse input { font-size: 1.3rem !important; }
.time-choice { border: 1px solid var(--border); border-radius: 12px; padding: .6rem .9rem .8rem; margin: 0 0 1rem; }
.time-choice legend { font-weight: 700; font-size: .95rem; padding: 0 .3rem; }
.radio { display: flex; gap: .7rem; align-items: flex-start; padding: .55rem 0; color: var(--text); font-size: 1rem; cursor: pointer; }
.radio input { width: 22px; height: 22px; margin: .1rem 0 0; accent-color: var(--primary); flex: none; }

/* ---------- history ---------- */
.day { margin: 1rem 0 .4rem; font-size: .85rem; font-weight: 700; color: var(--muted); }
.item {
  display: flex; align-items: center; gap: .8rem; width: 100%; text-align: left; font: inherit; color: inherit;
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: .75rem .9rem; margin-bottom: .5rem; cursor: pointer; min-height: 60px;
}
.item .bar { width: 6px; align-self: stretch; border-radius: 3px; flex: none; }
.item .bar.normal { background: #16a34a; } .item .bar.elevated { background: #ca8a04; }
.item .bar.stage1 { background: #ea580c; } .item .bar.stage2 { background: #dc2626; } .item .bar.crisis { background: #7f1d1d; }
.item .vals { font-weight: 800; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.item .sub { font-size: .82rem; color: var(--muted); }
.item .right { margin-left: auto; text-align: right; font-size: .85rem; color: var(--muted); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--card); border-top: 1px solid var(--border);
}
.tabbar button { font: inherit; font-size: .72rem; font-weight: 600; background: none; border: 0; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; cursor: pointer; }
.tabbar button.active { color: var(--primary); }
.tabbar .ico { font-size: 1.25rem; line-height: 1; }
.tabbar .add .plus { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; background: var(--primary); color: var(--primary-ink); font-size: 1.5rem; font-weight: 700; }

/* ---------- sheet / toast ---------- */
.sheet { border: 0; padding: 1.1rem; width: 100%; max-width: 560px; margin: auto auto 0; border-radius: 22px 22px 0 0; background: var(--card); color: var(--text); max-height: 92dvh; padding-bottom: calc(1.1rem + var(--safe-b)); }
.sheet::backdrop { background: rgba(0,0,0,.45); }
.toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); transform: translateX(-50%) translateY(20px); background: #13201e; color: #fff; padding: .7rem 1.1rem; border-radius: 12px; font-size: .92rem; opacity: 0; pointer-events: none; transition: .25s; z-index: 50; max-width: calc(100% - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- desktop ---------- */
@media (min-width: 900px) {
  .view { max-width: 720px; }
  .tabbar { left: 50%; right: auto; width: 480px; transform: translateX(-50%); bottom: 16px; border-radius: 20px; border: 1px solid var(--border); height: var(--tabbar-h); padding: 0; box-shadow: 0 8px 30px rgba(0,0,0,.08); }
  .view { padding-bottom: calc(var(--tabbar-h) + 3rem); }
  .chart-wrap { height: 320px; }
  .sheet { margin: auto; border-radius: 22px; }
}
