/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: #0d0d1a;
  color: #e8e8e8;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ===== Loading Screen ===== */
.loading-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  background: #0d0d1a;
  z-index: 100;
}

.loading-screen p {
  font-size: 1.1rem;
  font-weight: 300;
  letter-spacing: 0.05em;
  color: #8888aa;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.08);
  border-top-color: #C4943C;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ===== Grid Container ===== */
.grid-container {
  width: 100vw;
  height: 100vh;
  overflow: auto;
  scrollbar-color: #2a2a4a #0d0d1a;
}

.grid-container::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.grid-container::-webkit-scrollbar-track {
  background: #0d0d1a;
}

.grid-container::-webkit-scrollbar-thumb {
  background: #2a2a4a;
  border-radius: 5px;
}

.grid-container::-webkit-scrollbar-thumb:hover {
  background: #3a3a5a;
}

.grid-container::-webkit-scrollbar-corner {
  background: #0d0d1a;
}

/* ===== Timeline Grid ===== */
.timeline-grid {
  display: grid;
  grid-template-columns: 140px repeat(31, 200px);
  grid-template-rows: 48px repeat(18, 140px);
  background: #1a1a2e;
}

/* ===== Corner Cell ===== */
.cell-corner {
  position: sticky;
  left: 0;
  top: 0;
  z-index: 20;
  background: #0d0d1a;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  border-right: 1px solid rgba(255, 255, 255, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 300;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #6a6a8a;
}

/* ===== Column Headers (Time Periods) ===== */
.cell-col-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #0d0d1a;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  border-right: 1px solid rgba(255, 255, 255, 0.04);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: var(--grid-era-size, 0.82rem);
  font-weight: 500;
  color: #b0b0cc;
  padding: 0 8px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* ===== Row Headers (Regions) ===== */
.cell-row-header {
  position: sticky;
  left: 0;
  z-index: 10;
  border-right: 1px solid rgba(255, 255, 255, 0.12);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  display: flex;
  align-items: center;
  padding: 0 12px;
  font-size: var(--grid-region-size, 0.82rem);
  font-weight: 600;
  letter-spacing: 0.03em;
  color: #fff;
  min-height: 140px;
}

.cell-row-header span {
  display: block;
  line-height: 1.3;
}

/* ===== Event Cells ===== */
.cell-event {
  position: relative;
  border-right: 1px solid rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.cell-event:hover,
.cell-event:focus-visible {
  transform: scale(1.02);
  z-index: 5;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.cell-event:focus-visible {
  outline: 2px solid #C4943C;
  outline-offset: -2px;
}

.cell-event img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: filter 0.2s ease;
}

.cell-event:hover img {
  filter: brightness(1.1);
}

.cell-event .event-title-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 24px 10px 8px;
  background: linear-gradient(to top, rgba(0,0,0,0.85) 0%, rgba(0,0,0,0.4) 60%, transparent 100%);
  font-size: var(--grid-event-size, 0.85rem);
  font-weight: 500;
  color: #fff;
  line-height: 1.3;
  pointer-events: none;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

.cell-event .event-date-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  padding: 2px 7px;
  border-radius: 3px;
  font-size: 0.65rem;
  font-weight: 600;
  color: #fff;
  letter-spacing: 0.02em;
  pointer-events: none;
}

/* Image placeholder for missing images */
.cell-event .event-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  opacity: 0.15;
}

/* ===== Empty Cells ===== */
.cell-empty {
  border-right: 1px solid rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  background: #12122a;
}

/* Subtle alternating pattern for empty cells */
.cell-empty.alt {
  background: #141430;
}

/* ===== Tooltip ===== */
.tooltip {
  position: fixed;
  z-index: 50;
  max-width: 320px;
  padding: 10px 14px;
  background: #1e1e3a;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  font-size: var(--tooltip-size, 0.85rem);
  line-height: 1.5;
}

.tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}

.tooltip .tooltip-date {
  display: block;
  font-weight: 600;
  margin-bottom: 4px;
  font-size: inherit;
}

.tooltip .tooltip-text {
  color: #b0b0cc;
  font-weight: 300;
}

/* ===== Detail Panel (Modal) ===== */
.detail-panel {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.detail-panel.open {
  opacity: 1;
  visibility: visible;
}

.detail-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
}

.detail-content {
  position: relative;
  z-index: 1;
  width: 90%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  background: #16162e;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.7);
  transform: translateY(20px) scale(0.97);
  transition: transform 0.3s ease;
}

.detail-panel.open .detail-content {
  transform: translateY(0) scale(1);
}

.detail-content::-webkit-scrollbar {
  width: 6px;
}

.detail-content::-webkit-scrollbar-track {
  background: transparent;
}

.detail-content::-webkit-scrollbar-thumb {
  background: #2a2a4a;
  border-radius: 3px;
}

.detail-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 2;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #e8e8e8;
  font-size: 1.5rem;
  cursor: pointer;
  transition: background 0.2s ease;
  line-height: 1;
}

.detail-close:hover {
  background: rgba(255, 255, 255, 0.15);
}

.detail-image-wrap {
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #0d0d1a;
}

.detail-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.detail-body {
  padding: 24px 28px 32px;
}

.detail-date {
  display: inline-block;
  padding: 6px 16px;
  border-radius: 4px;
  font-size: var(--detail-date-size, 1.0rem);
  font-weight: 800;
  color: #fff;
  margin-bottom: 14px;
  letter-spacing: 0.04em;
}

.detail-title {
  font-size: 2.4rem;
  font-weight: 700;
  margin-bottom: 12px;
  line-height: 1.25;
  color: #fff;
}

.detail-region {
  display: inline-block;
  font-size: var(--detail-region-size, 0.85rem);
  font-weight: 400;
  color: #8888aa;
  margin-bottom: 18px;
  padding-bottom: 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  width: 100%;
}

.detail-description {
  font-size: var(--detail-desc-size, 0.95rem);
  font-weight: 300;
  line-height: 1.8;
  color: #c0c0d8;
}

.detail-wiki-link {
  display: inline-block;
  margin-top: 20px;
  font-size: 0.9rem;
  font-weight: 500;
  color: #C4943C;
  text-decoration: none;
  transition: color 0.2s ease;
}

.detail-wiki-link:hover {
  color: #e0b050;
  text-decoration: underline;
}

/* ===== Region Color Utilities (row header backgrounds) ===== */
.region-northern-europe { background: linear-gradient(135deg, #6B7FA3 0%, #4a5f83 100%); }
.region-western-europe { background: linear-gradient(135deg, #4169B1 0%, #2f4f91 100%); }
.region-central-europe { background: linear-gradient(135deg, #5B7553 0%, #3f5539 100%); }
.region-southern-europe { background: linear-gradient(135deg, #C67A5C 0%, #a65a3c 100%); }
.region-eastern-europe { background: linear-gradient(135deg, #7B3F61 0%, #5b2545 100%); }
.region-middle-east { background: linear-gradient(135deg, #C4943C 0%, #a47420 100%); }
.region-north-africa { background: linear-gradient(135deg, #C4A97D 0%, #a48960 100%); }
.region-sub-saharan-africa { background: linear-gradient(135deg, #8B6914 0%, #6b4d0a 100%); }
.region-southern-africa { background: linear-gradient(135deg, #6B8E23 0%, #4b6e10 100%); }
.region-indian-subcontinent { background: linear-gradient(135deg, #D4842A 0%, #b46410 100%); }
.region-central-asia { background: linear-gradient(135deg, #B8A04A 0%, #988030 100%); }
.region-china { background: linear-gradient(135deg, #B22234 0%, #920818 100%); }
.region-japan { background: linear-gradient(135deg, #C9847C 0%, #a96460 100%); }
.region-southeast-asia { background: linear-gradient(135deg, #4A8B6E 0%, #306b50 100%); }
.region-australia-oceania { background: linear-gradient(135deg, #3D7A8A 0%, #255a6a 100%); }
.region-north-america { background: linear-gradient(135deg, #B8860B 0%, #986600 100%); }
.region-central-america { background: linear-gradient(135deg, #2E8B57 0%, #1a6b3d 100%); }
.region-south-america { background: linear-gradient(135deg, #CD853F 0%, #ad6525 100%); }

/* ===== Loading to Grid Transition ===== */
.grid-container.fade-in {
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.loading-screen.fade-out {
  opacity: 0;
  transition: opacity 0.3s ease-out;
}

/* ===== Skip to content (Accessibility) ===== */
.skip-link {
  position: fixed;
  top: -100%;
  left: 16px;
  z-index: 100;
  padding: 8px 16px;
  background: #C4943C;
  color: #0d0d1a;
  font-weight: 600;
  font-size: 0.85rem;
  border-radius: 0 0 6px 6px;
  text-decoration: none;
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: 0;
}

/* ===== Controls Wrapper (zoom-compensated) ===== */
.controls-wrap {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 65;
  transform-origin: bottom right;
}

/* ===== Language Button ===== */
.lang-btn {
  position: absolute;
  bottom: 76px;
  right: 20px;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: #1e1e3a;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #b0b0cc;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, color 0.2s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.lang-btn:hover {
  background: #2a2a4a;
  color: #C4943C;
}

/* ===== Settings Panel ===== */
.settings-btn {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: #1e1e3a;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #8888aa;
  font-size: 1.3rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, color 0.2s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.settings-btn:hover {
  background: #2a2a4a;
  color: #C4943C;
}

.settings-panel {
  position: absolute;
  bottom: 132px;
  right: 20px;
  width: 260px;
  background: #16162e;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  padding: 18px 20px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
  transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
}

.settings-panel.open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.settings-panel h3 {
  font-size: var(--settings-size, 0.9rem);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #6a6a8a;
  margin-bottom: 16px;
}

.settings-panel h3 + .settings-slider-group ~ h3 {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.settings-slider-group {
  margin-bottom: 14px;
}

.settings-slider-group:last-child {
  margin-bottom: 0;
}

.settings-slider-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  font-size: var(--settings-size, 0.9rem);
  color: #b0b0cc;
}

.settings-slider-value {
  font-size: var(--settings-size, 0.9rem);
  color: #6a6a8a;
  font-variant-numeric: tabular-nums;
}

.settings-panel input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: #2a2a4a;
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.settings-panel input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  background: #C4943C;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.15s ease;
}

.settings-panel input[type="range"]::-webkit-slider-thumb:hover {
  background: #d4a44c;
}

.settings-panel input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: #C4943C;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .timeline-grid {
    grid-template-columns: 100px repeat(31, 140px);
    grid-template-rows: 40px repeat(18, 110px);
  }

  .cell-row-header {
    font-size: 0.72rem;
    padding: 0 10px;
    min-height: 110px;
  }

  .cell-col-header {
    font-size: 0.68rem;
  }

  .detail-content {
    width: 95%;
    max-height: 95vh;
  }

  .detail-body {
    padding: 18px 20px 24px;
  }

  .detail-title {
    font-size: 1.2rem;
  }
}
