:root {
  --bg: #0d1220;
  --bg-deep: #090d17;
  --panel: #141b2e;
  --panel-2: #1a2238;
  --border: #2a3452;
  --border-soft: #202945;
  --text: #eceff7;
  --muted: #98a2c3;
  --muted-dim: #6b7594;
  --gold: #d4a937;
  --gold-bright: #f0c75e;
  --gold-dim: #8a6f24;
  --accent-blue: #7fb3d5;
  --danger: #e0705f;
  --radius: 10px;
  --font-display: 'Cinzel', serif;
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: radial-gradient(1200px 500px at 50% -80px, #16203a 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */
.app-header {
  border-bottom: 1px solid var(--border-soft);
  background: rgba(9, 13, 23, 0.75);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.header-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 14px 20px;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark { color: var(--gold); flex: none; }
.brand h1 {
  font-family: var(--font-display);
  font-size: 22px;
  letter-spacing: 0.06em;
  margin: 0;
  color: var(--text);
}
.brand h1 .gold { color: var(--gold-bright); }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

/* ---------- Layout ---------- */
.main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 26px 20px 40px;
  width: 100%;
  flex: 1;
}
.app-footer {
  text-align: center;
  color: var(--muted-dim);
  font-size: 13px;
  padding: 18px 12px 26px;
  border-top: 1px solid var(--border-soft);
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

/* ---------- State views ---------- */
.state-view {
  text-align: center;
  padding: 80px 20px;
  color: var(--muted);
}
.state-view h2 { font-family: var(--font-display); color: var(--text); letter-spacing: 0.04em; }
.spinner {
  width: 42px; height: 42px; margin: 0 auto 18px;
  border: 3px solid var(--border);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Buttons / inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 16px;
  font: 500 14px var(--font-ui);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--gold-dim); background: #1f2942; }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: linear-gradient(180deg, #b98f2c, #96711f);
  border-color: #d4a937;
  color: #141003;
  font-weight: 600;
}
.btn-primary:hover { background: linear-gradient(180deg, #d4a937, #a8821f); border-color: var(--gold-bright); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); background: var(--panel); border-color: var(--border-soft); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

input[type="text"], input[type="search"], select {
  background: var(--bg-deep);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 9px 12px;
  font: 400 14px var(--font-ui);
  width: 100%;
}
input[type="text"]:focus, input[type="search"]:focus, select:focus {
  outline: none;
  border-color: var(--gold-dim);
  box-shadow: 0 0 0 2px rgba(212, 169, 55, 0.15);
}
select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%2398a2c3' stroke-width='1.6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 30px; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 5px; font-weight: 500; }

/* ---------- Browse: game tiles ---------- */
.game-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}
.game-tile {
  background: linear-gradient(180deg, var(--panel), var(--bg-deep));
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px 14px;
  cursor: pointer;
  text-align: left;
  color: var(--text);
  font-family: var(--font-ui);
  transition: border-color 0.15s, transform 0.1s;
  position: relative;
}
.game-tile:hover { border-color: var(--gold-dim); }
.game-tile.active { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold), 0 4px 18px rgba(212, 169, 55, 0.12); }
.game-tile .code {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--gold-bright);
  display: block;
}
.game-tile .gname { font-size: 13px; color: var(--muted); display: block; margin-top: 2px; }
.game-tile .gcount { font-size: 12px; color: var(--muted-dim); display: block; margin-top: 6px; }
.game-tile.all .code { font-size: 19px; }

/* ---------- Catalog ---------- */
.catalog-bar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}
.catalog-bar input { max-width: 340px; }
.dataset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.dataset-card {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
  text-align: left;
  color: var(--text);
  font-family: var(--font-ui);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.dataset-card:hover { border-color: var(--gold-dim); transform: translateY(-2px); }
.dataset-card .card-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dataset-card h3 { margin: 0; font-size: 16px; font-weight: 600; }
.dataset-card .desc { margin: 0; font-size: 13.5px; color: var(--muted); flex: 1; }
.dataset-card .card-meta { font-size: 12.5px; color: var(--muted-dim); display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.game-tag {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--gold-bright);
  border: 1px solid var(--gold-dim);
  border-radius: 20px;
  padding: 2px 10px;
}
.badge {
  font-size: 11.5px;
  font-weight: 600;
  border-radius: 20px;
  padding: 2px 10px;
  white-space: nowrap;
}
.badge-stale { background: rgba(224, 112, 95, 0.14); color: #f0957f; border: 1px solid rgba(224, 112, 95, 0.4); }

/* ---------- Builder ---------- */
.back-btn { margin-bottom: 14px; }
.builder-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.builder-head h2 { font-family: var(--font-display); letter-spacing: 0.03em; margin: 0 0 6px; font-size: 24px; }
.ds-meta-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.builder-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.builder-actions input { width: 220px; }

.builder-grid {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}
.controls {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  position: sticky;
  top: 76px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}
.control-block h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin: 0 0 12px;
  font-weight: 600;
}
.chart-type-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.chart-type-btn {
  background: var(--bg-deep);
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 10px 4px 8px;
  cursor: pointer;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  font: 500 11.5px var(--font-ui);
  transition: all 0.15s;
}
.chart-type-btn svg { width: 22px; height: 22px; }
.chart-type-btn:hover:not(:disabled) { border-color: var(--gold-dim); color: var(--text); }
.chart-type-btn.active { border-color: var(--gold); color: var(--gold-bright); background: rgba(212, 169, 55, 0.08); }
.chart-type-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ---------- Filters ---------- */
.filter-group { margin-bottom: 14px; }
.filter-group > .filter-label {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.filter-group .unit { color: var(--muted-dim); font-weight: 400; font-size: 12px; }
.check-list {
  max-height: 150px;
  overflow-y: auto;
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 6px;
  background: var(--bg-deep);
}
.check-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
.check-list label:hover { background: var(--panel-2); }
.check-list input[type="checkbox"] { accent-color: var(--gold); width: 15px; height: 15px; flex: none; }
.mini-btns { display: flex; gap: 8px; margin-top: 6px; }
.mini-btn {
  background: none; border: none; color: var(--gold-bright);
  font: 500 12px var(--font-ui); cursor: pointer; padding: 2px 0;
}
.mini-btn:hover { text-decoration: underline; }

/* Dual range slider */
.range-slider { position: relative; height: 34px; margin: 4px 2px 0; }
.range-slider input[type="range"] {
  position: absolute;
  left: 0; right: 0; top: 8px;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  pointer-events: none;
  height: 18px;
  margin: 0;
  padding: 0;
}
.range-slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  pointer-events: auto;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--gold-bright);
  border: 2px solid #0d1220;
  box-shadow: 0 0 0 1px var(--gold-dim);
  cursor: ew-resize;
  margin-top: 0;
}
.range-slider input[type="range"]::-moz-range-thumb {
  pointer-events: auto;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--gold-bright);
  border: 2px solid #0d1220;
  cursor: ew-resize;
}
.range-track {
  position: absolute;
  top: 15px; left: 0; right: 0;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}
.range-fill {
  position: absolute;
  top: 0; height: 100%;
  background: var(--gold);
  border-radius: 2px;
}
.range-values {
  display: flex;
  justify-content: space-between;
  font-size: 12.5px;
  color: var(--muted);
  margin-top: 2px;
}

/* ---------- Preview ---------- */
.preview {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 18px;
  min-height: 480px;
  display: flex;
  flex-direction: column;
}
#chart { width: 100%; height: 460px; }
#table-wrap { overflow: auto; max-height: 460px; }
#data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
#data-table th, #data-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-soft);
  white-space: nowrap;
}
#data-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  background: var(--panel);
}
#data-table th:hover { color: var(--gold-bright); }
#data-table tbody tr:hover { background: var(--panel-2); }
#preview-note { margin: 10px 0 0; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .builder-grid { grid-template-columns: 1fr; }
  .preview { order: -1; min-height: 380px; }
  #chart { height: 360px; }
  .controls { position: static; max-height: none; }
  .builder-actions input { width: 100%; flex: 1 1 100%; }
  .game-tiles { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
  .game-tile .code { font-size: 19px; }
  .brand h1 { font-size: 18px; }
}
