:root {
  --bg: #f4f5fa;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f0f1f7;
  --text: #12131a;
  --text-sub: #6b6f80;
  --border: rgba(18, 19, 26, 0.08);
  --shadow: 0 6px 20px rgba(18, 19, 26, 0.08);
  --accent: #4a4de7;
  --accent-ink: #ffffff;

  /* 混雑度カラー */
  --c-empty: #16a34a;
  --c-low: #65a30d;
  --c-mid: #d97706;
  --c-high: #ea580c;
  --c-full: #dc2626;
  --c-off: #94a3b8;

  --header-h: env(safe-area-inset-top, 0px);
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --bg-elev: #12172a;
    --surface: #161c33;
    --surface-2: #1e2540;
    --text: #eef1fb;
    --text-sub: #9aa2bd;
    --border: rgba(255, 255, 255, 0.08);
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --accent: #7c7fff;
    --accent-ink: #0b1020;
    --c-empty: #22c55e;
    --c-low: #84cc16;
    --c-mid: #f59e0b;
    --c-high: #fb923c;
    --c-full: #f87171;
    --c-off: #64748b;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans",
    "Noto Sans JP", "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
}

/* ---------- Header ---------- */
.header {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(env(safe-area-inset-top, 0px) + 12px)
    max(16px, env(safe-area-inset-left)) 12px
    max(16px, env(safe-area-inset-right));
  background: color-mix(in srgb, var(--bg-elev) 82%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid var(--border);
}

.header__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header__title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.header__logo {
  font-size: 26px;
  line-height: 1;
}

.header__titletext {
  min-width: 0;
}

.header__title h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.header__subtitle {
  margin: 2px 0 0;
  font-size: 11px;
  color: var(--text-sub);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.updated {
  margin: 0;
  font-size: 12px;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
}

/* ---------- 全体の混雑傾向 ---------- */
.overview {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 5px solid var(--ov, var(--c-mid));
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;
}

.overview__head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px 8px;
}

.overview__dot {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--ov, var(--c-mid));
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ov, var(--c-mid)) 22%, transparent);
}

.overview__body {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.overview__level {
  font-size: 16px;
  font-weight: 800;
}

.overview__trend {
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.overview__trend--wait {
  color: var(--text-sub);
  font-weight: 500;
}

.overview__content {
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 0 16px 12px;
}

.overview__stats {
  flex: none;
  width: 52px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.overview__charts {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.overview__chart {
  min-width: 0;
}

.overview__mlabel {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-sub);
  margin-bottom: 4px;
  text-align: center;
}

.ovstat {
  background: var(--surface-2);
  border-radius: 8px;
  padding: 4px 4px;
  text-align: center;
}

.ovstat span {
  display: block;
  font-size: 9px;
  color: var(--text-sub);
}

.ovstat b {
  font-size: 14px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.ovstat b i {
  font-size: 10px;
  font-weight: 700;
  font-style: normal;
  color: var(--text-sub);
  margin-left: 1px;
}

/* ---------- ツールバー（更新時刻＋説明＋ソート） ---------- */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 2px 10px;
}

.toolbar__left {
  min-width: 0;
}

.note {
  margin: 2px 0 0;
  font-size: 10px;
  color: var(--text-sub);
  line-height: 1.35;
}

.sort {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  flex: none;
}

.sortbtn {
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.sortbtn.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

.iconbtn {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
}

.iconbtn:active {
  transform: scale(0.92);
  background: var(--surface-2);
}

.iconbtn.is-loading svg {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Search ---------- */
.search {
  position: relative;
  margin-top: 12px;
  display: flex;
  align-items: center;
}

.search__icon {
  position: absolute;
  left: 12px;
  color: var(--text-sub);
  pointer-events: none;
}

.search input {
  width: 100%;
  height: 44px;
  padding: 0 40px 0 38px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px; /* iOSでのズーム防止 */
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.search__clear {
  position: absolute;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-sub);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

/* ---------- Stats ---------- */
.stats {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 6px;
  text-align: center;
}

.stat__num {
  display: block;
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat__label {
  font-size: 11px;
  color: var(--text-sub);
}

/* ---------- Main / list ---------- */
.main {
  flex: 1;
  padding: 14px max(16px, env(safe-area-inset-left)) 24px
    max(16px, env(safe-area-inset-right));
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
  animation: rise 0.35s ease both;
}

.card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  background: var(--tier, var(--c-off));
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

.card__top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.card__name {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
  min-width: 0;
  flex: 1;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card__meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

.card__badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 700;
  color: var(--tier, var(--c-off));
}

.card__badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.card__range {
  font-size: 11px;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
}

.card__stats {
  display: flex;
  gap: 5px;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.cstat {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  background: var(--surface-2);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 11px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.4;
}

.cstat i {
  font-size: 9px;
  font-weight: 700;
  font-style: normal;
  color: var(--text-sub);
}

.card__time {
  flex: none;
  text-align: right;
  min-width: 66px;
}

.card__num {
  font-size: 32px;
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--tier, var(--text));
}

.card__unit {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-sub);
  margin-left: 2px;
}

.card__na {
  font-size: 15px;
  font-weight: 800;
  color: var(--c-off);
}

/* 時間帯別グラフ（スパークライン＋目盛り） */
.card__chart {
  display: grid;
  grid-template-columns: 24px 1fr;
  grid-template-rows: 1fr 13px;
  column-gap: 4px;
  height: 68px;
  margin-top: 4px;
}

.card__chart.is-empty {
  display: flex;
  align-items: center;
  position: relative;
}

.chart-yaxis {
  grid-column: 1;
  grid-row: 1;
  position: relative;
}

.chart-plot {
  grid-column: 2;
  grid-row: 1;
  position: relative;
}

.chart-xaxis {
  grid-column: 2;
  grid-row: 2;
  position: relative;
}

.chart-plot svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.ylab {
  position: absolute;
  right: 0;
  font-size: 9px;
  line-height: 1;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.xlab {
  position: absolute;
  top: 1px;
  font-size: 7.5px;
  line-height: 1;
  color: var(--text-sub);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.spark-area {
  fill: var(--tier, var(--c-off));
  opacity: 0.12;
}

.spark-line {
  fill: none;
  stroke: var(--tier, var(--c-off));
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

.spark-dot {
  fill: var(--tier, var(--c-off));
}

.spark-grid {
  stroke: var(--border);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.chart-dot {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--tier, var(--c-off));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--tier, var(--c-off)) 25%, transparent);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.chart-note {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--text-sub);
}

.chart-val {
  position: absolute;
  font-size: 8px;
  font-weight: 700;
  line-height: 1;
  color: var(--text);
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  padding: 1px 2px;
  border-radius: 3px;
  white-space: nowrap;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

/* 取得不可カード */
.card.is-off {
  box-shadow: none;
  background: var(--surface-2);
  opacity: 0.78;
}

/* セクション見出し */
.section-title {
  margin: 18px 4px 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--text-sub);
}

.section-title:first-child {
  margin-top: 2px;
}

/* ---------- Skeleton ---------- */
.skeletons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.skel {
  height: 74px;
  border-radius: var(--radius);
  background: linear-gradient(
    100deg,
    var(--surface) 30%,
    var(--surface-2) 50%,
    var(--surface) 70%
  );
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
}

@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

/* ---------- States ---------- */
.offline {
  background: color-mix(in srgb, var(--c-mid) 18%, var(--surface));
  color: var(--text);
  border: 1px solid color-mix(in srgb, var(--c-mid) 40%, transparent);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 12px;
  text-align: center;
}

.empty {
  text-align: center;
  color: var(--text-sub);
  padding: 40px 0;
}

.error {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-sub);
}

.btn {
  margin-top: 12px;
  padding: 10px 22px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.btn:active {
  transform: scale(0.96);
}

/* ---------- Footer ---------- */
.footer {
  padding: 10px 16px calc(env(safe-area-inset-bottom, 0px) + 14px);
  text-align: center;
  font-size: 11px;
  color: var(--text-sub);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 50;
  pointer-events: none;
}

.toast.is-show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- FAB (scroll top) ---------- */
.fab {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  box-shadow: var(--shadow);
  cursor: pointer;
  opacity: 0;
  transform: translateY(16px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 40;
}

.fab.is-show {
  opacity: 1;
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
