:root {
  --color-bg: #0d1117;
  --color-surface: #161b22;
  --color-surface-hover: #1c2333;
  --color-border: #30363d;
  --color-text: #e6edf3;
  --color-text-muted: #8b949e;
  --color-accent: #58a6ff;
  --color-accent-glow: rgba(88, 166, 255, 0.4);
  --color-dynasty: #f0883e;
  --color-dynasty-active: #ffa657;
  --color-danger: #f85149;
  --color-success: #3fb950;
  --font-sans: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", "Cascadia Code", Consolas, monospace;
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --transition-fast: 150ms ease;
  --transition-normal: 300ms ease;
  --header-height: 34px;
  --dynasty-row-height: 18px;
  --dynasty-timeline-height: 30px;
  --calendar-timeline-height: 36px;
  --year-indicator-height: 20px;
  --event-list-height: 160px;
  --copyright-height: 24px;
  --timeline-area-height: calc(var(--dynasty-timeline-height) + var(--calendar-timeline-height) + var(--year-indicator-height) + var(--spacing-sm) * 2);
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 14px;
  line-height: 1.5;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-height);
  padding: 0 var(--spacing-md);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  z-index: 100;
}

#site-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--color-accent);
}

.icon-btn {
  background: none;
  border: 1px solid var(--color-border);
  color: var(--color-text);
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-fast);
}

.icon-btn:hover {
  background: var(--color-surface-hover);
}

#timeline-area {
  flex-shrink: 0;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

#dynasty-timeline-container {
  position: relative;
  height: var(--dynasty-timeline-height);
  cursor: pointer;
}

#dynasty-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#calendar-timeline-container {
  position: relative;
  height: var(--calendar-timeline-height);
  cursor: grab;
}

#calendar-timeline-container:active {
  cursor: grabbing;
}

#calendar-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#year-indicator {
  height: var(--year-indicator-height);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--color-text-muted);
  font-family: var(--font-mono);
  border-top: 1px solid var(--color-border);
}

#current-year-display {
  background: var(--color-accent);
  color: #fff;
  padding: 1px 8px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 12px;
}

.tooltip {
  position: absolute;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--spacing-sm) var(--spacing-md);
  font-size: 12px;
  pointer-events: none;
  box-shadow: var(--shadow-md);
  z-index: 200;
  white-space: nowrap;
  transition: opacity var(--transition-fast);
}

.tooltip.hidden {
  opacity: 0;
  visibility: hidden;
}

#map-and-events {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#map-area {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

#map-container {
  position: absolute;
  inset: 0;
}

#map {
  width: 100%;
  height: 100%;
}

#map-empty-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(22, 27, 34, 0.9);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--spacing-md) var(--spacing-lg);
  font-size: 14px;
  color: var(--color-text-muted);
  z-index: 500;
  pointer-events: none;
}

#map-empty-hint.hidden {
  display: none;
}

#map-layer-toggle {
  position: absolute;
  bottom: 12px;
  right: 12px;
  z-index: 500;
  display: flex;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-md);
}

.layer-btn {
  background: var(--color-surface);
  border: none;
  color: var(--color-text-muted);
  padding: 4px 10px;
  font-size: 11px;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.layer-btn:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

.layer-btn.active {
  background: var(--color-accent);
  color: #fff;
}

#event-detail-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 360px;
  max-width: 90vw;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  box-shadow: var(--shadow-lg);
  z-index: 600;
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-normal);
}

#event-detail-panel.hidden {
  transform: translateX(100%);
}

#event-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-md);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

#event-detail-title {
  font-size: 16px;
  font-weight: 600;
  flex: 1;
  margin-right: var(--spacing-sm);
}

#event-detail-body {
  padding: var(--spacing-md);
  overflow-y: auto;
  flex: 1;
}

#event-detail-year {
  font-size: 13px;
  color: var(--color-accent);
  margin-bottom: var(--spacing-xs);
}

#event-detail-category {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-hover);
  border: 1px solid var(--color-border);
  margin-bottom: var(--spacing-xs);
  margin-right: 6px;
}

#event-detail-region {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  background: rgba(240, 136, 62, 0.15);
  border: 1px solid rgba(240, 136, 62, 0.3);
  color: var(--color-dynasty);
  margin-bottom: var(--spacing-md);
}

#event-detail-description {
  font-size: 14px;
  line-height: 1.7;
  color: var(--color-text-muted);
}

#event-list-panel {
  flex-shrink: 0;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  max-height: var(--event-list-height);
  display: flex;
  flex-direction: column;
}

#event-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px var(--spacing-md);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

#event-list-year {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-accent);
}

#event-list-count {
  font-size: 11px;
  color: var(--color-text-muted);
}

#event-list-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--spacing-xs) var(--spacing-md);
}

.event-region-group {
  margin-bottom: var(--spacing-xs);
}

.event-region-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--color-dynasty);
  padding: 2px 0;
  border-bottom: 1px solid rgba(240, 136, 62, 0.2);
  margin-bottom: 2px;
}

.event-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  font-size: 12px;
  cursor: pointer;
  transition: color var(--transition-fast);
}

.event-item:hover {
  color: var(--color-accent);
}

.event-item-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--color-dynasty);
  flex-shrink: 0;
}

.event-item-cat {
  font-size: 10px;
  padding: 0 4px;
  border-radius: 2px;
  background: var(--color-surface-hover);
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.event-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#copyright {
  flex-shrink: 0;
  height: var(--copyright-height);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  font-size: 10px;
  color: var(--color-text-muted);
}

#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(13, 17, 23, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  gap: var(--spacing-md);
}

#loading-overlay.hidden {
  display: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#error-toast {
  position: fixed;
  bottom: var(--spacing-lg);
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-danger);
  color: #fff;
  padding: var(--spacing-sm) var(--spacing-lg);
  border-radius: var(--radius-md);
  font-size: 13px;
  z-index: 1100;
  box-shadow: var(--shadow-md);
  transition: opacity var(--transition-normal);
}

#error-toast.hidden {
  opacity: 0;
  visibility: hidden;
}

#shortcut-panel {
  position: fixed;
  top: calc(var(--header-height) + var(--spacing-sm));
  right: var(--spacing-md);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--spacing-md);
  z-index: 700;
  box-shadow: var(--shadow-lg);
  min-width: 220px;
  transition: opacity var(--transition-fast), transform var(--transition-fast);
}

#shortcut-panel.hidden {
  opacity: 0;
  visibility: hidden;
  transform: translateY(-8px);
}

#shortcut-panel h3 {
  font-size: 14px;
  margin-bottom: var(--spacing-sm);
  color: var(--color-accent);
}

#shortcut-panel ul {
  list-style: none;
}

#shortcut-panel li {
  font-size: 13px;
  padding: var(--spacing-xs) 0;
  color: var(--color-text-muted);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  font-size: 12px;
  font-family: var(--font-mono);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
}

.leaflet-popup-content-wrapper {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border: 1px solid var(--color-border) !important;
  border-radius: var(--radius-md) !important;
  box-shadow: var(--shadow-md) !important;
}

.leaflet-popup-tip {
  background: var(--color-surface) !important;
}

.leaflet-popup-content {
  font-family: var(--font-sans) !important;
  font-size: 13px !important;
  margin: var(--spacing-sm) var(--spacing-md) !important;
}

.leaflet-popup-content h4 {
  font-size: 14px;
  margin-bottom: 4px;
  color: var(--color-accent);
}

.leaflet-popup-content p {
  color: var(--color-text-muted);
  line-height: 1.5;
}

.custom-marker {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--color-dynasty);
  border: 2px solid #fff;
  box-shadow: 0 0 8px var(--color-dynasty-active);
  cursor: pointer;
  transition: transform var(--transition-fast);
}

.custom-marker:hover {
  transform: scale(1.4);
  box-shadow: 0 0 14px var(--color-dynasty-active);
}

.custom-marker.active {
  background: var(--color-accent);
  box-shadow: 0 0 14px var(--color-accent-glow);
  transform: scale(1.3);
  z-index: 1000 !important;
}

.leaflet-tooltip {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border: 1px solid var(--color-border) !important;
  border-radius: var(--radius-md) !important;
  box-shadow: var(--shadow-md) !important;
  padding: 6px 10px !important;
  font-family: var(--font-sans) !important;
  font-size: 13px !important;
  white-space: nowrap;
}

.leaflet-tooltip::before {
  border-top-color: var(--color-surface) !important;
}

@media (max-width: 767px) {
  :root {
    --dynasty-row-height: 14px;
    --dynasty-timeline-height: 26px;
    --calendar-timeline-height: 32px;
    --year-indicator-height: 18px;
    --event-list-height: 120px;
  }

  #header {
    padding: 0 var(--spacing-xs);
  }

  #site-title {
    font-size: 14px;
  }

  #event-detail-panel {
    width: 100%;
    max-width: 100vw;
    right: 0;
    top: auto;
    bottom: 0;
    height: 50vh;
    border-left: none;
    border-top: 1px solid var(--color-border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    transition: transform var(--transition-normal);
  }

  #event-detail-panel.hidden {
    transform: translateY(100%);
  }

  .custom-marker {
    width: 12px;
    height: 12px;
  }
}

@media (min-width: 768px) and (max-width: 1199px) {
  :root {
    --dynasty-row-height: 16px;
    --dynasty-timeline-height: 28px;
    --calendar-timeline-height: 34px;
  }

  #event-detail-panel {
    width: 300px;
  }
}

@media (min-width: 1200px) {
  :root {
    --dynasty-row-height: 18px;
    --dynasty-timeline-height: 34px;
    --calendar-timeline-height: 40px;
  }
}

@media (min-width: 2560px) {
  :root {
    --dynasty-row-height: 22px;
    --dynasty-timeline-height: 42px;
    --calendar-timeline-height: 52px;
    --year-indicator-height: 26px;
    --header-height: 40px;
    --event-list-height: 200px;
    --copyright-height: 28px;
  }

  #site-title {
    font-size: 22px;
  }

  #event-detail-panel {
    width: 420px;
  }
}

@media (min-width: 3440px) {
  :root {
    --dynasty-row-height: 24px;
    --dynasty-timeline-height: 48px;
    --calendar-timeline-height: 60px;
    --year-indicator-height: 30px;
    --header-height: 46px;
    --event-list-height: 220px;
    --copyright-height: 30px;
  }

  #site-title {
    font-size: 24px;
  }

  #event-detail-panel {
    width: 480px;
  }
}