:root {
  --bg: #070b16;
  --bg-2: #0b1122;
  --panel: #0e1529;
  --panel-2: #121b33;
  --line: #1e2a47;
  --line-2: #2a3a60;
  --text: #e2e8f0;
  --text-2: #94a3b8;
  --muted: #64748b;
  --accent: #22d3ee;
  --accent-2: #f472b6;
  --good: #4ade80;
  --bad: #f87171;
  --warn: #fbbf24;
  --core-empty: #111a30;
  --radius: 10px;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(34, 211, 238, 0.07), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(244, 114, 182, 0.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--sans);
  min-height: 100vh;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 20px; border-bottom: 1px solid var(--line);
  background: rgba(7, 11, 22, 0.8); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); }
.brand-mark { display: grid; grid-template-columns: 8px 8px; gap: 2px; }
.brand-mark i { width: 8px; height: 8px; border-radius: 2px; background: #334155; }
.brand-mark i:nth-child(1) { background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.brand-mark i:nth-child(4) { background: var(--accent-2); box-shadow: 0 0 8px var(--accent-2); }
.brand-name { font: 700 18px/1 var(--mono); letter-spacing: 0.12em; }
.brand-name b { color: var(--accent); font-weight: 700; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a {
  color: var(--text-2); text-decoration: none; padding: 6px 12px; border-radius: 8px; font-weight: 500;
}
.nav a:hover { color: var(--text); background: var(--panel-2); }
.nav a[aria-current="page"] { color: var(--text); background: var(--panel-2); }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(340px, 420px) minmax(0, 1fr) 300px;
  gap: 16px; padding: 16px 20px 32px; align-items: start;
}
.panel {
  background: linear-gradient(180deg, var(--panel), var(--bg-2));
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; min-width: 0;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
h2, h3 { margin: 0; font-weight: 600; letter-spacing: 0.01em; }
h2 { font-size: 15px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-2); margin-bottom: 8px; }

@media (max-width: 1280px) {
  .layout { grid-template-columns: minmax(320px, 400px) minmax(0, 1fr); }
  .side-panel { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px 24px; }
  .arena-panel { order: -1; }
  .topbar { padding: 10px 16px; }
}

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--bg-2);
  border-radius: 8px; padding: 4px 10px; cursor: pointer; color: var(--text-2); font-size: 13px; max-width: 150px;
}
.tab span.label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.tab[aria-selected="true"] { color: var(--text); border-color: var(--line-2); background: var(--panel-2); }
.tab.err .label { color: var(--bad); }
.tab.add { color: var(--muted); }

/* ---------- form controls ---------- */
.editor-toolbar { display: flex; gap: 8px; align-items: end; margin-bottom: 10px; }
.editor-toolbar .field { flex: 1; min-width: 0; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
select, input[type="number"] {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; width: 100%; min-width: 0;
}
input[type="number"] { width: 90px; }
.settings-grid input[type="number"] { width: 100%; }
select:focus, input:focus, textarea:focus, button:focus-visible { outline: 2px solid rgba(34, 211, 238, 0.5); outline-offset: 1px; }

.btn {
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 8px; padding: 7px 14px;
  cursor: pointer; font-weight: 500; white-space: nowrap; transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #1a2646; border-color: #3a4f80; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: linear-gradient(180deg, #0e7490, #0b5c72); border-color: #22d3ee66; color: #ecfeff; min-width: 96px; }
.btn.primary:hover { background: linear-gradient(180deg, #0891b2, #0e7490); }
.btn.ghost { background: transparent; }
.icon-btn {
  background: transparent; border: 1px solid var(--line); border-radius: 8px; width: 34px; height: 34px;
  cursor: pointer; color: var(--muted); flex: none;
}
.icon-btn:hover { color: var(--bad); border-color: var(--bad); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }

/* ---------- code editor ---------- */
.code-editor {
  display: flex; border: 1px solid var(--line); border-radius: 8px; background: #060a14; overflow: hidden;
  height: min(52vh, 520px); min-height: 260px;
}
.gutter {
  flex: none; width: 42px; padding: 10px 6px 10px 0; text-align: right; color: #3b4a6b;
  font: 12.5px/1.6 var(--mono); overflow: hidden; user-select: none; background: #080d1a; border-right: 1px solid var(--line);
  white-space: pre;
}
.gutter .err { color: var(--bad); background: rgba(248, 113, 113, 0.12); display: block; }
.code-editor textarea {
  flex: 1; min-width: 0; resize: none; border: 0; background: transparent; padding: 10px 12px;
  font: 12.5px/1.6 var(--mono); color: #cbd5e1; tab-size: 8; white-space: pre; overflow: auto;
}
.code-editor textarea:focus { outline: none; }
.code-editor:focus-within { border-color: var(--line-2); }

.compile-status { margin-top: 8px; font: 12px/1.5 var(--mono); min-height: 18px; }
.compile-status .ok { color: var(--good); }
.compile-status .bad { color: var(--bad); }
.compile-status .pending { color: var(--warn); }
.compile-status ul { margin: 4px 0 0; padding-left: 18px; color: #fca5a5; }
.strategy { color: var(--text-2); font-size: 13px; margin: 8px 0 0; white-space: pre-line; }
.strategy:empty { display: none; }
.generated { margin-top: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg-2); }
.generated summary { cursor: pointer; padding: 8px 10px; font-size: 13px; color: var(--text-2); }
.generated pre { margin: 0; padding: 10px 12px; font: 12px/1.55 var(--mono); max-height: 260px; overflow: auto; border-top: 1px solid var(--line); color: #a5b4cf; }

/* ---------- arena ---------- */
.scoreboard { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin-bottom: 12px; }
.fighter {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--bg-2);
  border-left: 3px solid var(--c); position: relative; overflow: hidden;
}
.fighter.dead { opacity: 0.45; }
.fighter.winner { box-shadow: 0 0 0 1px var(--c), 0 0 24px -6px var(--c); }
.fighter .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fighter .meta { display: flex; justify-content: space-between; font: 12px var(--mono); color: var(--text-2); margin-top: 2px; }
.fighter .bar { height: 3px; background: var(--line); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.fighter .bar i { display: block; height: 100%; background: var(--c); width: 0; transition: width 0.2s; }
.fighter .badge { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; }

.core-wrap { position: relative; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: #050812; }
#core { display: block; width: 100%; cursor: crosshair; image-rendering: pixelated; }
.overlay {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  background: rgba(5, 8, 18, 0.72); backdrop-filter: blur(2px); font-size: 15px;
}
.overlay[hidden] { display: none; }
.overlay .big { font: 700 26px/1.2 var(--mono); letter-spacing: 0.04em; }
.overlay .sub { color: var(--text-2); margin-top: 6px; }

.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
.speed { display: flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 12px; flex: 1; min-width: 200px; }
.speed input { flex: 1; accent-color: var(--accent); min-width: 80px; }
.speed output { font: 12px var(--mono); color: var(--text); min-width: 74px; }
.cycle { font: 600 13px var(--mono); color: var(--text-2); margin-left: auto; }
.progress { height: 3px; background: var(--line); border-radius: 2px; margin-top: 10px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
.progress.small { margin-top: 10px; }

.chart-wrap { margin-top: 14px; }
.chart-head { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.sw.code { background: var(--accent); }
.sw.bomb { background: radial-gradient(circle, var(--accent) 35%, var(--core-empty) 40%); }
.sw.pc { background: #fff; box-shadow: 0 0 6px #fff; }
#chart { display: block; width: 100%; height: 90px; border: 1px solid var(--line); border-radius: 8px; background: #050812; }

/* ---------- side panel ---------- */
.side-panel { display: flex; flex-direction: column; gap: 12px; padding: 0; background: none; border: 0; }
.card { background: linear-gradient(180deg, var(--panel), var(--bg-2)); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.inspector { margin: 0; font: 12px/1.6 var(--mono); white-space: pre; overflow-x: auto; color: #a5b4cf; min-height: 40px; }
.inspector .cur { color: #fff; background: rgba(255, 255, 255, 0.07); display: inline-block; width: 100%; }
.log { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow: auto; font: 12px/1.55 var(--mono); color: var(--text-2); }
.log li { padding: 2px 0; border-bottom: 1px dashed #18223b; }
.log li b { font-weight: 600; }
/* Collapsible cards */
.fold > summary { cursor: pointer; list-style: none; display: flex; justify-content: space-between; align-items: center; }
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary h3 { display: inline; margin: 0; }
.fold > summary::after { content: '▸'; color: var(--muted); }
.fold[open] > summary::after { content: '▾'; }
.fold[open] > summary { margin-bottom: 8px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }

table.results { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 12px; }
table.results th, table.results td { text-align: right; padding: 4px 4px; border-bottom: 1px solid var(--line); }
table.results th:first-child, table.results td:first-child { text-align: left; }
table.results th { color: var(--muted); font-weight: 500; }
table.results td.name { display: flex; align-items: center; gap: 6px; max-width: 120px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
table.results .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
table.results tr.you td { color: var(--text); font-weight: 600; }
table.results.ladder th:nth-child(2), table.results.ladder td:nth-child(2) { text-align: left; }
table.results.ladder td.name { max-width: 150px; }
.ladder-status { min-height: 18px; margin: 8px 0 0; color: var(--text-2); }

/* Species map: a grid of behaviour cells with a dot per warrior */
.species-grid { display: grid; gap: 3px; align-items: stretch; }
.sp-y { font: 10px/1 var(--mono); color: var(--muted); display: flex; align-items: center; justify-content: flex-end; padding-right: 4px; white-space: nowrap; }
.sp-x { font: 10px/1.2 var(--mono); color: var(--muted); text-align: center; padding-top: 3px; overflow-wrap: anywhere; }
.sp-cell { aspect-ratio: 1; min-width: 0; border: 1px solid var(--line); border-radius: 5px; background: var(--bg); padding: 3px; cursor: pointer;
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 2px; overflow: hidden; }
.sp-cell.used { background: var(--panel-2); }
.sp-cell.sel { border-color: var(--accent); }
.sp-cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.sp-lib, .sp-you { display: inline-block; width: 6px; height: 6px; border-radius: 50%; flex: none; }
.sp-lib { background: var(--muted); }
.sp-you { box-shadow: 0 0 0 1px var(--bg); }
.sp-axes { margin: 8px 0 0; }
.sp-axes i { vertical-align: middle; margin: 0 2px 0 6px; }
.species-info { margin: 8px 0 0; line-height: 1.6; color: var(--text-2); }
.species-info .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }

/* ---------- import ---------- */
.import-btn { padding: 6px 10px; flex: none; }
.origin { margin: -4px 0 10px; font-size: 12px; color: var(--text-2); }
.origin a { word-break: break-all; }
.code-editor.dropping { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(34, 211, 238, 0.25); }
.rules-hint { margin-top: 6px; font-family: var(--sans); }
.rules-hint .btn { padding: 3px 10px; font-size: 12px; margin-left: 6px; }
.dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line-2); border-radius: 12px;
  padding: 18px 20px; width: min(520px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(3, 6, 14, 0.7); backdrop-filter: blur(2px); }
.dialog h3 { color: var(--text); text-transform: none; letter-spacing: 0; font-size: 17px; }
.dialog .field { margin-top: 12px; }
.dialog input[type="url"], .dialog input[type="file"] { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; width: 100%; }
.dialog code { font: 12px var(--mono); }
.import-status { min-height: 18px; font-size: 13px; margin: 10px 0 0; white-space: pre-line; }
.import-status.bad { color: var(--bad); }
.import-status.pending { color: var(--warn); }

/* ---------- post-mortem ---------- */
.postmortem { border-color: var(--line-2); }
.pm-summary { margin: 0 0 6px; font-size: 13px; line-height: 1.55; color: var(--text); }
.pm-timeline { list-style: none; margin: 10px 0 0; padding: 0; font-size: 12px; color: var(--text-2); }
.pm-timeline li { padding: 3px 0 3px 10px; border-left: 2px solid var(--line-2); margin-left: 2px; }
.pm-timeline li b { font-family: var(--mono); color: var(--text); font-weight: 600; }
.pm-timeline li.eliminated { border-left-color: var(--bad); }
.pm-timeline li.conversion { border-left-color: var(--warn); }
.pm-details { margin-top: 10px; }
.pm-details summary { cursor: pointer; font-size: 12px; color: var(--text-2); }
.pm-w { margin-top: 10px; font-size: 12px; }
.pm-w h4 { margin: 0 0 4px; font-size: 13px; }
.pm-w ul { margin: 2px 0 6px; padding-left: 16px; color: var(--text-2); }
.pm-code { width: 100%; border-collapse: collapse; font: 11px/1.5 var(--mono); }
.pm-code td { padding: 1px 4px; border-bottom: 1px solid #18223b; white-space: nowrap; }
.pm-code tr.changed td { color: #fca5a5; }
.pm-code td.by { color: var(--muted); font-family: var(--sans); }
.pm-code-wrap { overflow-x: auto; }
.replay-tag { color: var(--warn); font-weight: 600; }

/* ---------- convert ---------- */
.convert-row { margin: 8px 0 0; font-size: 12px; color: var(--text-2); }
.linkbtn { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: 12px; text-decoration: underline; text-underline-offset: 2px; }
.linkbtn:disabled { color: var(--muted); cursor: wait; }

/* ---------- footer ---------- */
.site-footer { max-width: 1180px; margin: 0 auto; padding: 24px 20px 40px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
.site-footer a { color: var(--text-2); }

/* ---------- icons ---------- */
.icon { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.btn, .icon-btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.icon-btn .icon { width: 16px; height: 16px; }
.menu-btn { display: none; }
.tabbar, .cell-peek { display: none; }

/* ---------- symbol bar (touch screens, while editing) ---------- */
.keybar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: flex; gap: 6px; padding: 6px 8px;
  overflow-x: auto; background: #0b1122f2; border-top: 1px solid var(--line-2); backdrop-filter: blur(6px);
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.keybar[hidden] { display: none; }
.keybar button {
  flex: none; min-width: 40px; height: 40px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--panel-2); color: var(--text); font: 600 16px var(--mono); touch-action: manipulation;
}
.keybar button.word { font-size: 13px; color: var(--accent); }
.keybar .icon { width: 18px; height: 18px; }

/* ---------- touch screens ---------- */
@media (pointer: coarse) {
  .btn, .icon-btn, .tab { min-height: 44px; }
  .icon-btn { width: 44px; }
  /* 16px or more stops iOS from zooming into form fields */
  select, input[type="number"], input[type="url"], textarea { font-size: 16px; }
  .speed input { height: 32px; }
}

/* ---------- phones: three views and a bottom tab bar ---------- */
@media (max-width: 860px) {
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .topbar { padding: 8px 12px; position: sticky; }
  .brand-name { font-size: 16px; }
  .menu-btn { display: inline-flex; border-color: var(--line-2); color: var(--text-2); }
  .nav {
    display: none; position: absolute; top: calc(100% + 4px); right: 12px; z-index: 30; flex-direction: column;
    min-width: 200px; padding: 6px; background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  }
  .nav.open { display: flex; }
  .nav a { padding: 10px 12px; }

  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding-bottom: env(safe-area-inset-bottom); background: #0b1122f5; border-top: 1px solid var(--line-2); backdrop-filter: blur(8px);
  }
  .tabbar button {
    flex: 1; min-height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    background: none; border: 0; color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: 0.03em; position: relative; cursor: pointer;
  }
  .tabbar button .icon { width: 22px; height: 22px; }
  .tabbar button[aria-current="page"] { color: var(--accent); }
  .tabbar .badge { position: absolute; top: 9px; left: calc(50% + 8px); width: 9px; height: 9px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 8px var(--accent-2); }
  .tabbar .badge[hidden] { display: none; }

  .layout { padding: 10px 10px 16px; gap: 10px; }
  .layout > .panel, .layout > .side-panel { display: none; }
  body[data-view="arena"] .arena-panel,
  body[data-view="code"] .editor-panel { display: block; }
  body[data-view="details"] .side-panel { display: flex; }
  .panel { padding: 12px; }

  /* Arena */
  .scoreboard { grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 8px; }
  .fighter { padding: 6px 8px; }
  .controls { gap: 6px; margin-top: 8px; }
  .controls .btn .label { display: none; }
  .controls .btn { min-width: 52px; min-height: 44px; padding: 0 12px; }
  .controls .btn .icon { width: 20px; height: 20px; }
  .speed { order: 5; flex-basis: 100%; }
  .cycle { margin-left: auto; font-size: 12px; }
  .cell-peek { display: block; margin: 6px 0 0; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg-2); font: 12px/1.5 var(--mono); color: var(--text-2); }
  .cell-peek[hidden] { display: none; }
  .cell-peek b { color: var(--text); font-weight: 600; }
  .chart-head .legend { display: none; }

  /* Code */
  .editor-toolbar { flex-wrap: wrap; }
  .editor-toolbar .field:first-child { flex-basis: 100%; }
  .import-btn .label { display: none; }
  .import-btn { width: 44px; padding: 0; }
  .code-editor { height: calc(100dvh - 330px); min-height: 260px; }
  .generated pre { max-height: 200px; }

  /* Details */
  .side-panel { display: none; flex-direction: column; gap: 10px; }
}
/* While editing on a touch screen the symbol bar takes the tab bar's place. */
body.editing .tabbar { display: none; }
