/* 中國歷史地圖 · Version 1.0 — Traditional Chinese UI */
:root {
  --bg: #f4f1ea;
  --panel: #fffdf8;
  --ink: #1a1a1a;
  --muted: #5c5c5c;
  --accent: #8b1e1e;
  --accent-soft: #c45c5c;
  --border: #d9d0c0;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  --radius: 10px;
  --header-h: 56px;
  --footer-h: 40px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC",
    sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Header */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0 0.85rem;
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
  z-index: 1000;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(0.95rem, 2.8vw, 1.15rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.15);
  color: inherit;
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  line-height: 1.2;
}

.btn:hover,
.btn:focus-visible {
  background: rgba(255, 255, 255, 0.28);
  outline: 2px solid #fff;
  outline-offset: 1px;
}

.btn-panel {
  background: var(--panel);
  color: var(--ink);
  border-color: var(--border);
}

.btn-panel:hover,
.btn-panel:focus-visible {
  background: #f0ebe0;
  outline-color: var(--accent);
}

/* Layout */
.app-main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  min-height: 0;
  position: relative;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  z-index: 500;
}

.map-wrap {
  position: relative;
  min-height: 280px;
}

#map {
  width: 100%;
  height: 100%;
  min-height: 280px;
}

.banner-test {
  position: absolute;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 450;
  background: #fff3cd;
  color: #664d03;
  border: 1px solid #ffecb5;
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
  font-size: 0.78rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  pointer-events: none;
  white-space: nowrap;
}

/* Filters */
.panel-section h2 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.35rem;
}

.hint {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.45;
}

.dynasty-list,
.check-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.dynasty-list label,
.check-list label {
  display: flex;
  align-items: flex-start;
  gap: 0.45rem;
  padding: 0.35rem 0.4rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
  line-height: 1.35;
}

.dynasty-list label:hover,
.check-list label:hover {
  background: #f3eee4;
}

.dynasty-list input,
.check-list input {
  margin-top: 0.15rem;
  accent-color: var(--accent);
}

.sub-options {
  margin: 0.15rem 0 0.35rem 1.4rem;
  padding: 0.35rem 0.5rem;
  border-left: 2px solid var(--border);
  display: none;
  flex-direction: column;
  gap: 0.2rem;
}

.sub-options.open {
  display: flex;
}

.legend {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.8rem;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #333;
  flex-shrink: 0;
}

.swatch.city {
  background: #2a6fdb;
}
.swatch.battle {
  background: #c0392b;
  border-radius: 2px;
  transform: rotate(45deg);
}
.swatch.person {
  background: #1e8449;
  border-radius: 2px;
}
.swatch.conf-certain {
  border-style: solid;
}
.swatch.conf-approx {
  border-style: dashed;
  opacity: 0.85;
}
.swatch.conf-disputed {
  border-style: dotted;
  background: repeating-linear-gradient(
    45deg,
    #888,
    #888 2px,
    #fff 2px,
    #fff 4px
  );
}

.border-legend {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
}

.border-swatch {
  width: 28px;
  height: 14px;
  background: rgba(139, 30, 30, 0.18);
  border: 2px dashed var(--accent);
  flex-shrink: 0;
}

.stats {
  font-size: 0.8rem;
  color: var(--muted);
}

/* Footer */
.app-footer {
  min-height: var(--footer-h);
  padding: 0.45rem 0.85rem calc(0.45rem + var(--safe-bottom));
  background: #2c241c;
  color: #f0e9dc;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.78rem;
  flex-shrink: 0;
}

.app-footer a {
  color: #ffd9a0;
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
  font: inherit;
  padding: 0;
}

.app-footer a:hover,
.app-footer a:focus-visible {
  color: #fff;
}

/* Modal / changelog */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 2000;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  background: var(--panel);
  color: var(--ink);
  width: min(640px, 100%);
  max-height: min(85dvh, 720px);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  background: #f8f3ea;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
  color: var(--accent);
}

.modal-body {
  padding: 1rem;
  overflow-y: auto;
  font-size: 0.9rem;
  line-height: 1.55;
}

.changelog-entry {
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.changelog-entry:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.changelog-entry h3 {
  margin: 0 0 0.35rem;
  font-size: 1rem;
}

.changelog-meta {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

.changelog-entry h4 {
  margin: 0.55rem 0 0.25rem;
  font-size: 0.85rem;
  color: var(--accent);
}

.changelog-entry ul {
  margin: 0;
  padding-left: 1.2rem;
}

.sidebar-toggle {
  display: none;
}

/* Leaflet popup tweaks */
.leaflet-popup-content-wrapper {
  border-radius: 10px;
  font-family: var(--font);
}

.popup-card h3 {
  margin: 0 0 0.4rem;
  font-size: 1rem;
  color: var(--accent);
}

.popup-card .tag {
  display: inline-block;
  font-size: 0.72rem;
  padding: 0.12rem 0.4rem;
  border-radius: 4px;
  background: #f0e6d8;
  margin-right: 0.25rem;
  margin-bottom: 0.35rem;
}

.popup-card dl {
  margin: 0.4rem 0 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.2rem 0.6rem;
  font-size: 0.82rem;
}

.popup-card dt {
  color: var(--muted);
  font-weight: 600;
}

.popup-card dd {
  margin: 0;
}

.popup-source {
  margin-top: 0.55rem;
  padding-top: 0.45rem;
  border-top: 1px dashed var(--border);
  font-size: 0.78rem;
  color: var(--muted);
}

.confidence-確定 {
  border-left: 3px solid #1e8449;
  padding-left: 0.45rem;
}
.confidence-大概 {
  border-left: 3px solid #d68910;
  padding-left: 0.45rem;
}
.confidence-有爭議 {
  border-left: 3px solid #922b21;
  padding-left: 0.45rem;
}

/* Mobile */
@media (max-width: 800px) {
  .app-main {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }

  .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(92vw, 340px);
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
    border-right: 1px solid var(--border);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: inline-flex;
  }

  .banner-test {
    bottom: 24px;
    font-size: 0.72rem;
    max-width: 92%;
    white-space: normal;
    text-align: center;
  }

  .modal {
    border-radius: 14px 14px 0 0;
  }
}

@media (min-width: 801px) {
  .modal-backdrop {
    align-items: center;
    padding: 1rem;
  }

  .modal {
    border-radius: var(--radius);
    max-height: 80vh;
  }
}

/* 底圖提示（天地圖載入失敗／開發底圖） */
.basemap-notice {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 460;
  max-width: min(360px, 80%);
  background: #fdecea;
  color: #7a1c14;
  border: 1px solid #f5c2c0;
  border-radius: 8px;
  padding: 0.35rem 0.65rem;
  font-size: 0.75rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}
@media (max-width: 800px) {
  .basemap-notice { top: 8px; right: 8px; left: 8px; max-width: none; }
}

.banner-test[hidden] { display: none; }

/* 參考資料 */
.ref-detail { color: #666; font-size: 0.9em; }
.ref-empty { color: #888; font-style: italic; margin: 0.3rem 0; }

/* 時期顏色小圓點 */
.period-chip {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 0.3rem;
  vertical-align: -1px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
  flex-shrink: 0;
}

/* 合併標記（同一地點多項） */
.chm-merged { background: none; border: none; }
.merged-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
  box-sizing: border-box;
}
.merged-count {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  color: #1a1a1a;
  font: 700 11px/1 var(--font);
}
.merged-popup .merged-head {
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 0.35rem;
  font-size: 0.9rem;
}
.merged-item {
  border-top: 1px solid var(--border);
  padding: 0.3rem 0;
}
.merged-item summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 0.3rem;
  font-size: 0.85rem;
  padding: 0.15rem 0;
}
.merged-item summary::-webkit-details-marker { display: none; }
.merged-item summary::before {
  content: "▸";
  color: var(--muted);
  width: 0.8rem;
}
.merged-item[open] summary::before { content: "▾"; }
.merged-name { font-weight: 700; }
.merged-meta { color: var(--muted); font-size: 0.75rem; width: 100%; padding-left: 0.8rem; }
.merged-item .popup-card { margin: 0.35rem 0 0.2rem 0.8rem; }
.merged-item .popup-card h3 { display: none; }

/* 地圖圖例（Leaflet 控制項） */
.map-legend {
  background: rgba(255, 253, 248, 0.95);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 0.75rem;
  max-width: 190px;
  max-height: 55vh;
  overflow-y: auto;
}
.map-legend summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--accent);
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
}
.map-legend-body { padding: 0 0.6rem 0.5rem; }
.ml-title { font-weight: 700; color: var(--muted); margin: 0.35rem 0 0.2rem; font-size: 0.72rem; }
.ml-row { display: flex; align-items: center; gap: 0.4rem; margin: 0.15rem 0; }
.ml-row .swatch { width: 12px; height: 12px; }
.ml-period {
  width: 22px;
  height: 12px;
  border: 2.5px solid;
  border-radius: 2px;
  flex-shrink: 0;
}
.ml-merged {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 3px #888;
  font-weight: 700;
  font-size: 10px;
  margin: 0 3px;
}
.ml-empty { color: var(--muted); font-style: italic; }
.ml-note { margin-top: 0.35rem; color: var(--muted); font-size: 0.7rem; }

@media (max-width: 800px) {
  .banner-test { bottom: 56px; }
  .map-legend { max-width: 170px; max-height: 45vh; }
}
