:root {
  --bg: #12131c;
  --panel: #1b1d2a;
  --panel-border: #2e3146;
  --text: #eceaf4;
  --muted: #a4a3b8;
  --accent: #f2a65a;
  --focus: #8fd3ff;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4;
}

header, footer {
  padding: 1rem 1.25rem 0.5rem;
}

h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: normal;
  letter-spacing: 0.02em;
}

h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--accent);
  margin: 1rem 0 0.25rem;
}

.subtitle, footer {
  color: var(--muted);
  margin: 0.25rem 0 0;
  max-width: 60rem;
}

footer {
  font-size: 0.9rem;
}

main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 21rem;
  gap: 1rem;
  padding: 0.5rem 1.25rem 1rem;
  align-items: start;
}

.lattice-wrap {
  width: min(100%, calc(100vh - 11rem));
  min-width: 16rem;
  aspect-ratio: 1;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  overflow: hidden;
}

#lattice {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
  cursor: crosshair;
}

.stage-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
  max-width: min(100%, calc(100vh - 11rem));
}

.status {
  margin: 0;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.4rem 0 0;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

fieldset {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  margin: 0;
  min-width: 0;
  padding: 0.6rem 0.8rem 0.8rem;
}

legend {
  padding: 0 0.3rem;
  color: var(--accent);
  font-weight: 600;
}

.readout {
  margin: 0 0 0.25rem;
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.control-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.25rem 0.5rem;
  margin: 0.35rem 0;
}

.control-row input[type="range"] {
  grid-column: 1 / -1;
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.4rem;
}

select {
  min-width: 0;
  max-width: 100%;
}

button, select {
  font: inherit;
  color: var(--text);
  background: #262a3c;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 0.3rem 0.6rem;
}

button:hover {
  border-color: var(--accent);
}

button[aria-pressed="true"] {
  background: var(--accent);
  color: #1b1d2a;
}

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

.stats {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.2rem 0.75rem;
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.stats dt {
  color: var(--muted);
}

.stats dd {
  margin: 0;
  text-align: right;
}

.chart-block {
  max-width: min(100%, calc(100vh - 11rem));
}

.chart {
  display: block;
  width: 100%;
  height: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
}

.legend {
  margin: 0.3rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.key {
  display: inline-block;
  width: 1.2em;
  height: 0.25em;
  vertical-align: middle;
  margin: 0 0.25em 0 0.6em;
  border-radius: 2px;
}

.key:first-child {
  margin-left: 0;
}

.key-first {
  background: var(--accent);
}

.key-second {
  background: var(--focus);
}

progress {
  width: 100%;
  margin-top: 0.5rem;
  accent-color: var(--accent);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (max-width: 52rem) {
  main {
    grid-template-columns: minmax(0, 1fr);
  }

  .lattice-wrap, .stage-bar, .chart-block {
    width: 100%;
    max-width: 100%;
  }
}
