:root {
  color-scheme: light;

  /* 面と文字 */
  --bg: #faf6ef;
  --card-bg: #ffffff;
  --text: #3d332a;
  --muted: #8c7b6b;
  --accent: #d0603a;
  --accent-soft: #f6e7de;
  --border: #ece2d4;
  --shadow: 0 1px 2px rgba(61, 51, 42, 0.05);

  /* グラフの線と軸（背景から1段だけ浮かせる） */
  --grid: #efe8dc;
  --axis: #ddd2c1;
  --tooltip-bg: rgba(61, 51, 42, 0.92);
  --tooltip-text: #ffffff;

  /* 3系列の色。validate_palette.js で検証済み（light / surface #ffffff） */
  --series-weight: #eb6834;
  --series-run: #1baf7a;
  --series-alcohol: #2a78d6;

  /* 状態 */
  --good: #0e7a3c;
  --bad: #c0392b;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --bg: #211b17;
    --card-bg: #322921;
    --text: #f0e6d8;
    --muted: #b3a390;
    --accent: #e8956a;
    --accent-soft: #47392c;
    --border: #453a2f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);

    --grid: #40352b;
    --axis: #4f4237;
    --tooltip-bg: rgba(12, 9, 7, 0.94);
    --tooltip-text: #f5ede2;

    /* 同じ3色をダーク面用に取り直したもの（dark / surface #322921） */
    --series-weight: #d95926;
    --series-run: #199e70;
    --series-alcohol: #3987e5;

    --good: #4cc47a;
    --bad: #ef8b7f;
  }
}

* { box-sizing: border-box; }

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

h1, h2, h3 {
  font-family: "Zen Maru Gothic", "Noto Sans JP", sans-serif;
}

/* ---- ヘッダー ---- */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
  padding: 22px 18px 4px;
}

.page-header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0;
  line-height: 1.2;
}

.page-date {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.sync-dot {
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
}

.sync-dot.busy { background: var(--muted); animation: pulse 1.2s ease-in-out infinite; }
.sync-dot.ok { background: var(--good); }
.sync-dot.error { background: var(--bad); }

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .sync-dot.busy { animation: none; opacity: 0.6; }
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 8px 18px calc(32px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- カード ---- */

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 20px 18px;
}

.card h2 {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
  color: var(--text);
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.hint {
  margin: 14px 0 0;
  font-size: 0.75rem;
  line-height: 1.6;
  color: var(--muted);
  text-align: center;
}

.hint-left {
  text-align: left;
  margin: 0 0 10px;
}

/* ---- 今の数字 ---- */

.stat-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 14px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  /* 系列の色は左の細い帯だけが持つ。数字は本文色のまま */
  border-left: 3px solid var(--series-weight);
}

.stat[data-series="run"] { border-left-color: var(--series-run); }
.stat[data-series="alcohol"] { border-left-color: var(--series-alcohol); }

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
}

.stat-value {
  font-family: "Zen Maru Gothic", sans-serif;
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.15;
  color: var(--text);
  white-space: nowrap;
}

.stat-unit {
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--muted);
  margin-left: 2px;
}

.stat-sub {
  font-size: 0.65rem;
  line-height: 1.4;
  color: var(--muted);
}

.stat-sub.good { color: var(--good); }
.stat-sub.bad { color: var(--bad); }

/* ---- レース帯 ---- */

.race-band {
  background: var(--accent-soft);
  border-radius: 14px;
  padding: 12px 16px;
}

.race-band[hidden] { display: none; }

.race-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.race-name {
  font-family: "Zen Maru Gothic", sans-serif;
  font-weight: 700;
  font-size: 0.95rem;
}

.race-count {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}

.race-count strong {
  font-family: "Zen Maru Gothic", sans-serif;
  font-size: 1.3rem;
  color: var(--accent);
  margin: 0 2px;
}

.race-sub {
  margin: 2px 0 0;
  font-size: 0.72rem;
  color: var(--muted);
}

/* ---- 期間タブ ---- */

.period-tabs {
  display: flex;
  gap: 6px;
}

.period-tab {
  flex: 0 0 auto;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 13px;
  font-size: 0.75rem;
  font-weight: 500;
  font-family: inherit;
  white-space: nowrap;
  cursor: pointer;
}

.period-tab.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 700;
}

/* ---- 推移グラフ（3段） ---- */

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stack-head {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text);
  /* 縦軸の幅（JS側の AXIS_WIDTH）に合わせて、ラベルを目盛りの右に置く */
  padding-left: 44px;
}

.swatch {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  background: var(--series-weight);
}

.swatch[data-series="run"] { background: var(--series-run); }
.swatch[data-series="alcohol"] { background: var(--series-alcohol); }

.stack-unit {
  font-weight: 400;
  color: var(--muted);
}

.plot {
  position: relative;
}

.plot canvas {
  width: 100% !important;
  height: 100% !important;
}

.plot-weight { height: 148px; }
.plot-run { height: 92px; }
/* いちばん下だけ、横軸の日付ぶんの高さを含む */
.plot-alcohol { height: 116px; }

/* ---- 折りたたみ ---- */

.fold {
  padding: 0;
}

.fold summary {
  list-style: none;
  cursor: pointer;
  padding: 16px 18px;
  font-family: "Zen Maru Gothic", "Noto Sans JP", sans-serif;
  font-size: 0.95rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.fold summary::-webkit-details-marker { display: none; }

.fold summary::after {
  content: "＋";
  color: var(--accent);
  font-size: 1rem;
  font-weight: 700;
}

.fold[open] summary::after { content: "－"; }

.fold[open] summary {
  border-bottom: 1px solid var(--border);
}

.fold-body {
  padding: 16px 18px 20px;
}

.setting-block + .setting-block {
  margin-top: 22px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.setting-block h3 {
  margin: 0 0 10px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 700;
}

/* ---- フォーム部品 ---- */

input, textarea, button {
  font-size: 1rem;
  font-family: inherit;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.stacked-label { margin-top: 10px; }

input[type="text"], input[type="number"], input[type="date"], textarea {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
}

button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 13px 20px;
  font-weight: 700;
  cursor: pointer;
  min-height: 44px;
}

button:active { opacity: 0.85; }

.button-link {
  display: block;
  text-align: center;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 13px 20px;
  font-weight: 700;
  text-decoration: none;
}

.button-link[hidden] { display: none; }

.sync-status {
  margin: 10px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
}

/* ---- 表 ---- */

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

th, td {
  text-align: left;
  padding: 9px 6px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

/* 基準値は補助情報。狭い画面で検査値の列を押し出さないよう控えめに */
#blood-test-table th:nth-child(2),
#blood-test-table td:nth-child(2) {
  font-size: 0.75rem;
  color: var(--muted);
}

th {
  color: var(--muted);
  font-weight: 700;
}

#blood-test-table th:first-child,
#blood-test-table td:first-child {
  position: sticky;
  left: 0;
  background: var(--card-bg);
}

#blood-test-table td.blood-test-group { position: static; }

.blood-test-group {
  color: var(--muted);
  font-weight: 700;
  font-size: 0.75rem;
  background: var(--bg);
}

.blood-test-flag {
  color: var(--bad);
  font-weight: 700;
}

/* ---- 体組成の数字 ---- */

.metric-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.metric-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--bg);
  border-radius: 12px;
  padding: 10px 6px;
  text-align: center;
}

.metric-label {
  font-size: 0.65rem;
  color: var(--muted);
}

.metric-value {
  font-family: "Zen Maru Gothic", sans-serif;
  font-size: 1rem;
  font-weight: 700;
}

/* ---- カレンダー ---- */

.calendar-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.calendar-nav h2 {
  margin: 0;
  font-size: 1rem;
}

.calendar-nav button {
  background: none;
  color: var(--accent);
  padding: 6px 12px;
  font-weight: 700;
  min-height: 44px;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
  margin-top: 12px;
}

.calendar-dow {
  text-align: center;
  font-size: 0.7rem;
  color: var(--muted);
  padding-bottom: 2px;
}

.calendar-day {
  min-width: 0;
  width: 100%;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid transparent;
  font-family: inherit;
  color: var(--text);
  padding: 4px 2px;
  cursor: pointer;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.calendar-day.today {
  border-color: var(--accent);
}

.calendar-day.empty {
  visibility: hidden;
  cursor: default;
}

.calendar-day.future {
  color: var(--muted);
  opacity: 0.45;
  cursor: default;
}

.calendar-day .day-num {
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1;
}

.calendar-day .day-icons {
  display: flex;
  gap: 1px;
  line-height: 1;
}

.calendar-day .day-icon {
  font-size: 0.85rem;
  line-height: 1;
}

.calendar-day .day-grams {
  font-size: 0.55rem;
  line-height: 1;
  color: var(--muted);
}

/* ---- 日別の詳細シート ---- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(45, 37, 30, 0.45);
  display: flex;
  align-items: flex-end;
  z-index: 30;
}

.overlay[hidden] { display: none; }

.overlay-sheet {
  background: var(--card-bg);
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 24px 20px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.overlay-sheet h2 {
  margin: 0;
  font-size: 1.1rem;
  text-align: center;
}

.overlay-hint {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
}

.day-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.day-info:empty { display: none; }

.day-info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--accent-soft);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 0.82rem;
}

.day-info-row .day-info-label { color: var(--muted); }
.day-info-row .day-info-value { font-weight: 700; }

.category-tabs {
  display: flex;
  flex-shrink: 0;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}

.category-tab {
  flex: 0 0 auto;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.category-tab.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.menu-row-info .menu-label {
  font-weight: 700;
  font-size: 0.92rem;
}

.menu-row-info .menu-meta {
  font-size: 0.72rem;
  color: var(--muted);
}

.stepper {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.stepper button {
  width: 40px;
  height: 40px;
  min-height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 1.15rem;
  line-height: 1;
}

.stepper button.stepper-minus {
  background: var(--accent-soft);
  color: var(--text);
}

.stepper-count {
  min-width: 1.2em;
  text-align: center;
  font-weight: 700;
}

.link-btn {
  background: none;
  color: var(--accent);
  text-decoration: underline;
  padding: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  align-self: flex-start;
  min-height: 0;
}

.custom-menu-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
  border-radius: 14px;
  padding: 14px;
}

/* display:flex が hidden属性に勝ってしまうので、明示的に閉じる */
.custom-menu-form[hidden] { display: none; }

.custom-menu-row {
  display: flex;
  gap: 10px;
}

.custom-menu-row label { flex: 1; }

.overlay-total {
  text-align: center;
  font-size: 0.95rem;
  font-weight: 700;
}

.overlay-actions {
  display: flex;
  gap: 10px;
}

.overlay-actions button { flex: 1; }

.secondary-btn {
  background: var(--accent-soft);
  color: var(--text);
}

/* ---- 画面が広いとき ---- */

@media (min-width: 560px) {
  .stat-value { font-size: 2rem; }
  .card { padding: 24px 22px; }
  .fold summary { padding: 18px 22px; }
  .fold-body { padding: 18px 22px 22px; }
}
