:root {
  --ink: #0d1a26;
  --navy: #13263a;
  --navy-2: #1b3550;
  --gold: #c9a45c;
  --gold-hi: #f0d79a;
  --text: #efe6d2;
  --muted: #a9b4bf;
  --green: #0b6b35;
  --serif: "Cinzel", "Trajan Pro", Georgia, serif;
  --sans: "Alegreya Sans", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #0b2238; color: var(--text); font-family: var(--sans); }
canvas#map { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; cursor: grab; touch-action: none; }
canvas#map.dragging { cursor: grabbing; }
button { font-family: var(--sans); }

/* ---- top bar ---- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 40px; z-index: 15;
  display: flex; align-items: center; gap: 14px; padding: 0 10px;
  background: linear-gradient(#1c3650, #10233a); border-bottom: 1px solid var(--gold);
  box-shadow: 0 2px 10px rgba(0,0,0,.5);
}
.nation { display: flex; align-items: center; gap: 8px; }
.emblem { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--green); border: 1.5px solid var(--gold); font-size: 14px; color: #fff; }
.nation-name { font-family: var(--serif); font-weight: 700; letter-spacing: .14em; text-transform: uppercase; font-size: 17px; color: var(--text); }
.stats { display: flex; gap: 14px; align-items: center; white-space: nowrap; overflow: hidden; }
.stat { display: flex; align-items: center; gap: 4px; font-weight: 700; font-size: 15px; cursor: default; }
.stat svg { width: 15px; height: 15px; }
.stat .sv { display: flex; flex-direction: column; line-height: 1.05; }
.stat .sv small { font-size: 9.5px; font-weight: 500; color: var(--muted); letter-spacing: .02em; }
#legend .lg-title { cursor: pointer; }
#legend .lg-title::after { content: " ▾"; color: var(--muted); }
#legend.collapsed .lg-title::after { content: " ▸"; }
#legend.collapsed > :not(.lg-title) { display: none; }
:focus-visible { outline: 2px solid var(--gold-hi); outline-offset: 2px; }
.search:focus-within { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(240,215,154,.35); }
.spacer { flex: 1; }
.search { position: relative; display: flex; align-items: center; gap: 6px; background: rgba(0,0,0,.3); border: 1px solid #47607a; border-radius: 14px; padding: 3px 8px; color: var(--muted); }
.search input { background: none; border: 0; outline: 0; color: var(--text); font: 15px var(--sans); width: 210px; }
.search kbd { font: 11px var(--sans); border: 1px solid #5c7187; border-radius: 3px; padding: 0 4px; }
#results { position: absolute; top: 30px; left: 0; right: 0; list-style: none; margin: 0; padding: 4px; background: var(--navy); border: 1px solid var(--gold); border-radius: 6px; max-height: 340px; overflow: auto; z-index: 30; }
#results li { padding: 5px 8px; border-radius: 4px; cursor: pointer; display: flex; justify-content: space-between; gap: 8px; color: var(--text); }
#results li small { color: var(--muted); }
#results li.active, #results li:hover { background: var(--navy-2); }
.tb-btn { background: linear-gradient(#2a4763, #1a3049); color: var(--text); border: 1px solid var(--gold); border-radius: 4px; padding: 4px 10px; font-size: 14px; cursor: pointer; }
.tb-btn.icon { width: 30px; padding: 4px 0; }
.tb-btn:hover { border-color: var(--gold-hi); color: #fff; }

/* ---- lens bar ---- */
#lensbar { position: fixed; right: 14px; bottom: 14px; z-index: 10; display: flex; gap: 6px; align-items: center; padding: 6px; background: rgba(16,35,58,.92); border: 1px solid var(--gold); border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,.5); flex-wrap: wrap; }
#lensbar button { background: #1d3550; color: var(--text); border: 1px solid #3d5a78; border-radius: 4px; padding: 5px 11px; font-size: 14px; cursor: pointer; }
#lensbar button.on { background: linear-gradient(#5a4a22, #3b301a); border-color: var(--gold); color: var(--gold-hi); }
#lensbar .sep { width: 1px; height: 22px; background: #3d5a78; margin: 0 2px; }

#lensbar select { background: #1d3550; color: var(--gold-hi); border: 1px solid var(--gold); border-radius: 4px; padding: 4px 6px; font: 14px var(--sans); max-width: 230px; }
#legend { position: fixed; left: 14px; bottom: 260px; z-index: 10; width: 228px; padding: 9px 11px; background: rgba(16,35,58,.94); border: 1px solid var(--gold); border-radius: 6px; font-size: 12px; }
#legend .lg-title { font-weight: 700; font-size: 13px; margin-bottom: 6px; }
#legend .lg-bar { height: 10px; border-radius: 3px; }
#legend .lg-ticks { display: flex; justify-content: space-between; color: var(--muted); margin-top: 3px; }
#lensbar .seg { display: inline-flex; border: 1px solid var(--gold); border-radius: 4px; overflow: hidden; }
#lensbar .seg button { border: 0; border-radius: 0; padding: 5px 8px; }
#legend .lg-cats { display: flex; flex-wrap: wrap; gap: 3px 10px; margin: 2px 0 4px; }
#legend .lg-cats i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
#legend .lg-cats small { color: var(--muted); }
#legend .lg-note { color: var(--muted); margin-top: 5px; line-height: 1.3; }
.mrow { display: grid; grid-template-columns: 130px 1fr 52px; align-items: center; gap: 8px; font-size: 13px; margin: 3px 0; }
.mrow span { color: var(--muted); }
.mrow b { text-align: right; font-variant-numeric: tabular-nums; }
.mbar { display: block; height: 8px; background: #1a3049; border-radius: 4px; overflow: hidden; }
.mbar i { display: block; height: 100%; border-radius: 4px; }
.p-body a, .p-head a { color: var(--gold-hi); cursor: pointer; text-decoration: none; }
.p-body a:hover, .p-head a:hover { text-decoration: underline; }
details.loc { margin: 12px 0 6px; font-size: 13px; }
details.loc summary { cursor: pointer; color: var(--gold); }
details.loc ul { margin: 6px 0 0; padding-left: 18px; color: var(--muted); max-height: 160px; overflow: auto; }

#attrib { position: fixed; left: 250px; bottom: 4px; z-index: 9; font-size: 11px; color: #c8d0d8; background: rgba(10,22,36,.72); padding: 2px 7px; border-radius: 4px; max-width: calc(100vw - 260px - var(--lensbar-w, 560px)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#attrib a { color: var(--gold-hi); }

/* ---- minimap ---- */
#minimap-wrap { position: fixed; left: 14px; bottom: 14px; z-index: 10; padding: 4px; background: var(--navy); border: 1px solid var(--gold); border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,.5); }
#minimap { display: block; width: 220px; cursor: crosshair; }

/* ---- side panel ---- */
#panel { position: fixed; top: 52px; right: 14px; width: 340px; max-height: calc(100vh - 130px); overflow: auto; z-index: 12; background: rgba(15,32,52,.97); border: 1px solid var(--gold); border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.6); }
.p-head { padding: 14px 16px 12px; position: relative; border-bottom: 1px solid #36506b; }
.p-head .band { position: absolute; inset: 0 0 auto 0; height: 4px; border-radius: 8px 8px 0 0; }
.p-head h2 { margin: 4px 0 0; font-family: var(--serif); font-size: 22px; letter-spacing: .08em; }
.p-head .ur { font-family: "Noto Nastaliq Urdu", serif; font-size: 18px; color: var(--gold-hi); float: right; line-height: 1.6; margin: 6px 22px 0 0; }
.p-head .sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.p-head .close { position: absolute; top: 8px; right: 10px; background: none; border: 0; color: var(--muted); font-size: 16px; cursor: pointer; }
.p-body { padding: 12px 16px 16px; font-size: 15px; line-height: 1.4; }
.p-body p { margin: 0 0 10px; }
.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; margin: 8px 0 12px; }
.kv div { display: flex; flex-direction: column; }
.kv b { font-size: 17px; }
.kv span { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; }
.yields { display: flex; gap: 6px; margin: 6px 0 12px; }
.yield { flex: 1; text-align: center; padding: 6px 2px; border-radius: 5px; background: #1a3049; border: 1px solid #33506d; }
.yield svg { width: 16px; height: 16px; display: block; margin: 0 auto 2px; }
.yield b { font-size: 16px; }
.yield small { display: block; color: var(--muted); font-size: 11px; }
h3.sec { font-family: var(--serif); font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--gold); margin: 14px 0 6px; border-bottom: 1px solid #36506b; padding-bottom: 3px; }
.bar { display: flex; height: 12px; border-radius: 3px; overflow: hidden; margin: 4px 0; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { font: inherit; font-size: 13px; background: #1d3550; border: 1px solid #3d5a78; border-radius: 12px; padding: 2px 9px; font-size: 13px; cursor: pointer; color: var(--text); }
.chip:hover { border-color: var(--gold); }
.note { color: var(--muted); font-size: 12px; }
ol.rank { margin: 0; padding-left: 22px; }
ol.rank li { cursor: pointer; padding: 1px 0; }
ol.rank li:hover { color: var(--gold-hi); }
ol.rank small { color: var(--muted); }

/* ---- tooltip ---- */
#tooltip { position: fixed; z-index: 20; pointer-events: none; background: rgba(12,26,42,.95); border: 1px solid var(--gold); border-radius: 5px; padding: 6px 9px; font-size: 13px; line-height: 1.35; max-width: 260px; box-shadow: 0 3px 10px rgba(0,0,0,.5); }
#tooltip b { font-size: 14px; }
#tooltip .m { color: var(--muted); }

/* ---- modal ---- */
#modal { position: fixed; inset: 0; z-index: 40; background: rgba(3,10,18,.65); display: grid; place-items: center; }
#modal[hidden] { display: none; }
.modal-card { position: relative; width: min(760px, 92vw); max-height: 86vh; overflow: auto; background: var(--navy); border: 1px solid var(--gold); border-radius: 10px; padding: 22px 26px; box-shadow: 0 10px 40px rgba(0,0,0,.7); }
.modal-card .close { position: absolute; top: 10px; right: 12px; background: none; border: 0; color: var(--muted); font-size: 18px; cursor: pointer; }
.modal-body h2 { font-family: var(--serif); letter-spacing: .1em; margin: 0 0 8px; }
.modal-body h3 { font-family: var(--serif); font-size: 14px; letter-spacing: .1em; text-transform: uppercase; color: var(--gold); margin: 18px 0 6px; }
.modal-body table { width: 100%; border-collapse: collapse; font-size: 14px; }
.modal-body th, .modal-body td { text-align: left; padding: 4px 6px; border-bottom: 1px solid #2c4560; }
.modal-body td.n, .modal-body th.n { text-align: right; font-variant-numeric: tabular-nums; }
.modal-body a { color: var(--gold-hi); }
.modal-body kbd { border: 1px solid #5c7187; border-radius: 3px; padding: 0 5px; font-size: 12px; }

#toast { position: fixed; left: 50%; top: 56px; transform: translateX(-50%); z-index: 50; background: var(--navy-2); border: 1px solid var(--gold); padding: 6px 14px; border-radius: 6px; }
#loading { position: fixed; inset: 0; display: grid; place-items: center; font-family: var(--serif); letter-spacing: .15em; font-size: 20px; color: var(--gold-hi); background: #0b2238; z-index: 100; transition: opacity .5s; }
#loading.done { opacity: 0; pointer-events: none; }

@media (max-width: 820px) {
  #topbar { gap: 8px; padding: 0 6px; }
  .nation-name { display: none; }
  .stats { gap: 9px; flex: 0 0 auto; }
  .stat { font-size: 13px; }
  .stats .stat:nth-child(n+2) { display: none; }
  .spacer { display: none; }
  .search { flex: 1 1 auto; min-width: 0; }
  .search input { width: 100%; min-width: 40px; }
  .search kbd { display: none; }
  #btn-share { padding: 4px 7px; font-size: 13px; }
  .stat .sv small { display: none; }
  #btn-almanac { padding: 4px 7px; font-size: 13px; }
  #minimap-wrap { display: none; }
  #legend { top: 48px; bottom: auto; left: 8px; width: 210px; padding: 6px 9px; }
  #attrib { left: 8px; right: 8px; max-width: none; bottom: calc(var(--lensbar-h, 56px) + 10px); text-align: center; font-size: 10px; }
  #legend .lg-note { display: none; }
  #panel { left: 8px; right: 8px; width: auto; top: auto; bottom: calc(var(--lensbar-h, 56px) + 16px); max-height: min(55vh, calc(100vh - var(--lensbar-h, 56px) - 80px)); }
  #lensbar { left: 8px; right: 8px; bottom: 8px; justify-content: center; }
  #lensbar button { padding: 4px 7px; font-size: 13px; }
}

#lensbar [hidden], #legend[hidden] { display: none !important; }

.note.warn { color: #f0c27a; }
