:root {
  --bg-0: #0d1117;
  --bg-1: #161b22;
  --bg-2: #1f2630;
  --bg-3: #2a323d;
  --border: #30363d;
  --border-bright: #4a5562;
  --text: #e6edf3;
  --text-dim: #9da7b3;
  --text-muted: #6e7681;
  --gold: #d4a72c;
  --gold-bright: #f1c232;
  --att: #f06560;
  --att-dim: #b0413d;
  --def: #5b9dff;
  --def-dim: #3a6bb0;
  --ok: #56d364;
  --warn: #e3b341;
  --shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

body {
  background:
    radial-gradient(ellipse 1200px 600px at 50% -200px, rgba(212, 167, 44, 0.08), transparent),
    var(--bg-0);
}

/* Header / nav */
header {
  border-bottom: 1px solid var(--border);
  background: rgba(13, 17, 23, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-bar {
  display: flex;
  align-items: stretch;
  gap: 24px;
  padding: 0 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.brand h1 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 0.5px;
  color: var(--gold-bright);
  text-transform: uppercase;
}

.brand .crest {
  width: 22px;
  height: 22px;
  align-self: center;
}

nav#tabs {
  display: flex;
  gap: 2px;
  flex: 1 1 auto;
  align-items: stretch;
  overflow-x: auto;
  scrollbar-width: none;
}

nav#tabs::-webkit-scrollbar { display: none; }

nav#tabs button {
  appearance: none;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  padding: 16px 16px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.4px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

nav#tabs button:hover { color: var(--text); }
nav#tabs button.active {
  color: var(--gold-bright);
  border-bottom-color: var(--gold);
}

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
}

.tab-panel {
  display: none;
  animation: fade 0.2s ease;
}
.tab-panel.active { display: block; }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Layout primitives */
.panel {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.row > .panel { flex: 1; min-width: 280px; }

.panel h2 {
  margin: 0 0 4px 0;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--gold-bright);
  font-weight: 600;
}

.panel .desc {
  margin: 0 0 16px 0;
  color: var(--text-muted);
  font-size: 13px;
}

/* Stats */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.stat {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
}

.stat .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.stat .value {
  font-family: var(--mono);
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}

.stat.attacker .value { color: var(--att); }
.stat.defender .value { color: var(--def); }
.stat.gold .value { color: var(--gold-bright); }

/* Inputs */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-muted);
  font-weight: 500;
}

.field input[type="number"], .field input[type="text"] {
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 16px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s;
}

.field input:focus {
  border-color: var(--gold);
}

button.primary, button.secondary {
  appearance: none;
  border: 1px solid var(--gold);
  background: var(--gold);
  color: #1a1a1a;
  padding: 10px 18px;
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}

button.primary:hover { background: var(--gold-bright); }
button.primary:active { transform: translateY(1px); }
button.primary:disabled { opacity: 0.5; cursor: not-allowed; }

button.secondary {
  background: transparent;
  color: var(--gold);
}

button.secondary:hover {
  background: rgba(212, 167, 44, 0.1);
}

button.danger {
  background: transparent;
  color: var(--att);
  border: 1px solid var(--att-dim);
  appearance: none;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  cursor: pointer;
}

button.danger:hover { background: rgba(240, 101, 96, 0.1); }

.controls-row {
  display: flex;
  align-items: end;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.controls-row .field { flex: 1; min-width: 100px; }

/* Battle Grid */
.grid-layout {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}

.grid-layout > .grid-sidebar {
  flex: 0 0 240px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
}

.grid-layout > .grid-sidebar > .controls-row {
  margin-bottom: 0;
  flex-direction: column;
  align-items: stretch;
}

.grid-layout > .grid-sidebar > .controls-row .field {
  flex: 0 0 auto;
  width: 100%;
  min-width: 0;
}

.grid-layout > #grid-canvas-wrap {
  flex: 1 1 auto;
  min-width: 0;
}

.grid-layout > .grid-sidebar > .stat-grid {
  grid-template-columns: 1fr;
}

@media (max-width: 900px) {
  .grid-layout { flex-direction: column; }
  .grid-layout > .grid-sidebar {
    flex: 0 0 auto;
    width: 100%;
  }
  .grid-layout > .grid-sidebar > .controls-row {
    flex-direction: row;
  }
  .grid-layout > .grid-sidebar > .controls-row .field { flex: 1 1 100px; }
  .grid-layout > .grid-sidebar > .stat-grid {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  }
}

#grid-canvas-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  overflow: hidden;
}

#grid-canvas {
  max-width: 100%;
  height: auto;
  cursor: crosshair;
  border-radius: 4px;
}

#grid-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--bg-3);
  border: 1px solid var(--border-bright);
  border-radius: 4px;
  padding: 6px 10px;
  font-family: var(--mono);
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.1s;
  box-shadow: var(--shadow);
  z-index: 20;
}

.legend {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 14px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-dim);
}

.legend .swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: middle;
}

/* Evolution tab */
.evo-canvas-wrap {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  justify-content: center;
}

#evo-canvas {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  image-rendering: pixelated;
}

#evo-ts-canvas {
  max-width: 100%;
  height: auto;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: block;
}

.evo-slider-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
}

.evo-slider-label {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--gold-bright);
  min-width: 90px;
}

.evo-slider-row input[type="range"] {
  flex: 1;
  accent-color: var(--gold);
}

/* Distribution chart */
.dist-chart {
  display: flex;
  align-items: stretch;
  gap: 4px;
  height: 200px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.dist-bar {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
  min-width: 24px;
  position: relative;
}

.dist-bar .bar {
  width: 100%;
  background: linear-gradient(to top, var(--att-dim), var(--att));
  border-radius: 3px 3px 0 0;
  transition: height 0.2s;
  min-height: 1px;
  position: relative;
}

.dist-bar.defender .bar {
  background: linear-gradient(to top, var(--def-dim), var(--def));
}

.dist-bar .pct {
  font-size: 10px;
  color: var(--text-muted);
  font-family: var(--mono);
  position: absolute;
  bottom: 100%;
  margin-bottom: 4px;
  white-space: nowrap;
}

.dist-bar .lbl {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--mono);
  margin-top: 4px;
}

.chart-section {
  margin-top: 16px;
}

.chart-section h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 0 0 8px 0;
  color: var(--text-dim);
  font-weight: 600;
}

/* Campaign / territories */
.territory-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.territory {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.territory .num {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-muted);
  width: 24px;
  text-align: center;
}

.territory .name {
  font-weight: 500;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 4px 0;
  outline: none;
  border-bottom: 1px dashed transparent;
}

.territory .name:focus { border-bottom-color: var(--gold); }

.territory .defenders {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--def);
  font-family: var(--mono);
}

.territory .defenders input {
  width: 50px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--def);
  font-family: var(--mono);
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 4px;
  text-align: center;
  outline: none;
}

/* Simulator dice */
.dice-row {
  display: flex;
  gap: 16px;
  margin: 16px 0;
  align-items: center;
}

.dice-side {
  flex: 1;
  text-align: center;
}

.dice-side h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin: 0 0 8px 0;
  color: var(--text-muted);
}

.dice-side.attacker h3 { color: var(--att); }
.dice-side.defender h3 { color: var(--def); }

.dice-set {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.die {
  width: 48px;
  height: 48px;
  background: var(--bg-3);
  border: 2px solid var(--border-bright);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  font-family: var(--mono);
  color: var(--text);
  transition: transform 0.1s;
}

.die.att { border-color: var(--att-dim); color: var(--att); }
.die.def { border-color: var(--def-dim); color: var(--def); }

.die.win { box-shadow: 0 0 12px rgba(86, 211, 100, 0.6); border-color: var(--ok); }
.die.lose { opacity: 0.5; }

.die.rolling { animation: shake 0.4s; }

@keyframes shake {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-15deg) translateY(-4px); }
  50% { transform: rotate(15deg) translateY(2px); }
  75% { transform: rotate(-10deg); }
}

.battle-state {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 24px;
  font-family: var(--mono);
  font-size: 18px;
  margin: 12px 0;
}

.battle-state .att-count { color: var(--att); }
.battle-state .def-count { color: var(--def); }
.battle-state .vs { color: var(--text-muted); font-size: 14px; }

.log {
  max-height: 220px;
  overflow-y: auto;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-dim);
}

.log .entry { padding: 2px 0; }
.log .entry.att-loss { color: var(--att); }
.log .entry.def-loss { color: var(--def); }

/* Odds reference */
.odds-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
}

.odds-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
}

.odds-card h3 {
  margin: 0 0 4px 0;
  font-family: var(--mono);
  color: var(--gold-bright);
  font-size: 16px;
}

.odds-card .sub {
  margin: 0 0 12px 0;
  color: var(--text-muted);
  font-size: 12px;
}

.odds-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 6px 0;
  font-size: 13px;
}

.odds-row .pct {
  font-family: var(--mono);
  color: var(--gold-bright);
  font-weight: 600;
}

.odds-bar {
  grid-column: 1 / -1;
  height: 6px;
  background: var(--bg-3);
  border-radius: 3px;
  overflow: hidden;
}

.odds-bar .fill {
  height: 100%;
  border-radius: 3px;
}

.odds-bar .fill.both { background: linear-gradient(90deg, var(--att) 50%, var(--def) 50%); }
.odds-bar .fill.att { background: var(--att); }
.odds-bar .fill.def { background: var(--def); }

/* Roll matrix (per-card disclosure) */
.odds-matrix-wrap {
  margin-top: 14px;
  border-top: 1px dashed var(--border);
  padding-top: 10px;
}

.odds-matrix-wrap > summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-dim);
  user-select: none;
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.odds-matrix-wrap > summary::-webkit-details-marker { display: none; }

.odds-matrix-wrap > summary::before {
  content: '▸';
  display: inline-block;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform 0.15s ease;
}

.odds-matrix-wrap[open] > summary::before { transform: rotate(90deg); }

.odds-matrix-wrap > summary:hover { color: var(--gold-bright); }

.odds-matrix-cap {
  margin: 8px 0;
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.45;
}

.odds-matrix-scroll {
  overflow-x: auto;
  padding-bottom: 4px;
  position: relative;
}

.odds-matrix {
  --mc-size: 18px;
  display: grid;
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1;
  width: max-content;
  border: 1px solid var(--border);
}

.odds-matrix .mc-cell,
.odds-matrix .mc-head,
.odds-matrix .mc-rh,
.odds-matrix .mc-corner {
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid rgba(48, 54, 61, 0.55);
  border-bottom: 1px solid rgba(48, 54, 61, 0.55);
  overflow: hidden;
}

.odds-matrix .mc-corner,
.odds-matrix .mc-head,
.odds-matrix .mc-rh {
  background: var(--bg-3);
  color: var(--text-dim);
  font-weight: 600;
}

.odds-matrix .mc-label {
  color: var(--text);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0 6px;
}

.odds-matrix .mc-count {
  color: var(--gold-bright);
  font-size: 9px;
  font-weight: 700;
}

.odds-matrix .mc-rh.mc-count {
  padding: 0 4px;
  min-width: var(--mc-size);
}

.odds-matrix .mc-cell {
  color: var(--text);
  font-weight: 600;
}

.odds-matrix .mc-cell.def { background: rgba(86, 211, 100, 0.38); color: #b9f0c4; }
.odds-matrix .mc-cell.att { background: rgba(240, 101, 96, 0.38); color: #ffc8c4; }
.odds-matrix .mc-cell.both { background: rgba(110, 118, 129, 0.20); color: #c2c8d1; }

/* Matrix → outcome-bar reduction */
.odds-reduce-btn {
  margin: 8px 0;
  padding: 4px 10px;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--border-bright);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 11px;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.odds-reduce-btn:hover {
  color: var(--gold-bright);
  border-color: var(--gold);
}

.odds-reduce-overlay {
  position: absolute;
  pointer-events: none;
  opacity: 0;
  transition: opacity 600ms ease 2200ms;
}

.odds-reduce-overlay.shown { opacity: 1; }

.odds-reduce-seg {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  font-family: var(--mono);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  overflow: hidden;
  padding: 0 8px;
}

.odds-reduce-pct {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.1;
}

.odds-reduce-name {
  font-size: 10px;
  margin-top: 2px;
  opacity: 0.85;
}

.odds-reduce-seg.def { color: #d6f5dd; }
.odds-reduce-seg.att { color: #ffd7d4; }
.odds-reduce-seg.both { color: #d8dde6; }

/* Reductions tab — presentation-mode roll matrix. */
.red-panel { padding-bottom: 32px; }

.red-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 16px 0 4px;
}

.red-pills button {
  padding: 6px 14px;
  background: var(--bg-3);
  color: var(--text-dim);
  border: 1px solid var(--border-bright);
  border-radius: 999px;
  font-family: var(--mono);
  font-size: 13px;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.red-pills button:hover { color: var(--gold-bright); border-color: var(--gold); }
.red-pills button.active {
  background: var(--gold);
  border-color: var(--gold-bright);
  color: var(--bg-1, #0d1117);
}

.red-subtitle {
  margin: 6px 0 18px;
  font-size: 13px;
  color: var(--text-muted);
  font-family: var(--mono);
}

.red-stage {
  display: flex;
  justify-content: center;
  padding: 24px 0;
  min-height: 200px;
}

.red-matrix-wrap {
  position: relative;
  overflow: visible;
}

.red-controls {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}

.red-toggle {
  padding: 10px 22px;
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border-bright);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.red-toggle:hover {
  color: var(--gold-bright);
  border-color: var(--gold);
  background: var(--bg-2, #161b22);
}

/* Help text */
.hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
}

.hint code {
  background: var(--bg-2);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: var(--mono);
  color: var(--gold-bright);
}

/* Mobile */
@media (max-width: 700px) {
  main { padding: 12px; }
  .panel { padding: 14px; }
  .header-bar { flex-direction: column; gap: 0; padding: 0; align-items: stretch; }
  .brand { padding: 12px 16px 4px; }
  nav#tabs { padding: 0 8px; }
  nav#tabs button { padding: 10px 12px; font-size: 12px; }
  .stat .value { font-size: 18px; }
  .die { width: 40px; height: 40px; font-size: 20px; }
}
