:root {
  --bg: #121318;
  --panel: #1a1c22;
  --panel-2: #22252d;
  --border: #2e323c;
  --text: #e3e6ec;
  --muted: #8b93a3;
  --accent: #b34ad0;
  --good: #58c45a;
  --bad: #e05050;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", sans-serif;
}

body { display: flex; flex-direction: column; }

button, select, input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
}

button { padding: 5px 12px; cursor: pointer; }
button, select, summary { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:hover { border-color: #4a5060; background: #2a2e38; }
button:disabled { opacity: 0.4; cursor: default; border-color: var(--border); background: var(--panel-2); }
button.active { border-color: var(--accent); background: #3a2444; }
button.small { padding: 2px 8px; font-size: 12px; }
button.danger { color: #ff9a9a; }
select { padding: 4px 6px; }

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.bar h1 { font-size: 16px; margin: 0 8px 0 0; letter-spacing: 0.5px; }
.bar label { color: var(--muted); display: flex; align-items: center; gap: 6px; }
.bar .spacer { flex: 1; }
/* On wide screens the menu is just part of the toolbar; on phones it folds under the ☰ button. */
.bar-menu, .sub-opts, .menu-group { display: contents; }
.menu-btn, .bar-title, .bar-quick, .menu-heading { display: none; }

main {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 16px;
  padding: 12px 16px;
}

#stage {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#canvas { display: block; }
#stage { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

.info {
  width: 300px;
  flex-shrink: 0;
  overflow-y: auto;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 12px;
}
.panel summary { cursor: pointer; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 1px; user-select: none; }
.panel[open] summary { margin-bottom: 6px; }
.info p { margin: 0 0 4px; }
#goal-text { font-size: 16px; font-weight: 700; }
.info dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0 0 8px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); font-size: 12px; }
#keys-help { white-space: pre-line; }
.debug dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 8px 0; }
.debug dt { color: var(--muted); }
.debug dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.guide { font-size: 13px; color: #c9ced8; }
.guide p { margin: 0 0 8px; }
.guide ul { margin: 0 0 8px; padding-left: 18px; }
.guide li { margin-bottom: 4px; }
.guide a, .info a { color: #c58be0; }
#demo-caption {
  min-height: 3.6em;
  margin: 8px 0;
  padding: 8px;
  background: var(--panel-2);
  border-left: 3px solid #ffd75a;
  border-radius: 4px;
  font-size: 13px;
}
.demo-buttons { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.demo-buttons button { padding: 4px 10px; }
#demo-exit { margin-left: auto; }

@media (max-width: 760px) {
  /* The page doesn't scroll: the play area takes the whole screen under the toolbar (all of it takes touch
     gestures), and the panels open over it with the ℹ button. */
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { height: 100dvh; }
  .bar { gap: 8px; padding: 6px 8px; position: relative; flex-wrap: nowrap; }
  .bar h1 { display: none; }
  .bar .spacer { display: none; }
  .bar button { padding: 4px 8px; }
  .menu-btn { display: block; font-size: 18px; line-height: 1; padding: 4px 10px; }
  .bar-title { display: block; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
  .bar-quick { display: flex; gap: 6px; }
  .bar-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 5;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 8px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.4);
  }
  .bar.open .bar-menu { display: flex; flex-direction: column; gap: 0; padding: 4px 12px 12px; }
  /* Menu rows: the drill selects as a label / select grid, then rows of buttons, each group under a heading. */
  .bar-menu .spacer { display: none; }
  .menu-group { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 0; border-top: 1px solid var(--border); }
  .menu-group:first-child { border-top: none; }
  .menu-group:not(:has(> button:not(.hidden))):not(.menu-drill) { display: none; }
  .menu-heading {
    display: block; width: 100%; color: var(--muted); font-size: 11px; font-weight: 600;
    letter-spacing: 0.08em; text-transform: uppercase;
  }
  .menu-drill { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px 12px; align-items: center; }
  .menu-drill .menu-heading { grid-column: 1 / -1; }
  .menu-drill label, .menu-drill .sub-opts { display: contents; }
  .menu-drill select { width: 100%; padding: 6px; }
  .menu-buttons button { flex: 1; padding: 8px 10px; }
  .menu-buttons button.star { flex: 0 0 auto; }
  .menu-buttons button.star::after { content: ' Save'; font-size: 14px; }
  .menu-buttons button.star.on::after { content: ' Saved'; }
  .bar.open .menu-btn { border-color: var(--accent); background: #3a2444; }
  main { position: relative; padding: 6px 8px; }
  #stage { align-items: flex-start; }
  .info {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 4;
    width: auto;
    padding: 8px;
    background: var(--bg);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  body.info-open .info { display: block; }
  body.info-open #q-info { border-color: var(--accent); background: #3a2444; }
}

/* Settings modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.hidden { display: none !important; }
.modal-box {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.modal-box > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-box > header h2 { margin: 0; font-size: 16px; }
.tabs { display: flex; gap: 4px; padding: 8px 16px 0; border-bottom: 1px solid var(--border); }
.tabs button { border-radius: 6px 6px 0 0; border-bottom: none; background: transparent; color: var(--muted); }
.tabs button.active { background: var(--panel-2); color: var(--text); }
.tab { padding: 12px 16px; overflow-y: auto; }

table.bindings { width: 100%; border-collapse: collapse; }
.bindings th, .bindings td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.bindings thead th { color: var(--muted); font-weight: 500; font-size: 12px; }
.bindings tbody th { font-weight: 500; white-space: nowrap; width: 120px; }
.chip { padding: 2px 8px; margin: 2px 4px 2px 0; font-size: 12px; border-radius: 12px; }
.chip:hover { border-color: var(--bad); }
.chip.add { color: var(--muted); }
.chip.add:hover { border-color: var(--good); }
.chip.capturing { border-color: var(--accent); color: var(--text); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }

.fields { display: flex; flex-direction: column; gap: 10px; }
.field { display: grid; grid-template-columns: 200px 1fr 80px 90px; align-items: center; gap: 4px 10px; }
.field .field-name { font-weight: 500; }
.fields-heading { margin: 10px 0 0; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field input[type="number"] { width: 80px; padding: 3px 6px; }
.field input[type="checkbox"] { justify-self: start; width: 18px; height: 18px; accent-color: var(--accent); }
.field select { justify-self: start; }
.field .unit { color: var(--muted); font-size: 12px; }
.field small { grid-column: 2 / -1; color: var(--muted); font-size: 11px; margin-top: -4px; }

.io-buttons { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 4px 0; }
#io-text { width: 100%; height: 280px; margin: 8px 0; padding: 8px; font: 12px/1.4 Consolas, monospace; resize: vertical; }
#io-msg.good { color: var(--good); }
#io-msg.bad { color: var(--bad); }

/* Favorites */
button.star { font-size: 16px; line-height: 1; padding: 4px 10px; }
button.star.on { color: #ffd75a; border-color: #8a7330; }
.fav-list { display: flex; flex-direction: column; gap: 8px; max-height: 70vh; }
.fav-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.fav-card:hover { border-color: var(--accent); }
.fav-card canvas { flex-shrink: 0; border: 1px solid var(--border); }
.fav-info { flex: 1; min-width: 0; }
.fav-title { font-weight: 600; margin-bottom: 2px; }

/* Settings fields on phones (after the base .field rules so these win). */
@media (max-width: 760px) {
  .field { grid-template-columns: minmax(0, 1fr) auto; }
  .field > * { min-width: 0; }
  .field input[type="range"] { grid-column: 1 / -1; }
  .field small { grid-column: 1 / -1; margin-top: 0; }
  .tabs { flex-shrink: 0; padding: 8px 8px 0; overflow-x: auto; scrollbar-width: none; }
  .tabs button { flex-shrink: 0; white-space: nowrap; padding: 5px 10px; }
}
