:root {
  --bg: #0b1220;
  --bg-panel: #111a2b;
  --bg-elev: #17233a;
  --border: #22304b;
  --text: #e8eef9;
  --text-dim: #8fa0bd;
  --accent: #38bdf8;
  --accent-dim: #0b4f6c;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --topbar-h: 48px;
  --sidebar-w: 340px;
  font-synthesis-weight: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 "Hiragino Sans", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;
}

/* ---------- 上部バー ---------- */
#topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 1200;
}
#topbar h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .02em;
  white-space: nowrap;
}
#counts { display: flex; gap: 8px; align-items: baseline; margin-left: auto; white-space: nowrap; }
#counts .count { font-size: 12px; color: var(--text-dim); }
#counts strong { color: var(--text); font-variant-numeric: tabular-nums; font-size: 13px; }
.dim { color: var(--text-dim); }

.conn {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--bg-elev); white-space: nowrap;
}
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); flex: none; }
.conn-ok .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.conn-warn .dot { background: var(--warn); }
.conn-err .dot { background: var(--err); }
.conn-ok { color: var(--ok); } .conn-warn { color: var(--warn); } .conn-err { color: var(--err); }

.icon-btn {
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  width: 30px; height: 28px; cursor: pointer; font-size: 14px;
  display: grid; place-items: center; flex: none; padding: 0;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.icon-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }
.icon-btn:disabled { opacity: .45; cursor: default; }
.icon-btn.spinning { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 本体 ---------- */
main {
  display: flex;
  height: calc(100% - var(--topbar-h));
  position: relative;
}

#map { flex: 1; height: 100%; background: #0a1018; }
.leaflet-container { background: #0a1018; font-family: inherit; }

/* ---------- サイドバー ---------- */
#sidebar {
  width: var(--sidebar-w);
  flex: none;
  height: 100%;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: margin-left .18s ease;
}
body.sidebar-hidden #sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
body.sidebar-hidden #legend { left: 12px; }

.panel-section {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex: none;
  min-height: 0;
}
.panel-section.grow { flex: 1; display: flex; flex-direction: column; border-bottom: none; }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.section-actions { display: flex; gap: 8px; }
.field-label { display: block; font-size: 11px; color: var(--text-dim); margin-bottom: 4px; letter-spacing: .04em; }
.field { display: block; }

input[type="search"] {
  width: 100%; padding: 7px 9px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; font: inherit;
}
input[type="search"]:focus { outline: none; border-color: var(--accent); }

input[type="range"] { width: 100%; accent-color: var(--accent); }
output { color: var(--text); font-variant-numeric: tabular-nums; }

.link-btn {
  background: none; border: none; color: var(--accent);
  font: inherit; font-size: 11px; cursor: pointer; padding: 0;
}
.link-btn:hover { text-decoration: underline; }

.hint { font-size: 11px; color: var(--text-dim); margin-top: 4px; min-height: 1.4em; }

/* 船種フィルタ */
.cat-list { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; }
.cat {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; cursor: pointer; padding: 2px 0; user-select: none;
}
.cat input { margin: 0; accent-color: var(--accent); flex: none; }
.cat .swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.cat .cat-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat .cat-n { margin-left: auto; color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: 11px; }
.cat.empty-cat { opacity: .45; }

/* チップ */
.chip-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 9px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--bg); font-size: 12px; cursor: pointer; user-select: none;
}
.chip input { margin: 0; accent-color: var(--accent); }
.chip:has(input:checked) { border-color: var(--accent-dim); background: #0e2235; }
.chip.solo { margin-top: 8px; }

/* ---------- 一覧テーブル ---------- */
.table-wrap { flex: 1; overflow: auto; min-height: 0; border-top: 1px solid var(--border); margin: 0 -12px -10px; }
#vessel-table { width: 100%; border-collapse: collapse; font-size: 12px; }
#vessel-table thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--bg-elev); color: var(--text-dim);
  font-weight: 500; font-size: 11px; text-align: left;
  padding: 6px 8px; cursor: pointer; white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
#vessel-table thead th:hover { color: var(--accent); }
#vessel-table thead th.sorted::after { content: " ▾"; color: var(--accent); }
#vessel-table thead th.sorted.asc::after { content: " ▴"; }
#vessel-table th.num, #vessel-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
#vessel-table tbody tr { cursor: pointer; border-bottom: 1px solid #1a2436; }
#vessel-table tbody tr:hover { background: var(--bg-elev); }
#vessel-table tbody tr.selected { background: #123048; }
#vessel-table td { padding: 5px 8px; max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#vessel-table td.vname { font-weight: 500; }
#vessel-table td.vname .no-name { color: var(--text-dim); font-weight: 400; font-style: italic; }
.type-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 5px; vertical-align: 1px; }
.empty { padding: 20px 12px; text-align: center; color: var(--text-dim); font-size: 12px; }

/* ---------- 注意書き ---------- */
.disclaimer {
  flex: none; padding: 8px 12px;
  font-size: 10px; line-height: 1.45; color: var(--text-dim);
  background: var(--bg); border-top: 1px solid var(--border);
}
.disclaimer strong { color: var(--warn); font-weight: 600; }

/* ---------- 詳細パネル ---------- */
#detail {
  position: absolute; top: 12px; right: 12px; width: 310px; max-height: calc(100% - 24px);
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
  z-index: 1100; display: flex; flex-direction: column; overflow: hidden;
}
#detail[hidden] { display: none; }
.detail-head {
  display: flex; align-items: flex-start; gap: 8px; padding: 10px 12px;
  border-bottom: 1px solid var(--border); background: var(--bg-elev);
}
.detail-head h2 { margin: 0; font-size: 14px; font-weight: 600; word-break: break-word; }
.detail-sub { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.detail-body { padding: 10px 12px; overflow: auto; }
#d-fields { margin: 0; display: grid; grid-template-columns: 78px 1fr; gap: 3px 10px; font-size: 12px; }
#d-fields dt { color: var(--text-dim); font-size: 11px; }
#d-fields dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-word; }
#d-fields dd.na { color: var(--text-dim); font-variant-numeric: normal; }
.d-group { grid-column: 1 / -1; margin-top: 8px; padding-top: 6px; border-top: 1px solid var(--border);
  font-size: 10px; color: var(--text-dim); letter-spacing: .06em; }
.d-group:first-child { margin-top: 0; padding-top: 0; border-top: none; }

/* ---------- 凡例 ---------- */
#legend {
  /* #legend は main 基準なので、サイドバーの幅ぶん右へ寄せて地図の左下に置く */
  position: absolute; left: calc(var(--sidebar-w) + 12px); bottom: 16px; z-index: 1000;
  transition: left .18s ease;
  background: rgba(17,26,43,.92); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; backdrop-filter: blur(4px); max-width: 160px;
}
.legend-title { font-size: 10px; color: var(--text-dim); letter-spacing: .06em; margin-bottom: 5px; }
#legend-items { display: grid; gap: 2px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 11px; }
.legend-item .swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }

/* 暗色表示: OSM タイルを反転して夜間海図風にする（別タイル源を増やさずに済む） */
body.basemap-dark .leaflet-tile-pane {
  filter: invert(1) hue-rotate(185deg) brightness(.94) contrast(.86) saturate(.65);
}
/* 海図記号はもう一度反転させて元の色に戻す */
body.basemap-dark .leaflet-seamark-pane { filter: invert(1) hue-rotate(185deg); }

/* 地図上の船キャンバス。既定のカーソルは Leaflet の grab（地図を掴んで動かせる）に
   任せ、船の上に乗ったときだけ JS 側で pointer に変える */
.vessel-canvas { cursor: inherit; }

.leaflet-control-attribution {
  background: rgba(11,18,32,.82) !important; color: var(--text-dim) !important; font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--accent) !important; }
.leaflet-bar a { background: var(--bg-panel) !important; color: var(--text) !important; border-color: var(--border) !important; }
.leaflet-bar a:hover { background: var(--bg-elev) !important; }

/* ---------- 明るい地図のとき ---------- */
body.basemap-light { --bg: #eef2f7; --bg-panel: #ffffff; --bg-elev: #f1f5f9;
  --border: #cbd5e1; --text: #0f172a; --text-dim: #64748b; --accent: #0369a1; --accent-dim: #bae6fd; }
body.basemap-light #map, body.basemap-light .leaflet-container { background: #dfe6ee; }
body.basemap-light #vessel-table tbody tr { border-bottom-color: #e2e8f0; }
body.basemap-light #vessel-table tbody tr.selected { background: #dbeafe; }
body.basemap-light .chip:has(input:checked) { background: #e0f2fe; }
body.basemap-light #legend { background: rgba(255,255,255,.93); }
body.basemap-light .leaflet-control-attribution { background: rgba(255,255,255,.85) !important; }

/* ---------- 地図上の吹き出し ---------- */
.vessel-popup .leaflet-popup-content-wrapper {
  background: var(--bg-panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 26px rgba(0,0,0,.5);
  padding: 0;
}
.vessel-popup .leaflet-popup-content { margin: 0; width: auto !important; line-height: 1.5; }
.vessel-popup .leaflet-popup-tip { background: var(--bg-panel); border: 1px solid var(--border); }
.vessel-popup .leaflet-popup-close-button {
  color: var(--text-dim) !important; font-size: 18px; padding: 6px 8px 0 0;
}
.vessel-popup .leaflet-popup-close-button:hover { color: var(--text) !important; background: none; }

.vpop { padding: 10px 12px 11px; min-width: 190px; }
.vpop-head { display: flex; align-items: center; gap: 7px; padding-right: 16px; }
.vpop-dot { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.vpop-name { font-size: 13px; font-weight: 600; word-break: break-word; }
.vpop-sub { font-size: 11px; color: var(--text-dim); margin: 2px 0 8px 16px; }
.vpop-rows {
  margin: 0; display: grid; grid-template-columns: auto 1fr;
  gap: 3px 10px; font-size: 12px;
  border-top: 1px solid var(--border); padding-top: 7px;
}
.vpop-rows dt { color: var(--text-dim); font-size: 11px; white-space: nowrap; }
.vpop-rows dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-word; }
.vpop-rows dd.na { color: var(--text-dim); font-variant-numeric: normal; }
.vpop-more {
  margin-top: 9px; width: 100%; padding: 6px 8px;
  background: var(--accent-dim); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  font: inherit; font-size: 12px; cursor: pointer;
}
.vpop-more:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- 陸の交通量 ---------- */
/* アイコンの矩形全体で船のクリックを塞がないよう、実際の棒だけを対象にする */
.traffic-icon { background: none; border: none; pointer-events: none; }
.traffic-icon svg { display: block; filter: drop-shadow(0 1px 2px rgba(0,0,0,.5)); pointer-events: none; }
.traffic-icon svg rect { pointer-events: auto; cursor: pointer; }
.traffic-icon svg rect:hover { stroke: #fff; stroke-width: 1.5; }
.tpop .vpop-rows { grid-template-columns: 56px 1fr; }
.tpop .vpop-rows dd { white-space: normal; }
.tpop-warn {
  margin-top: 8px; padding: 5px 7px; font-size: 11px; line-height: 1.5;
  color: var(--warn); background: rgba(245,158,11,.1);
  border: 1px solid rgba(245,158,11,.35); border-radius: 5px;
}
.tpop-note { margin-top: 8px; font-size: 10px; color: var(--text-dim); line-height: 1.5; }
#legend-traffic { margin-top: 8px; padding-top: 7px; border-top: 1px solid var(--border); }
.legend-note { font-size: 10px; color: var(--text-dim); margin-top: 3px; white-space: pre-line; line-height: 1.5; }

/* ---------- 空状態（受信局のカバー外） ---------- */
#empty-state {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  z-index: 900; width: min(380px, calc(100% - 48px));
  padding: 20px 22px; text-align: center;
  background: rgba(17,26,43,.94); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 34px rgba(0,0,0,.5); backdrop-filter: blur(4px);
}
#empty-state[hidden] { display: none; }
.empty-icon { font-size: 28px; opacity: .6; margin-bottom: 6px; }
.empty-title { font-size: 14px; font-weight: 600; margin-bottom: 8px; }
.empty-body { margin: 0; font-size: 12px; line-height: 1.7; color: var(--text-dim); }
.empty-body strong { color: var(--accent); font-weight: 600; }
body.basemap-light #empty-state { background: rgba(255,255,255,.95); }

/* ---------- 海域セレクタ ---------- */
select#region-select {
  width: 100%; padding: 7px 9px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; font: inherit;
}
select#region-select:focus { outline: none; border-color: var(--accent); }
.hint.warn { color: var(--warn); }

/* ---------- 通知バナー ---------- */
#banner {
  position: absolute; left: 50%; transform: translateX(-50%);
  top: 12px; z-index: 1300; max-width: min(520px, calc(100% - 24px));
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px 9px 14px;
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--warn); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.45); font-size: 12px; line-height: 1.5;
}
#banner[hidden] { display: none; }
#banner button {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 14px; padding: 2px 4px; flex: none;
}
#banner button:hover { color: var(--text); }

/* ---------- ボトムシート用の取っ手（スマホでのみ表示） ---------- */
.sheet-handle { display: none; }

#sidebar-backdrop { display: none; }

/* ---------- 狭い画面（スマホ） ---------- */
@media (max-width: 760px) {
  :root { --topbar-h: 52px; }

  /* 上部バー: 題名を縮め、操作ボタンを優先して並べる */
  #topbar { gap: 8px; padding: 0 8px; padding-top: env(safe-area-inset-top); height: calc(var(--topbar-h) + env(safe-area-inset-top)); }
  #topbar h1 { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  #counts { display: none; }
  .conn { padding: 4px 8px; font-size: 11px; }
  #conn-text { max-width: 76px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .icon-btn { width: 36px; height: 34px; font-size: 15px; }
  #seamark-toggle { display: none; }

  main { height: calc(100% - var(--topbar-h) - env(safe-area-inset-top)); }

  /* サイドバーをボトムシート化する */
  #sidebar {
    position: absolute; left: 0; right: 0; bottom: 0; top: auto;
    width: auto; height: 78%; max-height: 78%;
    margin-left: 0 !important;
    border-right: none; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -8px 30px rgba(0,0,0,.5);
    transform: translateY(100%);
    transition: transform .22s ease;
    z-index: 1200;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.sidebar-open #sidebar { transform: translateY(0); }

  .sheet-handle {
    display: block; width: 38px; height: 4px; border-radius: 2px;
    background: var(--border); margin: 8px auto 2px; flex: none;
  }

  #sidebar-backdrop {
    display: block; position: absolute; inset: 0;
    background: rgba(0,0,0,.45); z-index: 1150;
  }
  #sidebar-backdrop[hidden] { display: none; }

  /* 指で押しやすい大きさにする */
  .panel-section { padding: 12px 14px; }
  .cat-list { grid-template-columns: 1fr 1fr; gap: 4px 10px; }
  .cat { padding: 7px 0; font-size: 13px; }
  .cat input { width: 17px; height: 17px; }
  .chip { padding: 8px 12px; font-size: 13px; }
  .chip input { width: 17px; height: 17px; }
  input[type="search"], select#region-select { padding: 11px 10px; font-size: 16px; } /* 16px 未満だと iOS が拡大する */
  input[type="range"] { height: 28px; }
  .link-btn { font-size: 13px; padding: 4px 2px; }
  #vessel-table thead th { padding: 9px 8px; font-size: 12px; }
  #vessel-table td { padding: 10px 8px; font-size: 13px; max-width: 42vw; }
  .table-wrap { margin: 0 -14px -12px; }

  /* 詳細パネルは下から全幅で出す */
  #detail {
    top: auto; left: 0; right: 0; bottom: 0; width: auto;
    max-height: 62%; border-radius: 14px 14px 0 0;
    border-left: none; border-right: none; border-bottom: none;
    box-shadow: 0 -8px 30px rgba(0,0,0,.5);
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 1250;
  }
  .detail-head { padding: 12px 14px; }
  .detail-head h2 { font-size: 15px; }
  .detail-body { padding: 12px 14px; }
  #d-fields { grid-template-columns: 88px 1fr; font-size: 13px; gap: 5px 12px; }

  /* 凡例は画面を塞ぐので隠す。詳細パネルが出ているときは地図操作を優先 */
  #legend { display: none; }
  #banner { top: 8px; font-size: 12px; }

  /* 地図のズームボタンは右下（親指の届く位置）へ */
  .leaflet-bottom.leaflet-right { margin-bottom: 8px; }
  body.detail-open .leaflet-bottom.leaflet-right { display: none; }
  .leaflet-control-attribution { font-size: 9px !important; max-width: 70vw; }
}

/* ---------- 極端に狭い画面 ---------- */
@media (max-width: 420px) {
  /* 題名は消さずに詰める。どの海域を見ているかが分からなくなるため */
  #topbar h1 { font-size: 11px; }
  #conn-text { max-width: 58px; }
  .icon-btn { width: 32px; }
}
@media (max-width: 380px) {
  .cat-list { grid-template-columns: 1fr; }
}

/* ---------- 指での操作 ---------- */
@media (pointer: coarse) {
  .vessel-canvas { cursor: default; }
}
