/* テーマ変数（真実源）。app.jsはgetComputedStyleでChart.jsへ反映する */
:root {
  --bg: #f6f8fa;
  --surface: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --grid: rgba(100, 116, 139, 0.22);
  --accent: #0284c7;
  --accent-2: #d97706;
  --accent-3: #059669;
  --warn: #dc2626;
  --skeleton: #e2e8f0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #334155;
    --grid: rgba(148, 163, 184, 0.18);
    --accent: #38bdf8;
    --accent-2: #fbbf24;
    --accent-3: #34d399;
    --warn: #f87171;
    --skeleton: #334155;
  }
}
:root[data-theme='light'] {
  --bg: #f6f8fa;
  --surface: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --grid: rgba(100, 116, 139, 0.22);
  --accent: #0284c7;
  --accent-2: #d97706;
  --accent-3: #059669;
  --warn: #dc2626;
  --skeleton: #e2e8f0;
}
:root[data-theme='dark'] {
  --bg: #0f172a;
  --surface: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #334155;
  --grid: rgba(148, 163, 184, 0.18);
  --accent: #38bdf8;
  --accent-2: #fbbf24;
  --accent-3: #34d399;
  --warn: #f87171;
  --skeleton: #334155;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}

.hidden { display: none !important; }

/* ---- ヘッダー ---- */
.app-header { margin-bottom: 20px; }
.title-row {
  display: flex;
  align-items: center;
  gap: 8px 16px;
  flex-wrap: wrap;
}
.title-row .title-actions { margin-left: auto; }
.title-row h1 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-muted);
  margin: 0;
}
.title-actions { display: flex; align-items: center; gap: 8px; }

.offline-badge {
  font-size: 0.75rem;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--warn);
  color: #fff;
  white-space: nowrap;
}

/* メタ情報はタイトル行に同居させる（大きな体重表示はカードと重複するため置かない） */
.header-meta {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 2px 16px;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}
.header-meta .warn { color: var(--warn); font-weight: 600; }
.view-toggle {
  padding: 6px 12px;
  font-size: 0.85rem;
}
.controls-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ---- ボタン ---- */
.ghost-btn,
.primary-btn,
.segment-btn {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
}
.primary-btn {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* ---- グラフ ---- */
.chart-wrap {
  position: relative;
  height: 45vh;
  touch-action: pan-y;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
}
@media (min-width: 768px) {
  .chart-wrap { height: 400px; padding: 16px; }
}

/* ---- 期間セグメント + 表トグル（カードとグラフ/表の間・常に1行） ---- */
.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 12px;
  flex-wrap: nowrap;
}
.segment {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  padding: 3px;
  gap: 2px;
  flex-shrink: 0;
}
/* 狭幅では短いラベル（カスタム→C、7日平均→平均）に切り替える */
.label-short { display: none; }
@media (max-width: 559px) {
  .label-long { display: none; }
  .label-short { display: inline; }
}
/* スマホ: セグメントを詰めて1行に収める */
@media (max-width: 419px) {
  .controls-row { gap: 6px; }
  .segment { padding: 2px; }
  .segment-btn { padding: 5px 7px; font-size: 0.8rem; }
  .header-meta { font-size: 0.78rem; }
  .view-toggle { padding: 5px 9px; font-size: 0.8rem; }
}
@media (max-width: 359px) {
  .segment-btn { padding: 4px 5px; font-size: 0.72rem; }
  .view-toggle { padding: 4px 7px; font-size: 0.72rem; }
}
.segment-btn {
  white-space: nowrap;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 8px;
  padding: 5px 12px;
  font-variant-numeric: tabular-nums;
}
.segment-btn.active {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.custom-range {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin: 0 0 16px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 0.875rem;
}
.custom-range label { display: inline-flex; align-items: center; gap: 6px; color: var(--text-muted); }
.custom-range input[type='date'] {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
}
.custom-error { color: var(--warn); }

/* ---- サマリーカード ---- */
/* 3指標は常に横1行（狭い画面では文字を縮小して収める）。グラフの上に置く */
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 12px;
}
@media (max-width: 479px) {
  .cards { gap: 8px; }
  .card { padding: 10px; }
  .card h3 { font-size: 0.68rem; white-space: nowrap; }
  .card-value { font-size: 1.1rem; }
  .card-sub { font-size: 0.66rem; white-space: nowrap; }
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
}
.card h3 {
  margin: 0 0 4px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}
.card-value {
  margin: 0 0 6px;
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.card-sub {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.unit {
  font-size: 0.7em;
  color: var(--text-muted);
  margin-left: 3px;
}

/* ---- テーブル ---- */
.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.table-controls {
  display: flex;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

/* ---- フッター ---- */
.app-footer {
  margin: 24px 0 8px;
  padding-bottom: env(safe-area-inset-bottom);
  text-align: center;
}
.app-footer a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 12px;
}
.app-footer a:hover {
  color: var(--text);
  text-decoration: underline;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}
.data-table th,
.data-table td {
  text-align: right;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table th:first-child,
.data-table td:first-child { text-align: left; }
.data-table thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--text-muted);
  font-weight: 600;
}
.data-table tbody tr:last-child td { border-bottom: none; }

/* ---- 状態表示 ---- */
.state-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px 16px;
  text-align: center;
  color: var(--text-muted);
}
.state-box .primary-btn { margin-top: 8px; }

.skeleton { border-radius: 12px; background: var(--skeleton); position: relative; overflow: hidden; }
.skeleton::after {
  content: '';
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.25), transparent);
  animation: shimmer 1.4s infinite;
}
.skeleton-chart { height: 45vh; }
@media (min-width: 768px) { .skeleton-chart { height: 400px; } }
.skeleton-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.skeleton-card { height: 110px; }
@keyframes shimmer { 100% { transform: translateX(100%); } }

/* reduced-motion: アニメーションを無効化（グラフ側はapp.jsで無効化） */
@media (prefers-reduced-motion: reduce) {
  .skeleton::after { animation: none; }
  * { transition: none !important; }
}
