/* =========================================================
   くまマップ
   淡色の地図の上に、警戒色のピンだけが浮くように配色する。
   地図以外の UI は半透明の板で、地図を隠しすぎないこと。
   ========================================================= */

/* 明暗は全部この変数だけで切り替わる。個々の部品に色を直接書かないこと。
   海や陸の色まで変数にしてあるのは、地図を canvas で描いていて
   JS 側から色を読む必要があるため（app.js の landStyle）。
   OS の設定に従うのが既定で、置き場所が :root[data-theme] を立てれば
   そちらが勝つ。 */
:root {
  /* 新しさ 5 段階。赤 → 灰。夜間でも淡色地図の上で沈まない彩度にしてある */
  --age-0: #e11d38;
  --age-1: #f2740c;
  --age-2: #d4a017;
  --age-3: #5f8d6b;
  --age-4: #8b93a3;

  --topbar-h: 92px;   /* 上部バーの実測高さ。JS が更新する */
  --sheet-peek: 168px;
  --sheet-h: 168px;   /* シートの実測高さ。JS が更新する */

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

:root, :root[data-theme="light"] {
  --bg: #ffffff;
  --bg-sheet: rgba(255, 255, 255, .94);
  --fg: #1b1d22;
  --fg-dim: #5c6270;
  --line: rgba(0, 0, 0, .1);
  --chip-bg: rgba(0, 0, 0, .05);
  --chip-on: #1b1d22;
  --chip-on-fg: #ffffff;
  --shadow: 0 2px 16px rgba(0, 0, 0, .16);

  --sea: #cdd8e0;
  --land: #e9e6df;
  --land-line: #c3bcae;
  --tile-filter: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --bg-sheet: rgba(24, 27, 32, .94);
    --fg: #eef0f4;
    --fg-dim: #a2aab8;
    --line: rgba(255, 255, 255, .12);
    --chip-bg: rgba(255, 255, 255, .09);
    --chip-on: #eef0f4;
    --chip-on-fg: #14161a;
    --shadow: 0 2px 20px rgba(0, 0, 0, .5);

    --sea: #1a222c;
    --land: #272c33;
    --land-line: #3a4149;
    /* 淡色地図は明るすぎるので夜は少しだけ落とす。読めなくなるほど暗くしない */
    --tile-filter: brightness(.82) contrast(1.04);
  }
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --bg-sheet: rgba(24, 27, 32, .94);
  --fg: #eef0f4;
  --fg-dim: #a2aab8;
  --line: rgba(255, 255, 255, .12);
  --chip-bg: rgba(255, 255, 255, .09);
  --chip-on: #eef0f4;
  --chip-on-fg: #14161a;
  --shadow: 0 2px 20px rgba(0, 0, 0, .5);

  --sea: #1a222c;
  --land: #272c33;
  --land-line: #3a4149;
  --tile-filter: brightness(.82) contrast(1.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  color: var(--fg);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#map {
  position: fixed;
  inset: 0;
  /* 海。この上に陸の輪郭（app.js が描く）、さらに上にタイルが載る */
  background: var(--sea);
}

.leaflet-tile-pane { filter: var(--tile-filter); }

/* ---------------------------------------------------------
   上部バー
   --------------------------------------------------------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 900;
  padding: calc(8px + var(--safe-t)) 10px 8px;
  background: linear-gradient(to bottom, var(--bg-sheet) 62%, transparent);
  pointer-events: none;
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px 6px;
}

.brand {
  margin: 0;
  font-size: 19px;
  line-height: 1;
  flex: 0 0 auto;
  pointer-events: auto;
}

/* ---------------------------------------------------------
   場所を探す
   --------------------------------------------------------- */
.search {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  pointer-events: auto;
}

.search input {
  width: 100%;
  min-height: 36px;
  padding: 0 32px 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-sheet);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  box-shadow: var(--shadow);
}
.search input::-webkit-search-cancel-button { display: none; }
.search input:focus { outline: 2px solid #1f6feb; outline-offset: -1px; }

.q-clear {
  position: absolute;
  right: 4px; top: 50%;
  transform: translateY(-50%);
  width: 28px; height: 28px;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--fg-dim);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}

.suggest {
  position: fixed;
  top: var(--topbar-h);
  left: 10px; right: 10px;
  z-index: 970;   /* シートより前に出す */
  list-style: none;
  margin: 0;
  padding: 4px;
  max-height: min(60svh, 420px);
  overflow-y: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-sheet);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  pointer-events: auto;
}

.sg-head {
  padding: 6px 8px 2px;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--fg-dim);
}

.sg {
  display: grid;
  grid-template-columns: 20px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 9px 8px;
  border-radius: 8px;
  cursor: pointer;
}
.sg:hover, .sg.is-active { background: var(--chip-bg); }

.sg-icon { font-size: 14px; text-align: center; }
.sg-main { min-width: 0; }
.sg-name {
  font-size: 13.5px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sg-sub { font-size: 11px; color: var(--fg-dim); margin-top: 1px; }
.sg-count { font-size: 11.5px; font-weight: 800; white-space: nowrap; }
.sg-count.lv-high { color: var(--age-0); }
.sg-count.lv-mid { color: var(--age-1); }

.sg-note { padding: 10px 8px; font-size: 12px; color: var(--fg-dim); text-align: center; }

.icon-btn {
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--chip-bg);
  color: var(--fg-dim);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  pointer-events: auto;
}

/* ---------------------------------------------------------
   チップ（期間 / 距離）
   --------------------------------------------------------- */
.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px 4px;
  pointer-events: auto;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-sheet);
  color: var(--fg);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background .12s, color .12s;
}

.chip.is-on {
  background: var(--chip-on);
  color: var(--chip-on-fg);
  border-color: transparent;
}

.chips-sm .chip { min-height: 30px; font-size: 12px; padding: 0 11px; }

/* 「◯年前の今ごろ」は今を見るためのものではなく、比べるためのもの。
   同じ並びに置きつつ、地続きではないことを枠線で示す */
.chip-alt { border-style: dashed; color: var(--fg-dim); }
.chip-alt.is-on { border-style: solid; color: var(--chip-on-fg); }

/* ---------------------------------------------------------
   丸ボタン
   --------------------------------------------------------- */
.fabs {
  position: fixed;
  right: 10px;
  bottom: calc(min(var(--sheet-h), 62svh) + 12px);
  z-index: 900;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: bottom .22s ease;
}

.fab {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-sheet);
  color: var(--fg);
  font-size: 17px;
  line-height: 1;
  box-shadow: var(--shadow);
  cursor: pointer;
  display: grid;
  place-items: center;
}

/* 記号より短い日本語のほうが迷わない */
.fab-text { font-size: 12px; font-weight: 800; letter-spacing: .02em; }

.fab-primary { background: #1f6feb; border-color: transparent; color: #fff; font-size: 18px; }
/* 追従中。この地図では赤＝新しい出没の意味なので、そちらには寄せない */
.fab.is-tracking { background: #0b4ea2; color: #fff; border-color: transparent; }

/* ---------------------------------------------------------
   凡例
   --------------------------------------------------------- */
.legend {
  position: fixed;
  left: 10px;
  bottom: calc(min(var(--sheet-h), 62svh) + 12px);
  z-index: 850;
  padding: 7px 9px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-sheet);
  box-shadow: var(--shadow);
  font-size: 10.5px;
  line-height: 1.5;
  transition: bottom .22s ease, opacity .15s;
}
.legend[hidden] { display: none; }
.legend-title { display: block; font-weight: 700; margin-bottom: 3px; }

/* 広すぎる縮尺のときに地図の真ん中へ出す案内 */
.map-notice {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 800;
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: min(300px, calc(100vw - 48px));
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-sheet);
  box-shadow: var(--shadow);
  text-align: center;
}
.map-notice[hidden] { display: none; }
.map-notice strong { font-size: 14px; }
.map-notice span { font-size: 11.5px; line-height: 1.6; color: var(--fg-dim); }
.legend small { display: block; margin-top: 3px; color: var(--fg-dim); font-size: 9.5px; }

.ramp {
  width: 84px; height: 8px;
  border-radius: 4px;
  /* heat.js の色見本と合わせてある */
  background: linear-gradient(to right, #ffd65c, #f2740c 55%, #e11d38 82%, #960c28);
}
.ramp-ends { display: flex; justify-content: space-between; color: var(--fg-dim); margin-top: 1px; }
.legend ul { list-style: none; margin: 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 5px; color: var(--fg-dim); }

.sw { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }
.sw-0 { background: var(--age-0); }
.sw-1 { background: var(--age-1); }
.sw-2 { background: var(--age-2); }
.sw-3 { background: var(--age-3); }
.sw-4 { background: var(--age-4); }

/* ---------------------------------------------------------
   下部シート
   --------------------------------------------------------- */
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 950;
  display: flex;
  flex-direction: column;
  max-height: 88svh;
  height: calc(var(--sheet-peek) + var(--safe-b));
  border-radius: 16px 16px 0 0;
  border-top: 1px solid var(--line);
  background: var(--bg-sheet);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 -2px 20px rgba(0, 0, 0, .18);
  transition: height .22s ease;
  padding-bottom: var(--safe-b);
}

.sheet[data-snap="half"] { height: 52svh; }
.sheet[data-snap="full"] { height: 88svh; }
.sheet.is-dragging { transition: none; }

.sheet-grip {
  padding: 8px 0 4px;
  display: grid;
  place-items: center;
  cursor: grab;
  touch-action: none;
  flex: 0 0 auto;
}
.grip-bar {
  width: 38px; height: 4px;
  border-radius: 2px;
  background: var(--fg-dim);
  opacity: .45;
}

.sheet-head { padding: 0 12px 8px; flex: 0 0 auto; }

/* 実データが入るまでの間、見間違えられないように出しておく */
.sample-banner {
  margin: 0 0 6px;
  padding: 6px 9px;
  border-radius: 8px;
  border: 1px solid var(--age-0);
  background: color-mix(in srgb, var(--age-0) 12%, transparent);
  color: var(--fg);
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1.5;
}

/* display を指定する要素は、素の [hidden] より強くなってしまう。
   隠す指定を明示しないと hidden を付けても消えない */
.onboard[hidden] { display: none; }

.onboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 8px;
  padding: 10px 11px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip-bg);
}
.onboard strong { font-size: 13px; }
.onboard-actions { display: flex; gap: 8px; }
.onboard-actions .btn { flex: 1 1 0; min-height: 38px; padding: 0 10px; font-size: 12.5px; }

.status { display: flex; align-items: center; gap: 10px; padding: 2px 2px 8px; }

.status-badge {
  flex: 0 0 auto;
  min-width: 46px;
  padding: 5px 8px;
  border-radius: 9px;
  background: var(--chip-bg);
  color: var(--fg-dim);
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  line-height: 1.25;
}
.status-badge.lv-high { background: var(--age-0); color: #fff; }
.status-badge.lv-mid  { background: var(--age-1); color: #fff; }
.status-badge.lv-low  { background: var(--age-3); color: #fff; }

.status-text { min-width: 0; }
.status-text strong { display: block; font-size: 14px; line-height: 1.35; }
.status-text small { display: block; font-size: 11.5px; color: var(--fg-dim); margin-top: 1px; }

.places { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 6px 2px 0; }
.places::-webkit-scrollbar { display: none; }

.place {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 30px;
  padding: 0 10px;
  border: 1px dashed var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--fg-dim);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.place.is-on { border-style: solid; background: var(--chip-on); color: var(--chip-on-fg); border-color: transparent; }
.place .x { opacity: .6; font-size: 13px; padding: 0 1px; }

/* ---------------------------------------------------------
   リスト
   --------------------------------------------------------- */
.list {
  list-style: none;
  margin: 0;
  padding: 0 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  flex: 1 1 auto;
}

.row {
  display: grid;
  grid-template-columns: 5px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.row:last-child { border-bottom: 0; }

.row-bar { width: 5px; height: 34px; border-radius: 3px; background: var(--age-4); }

.row-main { min-width: 0; }
.row-place {
  font-size: 13.5px;
  font-weight: 650;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-meta { font-size: 11.5px; color: var(--fg-dim); margin-top: 2px; }
.row-kind {
  display: inline-block;
  padding: 0 5px;
  margin-right: 5px;
  border-radius: 4px;
  background: var(--chip-bg);
  font-weight: 700;
}
.row-kind.k-injury { background: var(--age-0); color: #fff; }

.row-dist { font-size: 12px; font-weight: 700; color: var(--fg-dim); text-align: right; white-space: nowrap; }

.empty { padding: 24px 4px; text-align: center; color: var(--fg-dim); font-size: 13px; line-height: 1.7; }

.sheet-foot {
  margin: 0;
  padding: 8px 14px 10px;
  font-size: 10.5px;
  line-height: 1.5;
  color: var(--fg-dim);
  border-top: 1px solid var(--line);
  flex: 0 0 auto;
}

@media (max-width: 759px) {
  /* 閉じているあいだはリストの入る余地がないので隠し、注記は 1 行に収める */
  .sheet[data-snap="peek"] .list { display: none; }

  /* 案内や警告が出ているあいだは決まった高さに収まらない。中身に合わせる */
  .sheet[data-tall="1"][data-snap="peek"] { height: auto; max-height: 70svh; }
  .sheet[data-snap="peek"] .sheet-foot {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 地図の拡大縮小ボタンは、指で操作できるスマホでは邪魔なだけ */
  .leaflet-control-zoom { display: none; }
}

/* ---------------------------------------------------------
   ピン・まとまり
   --------------------------------------------------------- */
.pin {
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .45);
}
.pin-0 { background: var(--age-0); }
.pin-1 { background: var(--age-1); }
.pin-2 { background: var(--age-2); }
.pin-3 { background: var(--age-3); }
.pin-4 { background: var(--age-4); }

/* 24 時間以内だけは波紋をつけて、ひと目で「今」が分かるようにする */
.pin-0::after {
  content: "";
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 2px solid var(--age-0);
  animation: ping 1.9s ease-out infinite;
}
@keyframes ping {
  0%   { transform: scale(.7); opacity: .85; }
  100% { transform: scale(1.5); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .pin-0::after { animation: none; opacity: .5; }
}

/* 外周リング＝24時間以内の割合。中の丸＝一番新しい出没の色 */
.cluster-ring {
  display: grid;
  place-items: center;
  border-radius: 50%;
  box-shadow: 0 1px 6px rgba(0, 0, 0, .4);
}

.cluster {
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  font-weight: 800;
  font-size: 12px;
  font-family: system-ui, sans-serif;
}
.cluster-0 { background: var(--age-0); }
.cluster-1 { background: var(--age-1); }
.cluster-2 { background: var(--age-2); }
.cluster-3 { background: var(--age-3); }
.cluster-4 { background: var(--age-4); }

.marker-selected { z-index: 10000 !important; }
.marker-selected .pin { border-color: #1f6feb; border-width: 3px; }

/* 現在地 / 登録地点 */
.me-dot {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #1f6feb;
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .25);
}
.home-dot {
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--bg-sheet);
  border: 2px solid #1f6feb;
  font-size: 13px;
}

/* ---------------------------------------------------------
   吹き出し
   --------------------------------------------------------- */
.leaflet-popup-content-wrapper {
  border-radius: 12px;
  background: var(--bg-sheet);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.leaflet-popup-tip { background: var(--bg-sheet); }
.leaflet-popup-content { margin: 11px 13px; font-size: 13px; line-height: 1.55; }
.pop-when { font-weight: 800; font-size: 13.5px; }
.pop-meta { color: var(--fg-dim); font-size: 11.5px; margin-top: 3px; }
.leaflet-container a.leaflet-popup-close-button { color: var(--fg-dim); }
.leaflet-control-attribution {
  background: var(--bg-sheet) !important;
  color: var(--fg-dim) !important;
  font-size: 9.5px !important;
}
.leaflet-control-attribution a { color: var(--fg-dim) !important; }

/* ---------------------------------------------------------
   ダイアログ
   --------------------------------------------------------- */
.dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: 80svh;
  padding: 18px 20px 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog h2 { margin: 0 0 10px; font-size: 16px; }
.dialog h3 { margin: 16px 0 4px; font-size: 13px; }
.dialog p { margin: 0 0 8px; font-size: 13px; line-height: 1.7; }
.dialog .note { color: var(--fg-dim); font-size: 11.5px; line-height: 1.65; }
.source-list { margin: 0 0 8px; padding-left: 1.1em; font-size: 12px; line-height: 1.7; color: var(--fg-dim); }

.field { display: block; margin: 12px 0; font-size: 12px; font-weight: 700; }
.field span { display: block; margin-bottom: 4px; }
.field input {
  width: 100%;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--chip-bg);
  color: var(--fg);
  font: inherit;
  font-weight: 500;
}

.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }

.btn {
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--chip-bg);
  color: var(--fg);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary { background: #1f6feb; border-color: transparent; color: #fff; }

/* ---------------------------------------------------------
   広い画面：シートを左の柱に変える
   --------------------------------------------------------- */
@media (min-width: 760px) {
  .sheet {
    top: 0;
    right: auto;
    width: 372px;
    height: 100svh !important;
    max-height: none;
    border-radius: 0;
    border-top: 0;
    border-right: 1px solid var(--line);
    padding-top: calc(var(--topbar-h) + var(--safe-t));
  }
  .sheet-grip { display: none; }
  .topbar {
    left: 0;
    width: 372px;
    background: transparent;
    padding-top: calc(10px + var(--safe-t));
    z-index: 960;   /* 柱になったシートより前。でないと期間チップが押せない */
  }
  .fabs { bottom: calc(20px + var(--safe-b)); }
  .legend { left: 392px; bottom: calc(20px + var(--safe-b)); }

  /* 柱の幅では 1 行に収まらないので折り返す。横スクロールは気づかれにくい */
  .chips { flex-wrap: wrap; overflow: visible; }

  /* 候補は左の柱の中に収める */
  .suggest { left: 10px; right: auto; width: 352px; }
}
