/*
 * Personal BP Observatory — 画面のスタイル
 *
 * 前提
 *   * iPhone SE 相当（幅 320px）から破綻しないこと（設計書 §30）
 *   * 外部フォント・外部アイコンを読み込まない（CSP が 'self' しか許していない）
 *   * 下タブは固定。safe-area-inset を見て iPhone のホームバーに潜らせない
 */

:root {
  color-scheme: light dark;

  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #e9e9ef;
  --text: #11131a;
  --text-dim: #6b7280;
  --line: #d8d8e0;
  --accent: #0a6cff;
  --sbp: #d7263d;
  --dbp: #1c6dd0;
  --map: #7a5af8;
  --hr: #f08c00;
  --ok: #12805c;
  --warn: #b45309;
  --danger: #c0392b;

  --tabbar-h: 56px;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #15161a;
    --surface-2: #1f2027;
    --text: #f2f2f7;
    --text-dim: #9aa0aa;
    --line: #2c2d35;
    --accent: #4c9aff;
    --sbp: #ff6b7d;
    --dbp: #6fb1ff;
    --map: #b9a3ff;
    --hr: #ffc266;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
  overscroll-behavior-y: contain;
}

.appbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.appbar h1 { margin: 0 auto 0 0; font-size: 17px; font-weight: 700; letter-spacing: .01em; }
.appbar .sub { font-size: 12px; color: var(--text-dim); }

main { padding: 12px 12px 24px; max-width: 900px; margin-inline: auto; }

/* ------------------------------------------------------------------ card */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin: 0 0 12px;
}
.card h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.card h3 { margin: 14px 0 6px; font-size: 14px; }

/* ------------------------------------------------------------- big value */
.bp-hero { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.bp-hero .value { font-size: 46px; font-weight: 700; line-height: 1; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.bp-hero .unit { font-size: 13px; color: var(--text-dim); }
.bp-hero .when { font-size: 12px; color: var(--text-dim); margin-left: auto; }

.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 8px; margin-top: 12px; }
.metric { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.metric .k { font-size: 11px; color: var(--text-dim); }
.metric .v { font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; }
.metric .v small { font-size: 11px; font-weight: 400; color: var(--text-dim); margin-left: 2px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; border: 1px solid var(--line); }
.badge.g0 { color: var(--ok); }
.badge.g1 { color: var(--warn); }
.badge.g2 { color: var(--danger); }
.badge.type { color: var(--text-dim); }

/* ------------------------------------------------------------------ list */
.rows { list-style: none; margin: 0; padding: 0; }
.rows li {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 9px 0; border-bottom: 1px solid var(--line);
}
.rows li:last-child { border-bottom: none; }
.rows .time { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--text-dim); width: 46px; flex: none; padding-top: 2px; }
.rows .body { flex: 1; min-width: 0; }
.rows .body .main { font-size: 15px; }
.rows .body .sub { font-size: 12px; color: var(--text-dim); }
.rows .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 8px; flex: none; background: var(--text-dim); }
.dot.BP { background: var(--sbp); }
.dot.INTAKE { background: var(--dbp); }
.dot.ACTIVITY { background: var(--ok); }
.dot.SLEEP { background: var(--map); }
.dot.WEIGHT { background: var(--hr); }

.day-head { font-size: 12px; font-weight: 700; color: var(--text-dim); margin: 16px 0 4px; }

/* ----------------------------------------------------------------- forms */
form { display: block; }
.field { margin: 0 0 10px; }
.field label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 3px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

input, select, textarea, button {
  font: inherit;
  color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
  min-width: 0;
  /* iOS で入力欄にフォーカスすると拡大するのを防ぐ（16px 未満にしない） */
  font-size: 16px;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
textarea { min-height: 64px; resize: vertical; }

button { background: var(--accent); color: #fff; border-color: transparent; font-weight: 600; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: var(--surface-2); color: var(--text); border-color: var(--line); font-weight: 500; }
button.link { background: none; border: none; color: var(--accent); width: auto; padding: 4px 6px; font-size: 13px; }
button.danger { background: none; border: none; color: var(--danger); width: auto; padding: 2px 4px; font-size: 12px; }

.btnrow { display: flex; gap: 8px; margin-top: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { width: auto; padding: 7px 12px; font-size: 14px; }

.msg { font-size: 13px; margin: 8px 0 0; min-height: 1.2em; }
.msg.error { color: var(--danger); }
.msg.ok { color: var(--ok); }
.hint { font-size: 12px; color: var(--text-dim); margin: 6px 0 0; }
.empty { color: var(--text-dim); font-size: 14px; padding: 10px 0; }

/* ---------------------------------------------------------------- tables */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: right; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; font-variant-numeric: tabular-nums; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-dim); font-weight: 600; font-size: 11px; }

/* ----------------------------------------------------------------- chart */
.chart { width: 100%; height: auto; display: block; touch-action: pan-y; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--text-dim); margin-top: 6px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }

/* ---------------------------------------------------------------- tabbar */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  background: none; border: none; border-radius: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 10px; font-weight: 500; color: var(--text-dim); padding: 0;
}
.tabbar button .glyph { font-size: 17px; line-height: 1; }
.tabbar button[aria-current="page"] { color: var(--accent); }

/* ------------------------------------------------------------------ login */
.login { max-width: 380px; margin: 12vh auto 0; padding: 0 20px; }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login p { color: var(--text-dim); font-size: 13px; margin: 0 0 18px; }

.segmented { display: flex; gap: 4px; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 10px; }
.segmented button { background: none; color: var(--text-dim); border: none; border-radius: 8px; padding: 7px 4px; font-size: 13px; font-weight: 600; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); }

.note { font-size: 12px; color: var(--text-dim); border-left: 3px solid var(--line); padding-left: 10px; margin: 10px 0 0; }

@media (min-width: 768px) {
  /* iPad 横：解析画面を2ペインにする（設計書 §30） */
  main { padding: 16px; }
  .panes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: start; }
  .panes > * { margin-bottom: 0; }
}

/* ---------------------------------------------------------- 動的スタイルの逃げ道
 * CSP の style-src 'self' は style 属性を拒否する。
 * 静的な見た目はここに置き、値が動くもの（棒の幅・凡例の色）だけを
 * JS から CSSOM で当てる（app.client.js の el() の styles プロパティ）。 */

.daynav { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.daylabel { font-weight: 600; font-variant-numeric: tabular-nums; }
button.narrow { width: auto; }
.toggles { margin-bottom: 8px; }
button.off { opacity: .45; }

.histrow { display: flex; align-items: center; gap: 8px; margin: 2px 0; }
.histlabel { width: 64px; font-size: 11px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.histbar { height: 10px; min-width: 4px; border-radius: 3px; background: var(--accent); }
.histcount { font-size: 11px; color: var(--text-dim); }
