:root {
  --bg: #ffffff;
  --bg-2: #fafafa;
  --bg-3: #f5f5f5;
  --bg-card: #ffffff;
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #aeaeb2;
  --border: #e8e8ea;
  --border-2: #d4d4d4;
  --primary: #0071e3;
  --primary-light: #e8f4fd;
  --shadow: 0 1px 3px rgba(0,0,0,.04), 0 1px 2px rgba(0,0,0,.02);
  --shadow-md: 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px;
  --radius-sm: 10px;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-2: #0e0e10;
    --bg-3: #1c1c1e;
    --bg-card: #1c1c1e;
    --text: #f5f5f7;
    --text-2: #a1a1a6;
    --text-3: #636366;
    --border: #2c2c2e;
    --border-2: #3a3a3c;
    --primary: #0a84ff;
    --primary-light: rgba(10,132,255,.15);
    --shadow: 0 1px 3px rgba(0,0,0,.3);
    --shadow-md: 0 4px 16px rgba(0,0,0,.4);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: "Noto Sans JP", "Hiragino Sans", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg-2);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

.app { min-height: 100vh; padding-bottom: calc(var(--safe-bottom) + 70px); }

/* ============ ヘッダー ============ */
.hero {
  padding: calc(var(--safe-top) + 24px) 20px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.hero-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 8px;
}
.live-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #34c759;
  animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }
.hero h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}
.hero-sub {
  font-size: 13px;
  color: var(--text-2);
}

/* ============ KPIカード ============ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding: 16px 20px 0;
}
.kpi {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  box-shadow: var(--shadow);
}
.kpi-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.kpi-value {
  font-family: "DM Sans", "Noto Sans JP", sans-serif;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  font-feature-settings: "tnum";
  line-height: 1.1;
}
.kpi-sub {
  font-size: 11px;
  color: var(--text-3);
  margin-top: 2px;
  font-feature-settings: "tnum";
}

/* ============ セクション ============ */
.section {
  margin: 24px 20px 0;
  padding: 18px 16px;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 14px;
}
.section-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-emoji { font-size: 18px; }
.section-meta {
  font-size: 11px;
  color: var(--text-3);
  font-weight: 500;
  font-feature-settings: "tnum";
}

/* グラフ */
.chart-wrap {
  position: relative;
  height: 260px;
  margin: 0 -4px;
}
.chart-wrap.tall { height: 320px; }

/* 凡例 */
.legend-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  font-size: 13px;
}
.legend-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.legend-label {
  flex: 1;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.legend-bar-wrap {
  flex: 2;
  height: 6px;
  background: var(--bg-3);
  border-radius: 3px;
  overflow: hidden;
  max-width: 120px;
}
.legend-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 800ms var(--ease);
}
.legend-count {
  font-size: 12px;
  color: var(--text-2);
  font-feature-settings: "tnum";
  min-width: 50px;
  text-align: right;
}

/* ランキング */
.rank-list { display: flex; flex-direction: column; }
.rank-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.rank-row:last-child { border-bottom: none; }
.rank-num {
  font-family: "DM Sans", sans-serif;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-3);
  width: 22px;
  text-align: center;
  font-feature-settings: "tnum";
}
.rank-row.top3 .rank-num { color: var(--primary); }
.rank-name {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rank-count {
  font-family: "DM Sans", sans-serif;
  font-size: 14px;
  font-weight: 700;
  font-feature-settings: "tnum";
}
.rank-count-unit {
  font-size: 11px;
  color: var(--text-2);
  margin-left: 1px;
  font-weight: 500;
}

/* 満了ヒートマップ */
.expiry-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin-top: 12px;
}
.expiry-cell {
  aspect-ratio: 1;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--text);
}
.expiry-cell-month {
  font-size: 9px;
  color: var(--text-3);
  font-weight: 500;
}
.expiry-cell-count {
  font-family: "DM Sans", sans-serif;
  font-size: 11px;
  font-weight: 700;
  font-feature-settings: "tnum";
}
.expiry-note {
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-2);
}

/* 検索 */
.search-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 14px;
}
.search-box { flex: 1; position: relative; }
.search-input {
  width: 100%;
  height: 38px;
  padding: 0 36px 0 36px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--bg-3);
  border: none;
  border-radius: 19px;
  outline: none;
}
.search-icon {
  position: absolute;
  left: 12px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-3);
  pointer-events: none;
}

/* 結果リスト */
.result-list {
  margin-top: 8px;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.result-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: opacity 150ms;
}
.result-item:active { opacity: 0.6; }
.result-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}
.result-meta {
  font-size: 12px;
  color: var(--text-2);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}
.result-cat-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 6px;
  background: var(--bg-3);
  border-radius: 8px;
  font-size: 11px;
  font-weight: 500;
}
.result-empty {
  text-align: center;
  padding: 32px 0;
  color: var(--text-3);
  font-size: 13px;
}

/* タブバー */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--safe-bottom) + 64px);
  padding-bottom: var(--safe-bottom);
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 1000;
}
@media (prefers-color-scheme: dark) {
  .tabbar { background: rgba(0,0,0,0.85); }
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  color: var(--text-3);
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
  transition: color 150ms;
  padding: 4px 0;
}
.tab.active { color: var(--primary); }
.tab svg { width: 22px; height: 22px; }

/* ビュー切替 */
.view {
  display: none;
  animation: fadeIn 250ms var(--ease);
}
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* 地図 */
.map-view {
  position: fixed;
  top: 0; left: 0; right: 0;
  bottom: calc(var(--safe-bottom) + 64px);
}
.map-view #map { width: 100%; height: 100%; }
.map-header {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  left: 12px; right: 12px;
  z-index: 500;
  display: flex;
  gap: 8px;
}
.map-search-wrap {
  flex: 1;
  position: relative;
}
.map-search-wrap .search-icon {
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  z-index: 1;
}
.map-search {
  width: 100%;
  height: 40px;
  padding: 0 16px 0 38px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: rgba(255,255,255,0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: none;
  border-radius: 20px;
  outline: none;
  box-shadow: var(--shadow-md);
}
@media (prefers-color-scheme: dark) {
  .map-search { background: rgba(28,28,30,0.95); }
}
.map-status {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.85);
  color: white;
  padding: 8px 14px;
  border-radius: 18px;
  font-size: 12px;
  font-weight: 500;
  z-index: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow-md);
  transition: opacity 250ms, transform 250ms;
}
.map-status.hidden { opacity: 0; transform: translate(-50%, 10px); pointer-events: none; }
.spinner {
  width: 12px; height: 12px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.cust-marker {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

/* 詳細シート */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 250ms var(--ease);
}
.sheet-backdrop.visible { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 1501;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  max-height: 88vh;
  transform: translateY(100%);
  transition: transform 350ms var(--ease);
  box-shadow: 0 -10px 40px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sheet.visible { transform: translateY(0); }
.sheet-handle {
  display: flex;
  justify-content: center;
  padding: 8px 0 4px;
}
.sheet-handle::before {
  content: "";
  width: 36px; height: 4px;
  background: var(--border-2);
  border-radius: 2px;
}
.sheet-header {
  padding: 4px 20px 16px;
  border-bottom: 1px solid var(--border);
}
.sheet-title {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 4px;
  word-break: break-word;
}
.sheet-meta {
  font-size: 13px;
  color: var(--text-2);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px calc(var(--safe-bottom) + 32px);
  -webkit-overflow-scrolling: touch;
}
.detail-section { margin-bottom: 18px; }
.detail-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 8px;
}
.detail-row {
  display: flex;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.detail-row:last-child { border-bottom: none; }
.detail-key {
  font-size: 13px;
  color: var(--text-2);
  flex-shrink: 0;
  width: 84px;
}
.detail-val {
  font-size: 14px;
  color: var(--text);
  flex: 1;
  word-break: break-word;
}
.detail-val a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}
.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn {
  flex: 1;
  height: 42px;
  border: 1px solid var(--border-2);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
  transition: opacity 150ms;
}
.btn:active { opacity: 0.6; }
.btn-primary {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

/* 起動 */
.boot {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  transition: opacity 400ms var(--ease);
}
.boot.hidden { opacity: 0; pointer-events: none; }
.boot-emoji {
  font-size: 56px;
  margin-bottom: 14px;
  animation: float 2.4s ease-in-out infinite;
}
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
.boot-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 6px;
}
.boot-sub {
  font-size: 12px;
  color: var(--text-2);
  font-feature-settings: "tnum";
}
.boot-error {
  margin-top: 14px;
  padding: 12px 16px;
  background: var(--bg-3);
  border-radius: 10px;
  font-size: 12px;
  color: #ff3b30;
  max-width: 320px;
  text-align: center;
  display: none;
  line-height: 1.5;
}

/* ポップアップ */
.leaflet-popup-content-wrapper {
  border-radius: var(--radius-sm);
}
.leaflet-popup-content {
  margin: 12px 14px;
  font-family: inherit;
  font-size: 13px;
  min-width: 180px;
}
.popup-name { font-weight: 600; margin-bottom: 2px; }
.popup-type { color: var(--text-2); font-size: 12px; margin-bottom: 6px; }
.popup-link {
  color: var(--primary);
  font-weight: 600;
  font-size: 12px;
  text-decoration: none;
  cursor: pointer;
}
@media (prefers-color-scheme: dark) {
  .leaflet-popup-content-wrapper, .leaflet-popup-tip {
    background: var(--bg-card);
    color: var(--text);
  }
}

/* フッター */
.attribution {
  padding: 32px 20px calc(var(--safe-bottom) + 32px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.attribution-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--text-3);
  line-height: 1.6;
}
.attribution-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--text-2);
  text-transform: uppercase;
  margin-bottom: 2px;
}
.attribution a {
  color: var(--text-2);
  text-decoration: none;
}
.attribution a:hover { color: var(--primary); }
