:root {
  --bg-color: #0f1117;
  --panel-bg: rgba(25, 25, 35, 0.95);
  --text-main: #e4e4e7;
  --text-muted: #a1a1aa;
  --border-color: rgba(255, 255, 255, 0.15);
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
}

* { 
  box-sizing: border-box; 
  margin: 0; 
  padding: 0; 
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: "Segoe UI", system-ui, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-main);
}
@supports (height: 100dvh) {
  body, html { height: 100dvh; }
}

#app {
  position: relative;
  width: 100%; 
  height: 100%;
}

/* OpenSeadragon and Canvas Overlay */
#openseadragon1 {
  position: absolute; 
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 1;
}

#marker-canvas {
  position: absolute; 
  top: 0; left: 0;
  width: 100%; height: 100%;
  /* Crucial: lets drag/scroll events pass through to OpenSeadragon */
  pointer-events: none; 
  z-index: 2;
}

/* Top Controls */
#top-bar {
  position: absolute; 
  top: 10px; left: 10px; right: 10px;
  display: flex; 
  justify-content: space-between;
  z-index: 10; 
  pointer-events: none; /* Let un-clickable gaps pass through */
}

#left-controls, #right-controls, #search-container {
  pointer-events: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

#bottom-left-controls {
  position: absolute;
  bottom: 40px;
  left: 10px;
  z-index: 10;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}
.toggle-row { display: flex; gap: 6px; }

#bottom-left-controls .type-filter-group {
  flex-wrap: wrap;
  max-width: 340px;
  gap: 4px;
}

/* Segment button group */
.segment-btn-group {
  display: flex;
  gap: 2px;
}
.seg-btn {
  background: var(--panel-bg);
  color: var(--text-muted);
  border: 1px solid var(--border-color);
  border-radius: 5px;
  padding: 6px 9px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  line-height: 1;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.seg-btn:hover {
  background: rgba(50,50,60,0.95);
  color: var(--text-main);
}
.seg-btn.active {
  background: rgba(59,130,246,0.2);
  color: var(--accent);
  border-color: var(--accent);
}

/* Buttons */
.ctrl-btn {
  background: var(--panel-bg); 
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: 6px; 
  padding: 8px;
  cursor: pointer; 
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.ctrl-btn:hover { background: rgba(50, 50, 60, 0.95); }
.toggle-btn { padding: 8px 12px; font-weight: 600; font-size: 13px; }
.toggle-btn-sm { padding: 6px 8px; font-size: 11px; }
.toggle-btn.active { border-color: var(--accent); color: var(--accent); }

/* Search */
#search-input {
  background: var(--panel-bg); 
  color: var(--text-main);
  border: 1px solid var(--border-color); 
  border-radius: 6px;
  padding: 8px 12px; width: 250px; font-size: 14px; outline: none;
}
#search-input:focus { border-color: var(--accent); }

#search-results {
  position: absolute; 
  top: 40px; left: 0; right: 0;
  background: var(--panel-bg); 
  border: 1px solid var(--border-color);
  border-radius: 6px; 
  max-height: 300px; overflow-y: auto;
  display: flex; flex-direction: column; 
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
.search-item {
  padding: 10px; cursor: pointer; 
  border-bottom: 1px solid var(--border-color);
  display: flex; align-items: center; gap: 8px;
}
.search-item:hover { background: rgba(255,255,255,0.1); }
.si-latin { font-weight: 600; }
.si-modern { font-size: 12px; color: var(--text-muted); }

/* Info Panel */
#info-panel {
  position: absolute;
  right: 20px; top: 60px; width: 512px;
  background: var(--panel-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px; padding: 20px; z-index: 30;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  pointer-events: auto;
  overflow-y: auto;
  max-height: calc(100vh - 80px);
}
#close-panel {
  position: absolute; top: 12px; right: 12px;
  background: transparent; border: none; color: var(--text-muted);
  font-size: 24px; cursor: pointer; line-height: 1;
}
#close-panel:hover { color: #fff; }
#panel-latin { font-size: 28px; margin-bottom: 6px; }
#panel-modern { font-size: 22px; color: var(--text-muted); margin-bottom: 8px; font-weight: normal; }
#panel-country { font-size: 22px; margin-bottom: 14px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
#panel-country img { height: 1.2em; vertical-align: middle; border-radius: 2px; }
#panel-type { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; font-weight: 600; font-size: 26px; }
.type-icon  { font-size: 1.2em; line-height: 1; }
#panel-details dt { font-size: 14px; color: var(--text-muted); text-transform: uppercase; margin-top: 14px; }
#panel-details dd { font-size: 18px; margin-left: 0; margin-top: 4px; }
#panel-details dd.country-dd { font-size: 22px; margin-top: 6px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
#panel-header { display: block; margin-bottom: 0; }
#panel-map { margin-top: 18px; border-radius: 6px; overflow: hidden; line-height: 0; }
#panel-map iframe { display: block; width: 100%; height: 360px; border: 0; border-radius: 6px; pointer-events: auto; }
#panel-links { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.panel-link { font-size: 12px; color: var(--accent); text-decoration: none; }
.panel-link:hover { text-decoration: underline; }
#panel-sources { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border-color); font-size: 11px; color: var(--text-muted); line-height: 1.5; }
#panel-sources a { color: var(--accent); text-decoration: none; }
#panel-sources a:hover { text-decoration: underline; }
#mobile-menu-attribution { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border-color); font-size: 11px; color: var(--text-muted); text-align: center; line-height: 1.6; }
#mobile-menu-attribution a { color: var(--accent); text-decoration: none; }

/* Type filter buttons */
.type-filter-group { display: flex; gap: 3px; flex-wrap: wrap; }
.type-filter-btn {
  padding: 5px 9px; border-radius: 5px; border: 1px solid var(--border-color);
  background: var(--panel-bg); color: var(--text-muted);
  font-size: 11px; font-weight: 600; cursor: pointer; white-space: nowrap;
  display: flex; align-items: center; gap: 5px;
  transition: all 0.15s;
  line-height: 1;
  opacity: 0.6;
}
.type-filter-btn .tf-dot { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }
.type-filter-btn.active {
  opacity: 1;
  color: var(--text-main);
  border-color: rgba(255,255,255,0.45);
  background: rgba(40,40,55,0.98);
}
.type-filter-btn:hover { opacity: 0.9; background: rgba(40,40,55,0.95); color: var(--text-main); }

/* Tooltip */
#tooltip {
  position: absolute; background: rgba(0, 0, 0, 0.88); color: #fff;
  padding: 10px 16px; border-radius: 8px; pointer-events: none; z-index: 20;
  font-size: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.4); border: 1px solid rgba(255,255,255,0.12);
  max-width: 360px;
}
#tooltip.tt-rich { max-width: 360px; padding: 12px 16px; }
.tt-latin { font-weight: 600; font-size: 18px; margin-bottom: 3px; }
.tt-modern { color: #ddd; margin-bottom: 6px; font-size: 14px; font-weight: 500; }
.tt-type { display: flex; align-items: center; gap: 8px; font-size: 16px; color: #bbb; margin-bottom: 4px; margin-top: 6px; }
.tt-type-icon { font-size: 1.5em; line-height: 1; }
.tt-country { display: flex; align-items: center; gap: 6px; font-size: 14px; color: #fff; margin-bottom: 2px; }
.tt-country img { height: 1.2em; vertical-align: middle; border-radius: 2px; }
.tt-country-name { font-weight: 500; }
.tt-detail { font-size: 13px; color: #999; margin-top: 2px; }
.tt-detail span { color: #ccc; }

/* Legend, Status & Footer Elements */
#legend { position: absolute; bottom: 40px; left: 50%; transform: translateX(-50%); z-index: 10; }
#legend-toggle { background: var(--panel-bg); color: var(--text-main); border: 1px solid var(--border-color); border-radius: 6px; padding: 8px 12px; font-size: 13px; font-weight: 600; cursor: pointer; margin-left: auto; display: block; }
#legend-body { background: var(--panel-bg); border: 1px solid var(--border-color); border-radius: 6px; padding: 12px; margin-top: 8px; display: none; box-shadow: 0 4px 12px rgba(0,0,0,0.5); }
#legend.open #legend-body { display: block; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 8px; }
.legend-item:last-child { margin-bottom: 0; }
#status, #attribution { position: absolute; bottom: 10px; z-index: 10; font-size: 12px; color: var(--text-muted); background: rgba(15, 17, 23, 0.8); padding: 4px 8px; border-radius: 4px; pointer-events: none; }
#status { left: 10px; }
#attribution { right: 10px; pointer-events: auto; }
#attribution a { color: var(--accent); text-decoration: none; }
#attribution a:hover { text-decoration: underline; }

/* Utilities */
.hidden { display: none !important; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; border: 1px solid rgba(255,255,255,0.5); }

/* ── Hamburger + settings button wrapper ─────────────────────── */
#hamburger-wrap { display: flex; gap: 8px; }

/* ── Mobile menu overlay ─────────────────────────────────────── */
#mobile-menu-btn { display: none; }
#mobile-menu {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column; justify-content: flex-end;
}
#mobile-menu-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.55);
}
#mobile-menu-sheet {
  position: relative;
  background: var(--panel-bg);
  border-radius: 16px 16px 0 0;
  padding: 16px 16px 32px;
  max-height: 80svh;
  overflow-y: auto;
  border-top: 1px solid var(--border-color);
}
#mobile-menu-handle {
  width: 40px; height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  margin: 0 auto 16px;
}
#mobile-menu h3 {
  font-size: 11px; font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin: 14px 0 6px;
}
#mobile-menu h3:first-of-type { margin-top: 4px; }
#mobile-type-filter-buttons,
#mobile-segment-buttons { display: flex; flex-wrap: wrap; gap: 5px; }
#mobile-display-controls { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Mobile responsive overrides ────────────────────────────── */
@media (pointer: coarse), (max-width: 600px) {
  /* Show hamburger, hide desktop filter row + segment strip */
  #mobile-menu-btn { display: flex !important; }
  /* Settings button: shown in mobile menu, hidden from top bar */
  #settings-btn { display: none !important; }
  #segment-buttons { display: none; }
  /* Show only the two quick-access buttons; hide the full category row */
  #bottom-left-controls #type-filter-buttons { display: none; }
  #bottom-left-controls { bottom: 10px; flex-direction: row; gap: 6px; align-items: center; }
  #attribution { display: none; }

  /* Search fills remaining top-bar space */
  #search-container { flex: 1; min-width: 0; margin: 0 4px; }
  #search-input { width: 100%; min-width: 0; font-size: 16px; /* prevent iOS zoom on focus */ }

  /* Info panel → left-side panel */
  #info-panel {
    position: fixed !important;
    left: 0 !important; right: auto !important;
    top: 56px !important; bottom: 0 !important;
    width: min(280px, 55vw) !important;
    max-height: none !important;
    border-radius: 0 12px 12px 0 !important;
    padding: 12px 14px 24px;
    box-shadow: 4px 0 24px rgba(0,0,0,0.6);
    overflow-y: auto;
  }
  #info-panel::before { display: none; }

  /* Vertical stack for left panel */
  #panel-header { display: block !important; height: auto !important; }
  #panel-title  { flex: none; overflow: hidden; }
  #panel-map:not(.hidden) {
    display: block !important;
    margin: 10px 0 0;
    border-radius: 6px;
    overflow: hidden;
  }
  #panel-map:not(.hidden) iframe {
    width: 100%;
    height: 150px;
    display: block;
  }
  /* Slightly smaller fonts for narrow panel */
  #info-panel #panel-latin  { font-size: 14px; margin-bottom: 3px; }
  #info-panel #panel-modern { font-size: 12px; margin-bottom: 8px; }
  #info-panel #panel-type   { font-size: 16px; }

  /* Legend: move to right to avoid overlap with bottom-left buttons */
  #legend { bottom: 10px; left: auto; right: 10px; transform: none; }
  #legend-toggle { min-height: 38px; }

  /* Larger tap targets for controls */
  .ctrl-btn { min-width: 38px; min-height: 38px; }
}

/* ── Label settings panel ────────────────────────────────────── */
#settings-panel {
  position: absolute;
  right: 10px; top: 60px;
  width: 290px;
  background: var(--panel-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  z-index: 30;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  pointer-events: auto;
  overflow-y: auto;
  max-height: calc(100vh - 80px);
}
#settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px 10px;
  border-bottom: 1px solid var(--border-color);
  font-weight: 600; font-size: 13px;
}
#close-settings {
  background: transparent; border: none; color: var(--text-muted);
  font-size: 22px; cursor: pointer; line-height: 1; padding: 0;
}
#close-settings:hover { color: #fff; }
#settings-body { padding: 6px 14px 4px; }
.sp-section {
  font-size: 10px; font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .07em;
  margin: 10px 0 5px;
}
.sp-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 6px; margin-bottom: 5px;
}
.sp-label { font-size: 11px; color: var(--text-main); flex: 1; min-width: 0; }
.sp-right { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.sp-slider-wrap { display: flex; align-items: center; gap: 3px; }
.sp-slider { width: 72px; accent-color: var(--accent); cursor: pointer; }
.sp-step-btn {
  width: 20px; height: 20px; min-width: 20px; padding: 0;
  font-size: 15px; line-height: 1;
  background: var(--panel-bg); color: var(--text-main);
  border: 1px solid var(--border-color); border-radius: 4px;
  cursor: pointer; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.sp-step-btn:hover { background: rgba(50,50,60,0.95); }
.sp-step-btn:active { opacity: 0.6; }
.sp-val {
  font-size: 11px; color: var(--accent);
  min-width: 32px; text-align: right;
  font-variant-numeric: tabular-nums;
}
.sp-desc {
  font-size: 10px; color: var(--text-muted);
  line-height: 1.4; margin: -2px 0 6px;
  padding-left: 1px;
}
.sp-section-row {
  display: flex; align-items: center; justify-content: space-between;
  margin: 10px 0 5px;
}
.sp-section-row .sp-section { margin: 0; }
.sp-zoom-badge {
  font-size: 10px; font-weight: 700;
  color: var(--accent);
  background: rgba(59,130,246,0.12);
  border: 1px solid rgba(59,130,246,0.3);
  border-radius: 4px;
  padding: 1px 6px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}
.sp-curve-pt-label {
  font-size: 11px; font-weight: 600; color: var(--text-main);
  margin: 8px 0 2px;
}
.sp-subrow { padding-left: 10px; margin-bottom: 3px; }
#settings-footer {
  display: flex; gap: 8px; justify-content: flex-end;
  padding: 8px 14px 12px;
  border-top: 1px solid var(--border-color);
}
#settings-footer .ctrl-btn { padding: 6px 12px; font-size: 12px; font-weight: 600; }
#settings-save { border-color: var(--accent); color: var(--accent); }

@media (pointer: coarse), (max-width: 600px) {
  #settings-panel {
    position: fixed !important;
    left: 0 !important; right: 0 !important;
    bottom: 0 !important; top: auto !important;
    width: 100% !important;
    max-height: 75svh;
    border-radius: 16px 16px 0 0;
  }
  .sp-desc { display: none; }
}