:root {
  --bg: #12181f;
  --panel-bg: #1b232d;
  --panel-bg-alt: #232d38;
  --border: #2f3b47;
  --text: #e9edf1;
  --text-dim: #a9b6c2;
  --accent: #3d8f5b;
  --accent-2: #1f5f8b;
  --danger: #b9563f;
  --warn: #c9a15a;
  --radius: 10px;
  --header-h: 48px;
  --timeline-h: 90px;
  --font-heading: 'Cinzel', serif;
  --font-body: 'Lora', Georgia, 'Times New Roman', serif;
}

* { box-sizing: border-box; }

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

body {
  display: flex;
  flex-direction: column;
}

/* ---------------- Header ---------------- */
#app-header {
  min-height: var(--header-h);
  height: auto;
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

#app-title {
  font-family: var(--font-heading);
  font-size: 15px;
  margin: 0;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#mode-tabs {
  order: 3;
  flex-basis: 100%;
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 6px;
}

.mode-tab {
  font-family: var(--font-heading);
  border: 1px solid var(--border);
  background: var(--panel-bg-alt);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 13px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: normal;
  width: 100%;
  text-align: center;
}

.mode-tab.active {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #fff;
}

#panel-toggle-btn {
  font-family: var(--font-body);
  background: var(--accent-2);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  flex: 0 0 auto;
}

@media (min-width: 640px) {
  /* Equal 1fr columns on both sides of the nav center it on the full header
     width regardless of how wide the title/toggle-button siblings are —
     centering it inside a single flex:1 slot next to the title would instead
     bias it toward whichever side has less content. */
  #app-header { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; }
  #app-title { grid-column: 1; justify-self: start; }
  #mode-tabs { grid-column: 2; order: initial; flex-basis: auto; flex-direction: row; justify-content: center; width: auto; }
  #panel-toggle-btn { grid-column: 3; justify-self: end; }
  .mode-tab { width: auto; white-space: nowrap; }
}

/* ---------------- Layout ---------------- */
#main-layout {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

#map {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  background: #0c1116;
  position: relative;
  z-index: 0; /* isolate Leaflet's internal panes (z-index 200-700) below the app UI overlays */
}

/* Smooth, fluid transitions for markers/territories instead of hard pop in/out.
   Leaflet's SVG renderer draws plain <path>/<circle> elements — a CSS transition
   on opacity/fill-opacity animates any change made via layer.setStyle(). */
.leaflet-interactive {
  transition: opacity 380ms ease, fill-opacity 380ms ease, stroke-opacity 380ms ease;
}

/* ---------------- Floating event card (top-left over the map) ---------------- */
#event-card {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 15;
  max-width: 340px;
  background: rgba(27, 35, 45, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.3);
  pointer-events: auto;
}

#event-card h2 {
  font-family: var(--font-heading);
  margin: 0 0 4px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
}

#event-card p {
  margin: 0 0 6px;
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.35;
}

#event-card a {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

#event-card a:hover { text-decoration: underline; }

/* Permanent on-map territory name labels */
.territory-label {
  font-family: var(--font-heading);
  background: rgba(18,24,31,0.68);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 4px;
  white-space: nowrap;
  box-shadow: none;
  transition: opacity 380ms ease;
}
.territory-label::before { display: none; }

/* Map markers: category icon inside a circle colored/bordered by certainty
   (replaces the old plain circleMarker dot) */
.map-marker-wrap {
  background: transparent;
  border: none;
}

.map-marker-icon {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  line-height: 1;
  background: var(--accent);
  border: 2px solid #0c1116;
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  transition: opacity 380ms ease;
}

.map-marker-icon.cert-probable,
.map-marker-icon.cert-disputada {
  background: var(--warn);
  border-style: dashed;
}
.map-marker-icon.cert-disputada { background: var(--danger); }
.map-marker-icon.cert-desconocida {
  background: var(--text-dim);
  border-style: dotted;
}

.leaflet-marker-icon { transition: opacity 380ms ease; }

/* ---------------- Side / bottom panel ---------------- */
#side-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 30%;
  background: var(--panel-bg);
  border-top: 1px solid var(--border);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -6px 20px rgba(0,0,0,0.35);
  z-index: 30;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.25s ease;
  padding-bottom: var(--timeline-h);
}

#side-panel.open {
  transform: translateY(0);
}

#panel-drag-handle {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  padding: 8px 0 4px;
  cursor: grab;
}

#panel-drag-handle span {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  display: block;
}

#panel-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0 14px 14px;
}

#panel-body h2 {
  font-family: var(--font-heading);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin: 16px 0 8px;
}

#panel-body h2:first-child { margin-top: 4px; }

#search-input,
#book-search-input {
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel-bg-alt);
  color: var(--text);
  font-size: 14px;
}

#category-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--panel-bg-alt);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.chip.active {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #fff;
}

.loc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.loc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 13.5px;
}

.loc-item:hover, .loc-item:focus {
  background: var(--panel-bg-alt);
}

.loc-item .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: var(--accent);
}

.loc-item.certainty-probable .dot,
.loc-item.certainty-disputada .dot {
  background: var(--warn);
  border: 2px dashed var(--danger);
  width: 7px;
  height: 7px;
}

.loc-item.certainty-desconocida .dot {
  background: transparent;
  border: 2px dotted var(--text-dim);
  width: 7px;
  height: 7px;
}

.loc-item .refs {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
}

.loc-item.no-coords {
  cursor: default;
  opacity: 0.75;
}

.uncertain-note {
  font-size: 12px;
  color: var(--text-dim);
  padding: 0 4px 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 2px;
}

.uncertain-note strong {
  color: var(--text);
  display: block;
  margin-bottom: 2px;
  font-size: 13px;
}

.badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 6px;
}

.badge.probable { background: rgba(201,161,90,0.2); color: var(--warn); }
.badge.disputada { background: rgba(185,86,63,0.2); color: var(--danger); }
.badge.desconocida { background: rgba(169,182,194,0.15); color: var(--text-dim); }

#legend-list {
  font-size: 13px;
}

.legend-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 5px 0;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex: 0 0 auto;
  margin-top: 2px;
}

.legend-row strong {
  font-family: var(--font-heading);
}

.legend-row .legend-note {
  font-size: 11.5px;
  color: var(--text-dim);
  display: block;
  margin-top: 2px;
}

/* ---------------- Reference-mode (book search) panel ---------------- */
.book-testament-header {
  font-family: var(--font-heading);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  padding: 10px 4px 4px;
  border-bottom: none;
  cursor: default;
}

.book-testament-header:hover { background: none; }

.loc-item .book-place {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  text-align: right;
}

#book-clear-btn {
  font-family: var(--font-body);
  background: var(--panel-bg-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 11px;
  font-size: 12.5px;
  cursor: pointer;
  margin-bottom: 6px;
}

#book-detail-title {
  font-family: var(--font-heading);
  font-size: 17px;
  margin: 0 0 4px;
}

#book-detail-meta {
  font-size: 12.5px;
  color: var(--text-dim);
  margin: 0 0 10px;
}

/* ---------------- Timeline bar (fixed bottom) ---------------- */
#timeline-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--timeline-h);
  background: rgba(18, 24, 31, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid rgba(255,255,255,0.08);
  z-index: 40;
  padding: 8px 12px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#era-info,
#timeline-controls {
  width: 100%;
  max-width: 900px;
}

#era-info {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  background: none;
}

#era-label {
  font-family: var(--font-heading);
  font-size: 14px;
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
}

#year-label {
  font-size: 13px;
  color: var(--accent);
  font-weight: 700;
  white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
}

#timeline-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

#play-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#year-range {
  flex: 1 1 auto;
  width: 100%;
  accent-color: var(--accent);
}

#speed-select {
  flex: 0 0 auto;
  background: var(--panel-bg-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 11px;
  padding: 4px;
}

/* ---------------- Leaflet overrides ---------------- */
.leaflet-popup-content-wrapper {
  background: var(--panel-bg);
  color: var(--text);
  border-radius: var(--radius);
}

.leaflet-popup-tip { background: var(--panel-bg); }

.leaflet-popup-content h3 {
  font-family: var(--font-heading);
  margin: 0 0 4px;
  font-size: 14px;
}

.leaflet-popup-content .pop-refs {
  font-size: 12px;
  color: var(--text-dim);
  margin: 2px 0;
}

.leaflet-popup-content .pop-refs a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

.leaflet-popup-content .pop-refs a:hover {
  text-decoration: underline;
}

.leaflet-popup-content .pop-note {
  font-size: 11.5px;
  color: var(--warn);
  margin-top: 6px;
  border-top: 1px dashed var(--border);
  padding-top: 6px;
}

.territory-tooltip {
  background: var(--panel-bg);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 12px;
  max-width: 220px;
}

/* Backdrop for open panel on mobile */
#panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 25;
  display: none;
}

#panel-backdrop.show { display: block; }

/* ---------------- Tablet / Desktop layout ---------------- */
@media (min-width: 768px) {
  #app-header .subtitle { display: inline; margin-left: 8px; }

  #main-layout { flex-direction: row; }

  #side-panel {
    position: static;
    top: auto;
    transform: none;
    width: 340px;
    flex: 0 0 340px;
    border-radius: 0;
    border-top: none;
    border-right: 1px solid var(--border);
    box-shadow: none;
    padding-bottom: 0;
    height: 100%;
  }

  #panel-drag-handle { display: none; }

  #panel-backdrop { display: none !important; }

  #panel-toggle-btn { display: none; }

  #timeline-bar {
    left: 0;
    right: 340px;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  #timeline-bar { right: 300px; }
  #side-panel { width: 300px; flex-basis: 300px; }
}
