:root {
  --paper: #f4ead3;
  --paper-2: #ebdcbc;
  --ink: #3b2a1c;
  --ink-soft: #6e5640;
  --line: #cdb88f;
  --accent: #2f7f8f;
  --red: #a8432f;
  --shadow: 0 2px 6px rgba(40, 25, 10, .25), 0 8px 24px rgba(40, 25, 10, .18);
  --ui: "EB Garamond", Georgia, serif;
  --fell: "IM Fell English", "EB Garamond", Georgia, serif;
  --fellsc: "IM Fell English SC", "EB Garamond", Georgia, serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #2a2119;
    --paper-2: #362a1f;
    --ink: #f0e2c6;
    --ink-soft: #c5ad8c;
    --line: #5a4733;
    --accent: #6cc0cf;
    --red: #e07a62;
  }
}
:root[data-theme="dark"] {
  --paper: #2a2119; --paper-2: #362a1f; --ink: #f0e2c6; --ink-soft: #c5ad8c;
  --line: #5a4733; --accent: #6cc0cf; --red: #e07a62;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #e9dcbc; color: var(--ink); font-family: var(--ui); }
#map { position: fixed; inset: 0; background: #e9dcbc; }
.leaflet-container { font-family: var(--ui); background: #e9dcbc; }
button { font: inherit; color: inherit; }

/* ── top search bar ─────────────────────────────── */
#topbar { position: fixed; top: 12px; left: 12px; z-index: 1000; width: min(400px, calc(100vw - 24px)); }
.searchcard {
  display: flex; align-items: center; gap: 4px; padding: 4px 6px;
  background: var(--paper); border-radius: 26px; box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
#search {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink);
  font: 500 18px/1 var(--ui); padding: 10px 4px; outline: none;
}
#search::placeholder { color: var(--ink-soft); font-style: italic; }
#search::-webkit-search-cancel-button { display: none; }
.iconbtn[hidden] { display: none; }
.iconbtn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 50%;
  background: transparent; color: var(--ink-soft); cursor: pointer; flex: none;
}
.iconbtn:hover { background: var(--paper-2); color: var(--ink); }
#results {
  list-style: none; margin: 6px 0 0; padding: 6px 0; max-height: 60vh; overflow: auto;
  background: var(--paper); border-radius: 14px; box-shadow: var(--shadow); border: 1px solid var(--line);
}
#results li { padding: 8px 16px; cursor: pointer; display: flex; gap: 10px; align-items: baseline; }
#results li[aria-selected="true"], #results li:hover { background: var(--paper-2); }
#results .rn { font-weight: 600; }
#results .rk { color: var(--ink-soft); font-size: 14px; margin-left: auto; white-space: nowrap; }
#results .empty { color: var(--ink-soft); font-style: italic; cursor: default; }

/* ── info panel ─────────────────────────────────── */
#panel {
  position: fixed; z-index: 900; top: 0; left: 0; bottom: 0; width: 420px; max-width: 100vw;
  background: var(--paper); box-shadow: var(--shadow); overflow-y: auto; padding: 76px 0 24px;
  border-right: 1px solid var(--line);
}
#panel .close { position: absolute; top: 74px; right: 10px; }
.p-hero { padding: 6px 24px 14px; border-bottom: 1px solid var(--line); }
.p-kind { font: 600 13px/1 var(--ui); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-soft); }
.p-hero h1 { font: 400 32px/1.1 var(--fell); margin: 8px 40px 6px 0; }
.p-alt { color: var(--ink-soft); font-style: italic; font-size: 16px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px;
  border: 1px solid var(--line); font-size: 14px; background: var(--paper-2); cursor: default;
}
button.chip { cursor: pointer; }
button.chip:hover { border-color: var(--ink-soft); }
.chip .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.chip.tier-s4 { border-color: #b48a2c; }
.chip.tier-ln { border-color: var(--red); color: var(--red); }
.p-sec { padding: 14px 24px; border-bottom: 1px solid var(--line); }
.p-sec:last-child { border-bottom: 0; }
.p-sec h2 { font: 400 15px/1 var(--fellsc); letter-spacing: .06em; color: var(--ink-soft); margin: 0 0 10px; }
.p-desc { font-size: 18px; line-height: 1.5; margin: 0; }
.p-row { display: flex; gap: 12px; font-size: 17px; line-height: 1.4; margin: 4px 0; }
.p-row .k { color: var(--ink-soft); min-width: 64px; }
.events { list-style: none; margin: 0; padding: 0; }
.events li { position: relative; padding: 0 0 12px 22px; font-size: 17px; line-height: 1.45; }
.events li::before {
  content: ""; position: absolute; left: 4px; top: 8px; width: 8px; height: 8px; border-radius: 50%;
  background: var(--ink-soft);
}
.events li::after { content: ""; position: absolute; left: 7.5px; top: 18px; bottom: 0; width: 1px; background: var(--line); }
.events li:last-child::after { display: none; }
.events .src { display: block; font-size: 14px; color: var(--ink-soft); font-style: italic; }
.events .locked { color: var(--ink-soft); font-style: italic; }
.plist { list-style: none; margin: 0; padding: 0; columns: 2; column-gap: 16px; }
.plist li { break-inside: avoid; }
.plist button {
  border: 0; background: none; padding: 3px 0; cursor: pointer; text-align: left; font-size: 17px; color: var(--accent);
}
.plist button:hover { text-decoration: underline; }
.erahist { list-style: none; margin: 0; padding: 0; }
.erahist li { padding: 6px 0; font-size: 16px; line-height: 1.4; border-top: 1px dashed var(--line); }
.erahist li:first-child { border-top: 0; }
.erahist b { font-weight: 600; }
.erahist .cur { color: var(--accent); }
.approx { font-family: var(--ui); color: var(--ink-soft); }
.note-unc { font-size: 15px; color: var(--ink-soft); font-style: italic; margin-top: 10px; }

/* ── settings drawer ─────────────────────────────── */
#settings {
  position: fixed; z-index: 1100; top: 0; left: 0; bottom: 0; width: 360px; max-width: 100vw;
  background: var(--paper); box-shadow: var(--shadow); overflow-y: auto; border-right: 1px solid var(--line);
}
.sethead { display: flex; align-items: center; justify-content: space-between; padding: 16px 12px 8px 22px; }
.sethead h2 { font: 400 28px/1 var(--fell); margin: 0; }
#settings section { padding: 10px 22px 14px; border-bottom: 1px solid var(--line); }
#settings h3 { font: 400 15px/1 var(--fellsc); letter-spacing: .06em; color: var(--ink-soft); margin: 4px 0 10px; }
.seg { display: flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button {
  flex: 1 1 auto; border: 0; background: transparent; padding: 8px 10px; cursor: pointer; font-size: 16px;
  border-left: 1px solid var(--line);
}
.seg button:first-child { border-left: 0; }
.seg button[aria-checked="true"] { background: var(--ink); color: var(--paper); }
.seg button:disabled { opacity: .4; cursor: not-allowed; }
.hint { font-size: 14px; color: var(--ink-soft); margin: 8px 0 0; line-height: 1.35; }
.tog { display: flex; align-items: center; gap: 10px; font-size: 17px; padding: 3px 0; cursor: pointer; }
.tog input { width: 18px; height: 18px; accent-color: var(--accent); }
#legend { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; margin-top: 10px; font-size: 15px; }
#legend span { display: flex; align-items: center; gap: 6px; }
#legend i { width: 14px; height: 14px; border-radius: 3px; display: inline-block; border: 1px solid rgba(0,0,0,.25); }
#unlocated details { padding: 4px 0; font-size: 16px; }
#unlocated summary { cursor: pointer; font-weight: 500; }
#unlocated p { margin: 4px 0 6px 14px; color: var(--ink-soft); line-height: 1.4; }
#settings footer { padding: 16px 22px 28px; font-size: 14px; line-height: 1.45; color: var(--ink-soft); }

/* ── map labels & markers ───────────────────────── */
.rlabel {
  font-family: var(--fellsc); color: #3b2a1c; text-align: center; white-space: nowrap; pointer-events: none;
  letter-spacing: .14em; line-height: 1.05;
  text-shadow: 0 0 3px #f1e5c8, 0 0 6px #f1e5c8, 0 0 10px #f1e5c8;
  transform: translate(-50%, -50%); position: absolute;
}
.rlabel small { display: block; font-family: var(--fell); font-style: italic; letter-spacing: .02em; opacity: .85; }
.plabel {
  position: absolute; white-space: nowrap; font: 600 14px/1.1 var(--ui); color: #2b1d12; pointer-events: auto; cursor: pointer;
  text-shadow: 0 0 2px #f4ead3, 0 0 4px #f4ead3, 0 0 7px #f4ead3;
}
.plabel.nature { font-family: var(--fell); font-style: italic; font-weight: 400; color: #4f3b28; font-size: 15px; }
.plabel.sea { font-family: var(--fell); font-style: italic; font-weight: 400; color: #6d5a43; font-size: 20px; letter-spacing: .3em; }
.plabel.capital { font-size: 15px; }
.pmark { cursor: pointer; }
.pmark svg { display: block; filter: drop-shadow(0 1px 1.5px rgba(40, 25, 10, .45)); transition: transform .12s; }
.pmark:hover svg, .pmark.sel svg { transform: scale(1.25); }
.leaflet-tooltip.rtip {
  background: var(--paper); color: var(--ink); border: 1px solid var(--line); border-radius: 8px;
  font: 500 15px/1.2 var(--ui); box-shadow: var(--shadow); padding: 4px 9px;
}
.leaflet-tooltip.rtip::before { display: none; }
.leaflet-control-zoom { border: 1px solid var(--line) !important; box-shadow: var(--shadow) !important; border-radius: 10px !important; overflow: hidden; }
.leaflet-control-zoom a { background: var(--paper) !important; color: var(--ink) !important; width: 40px !important; height: 40px !important; line-height: 40px !important; font: 400 22px/40px var(--ui) !important; }
.leaflet-control-attribution { background: rgba(244, 234, 211, .85) !important; font: 13px var(--ui); color: var(--ink-soft); }
.leaflet-control-attribution a { color: var(--ink); }

/* hide by zoom: JS sets data-z on #map */
.leaflet-marker-icon.zhide { display: none !important; }

@media (max-width: 720px) {
  #panel {
    top: auto; width: 100vw; max-height: 62vh; border-radius: 18px 18px 0 0; padding-top: 18px;
    border-right: 0; border-top: 1px solid var(--line);
  }
  #panel .close { top: 10px; }
  .p-hero h1 { font-size: 27px; }
  #settings { width: 100vw; }
  .leaflet-bottom.leaflet-right .leaflet-control-zoom { display: none; }
}
