:root {
  --bg: #0a0a0f;
  --card: #101216;
  --border: #1b1f24;
  --border-strong: #2b3138;
  --text: #cdd2ce;
  --text-bright: #edf0ea;
  --text-dim: #7e857e;
  --accent: #e07a3c;
  --accent-deep: #7a4222;
  --gold: #FFD700;
  --blue: #4488cc;
  --green: #7bb661;
  --red: #e04040;
  --witchlight: #8fe3bf;
  --font-body: "Atkinson Hyperlegible", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", monospace;
  --topbar-h: 44px;
  --threat-h: 28px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  height: 100%; width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

button {
  font-family: var(--font-mono);
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 6px 10px;
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button:active { opacity: 0.7; }

/* ---- Top Bar ---- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 8px;
  gap: 6px;
  z-index: 20;
  font-family: var(--font-mono);
  font-size: 11px;
}

#home-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 18px;
  line-height: 1;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}
#home-link:hover { color: var(--accent); }

#status-left, #status-center, #status-right {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
#status-left { flex-shrink: 0; }
#status-center { flex: 1; justify-content: center; gap: 10px; }
#status-right { flex-shrink: 0; }

#actions-left { color: var(--witchlight); font-weight: 700; }
#reset-timer { color: var(--text-dim); font-size: 10px; }
#ore-display { color: var(--gold); }
#relics-display { color: var(--accent); }
#era-display { color: var(--text-dim); }
#tick-display { color: var(--text-dim); }

#topbar-buttons {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
#topbar-buttons button {
  min-width: 36px;
  min-height: 36px;
  padding: 4px 6px;
  font-size: 12px;
  font-weight: 700;
}

/* ---- Threat Bar ---- */
#threat-bar {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0;
  height: var(--threat-h);
  background: rgba(224, 64, 64, 0.15);
  border-bottom: 1px solid rgba(224, 64, 64, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--red);
  z-index: 19;
  padding: 0 12px;
}

/* ---- Canvas ---- */
#mountain {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0; bottom: 0;
  width: 100%; height: calc(100% - var(--topbar-h));
  display: block;
}
body.has-threat #mountain {
  top: calc(var(--topbar-h) + var(--threat-h));
  height: calc(100% - var(--topbar-h) - var(--threat-h));
}

/* ---- Tile Panel ---- */
#tile-panel {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--card);
  border-top: 1px solid var(--border-strong);
  padding: 12px 16px;
  z-index: 30;
  font-family: var(--font-mono);
  font-size: 13px;
  max-height: 50vh;
  overflow-y: auto;
}
#tile-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
#tile-name {
  font-weight: 700;
  color: var(--text-bright);
  flex: 1;
}
#tile-coords {
  color: var(--text-dim);
  font-size: 11px;
}
#tile-close {
  min-width: 32px;
  min-height: 32px;
  padding: 2px 6px;
  font-size: 18px;
}
#tile-info {
  color: var(--text);
  margin-bottom: 8px;
  font-size: 12px;
  line-height: 1.5;
}
#tile-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}
#tile-actions button {
  font-size: 12px;
  padding: 8px 12px;
  border-color: var(--border-strong);
}
#tile-actions button:hover { border-color: var(--accent); }
#tile-actions button.selected {
  border-color: var(--accent);
  background: var(--accent-deep);
}
#tile-actions button[disabled] {
  opacity: 0.35;
  cursor: default;
}

#tile-confirm {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
#confirm-text {
  flex: 1;
  font-size: 12px;
  color: var(--text-dim);
}
#confirm-yes {
  background: var(--accent-deep);
  border-color: var(--accent);
  color: var(--text-bright);
  font-weight: 700;
}
#confirm-no {
  border-color: var(--border-strong);
}

/* ---- Side Panels ---- */
#chronicle-panel, #archive-panel, #help-panel {
  position: fixed;
  top: var(--topbar-h); right: 0; bottom: 0;
  width: min(360px, 85vw);
  background: var(--card);
  border-left: 1px solid var(--border-strong);
  z-index: 25;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#chron-head, #archive-head, #help-head {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#chron-head h2, #archive-head h2, #help-head h2 {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-bright);
}
#chron-close, #archive-close, #help-close {
  min-width: 36px;
  min-height: 36px;
  font-size: 18px;
  padding: 2px 6px;
}

#chron-list, #archive-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
}
#chron-more {
  margin: 8px 12px;
  font-size: 12px;
}

.chron-entry {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.5;
  font-family: var(--font-mono);
  cursor: default;
}
.chron-entry[data-x] { cursor: pointer; }
.chron-entry[data-x]:hover { color: var(--text-bright); }
.chron-entry .chron-tick {
  color: var(--text-dim);
  font-size: 10px;
}

.archive-era {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.archive-era:hover { color: var(--text-bright); }
.archive-era .era-num { color: var(--accent); font-weight: 700; }
.archive-era .era-outcome { font-size: 11px; }
.archive-era .era-outcome.won { color: var(--green); }
.archive-era .era-outcome.fallen { color: var(--red); }

/* ---- Help Panel ---- */
#help-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  font-size: 13px;
  line-height: 1.6;
}
#help-body h3 {
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: 14px;
  margin-top: 16px;
  margin-bottom: 6px;
}
#help-body h3:first-child { margin-top: 0; }
#help-body h4 {
  color: var(--text-bright);
  font-family: var(--font-mono);
  font-size: 12px;
  margin-top: 12px;
  margin-bottom: 4px;
}
#help-body ul {
  padding-left: 18px;
  margin-bottom: 8px;
}
#help-body li { margin-bottom: 4px; }

#legend-table, #legend-table-en {
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 13px;
  margin: 8px 0;
}
#legend-table td, #legend-table-en td {
  padding: 3px 10px 3px 0;
}
.g {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 700;
  text-align: center;
  width: 24px;
  display: inline-block;
}
.g-soil { color: #8B7355; }
.g-stone { color: #6B5B45; }
.g-deep { color: #4A4035; }
.g-bed { color: #353030; }
.g-rubble { color: #777; }
.g-ore { color: #FFD700; }
.g-water { color: #4488cc; }
.g-relic { color: #FFE455; }
.g-vault { color: #c8a8ff; }
.g-support { color: #B8860B; }
.g-wall { color: #999; }
.g-gate { color: #999; }
.g-monster { color: #FF3333; }

/* ---- Era Overlay ---- */
#era-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}
#era-overlay-box {
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 32px 28px;
  text-align: center;
  max-width: 340px;
  width: 90%;
}
#era-result {
  font-family: var(--font-mono);
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 12px;
}
#era-result.won { color: var(--green); }
#era-result.fallen { color: var(--red); }
#era-message {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  margin-bottom: 16px;
}
#era-countdown {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
}

/* ---- Toast ---- */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 10px 18px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-bright);
  z-index: 60;
  pointer-events: none;
  white-space: nowrap;
}

/* ---- Loading ---- */
#loading {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 100;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
}
#loading[hidden] { display: none; }
#load-spinner {
  width: 28px; height: 28px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#load-status { transition: opacity 0.2s; }

/* ---- Responsive ---- */
@media (max-width: 420px) {
  #topbar { font-size: 10px; gap: 4px; padding: 0 6px; }
  #status-center { gap: 6px; }
  #topbar-buttons button { min-width: 32px; min-height: 32px; font-size: 11px; }
}
