:root {
  --bg: #0a1210;
  --bg-glass: rgba(10, 18, 16, 0.82);
  --panel: rgba(12, 22, 20, 0.92);
  --ink: #e8f0ee;
  --muted: #9fb2ae;
  --accent: #7dd3c0;
  --accent-2: #f0c674;
  --accent-3: #ff8a65;
  --skin: #c9a88d;
  --gold: #d4af37;
  --danger: #e57373;
  --focus: #4fc3f7;
  --radius: 12px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

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

#app {
  position: relative;
  width: 100%;
  height: 100%;
}

#scene-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hud {
  position: absolute;
  pointer-events: none;
}

.hud > * {
  pointer-events: auto;
}

.loading {
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  z-index: 50;
  transition: opacity 0.6s ease, visibility 0.6s;
}

.loading.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loader {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(125, 211, 192, 0.25);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

#top-bar {
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  gap: 16px;
  background: linear-gradient(to bottom, rgba(10,18,16,0.92), rgba(10,18,16,0.4));
  z-index: 20;
  flex-wrap: wrap;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.brand .subtitle {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.top-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.control-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.chip {
  appearance: none;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(255,255,255,0.08);
  color: var(--ink);
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
  outline: none;
}

.chip:hover, .chip:focus-visible {
  background: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.3);
}

.chip:active {
  transform: translateY(1px);
}

.chip.active, .chip[aria-pressed="true"] {
  background: rgba(125, 211, 192, 0.22);
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: 0 0 12px rgba(125,211,192,0.18);
}

.chip.action {
  background: rgba(240, 198, 116, 0.12);
  border-color: rgba(240, 198, 116, 0.35);
  color: var(--accent-2);
}

.chip.action:hover, .chip.action:focus-visible {
  background: rgba(240, 198, 116, 0.22);
}

#chapter-rail {
  top: 80px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 18;
  max-height: calc(100% - 140px);
  overflow-y: auto;
  background: rgba(10,18,16,0.55);
  padding: 8px;
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.chapter {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  text-align: right;
  padding: 8px 10px;
  font-size: 0.82rem;
  cursor: pointer;
  border-radius: 8px;
  outline: none;
  transition: background 0.15s, color 0.15s;
}

.chapter:hover, .chapter:focus-visible {
  color: var(--ink);
  background: rgba(255,255,255,0.08);
}

.chapter[aria-current="true"] {
  color: var(--accent);
  background: rgba(125,211,192,0.12);
}

#panel {
  top: 80px;
  left: 16px;
  width: min(420px, calc(100% - 140px));
  max-height: calc(100% - 140px);
  background: var(--panel);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 19;
  display: flex;
  flex-direction: column;
  padding: 18px;
  overflow: hidden;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: opacity 0.3s, transform 0.3s;
}

#panel[hidden] {
  display: none;
}

.panel-close {
  align-self: flex-end;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}

.panel-body {
  overflow-y: auto;
  padding-right: 6px;
}

.panel-body h2 {
  margin-top: 0;
  font-size: 1.2rem;
  color: var(--accent);
}

.panel-body h3 {
  font-size: 1rem;
  color: var(--accent-2);
  margin: 1.1em 0 0.4em;
}

.panel-body p, .panel-body li {
  font-size: 0.95rem;
  line-height: 1.55;
  color: var(--ink);
  margin: 0.6em 0;
}

.panel-body ul, .panel-body ol {
  padding-left: 1.2em;
  margin: 0.5em 0;
}

.panel-body .source {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.2em;
}

.panel-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.panel-body a:hover, .panel-body a:focus-visible {
  color: var(--ink);
}

.panel-body .range-bar {
  height: 8px;
  background: rgba(255,255,255,0.1);
  border-radius: 4px;
  margin: 8px 0;
  overflow: hidden;
}

.panel-body .range-bar span {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 4px;
}

#tooltip {
  position: absolute;
  background: rgba(10, 18, 16, 0.9);
  border: 1px solid rgba(125,211,192,0.35);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.8rem;
  color: var(--ink);
  pointer-events: none;
  z-index: 40;
  transform: translate(-50%, -140%);
  white-space: nowrap;
  transition: opacity 0.15s;
}

#tooltip[hidden] { opacity: 0; }

#controls-hint {
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.75rem;
  color: var(--muted);
  background: rgba(10,18,16,0.65);
  padding: 6px 12px;
  border-radius: 999px;
  z-index: 15;
  pointer-events: none;
}

#help {
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 45;
  background: rgba(0,0,0,0.55);
  padding: 20px;
}

#help[hidden] { display: none; }

.help-body {
  max-width: 420px;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,0.1);
}

.help-body h3 { margin-top: 0; color: var(--accent); }

.help-body ul { padding-left: 1.2em; }

.help-body li { margin: 0.5em 0; font-size: 0.95rem; }

.menu-toggle {
  display: none;
  top: 18px;
  right: 16px;
  z-index: 30;
  flex-direction: column;
  justify-content: center;
  width: 40px;
  height: 40px;
  gap: 5px;
  background: rgba(10,18,16,0.8);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 8px;
  padding: 0;
  cursor: pointer;
}

.menu-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--ink);
  margin: 0 auto;
}

.scale-overlay {
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 12;
  pointer-events: none;
  background: rgba(10,18,16,0.35);
}

.scale-overlay[hidden] { display: none; }

.scale-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 20px;
  max-width: min(520px, 90vw);
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,0.1);
}

.scale-card h3 {
  margin: 0 0 12px;
  color: var(--accent);
}

.scale-diagram {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.scale-hand svg {
  width: min(300px, 80vw);
  height: auto;
}

.scale-coin svg {
  width: 80px;
  height: 80px;
}

.scale-fact {
  font-size: 0.9rem;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 880px) {
  #top-bar {
    padding: 40px 12px 6px;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .brand h1 {
    font-size: 1.05rem;
  }

  .brand .subtitle {
    font-size: 0.68rem;
  }

  .chip {
    padding: 5px 10px;
    font-size: 0.78rem;
  }

  .menu-toggle {
    top: 8px;
    right: 10px;
    width: 36px;
    height: 36px;
    gap: 4px;
  }

  .menu-toggle span {
    width: 18px;
  }

  .top-controls {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    justify-content: flex-start;
    overflow-x: auto;
    padding-bottom: 4px;
    flex-wrap: nowrap;
    scrollbar-width: none;
  }

  .top-controls::-webkit-scrollbar {
    display: none;
  }

  .top-controls .control-group {
    flex-wrap: nowrap;
    flex: 0 0 auto;
  }

  .top-controls .chip {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .menu-toggle {
    display: flex;
  }

  #chapter-rail {
    position: fixed;
    top: 0;
    right: -220px;
    bottom: 0;
    left: auto;
    width: 220px;
    max-height: none;
    border-radius: 0;
    transform: none;
    transition: right 0.25s ease;
    padding-top: 68px;
    z-index: 25;
    background: var(--panel);
    pointer-events: none;
  }

  #chapter-rail.open {
    right: 0;
    pointer-events: auto;
  }

  #panel {
    top: auto;
    bottom: 80px;
    left: 12px;
    right: 12px;
    width: auto;
    max-height: 45vh;
  }

  .help-body {
    max-width: min(420px, 90vw);
    width: 100%;
  }

  #controls-hint {
    bottom: 8px;
    font-size: 0.7rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

button {
  touch-action: manipulation;
}
