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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #05060e;
  color: #dfe6f0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}


.hidden { display: none !important; }

/* --- overlays --- */
#loading-overlay, #error-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #0a0e22 0%, #05060e 75%);
  z-index: var(--z-loading);
}

.loading-card {
  width: min(420px, 86vw);
  text-align: center;
  padding: 32px 28px;
  border: 1px solid #1d2742;
  border-radius: 14px;
  background: rgba(10, 14, 30, 0.75);
  backdrop-filter: blur(6px);
}

.loading-title {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.35em;
  margin-right: -0.35em;
  background: linear-gradient(90deg, #7fd4ff, #7cf2b0);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.loading-sub {
  margin-top: 6px;
  font-size: 13px;
  color: #8fa0bd;
}

#progress-bar {
  margin: 22px 0 10px;
  height: 8px;
  border-radius: 4px;
  background: #131a30;
  border: 1px solid #202c4e;
  overflow: hidden;
}

#progress-bar-fill {
  height: 100%;
  width: 0%;
  border-radius: 4px;
  background: linear-gradient(90deg, #3f8fd2, #7cf2b0);
  transition: width 0.15s ease-out;
}

#stage-label { font-size: 12px; color: #8fa0bd; min-height: 1.2em; }

.error-text { margin-top: 14px; font-size: 14px; color: #ff9d9d; line-height: 1.5; }

/* --- HUD --- */
@media (max-width: 700px) {
  .loading-title { font-size: 24px; }
}


/* --- map layer switcher --- */
#layers {
  position: fixed;
  top: 14px;
  right: 16px;
  z-index: var(--z-hud);
  display: flex;
  gap: 6px;
}

#layers button {
  padding: 6px 12px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: #aebbd4;
  background: rgba(10, 14, 30, 0.72);
  border: 1px solid #202c4e;
  border-radius: 8px;
  cursor: pointer;
  backdrop-filter: blur(4px);
}

#layers button:hover { border-color: #3f8fd2; color: #dfe6f0; }

#layers button.active {
  color: #05060e;
  background: linear-gradient(90deg, #7fd4ff, #7cf2b0);
  border-color: transparent;
}

/* --- simulation HUD --- */
#sim-line { margin-top: 6px; font-size: 12px; color: #a8e0b8; }

#timectl {
  position: fixed;
  top: 52px;
  right: 16px;
  z-index: var(--z-hud);
  display: flex;
  gap: 6px;
}

#timectl button {
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 700;
  color: #aebbd4;
  background: rgba(10, 14, 30, 0.72);
  border: 1px solid #202c4e;
  border-radius: 8px;
  cursor: pointer;
  backdrop-filter: blur(4px);
}

#timectl button:hover { border-color: #3f8fd2; color: #dfe6f0; }

#timectl button.active {
  color: #05060e;
  background: linear-gradient(90deg, #ffd97f, #f2a97c);
  border-color: transparent;
}

#chronicle {
  position: fixed;
  bottom: 14px;
  left: 16px;
  z-index: var(--z-hud);
  max-width: min(46vw, 430px);
  pointer-events: none;
  font-size: 11px;
  line-height: 1.55;
  color: #b9c6dd;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
}

#chronicle .ev { opacity: 0.55; }
#chronicle .ev:nth-last-child(-n+3) { opacity: 1; }
#chronicle .ev-war, #chronicle .ev-collapse, #chronicle .ev-fall { color: #e8a1a1; }
#chronicle .ev-founded, #chronicle .ev-polity, #chronicle .ev-settler { color: #a8e0b8; }
#chronicle .ev-farming, #chronicle .ev-culture { color: #e6d49a; }
#chronicle .ev-era { color: #9ec9f0; font-weight: 700; }
#chronicle .ev-capital, #chronicle .ev-explore { color: #c9c2e8; }

/* --- realms table --- */
#realms {
  position: fixed;
  top: 92px;
  right: 16px;
  z-index: var(--z-panel);
  max-height: min(62vh, 560px);
  overflow-y: auto;
  padding: 12px 14px;
  background: rgba(8, 11, 24, 0.92);
  border: 1px solid #26335c;
  border-radius: 10px;
  backdrop-filter: blur(6px);
  font-size: 12px;
  color: #c6d2e6;
}

.realms-title { font-weight: 700; margin-bottom: 8px; color: #dfe6f0; }
.realms-hint { font-weight: 400; font-size: 10px; color: #74839f; }

#realms table { border-collapse: collapse; white-space: nowrap; }
#realms th {
  text-align: left;
  padding: 3px 10px 5px 0;
  color: #8fa3c8;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid #26335c;
}
#realms th:hover { color: #dfe6f0; }
#realms td { padding: 3px 10px 3px 0; }
#realms tbody tr:hover { background: rgba(63, 143, 210, 0.12); }
#realms .swatch {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 2px;
  margin-right: 6px;
}

/* --- live-forming stage strip --- */
#stage-strip {
  position: fixed;
  bottom: 42px;
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-hud);
  width: min(340px, 70vw);
  text-align: center;
  pointer-events: none;
}

#strip-label {
  font-size: 12px;
  color: #aebbd4;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
  margin-bottom: 6px;
}

#strip-bar {
  height: 4px;
  border-radius: 2px;
  background: rgba(19, 26, 48, 0.8);
  border: 1px solid #202c4e;
  overflow: hidden;
}

#strip-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #3f8fd2, #7cf2b0);
  transition: width 0.2s ease-out;
}

/* --- world stats panel --- */
#stats {
  position: fixed;
  top: 92px;
  left: 16px;
  z-index: var(--z-panel);
  width: min(46vw, 420px);
  padding: 12px 14px;
  background: rgba(8, 11, 24, 0.92);
  border: 1px solid #26335c;
  border-radius: 10px;
  backdrop-filter: blur(6px);
  color: #c6d2e6;
}
#stats-canvas { width: 100%; height: 300px; display: block; }

/* --- settlement inspection --- */
#sett-panel {
  position: fixed;
  bottom: 120px;
  right: 16px;
  z-index: var(--z-panel);
  min-width: 220px;
  padding: 10px 12px;
  background: rgba(8, 11, 24, 0.92);
  border: 1px solid #26335c;
  border-radius: 10px;
  backdrop-filter: blur(6px);
  font-size: 12px;
  color: #c6d2e6;
}
#sett-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
#sett-name { font-weight: 800; font-size: 14px; color: #efe6c8; letter-spacing: 0.04em; }
#sett-close { background: none; border: none; color: #74839f; cursor: pointer; font-size: 13px; }
#sett-close:hover { color: #dfe6f0; }
#sett-body div { margin: 3px 0; }
#sett-body b { color: #dfe6f0; }

/* --- main menu: minimal left column over the cinematic planet --- */
#menu {
  position: fixed;
  inset: 0;
  z-index: var(--z-menu);
  display: flex;
  align-items: center;
  background: linear-gradient(90deg, rgba(3, 4, 10, 0.82) 0%, rgba(3, 4, 10, 0.55) 26%, rgba(3, 4, 10, 0) 55%);
}
#menu-col {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 14px;
  padding-left: clamp(28px, 7vw, 110px);
}
#menu-title {
  font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, Georgia, 'Times New Roman', serif;
  /* 72px desktop, ~41px on a phone */
  font-size: clamp(41px, 10.2vw, 72px);
  font-weight: 600;
  letter-spacing: 0.3em;
  /* padding gives the clipped-text raster room for its shadows (they were
     cut at the element box); negative margins keep the layout unchanged.
     Symmetric sides + text-indent canceling the trailing letter-space —
     otherwise the centered title sits visibly off-axis. */
  padding: 0.4em 0.45em 0.45em 0.45em;
  margin: -0.4em -0.45em calc(26px - 0.45em) -0.45em;
  text-indent: 0.3em;
  background: linear-gradient(180deg, #fbeaa8 0%, #e9c96e 42%, #b8860b 78%, #8a6408 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  /* engraved-gold treatment: thin bronze stroke, hard underset shade,
     soft depth, faint warm halo */
  -webkit-text-stroke: 1.1px rgba(58, 38, 8, 0.6);
  filter:
    drop-shadow(0 2px 0 rgba(42, 26, 4, 0.85))
    drop-shadow(0 6px 13px rgba(0, 0, 0, 0.6))
    drop-shadow(0 0 17px rgba(244, 223, 154, 0.22));
}
@media (max-width: 700px) {
  #menu-col { padding-left: 22px; }
  .menu-item { font-size: 17px; }
  #menu-play { font-size: 20px; }
}
.menu-item {
  background: none;
  border: none;
  padding: 2px 0;
  font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, Georgia, serif;
  font-size: 21px;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: #cdb87e;
  cursor: pointer;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.8);
  transition: color 0.12s, transform 0.12s;
}
.menu-item:hover { color: #ffedb8; transform: translateX(6px); }

/* --- modals --- */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(3, 4, 10, 0.55);
}
.modal-card {
  min-width: 300px;
  padding: 22px 26px;
  background: rgba(14, 12, 8, 0.95);
  border: 1px solid #6d5424;
  border-radius: 6px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.65);
}
.modal-title { font-size: 17px; font-weight: 700; color: #f0e2b6; letter-spacing: 0.1em; margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.modal-actions button {
  padding: 7px 16px;
  color: #f0e2b6;
  background: rgba(90, 68, 28, 0.55);
  border: 1px solid #7c6228;
  border-radius: 4px;
  cursor: pointer;
}
.modal-actions button:hover { border-color: #d9b45c; color: #fff3cf; }
.menu-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 12px;
  color: #c9b586;
}
.menu-row label { min-width: 64px; text-align: left; letter-spacing: 0.08em; }
.menu-row input[type="number"], .menu-row select {
  flex: 1;
  padding: 6px 8px;
  color: #f0e2b6;
  background: rgba(20, 15, 8, 0.8);
  border: 1px solid #6d5424;
  border-radius: 4px;
}
.menu-row input[type="range"] { flex: 1; accent-color: #d9b45c; }

/* --- in-game menu button --- */
#menu-btn {
  position: fixed;
  bottom: 14px;
  right: 16px;
  z-index: var(--z-chrome);
  padding: 7px 12px;
  font-size: 14px;
  color: #aebbd4;
  background: rgba(10, 14, 30, 0.72);
  border: 1px solid #202c4e;
  border-radius: 8px;
  cursor: pointer;
  backdrop-filter: blur(4px);
}
#menu-btn:hover { border-color: #d9b45c; color: #ffedb8; }

/* --- floating city labels --- */
#city-labels { position: fixed; inset: 0; z-index: var(--z-label); pointer-events: none; }
.city-label {
  position: absolute;
  transform: translate(-50%, -100%);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #e6ecf7;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.95), 0 0 8px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
  text-shadow: none;
  paint-order: stroke fill;
  -webkit-text-stroke: 3px rgba(14, 12, 8, 0.85);
}
.city-label.capital { color: #ffd97f; font-size: 11px; }

/* ============================================================= */
/* UI rework: slate + brass, minimal, age-agnostic               */
/* ============================================================= */
:root {
  --ui-bg: rgba(13, 14, 17, 0.78);
  --ui-line: rgba(168, 138, 84, 0.5);
  --ui-line-soft: rgba(168, 138, 84, 0.28);
  --ui-ink: #d8d3c4;
  --ui-dim: #8e8878;
  --ui-accent: #c9a86a;
  --ui-active-ink: #10100c;      /* text on accent-filled controls */
  --ui-danger: #d97f5c;
  /* layer scale: labels < hud < chrome < panels < loading < menu < modal < flash */
  --z-label: 9;
  --z-hud: 10;
  --z-chrome: 11;
  --z-panel: 12;
  --z-loading: 20;
  --z-menu: 40;
  --z-modal: 45;
  --z-flash: 60;
  /* single-row bar height: every under-bar surface hangs off this ONE value */
  --topbar-h: 34px; /* single left bar height; panels open just under it */
}

#topbar {
  position: fixed;
  top: 10px;
  left: 12px;
  z-index: var(--z-chrome);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 10px;
  background: var(--ui-bg);
  border: 1px solid var(--ui-line-soft);
  border-radius: 6px;
  backdrop-filter: blur(6px);
}
#topbar #menu-btn {
  position: static;
  padding: 3px 8px;
  font-size: 13px;
  color: var(--ui-ink);
  background: none;
  border: none;
  cursor: pointer;
}
#topbar #menu-btn:hover { color: var(--ui-accent); }
#sim-line { font-size: 12px; color: var(--ui-ink); letter-spacing: 0.03em; margin: 0; white-space: nowrap; }
#timectl { position: static; display: flex; gap: 4px; align-items: center; }
#timectl button {
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--ui-dim);
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  backdrop-filter: none;
}
#timectl button:hover { color: var(--ui-ink); border-color: var(--ui-line-soft); }
#timectl button.active { color: #10100c; background: var(--ui-accent); border-color: transparent; }
.tb-sep { width: 1px; height: 14px; background: var(--ui-line-soft); margin: 0 4px; }

#layers {
  top: 10px;
  right: 12px;
  gap: 0;
  background: var(--ui-bg);
  border: 1px solid var(--ui-line-soft);
  border-radius: 6px;
  padding: 3px;
  backdrop-filter: blur(6px);
}
#layers button {
  padding: 4px 9px;
  font-size: 10.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ui-dim);
  background: none;
  border: none;
  border-radius: 4px;
}
#layers button:hover { color: var(--ui-ink); }
#layers button.active { color: #10100c; background: var(--ui-accent); }

#legend {
  top: auto;
  bottom: 34px;
  right: auto;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ui-bg);
  border-color: var(--ui-line-soft);
  color: var(--ui-dim);
}

#statusline {
  position: fixed;
  bottom: 10px;
  right: 12px;
  z-index: var(--z-label);
  display: flex;
  gap: 10px;
  font-size: 10px;
  color: var(--ui-dim);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
  pointer-events: none;
}
#tile-count, #seed-label, #fps { margin: 0; font-size: 10px; color: var(--ui-dim); }

#realms, #stats {
  background: var(--ui-bg);
  border-color: var(--ui-line);
  color: var(--ui-ink);
  top: 52px;
}
#realms th { color: var(--ui-dim); border-bottom-color: var(--ui-line-soft); }
#realms tbody tr:hover { background: rgba(201, 168, 106, 0.1); }
.realms-title { color: var(--ui-ink); }
.realms-hint { color: var(--ui-dim); }

#sett-panel { background: var(--ui-bg); border-color: var(--ui-line); }
#sett-name { color: var(--ui-accent); }

#chronicle { color: #c3bda9; }
#chronicle .ev-era { color: var(--ui-accent); }

.help-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  font-size: 12px;
  color: #c9b586;
}
.help-grid b { color: #f0e2b6; font-weight: 600; text-align: right; }

/* gameplay UI is invisible while the menu owns the screen */
body.in-menu #topbar, body.in-menu #layers, body.in-menu #legend,
body.in-menu #chronicle, body.in-menu #city-labels, body.in-menu #sett-panel,
body.in-menu #realms, body.in-menu #stats, body.in-menu #statusline,
body.in-menu #stage-strip { display: none !important; }

/* panels glide in when the world is ready */
@keyframes ui-reveal { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }
.ui-reveal { animation: ui-reveal 0.7s ease-out both; }

.pause-buttons { display: flex; flex-direction: column; gap: 8px; min-width: 220px; }
.pause-buttons button {
  padding: 9px 16px;
  font-size: 14px;
  letter-spacing: 0.06em;
  color: #f0e2b6;
  background: rgba(90, 68, 28, 0.4);
  border: 1px solid #7c6228;
  border-radius: 4px;
  cursor: pointer;
}
.pause-buttons button:hover { border-color: #d9b45c; color: #fff3cf; }
#pause-leave { margin-top: 10px; color: #d9a08a; border-color: #7c4a38; }
#pause-leave:hover { border-color: #d97f5c; color: #ffcdb8; }

.panel-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.panel-close {
  background: none;
  border: none;
  color: var(--ui-dim);
  font-size: 12px;
  cursor: pointer;
  padding: 0 0 6px 6px;
}
.panel-close:hover { color: var(--ui-ink); }

/* --- round six polish --- */
/* full-width topbar with fixed stat slots (no width jumping) */
#topbar {
  right: 12px;
  justify-content: space-between;
}
#sim-line {
  display: flex;
  align-items: center;
  gap: 18px;
  flex: 1;
  margin: 0 18px;
  font-variant-numeric: tabular-nums;
}
#tb-year { min-width: 96px; }
#tb-era { min-width: 110px; color: var(--ui-accent); }
.tb-stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 64px;
  color: var(--ui-dim);
}
.tb-stat svg { color: var(--ui-accent); flex: none; }
.tb-stat b { color: var(--ui-ink); font-weight: 600; }

/* labels position via transform now — anchor at the origin */
.city-label { left: 0; top: 0; transform: none; will-change: transform; }

/* menu text: white, not gold-on-the-cheap */
.menu-item { color: #e8e6df; }
.menu-item:hover { color: #ffffff; }
.menu-row { color: #d9d6cd; }
.menu-row label { color: #d9d6cd; }

/* layers as a dropdown under the topbar (the full-width bar covered it) */
#layers {
  top: 46px;
  right: auto;
  left: 12px;
  flex-direction: column;
  align-items: stretch;
  z-index: var(--z-panel);
}
#layers button { text-align: left; padding: 5px 12px; }

/* --- splash (menu page zero): title fixed, Begin fades, items slide in --- */
#menu-stack { position: relative; min-height: 150px; }
#menu-play {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 24px;
  letter-spacing: 0.22em;
  transition: opacity 0.55s ease;
}
#menu:not(.splash) #menu-play { opacity: 0; pointer-events: none; }
#menu-items { display: flex; flex-direction: column; align-items: flex-start; gap: 14px; }
#menu-items .menu-item {
  transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.2, 1), opacity 0.5s ease;
}
#menu.splash #menu-items .menu-item {
  opacity: 0;
  transform: translateX(-52px);
  pointer-events: none;
}
#menu-items .menu-item:nth-child(1) { transition-delay: 0.08s; }
#menu-items .menu-item:nth-child(2) { transition-delay: 0.16s; }
#menu-items .menu-item:nth-child(3) { transition-delay: 0.24s; }

/* Begin breathes while it waits */
@keyframes begin-pulse {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 1; }
}
#menu.splash #menu-play { animation: begin-pulse 2.4s ease-in-out infinite; }
#menu:not(.splash) #menu-play { animation: none; opacity: 0; pointer-events: none; }

/* --- edge-to-edge chrome: bar flush with the top, panels flush right --- */
#topbar {
  top: 0;
  left: 0;
  right: 0;
  border-radius: 0;
  border-width: 0 0 1px 0;
  padding: 7px 12px;
}
#layers { top: var(--topbar-h); left: 0; border-radius: 0 8px 8px 0; border-left: none; }
#legend { bottom: 0; border-radius: 8px 8px 0 0; border-bottom: none; }
#realms, #stats { top: calc(var(--topbar-h) + 4px); }
#realms { right: 0; border-radius: 0 0 0 10px; border-right: none; border-top: none; }
#stats { left: 0; border-radius: 0 0 10px 0; border-left: none; border-top: none; }
#sett-panel { right: 0; border-radius: 10px 0 0 10px; border-right: none; }
#statusline { bottom: 4px; right: 8px; }

/* --- mobile fit: single-row bar, fewer words, tighter everything --- */
@media (max-width: 700px) {
  #topbar { gap: 6px; padding: 5px 6px; }
  #sim-line { gap: 8px; margin: 0 4px; font-size: 10.5px; }
  #tb-year { min-width: 64px; }
  #tb-era { display: none; }
  .tb-stat { min-width: 44px; gap: 4px; }
  #timectl { gap: 2px; }
  #timectl button { padding: 3px 6px; font-size: 10px; letter-spacing: 0; }
  .tb-sep { margin: 0 2px; }
  #statusline { display: none; }
  #chronicle { max-width: 72vw; font-size: 10px; }
  #realms, #stats { max-width: 96vw; overflow-x: auto; }
  #stats { width: 96vw; }
  .city-label { font-size: 9px; }
}

/* two-row bar whenever a single row would clip (covers tablet portrait):
   every under-bar surface follows via --topbar-h */
@media (max-width: 900px) {
  :root { --topbar-h: 64px; }
  #topbar { flex-wrap: wrap; row-gap: 4px; }
  #sim-line { order: 3; width: 100%; margin: 0; justify-content: space-between; }
  #timectl { margin-left: auto; }
}

/* --- version tag --- */
#menu-version {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 11px;
  letter-spacing: 0.1em;
  color: #e8e6df;
}

/* --- stacked menu: applied whenever the planet's disc would crowd the
   menu items (measured live, with hysteresis) — centered column, planet
   pinned to the bottom edge, always a clear gap between them --- */
body.menu-stacked #menu {
  align-items: flex-start;
  background: linear-gradient(180deg, rgba(3, 4, 10, 0.8) 0%, rgba(3, 4, 10, 0.55) 45%, rgba(3, 4, 10, 0) 72%);
}
body.menu-stacked #menu-col {
  align-items: center;
  padding: 14vh 0 0;
  width: 100%;
}
body.menu-stacked #menu-stack { min-height: 130px; display: flex; flex-direction: column; align-items: center; }
body.menu-stacked #menu-play { position: static; }
body.menu-stacked #menu-items { align-items: center; }
body.menu-stacked .menu-item:hover { transform: none; }

/* full-screen sunrise glare: pure light, above everything, touches nothing */
#sun-flash {
  position: fixed;
  inset: 0;
  z-index: var(--z-flash);
  pointer-events: none;
  opacity: 0;
}

/* ============================================================= */
/* Consistency pass: focus, legend, small screens, safe areas    */
/* ============================================================= */

/* keyboard visibility: every interactive element shows its focus */
button:focus-visible,
select:focus-visible,
input:focus-visible,
th[tabindex]:focus-visible,
tr[tabindex]:focus-visible {
  outline: 2px solid var(--ui-accent);
  outline-offset: 2px;
  border-radius: 4px;
}
button:active { transform: translateY(1px); }
.menu-item:focus-visible, #menu-play:focus-visible { outline-offset: 5px; }

/* legend: real base styles (the old rules assumed a base that never existed) */
#legend {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  z-index: var(--z-hud);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px calc(5px + env(safe-area-inset-bottom, 0px));
  background: var(--ui-bg);
  border: 1px solid var(--ui-line-soft);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  font-size: 11px;
  color: var(--ui-dim);
  pointer-events: none;
}
.legend-bar {
  width: 140px;
  height: 10px;
  border-radius: 5px;
  border: 1px solid var(--ui-line-soft);
}

/* settlement panel: bounded on small screens; chronicle yields the lane */
@media (max-width: 700px) {
  #sett-panel {
    max-width: min(320px, 88vw);
    max-height: 42vh;
    overflow-y: auto;
  }
  #chronicle { max-width: 55vw; }
  .modal-card { max-height: 88vh; overflow-y: auto; }
}

/* notch / home-indicator safe areas (standalone PWA) */
#topbar { padding-top: calc(7px + env(safe-area-inset-top, 0px)); }
@media (max-width: 900px) {
  :root { --topbar-h: calc(64px + env(safe-area-inset-top, 0px)); }
}
#statusline { bottom: calc(4px + env(safe-area-inset-bottom, 0px)); }
#menu-version { top: calc(12px + env(safe-area-inset-top, 0px)); }

/* touch: comfortable targets on coarse pointers */
@media (pointer: coarse) {
  #timectl button, #layers button { min-height: 30px; min-width: 30px; }
  .panel-close, #sett-close { min-height: 28px; min-width: 28px; }
  #menu-btn { min-height: 34px; min-width: 34px; }
}

/* motion sensitivity: no pulses or entrance slides */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* chronicle event tints for the new sim systems */
.ev-succession { color: #cbbfa0; }
.ev-dynasty { color: #e0c98a; }
.ev-colony { color: #9ed4c9; }

/* settlement chronicle inside the panel */
.sett-history { margin-top: 8px; border-top: 1px solid var(--ui-line-soft); padding-top: 6px; }
.hist-title { color: var(--ui-dim); font-size: 10px; letter-spacing: 0.08em; text-transform: uppercase; margin-bottom: 3px; }
.hist-row { font-size: 11px; color: var(--ui-ink); }
.hist-year { color: var(--ui-dim); display: inline-block; min-width: 40px; }

/* R2 chronicle tints + golden age mark */
.ev-contact { color: #9fc3d8; }
.ev-incident { color: #d8b08a; }
.ev-deal { color: #b8d3a0; }
.ev-embargo { color: #d89a9a; }
.ev-golden { color: #ecd88f; }
.golden-mark { color: #ecd88f; }

/* R3 chronicle tints */
.ev-battle { color: #e0a875; }
.ev-sack { color: #e08a6a; }
.ev-peace { color: #a8d8c0; }

/* R4 chronicle tints */
.ev-alliance { color: #a8c8e8; }
.ev-treaty { color: #c8b8d8; }
.ev-coalition { color: #e8c8a8; }
.ev-vassal { color: #d8c8a0; }
.ev-independence { color: #f0d0a0; }
.ev-revolt { color: #e89a7a; }
.vassal-mark { color: #d8c8a0; font-size: 10px; }

/* cinematic narrator */
#narrator {
  position: fixed;
  left: 50%;
  bottom: 9vh;
  transform: translateX(-50%);
  max-width: min(760px, 86vw);
  text-align: center;
  font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, Georgia, serif;
  font-size: 21px;
  color: #f0e6c8;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.95), 0 0 30px rgba(0, 0, 0, 0.6);
  z-index: var(--z-chrome);
  pointer-events: none;
  opacity: 0;
}
#narrator.fade { animation: narr 7s ease-in-out forwards; }
@keyframes narr {
  0% { opacity: 0; transform: translateX(-50%) translateY(8px); }
  8% { opacity: 1; transform: translateX(-50%) translateY(0); }
  80% { opacity: 1; }
  100% { opacity: 0; }
}
body.cinematic #chronicle, body.cinematic #statusline { opacity: 0.25; }
#cine-btn.active { color: var(--ui-active-ink); background: var(--ui-accent); border-radius: 4px; }

/* realm names lettered on the map */
#realm-labels { position: fixed; inset: 0; pointer-events: none; z-index: var(--z-label); }
.realm-label {
  position: absolute;
  transform: translate(-50%, -50%);
  font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, Georgia, serif;
  font-weight: 600;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9), 0 0 12px rgba(0, 0, 0, 0.5);
  opacity: 0.85;
  white-space: nowrap;
}
/* ============================================================= */
/* UI overhaul v3: centred year/era + play-triangle speed control, */
/* right-side menu/help, clickable realm count, bottom-right tools  */
/* ============================================================= */
/* ONE left bar (stats · year · era · speed) flush to the top-left corner, and
   round help/menu buttons top-right in the Chronicles/Map-layers style. */
#topbar {
  top: 0; left: 0; right: 0; padding: 0;
  background: none; border: none; border-radius: 0; backdrop-filter: none; pointer-events: none;
}
.tb-left {
  position: fixed; top: 0; left: 0; height: 34px;
  display: flex; align-items: stretch; gap: 9px; padding: 0 12px;
  background: var(--ui-bg); border: 1px solid var(--ui-line-soft); border-top: none; border-left: none;
  border-radius: 0 0 10px 0; backdrop-filter: blur(6px); pointer-events: auto;
}
.tb-left > * { align-self: center; }
.tb-sep { width: 1px; height: 16px; background: var(--ui-line-soft); margin: 0 1px; }
.tb-stat { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--ui-dim); font-variant-numeric: tabular-nums; }
.tb-stat svg { color: var(--ui-accent); flex: none; }
.tb-stat b { color: var(--ui-ink); font-weight: 600; }
.tb-statbtn { background: none; border: 1px solid transparent; border-radius: 5px; padding: 3px 6px; cursor: pointer; }
.tb-statbtn:hover { border-color: var(--ui-line-soft); }
.tb-statbtn.active { border-color: var(--ui-accent); }
/* Year: gold word + ink number, at the population number's size */
.tb-yr { display: inline-flex; align-items: baseline; gap: 5px; font-size: 12px; font-variant-numeric: tabular-nums; }
.tb-yr-k { color: var(--ui-accent); font-weight: 600; }
#tb-year { font-size: 12px; font-weight: 600; color: var(--ui-ink); letter-spacing: 0; }
#tb-era { font-size: 12px; font-weight: 600; color: var(--ui-accent); min-width: 0; letter-spacing: 0; text-transform: none; opacity: 1; }
/* speed control: full-height segments to the right of the year, gold icons the
   size of the stat icons */
#timectl {
  position: static; align-self: stretch; display: flex; align-items: stretch; gap: 0;
  margin: 0 -12px 0 3px; padding: 0; background: none; border: none; backdrop-filter: none;
  border-radius: 0 0 10px 0; overflow: hidden;
}
#timectl .spd {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  min-width: 30px; padding: 0 6px;
  color: var(--ui-accent); background: none; border: none; border-radius: 0; cursor: pointer;
}
#timectl .spd svg { display: block; width: 12px; height: auto; }
#timectl .spd:hover { color: var(--ui-ink); background: rgba(255, 255, 255, 0.07); }
#timectl .spd.active { color: #10100c; background: var(--ui-accent); }
#timectl .spd i { position: absolute; right: 2px; bottom: 1px; font-style: normal; font-weight: 700; font-size: 6px; line-height: 1; pointer-events: none; }

/* round top-right buttons: same as the bottom-right map/chronicle tools */
.tb-right { position: fixed; top: 12px; right: 12px; display: flex; gap: 10px; pointer-events: none; }
.tb-right button, #topbar #menu-btn, #topbar #help-btn {
  width: 42px; height: 42px; border-radius: 50%; padding: 0; position: static; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
  color: var(--ui-ink); background: var(--ui-bg); border: 1px solid var(--ui-line-soft);
  cursor: pointer; backdrop-filter: blur(6px); pointer-events: auto;
}
.tb-right button:hover, #topbar #menu-btn:hover, #topbar #help-btn:hover { color: var(--ui-accent); border-color: var(--ui-accent); }

/* bottom-right circular tools: chronicle log + map layers */
#corner-tools { position: fixed; right: 12px; bottom: 28px; display: flex; gap: 10px; z-index: var(--z-chrome); }
#corner-tools button {
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: var(--ui-ink); background: var(--ui-bg); border: 1px solid var(--ui-line-soft);
  cursor: pointer; backdrop-filter: blur(6px);
}
#corner-tools button:hover { color: var(--ui-accent); border-color: var(--ui-accent); }
#corner-tools button.active { color: #10100c; background: var(--ui-accent); border-color: transparent; }

/* layer switcher pops up above the map button — a clean uniform list */
#layers {
  top: auto !important; left: auto !important;
  bottom: calc(80px + env(safe-area-inset-bottom, 0px)); right: 12px;
  flex-direction: column; align-items: stretch; gap: 2px; padding: 5px;
  border-radius: 8px; z-index: var(--z-panel);
}
#layers button {
  display: block; width: 100%; margin: 0; text-align: center;
  padding: 6px 18px; font-size: 10.5px; letter-spacing: 0.09em; text-transform: uppercase;
  color: var(--ui-dim); background: none; border: none; border-radius: 5px; cursor: pointer;
}
#layers button:hover { color: var(--ui-ink); }
#layers button.active { color: #10100c; background: var(--ui-accent); }

/* realms table shows every realm now — let it scroll */
#realms { max-height: calc(100vh - 84px); overflow-y: auto; }

/* debug info (opt-in via Options) sits under the top-left bar */
#statusline { top: 40px; left: 12px; bottom: auto; right: auto; max-width: 46vw; }

/* battle flourishes: crossed swords rising over a clash */
#battle-fx { position: fixed; inset: 0; z-index: var(--z-label); pointer-events: none; }
.battle-icon { position: absolute; left: 0; top: 0; color: #ffcf9a; filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.85)); will-change: transform, opacity; }

body.in-menu #corner-tools, body.in-menu #battle-fx { display: none !important; }
