/* Mobile-first, calm + modern, soft rounded rectangles */
:root{
  --bg: #f5f5f2;
  --panel: rgba(255,255,255,0.92);
  --panel-solid: #ffffff;
  --text: #1b1b1b;
  --muted: #6b6b6b;
  --hair: rgba(0,0,0,0.18);
  --hair2: rgba(0,0,0,0.10);
  --shadow: 0 10px 30px rgba(0,0,0,0.10);
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 44px;
  --topbarH: 64px;
}

*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow:hidden;
}

.sr-only{
  position:absolute; width:1px; height:1px; padding:0; margin:-1px;
  overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; border:0;
}

.hidden{ display:none !important; }

/* Top bar */
.topbar{
  position:fixed;
  top:0; left:0; right:0;
  height: var(--topbarH);
  z-index:60;
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding:12px 12px 10px;
  gap:10px;
  background: linear-gradient(to bottom, rgba(245,245,242,0.96), rgba(245,245,242,0.78));
  border-bottom: 1px solid rgba(0,0,0,0.06);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.titleblock h1{
  margin:0;
  font-size:18px;
  line-height:1.15;
  letter-spacing:0.2px;
}
.subtitle{
  margin:4px 0 0;
  font-size:12.5px;
  color: var(--muted);
}

.topbar-actions{
  display:flex; gap:8px; align-items:center;
}

.iconbtn{
  height: var(--tap);
  min-width: var(--tap);
  padding:0 12px;
  border-radius: 999px;
  border: 1px solid var(--hair2);
  background: rgba(255,255,255,0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--text);
  box-shadow: 0 4px 16px rgba(0,0,0,0.06);
}
.iconbtn.small{ height: 36px; min-width:36px; padding:0 10px; }
.iconbtn.subtle{ background: rgba(255,255,255,0.55); }
.iconbtn:active{ transform: translateY(1px); }

/* App layout */
.app{
  position:relative;
  height:100vh;
  padding-top: var(--topbarH);
}

.map-wrap{
  position:absolute;
  inset: var(--topbarH) 0 0 0;
  z-index:1;
}

#mapCanvas{
  position:absolute;
  inset:0;
  width:100%;
  height:100%;
  display:block;
  background: var(--bg);
  touch-action: none; /* let d3-zoom manage */
}

.attribution-pill{
  position:absolute;
  left:12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  padding:8px 10px;
  border-radius:999px;
  background: rgba(255,255,255,0.6);
  border: 1px solid var(--hair2);
  color: rgba(0,0,0,0.55);
  font-size:12px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 5;
}

/* Loading + error */
.loading{
  position:absolute;
  inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  gap:10px;
  z-index:10;
  background: linear-gradient(to bottom, rgba(245,245,242,0.92), rgba(245,245,242,0.75));
}
.spinner{
  width:18px; height:18px;
  border-radius:50%;
  border:2px solid rgba(0,0,0,0.12);
  border-top-color: rgba(0,0,0,0.35);
  animation: spin 0.9s linear infinite;
}
@keyframes spin{ to{ transform: rotate(360deg);} }
.loading-text{ color: var(--muted); font-size: 13px; }

.error{
  position:absolute;
  top:12px;
  left:12px;
  right:12px;
  padding:12px 12px;
  border-radius: var(--radius);
  background: rgba(255,230,230,0.95);
  border: 1px solid rgba(140,0,0,0.25);
  box-shadow: var(--shadow);
  z-index: 80;
  font-size: 13px;
}

/* Scrim (behind panels/modals) */
.scrim{
  position:fixed;
  inset:0;
  z-index:35;
  background: rgba(0,0,0,0.12);
}

/* Panels */
.panel{
  position:fixed;
  top: calc(var(--topbarH) + 10px);
  right: 12px;
  width: min(320px, calc(100vw - 24px));
  max-height: calc(100vh - 180px);
  background: var(--panel);
  border: 1px solid var(--hair2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index:40;
  overflow:hidden;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.panel-header{
  display:flex; align-items:center; justify-content:space-between;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--hair2);
}
.panel-title{ font-weight:600; }
.panel-body{
  padding: 12px;
  overflow:auto;
}
.field{ display:block; margin-bottom: 12px; }
.field > span{ display:block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input, .field select{
  width:100%;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--hair2);
  padding: 0 12px;
  font-size: 14px;
  outline:none;
  background: rgba(255,255,255,0.85);
}
.field select:disabled, .field input:disabled{ opacity:0.55; }
.help{ margin-top:6px; font-size:12px; color: var(--muted); }
.primary{
  width:100%;
  height: 44px;
  border-radius: 12px;
  border: 1px solid var(--hair2);
  background: rgba(0,0,0,0.06);
  font-weight:600;
}

/* Legend */
.legend{
  position:fixed;
  top: calc(var(--topbarH) + 10px);
  left: 12px;
  width: min(320px, calc(100vw - 24px));
  background: var(--panel);
  border: 1px solid var(--hair2);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index:40;
  overflow:hidden;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.legend-header{
  display:flex; align-items:center; justify-content:flex-end;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--hair2);
}
.legend-body{ padding: 12px; }
.legend-row{
  display:flex; align-items:center; gap:10px;
  padding: 7px 0;
}
.swatch{
  width: 16px; height: 16px;
  border-radius: 6px;
  border: 1px solid var(--hair);
}
.swatch.hatch{
  background:
    repeating-linear-gradient(135deg, rgba(0,0,0,0.10) 0 2px, rgba(0,0,0,0.00) 2px 6px),
    #e6e6e2;
}
.label{ font-size: 13px; color: var(--text); }

/* Bottom sheet */
.sheet{
  position:fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index:45;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--hair2);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow:hidden;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  touch-action: none;
  max-height: 100vh;
  transform: translateY(105%);
  transition: transform 220ms ease;
}
.sheet-handle{
  width: 54px; height: 5px;
  border-radius: 999px;
  background: rgba(0,0,0,0.18);
  margin: 10px auto 6px;
}
.sheet-inner{
  padding: 10px 14px 16px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  max-height: none; /* JS sets pixel maxHeight */
  display: flex;
  flex-direction: column;
}

.sheet-title{
  font-size: 18px;
  font-weight: 700;
  margin: 2px 0 2px;
}
.sheet-country{
  font-size: 16px;
  font-weight: 650;
  margin: 0 0 10px;
  color: rgba(0,0,0,0.75);
}
.sheet-desc{
  font-size: 16px;
  line-height: 1.4;
  color: rgba(0,0,0,0.85);
  margin-bottom: 10px;
}
.sheet-tags{ display:flex; flex-wrap:wrap; gap:8px; margin-bottom: 10px;}
.tag{
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--hair2);
  background: rgba(0,0,0,0.03);
}
.sheet-sources{
  font-size: 12px;
  color: rgba(0,0,0,0.55);
  border-top: 1px solid var(--hair2);
  padding-top: 10px;
}
.sheet-sources a{ color: rgba(0,0,0,0.62); text-decoration: underline; text-underline-offset: 2px; }

.source-row{ margin: 4px 0; }
.source-name{ color: rgba(0,0,0,0.45); }
.source-link{ word-break: break-all; }

/* Sheet snap states (used by JS) */
.sheet.collapsed{ transform: translateY(105%); }
.sheet.mid{ transform: translateY(45%); }
.sheet.full{ transform: translateY(0); }

/* Modal */
.modal{
  position:fixed;
  inset:0;
  z-index:55;
  display:flex;
  align-items:flex-start;
  justify-content:center;
  padding: calc(12px + env(safe-area-inset-top)) 12px 12px;
}
.modal-card{
  width: min(520px, calc(100vw - 24px));
  background: var(--panel-solid);
  border-radius: var(--radius);
  border: 1px solid var(--hair2);
  box-shadow: var(--shadow);
  overflow:hidden;
}
.modal-header{
  display:flex;
  justify-content:flex-end;
  align-items:center;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--hair2);
}
.modal-body{
  padding: 12px;
  color: rgba(0,0,0,0.80);
  font-size: 14px;
  line-height: 1.45;
}
.modal-body a{ color: rgba(0,0,0,0.80); text-decoration: underline; text-underline-offset: 2px; }
.attrib-note{ color: rgba(0,0,0,0.70); margin-top: 12px; }

/* Desktop tweaks */
@media (min-width: 860px){
  .panel{ right: 16px; }
  .legend{ left: 16px; }
  .sheet{
    left: 16px;
    right: auto;
    width: 420px;
    bottom: 16px;
  }
  .map-wrap{ inset: var(--topbarH) 0 0 0; }
}
