*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --bg-page: #0b1120;
  --bg-card: #111827;
  --bg-elev: #1e293b;
  --border-subtle: #334155;
  --text-main: #f8fafc;
  --text-muted: #cbd5e1;
  --accent-sky: #7dd3fc;
  --btn-blue: #0369a1;
  --btn-blue-hover: #0284c7;
  --btn-amber: #c2410c;
  --btn-amber-hover: #9a3412;
  --focus-ring: #38bdf8;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-page);
  color: var(--text-main);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.38;
}

body {
  max-width: 1024px;
  margin: 0 auto;
  padding: 7px 10px 22px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.site-header {
  margin-bottom: 7px;
}

.header-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 3px;
}

.site-header h1 {
  font-size: 1.24rem;
  margin: 0;
  color: #ffffff;
  letter-spacing: -0.01em;
}

.pill-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: #0c2d48;
  border: 1px solid #38bdf8;
  color: #e0f2fe;
  font-size: 0.74rem;
  font-weight: 600;
}

.lede {
  margin: 0;
  font-size: 0.81rem;
  color: var(--text-muted);
}

.lede code, .matrix-top code {
  background: var(--bg-elev);
  color: #e0f2fe;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.84em;
}

.stage-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.32fr) minmax(0, 1fr);
  gap: 9px;
  align-items: start;
  isolation: isolate;
}

.island-panel,
.control-panel,
.matrix-section {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 8px;
  min-width: 0;
}

.canvas-wrap {
  width: 100%;
  background: #070c18;
  border: 1px solid #1e293b;
  border-radius: 8px;
  overflow: hidden;
}

#island-canvas {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
  cursor: pointer;
}

#island-canvas:focus-visible,
button:focus-visible,
select:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.transport-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  margin-top: 7px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  color: #ffffff;
  background: var(--bg-elev);
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

.btn:hover {
  border-color: var(--accent-sky);
}

.btn-primary {
  background: var(--btn-blue);
  border-color: #38bdf8;
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--btn-blue-hover);
}

.btn-secondary {
  background: #1e293b;
  border-color: #475569;
  color: #f8fafc;
}

.btn-ghost {
  background: #0f172a;
  border-color: #334155;
  color: #e2e8f0;
  padding: 5px 7px;
  font-size: 0.72rem;
}

.btn-accent {
  background: var(--btn-amber);
  border-color: #fb923c;
  color: #ffffff;
}

.btn-accent:hover {
  background: var(--btn-amber-hover);
}

.btn-tiny {
  padding: 3px 7px;
  font-size: 0.70rem;
  background: #0c2d48;
  border-color: #38bdf8;
  color: #e0f2fe;
}

.step-pill {
  margin-left: auto;
  font-size: 0.73rem;
  font-variant-numeric: tabular-nums;
  padding: 3px 8px;
  border-radius: 999px;
  background: #0f172a;
  border: 1px solid #334155;
  color: #e2e8f0;
  font-weight: 600;
}

.canvas-hint {
  margin: 5px 0 0;
  font-size: 0.73rem;
  color: var(--text-muted);
}

.status-box {
  background: #0f172a;
  border: 1px solid #334155;
  border-left: 3px solid #38bdf8;
  border-radius: 7px;
  padding: 5px 8px;
  margin-bottom: 6px;
}

.status-box strong {
  display: block;
  font-size: 0.79rem;
  color: #ffffff;
  margin-bottom: 1px;
}

.status-box p {
  margin: 0;
  font-size: 0.73rem;
  color: var(--text-muted);
}

.control-row-two {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 0.85fr);
  gap: 6px;
  margin-bottom: 6px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.field-group label {
  font-size: 0.71rem;
  font-weight: 600;
  color: var(--text-muted);
}

select, input[type="range"] {
  width: 100%;
  min-width: 0;
}

select {
  background: #0f172a;
  color: #f8fafc;
  border: 1px solid #475569;
  border-radius: 6px;
  padding: 4px 5px;
  font-size: 0.73rem;
}

input[type="range"] {
  accent-color: #38bdf8;
  margin-top: 4px;
}

.roster-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.74rem;
  font-weight: 700;
  color: #e2e8f0;
  margin-bottom: 4px;
}

.mini-count {
  font-size: 0.70rem;
  padding: 1px 7px;
  border-radius: 999px;
  background: #0c2d48;
  border: 1px solid #38bdf8;
  color: #e0f2fe;
}

.tier-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-bottom: 5px;
}

.tier-tab {
  appearance: none;
  background: #0f172a;
  color: #cbd5e1;
  border: 1px solid #334155;
  border-radius: 5px;
  padding: 2px 6px;
  font-size: 0.68rem;
  font-weight: 600;
  cursor: pointer;
}

.tier-tab:hover {
  border-color: #7dd3fc;
}

.tier-tab.active {
  background: #0369a1;
  border-color: #7dd3fc;
  color: #ffffff;
}

.monster-roster {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  max-height: 162px;
  overflow-y: auto;
  padding-right: 2px;
  margin-bottom: 7px;
}

.monster-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  padding: 3px 5px;
  border-radius: 6px;
  border: 1px solid #334155;
  background: #0f172a;
  color: #cbd5e1;
  cursor: pointer;
  min-width: 0;
}

.monster-btn[aria-pressed="true"] {
  background: #0c2d48;
  border-color: #38bdf8;
  color: #e0f2fe;
}

.monster-btn.quint-card[aria-pressed="true"] {
  border-color: #fbbf24;
}

.monster-btn-top {
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  gap: 3px;
  font-size: 0.69rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.monster-state-tag {
  font-size: 0.62rem;
  padding: 0 4px;
  border-radius: 4px;
  background: #1e293b;
  color: #f8fafc;
  flex-shrink: 0;
}

.monster-btn[aria-pressed="true"] .monster-state-tag {
  background: #0369a1;
  color: #ffffff;
}

.monster-btn-sub {
  font-size: 0.63rem;
  color: #cbd5e1;
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.hatchery-card {
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 6px;
}

.hatchery-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 5px;
  margin-bottom: 4px;
}

.hatchery-title {
  font-size: 0.73rem;
  font-weight: 700;
  color: #fde68a;
}

.hatchery-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 5px;
  align-items: end;
}

.hatch-note {
  margin: 4px 0 5px;
  font-size: 0.70rem;
  color: var(--text-muted);
}

.recipe-grimoire {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 3px;
  max-height: 106px;
  overflow-y: auto;
  padding-right: 2px;
}

.recipe-chip {
  appearance: none;
  border: 1px solid #334155;
  border-radius: 5px;
  background: #111827;
  color: #cbd5e1;
  padding: 2px 3px;
  font-size: 0.62rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recipe-chip.discovered {
  background: #0c2d48;
  border-color: #38bdf8;
  color: #e0f2fe;
}

.recipe-chip.quint-chip {
  grid-column: span 2;
  border-color: #fbbf24;
}

.recipe-chip:hover {
  border-color: #fde047;
}

.matrix-section {
  margin-top: 9px;
}

.matrix-top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.matrix-top h2 {
  margin: 0;
  font-size: 0.94rem;
  color: #ffffff;
}

.matrix-sub {
  margin: 2px 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.section-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 7px;
}

.sec-pill-btn {
  appearance: none;
  background: #0f172a;
  color: #e2e8f0;
  border: 1px solid #334155;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
}

.sec-pill-btn:hover {
  border-color: #7dd3fc;
}

.sec-pill-btn.active {
  background: #0369a1;
  border-color: #7dd3fc;
  color: #ffffff;
}

.wav-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.wav-link {
  color: var(--accent-sky);
  font-size: 0.79rem;
  font-weight: 700;
  text-decoration: underline;
}

.wav-player {
  width: 100%;
  height: 34px;
  margin-bottom: 7px;
}

.step-matrix {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-x: auto;
  max-height: 340px;
  overflow-y: auto;
  padding-bottom: 4px;
}

.matrix-row {
  display: grid;
  grid-template-columns: 128px repeat(var(--cols, 32), minmax(13px, 1fr));
  gap: 2px;
  align-items: center;
  min-width: 560px;
}

.matrix-label {
  font-size: 0.69rem;
  font-weight: 600;
  color: #e2e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 2px 5px;
  border-radius: 4px;
  background: #0f172a;
  border: 1px solid #334155;
  text-align: left;
  cursor: pointer;
}

.matrix-label.muted {
  color: #94a3b8;
  opacity: 0.74;
}

.step-cell {
  height: 14px;
  border-radius: 2px;
  background: #0f172a;
  border: 1px solid #1e293b;
}

.step-cell.beat-start {
  border-left: 2px solid #475569;
}

.step-cell.bar-start {
  border-left: 2px solid #f59e0b;
}

.step-cell.has-note {
  background: #1e3a5f;
  border-color: #38bdf8;
}

.step-cell.has-note.muted-note {
  background: #1e293b;
  border-color: #475569;
}

.step-cell.playhead {
  box-shadow: inset 0 0 0 2px #fde047;
}

.step-cell.has-note.playhead {
  background: #0369a1;
}

.site-footer {
  margin-top: 11px;
  padding-top: 8px;
  border-top: 1px solid #1e293b;
  font-size: 0.76rem;
  color: var(--text-muted);
}

.site-footer p {
  margin: 0;
}

.site-footer a {
  color: var(--accent-sky);
  text-decoration: underline;
}

@media (max-width: 860px) {
  .stage-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .control-row-two {
    grid-template-columns: minmax(0, 1fr);
  }

  .monster-roster {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .recipe-grimoire {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .hatchery-controls {
    grid-template-columns: minmax(0, 1fr);
  }

  .step-pill {
    margin-left: 0;
  }
}

@media (max-width: 420px) {
  body {
    padding: 6px 6px 16px;
  }

  .monster-roster {
    grid-template-columns: minmax(0, 1fr);
  }

  .recipe-grimoire {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .recipe-chip.quint-chip {
    grid-column: span 2;
  }

  .transport-bar .btn {
    flex: 1 1 auto;
    text-align: center;
  }
}
