:root {
  --bg: #000000;
  --ink: #111;
  --grid: #d8d4c8;
  --grid-strong: #7a7668;
  --red: #e63946;
  --blue: #1d4ed8;
  --yellow: #f4b400;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; height: 100%; overflow: hidden; background: var(--bg); -webkit-text-size-adjust: 100%; }
body { touch-action: manipulation; }
body {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color: var(--ink);
  letter-spacing: 0.02em;
}
body.ui-hidden .hud,
body.ui-hidden #gui,
body.ui-hidden .frame,
body.ui-hidden .tick,
body.ui-hidden #textbox { display: none !important; }
#c { display: block; width: 100vw; height: 100vh; }

.hud {
  position: fixed;
  pointer-events: none;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  z-index: 10;
  color: #ffffff;
  opacity: 1;
}
.tl { top: 18px; left: 20px; }
.tr { top: 18px; right: 20px; text-align: right; }
.bl { bottom: 18px; left: 20px; }
.br { bottom: 18px; right: 20px; text-align: right; }

.legend { line-height: 1.9; }
.legend .row { display: flex; align-items: center; justify-content: flex-start; gap: 10px; }
.legend .sw {
  width: 10px; height: 10px; display: inline-block;
  border: 1px solid var(--ink);
}
.legend .red    { background: var(--red); }
.legend .blue   { background: var(--blue); }
.legend .yellow { background: var(--yellow); }

.frame {
  position: fixed; inset: 12px;
  border: 1px solid var(--ink);
  pointer-events: none;
  z-index: 5;
}
.tick {
  position: absolute;
  background: var(--ink);
}
.tick.h { width: 24px; height: 1px; }
.tick.v { width: 1px; height: 24px; }

/* ---------- text input ---------- */
#textbox {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 80px));
  z-index: 15;
  pointer-events: auto;
}
#textDisplay {
  width: 100%;
  color: #fff;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 18px;
  letter-spacing: 0.3em;
  text-align: left;
  padding: 16px 18px;
  outline: none;
  cursor: text;
  -webkit-user-select: text;
  user-select: text;
  border: 1px solid rgba(255, 255, 255, 0.5);
  background: transparent;
  resize: vertical;
  min-height: 54px;
  field-sizing: content;
}

/* ---------- GUI panel ---------- */
#gui {
  position: fixed;
  top: 18px;
  right: 20px;
  width: 260px;
  box-sizing: border-box;
  pointer-events: auto;
  z-index: 20;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.35);
  max-height: calc(100vh - 36px);
  overflow-y: auto;
}
#gui::-webkit-scrollbar { width: 4px; }
#gui::-webkit-scrollbar-track { background: transparent; }
#gui::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.25); }
#guiHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  font-size: 11px;
  letter-spacing: 0.28em;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  position: sticky;
  top: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  z-index: 1;
}
#guiHeader:hover { background: rgba(255, 255, 255, 0.05); }
#guiToggle { opacity: 0.7; font-size: 13px; line-height: 1; }
#guiBody {
  padding: 0 14px 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  padding-top: 12px;
}
#gui.collapsed #guiBody { display: none; }
#gui.collapsed #guiHeader { border: 0; }
#gui *, #gui *::before, #gui *::after { box-sizing: border-box; }
#gui h4 {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.22em;
  margin: 0 0 10px 0;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
#gui h4:hover { opacity: 0.7; }
#guiBody > h4:first-child {
  margin-top: 0;
  padding-bottom: 0;
  border-bottom: 0;
}
#gui .row {
  display: grid;
  grid-template-columns: 56px 1fr 44px;
  align-items: center;
  gap: 8px;
  margin-bottom: 7px;
  min-width: 0;
}
#gui .row label {
  opacity: 0.75;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#gui .row .val {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
}
#gui input[type="range"] {
  width: 100%;
  min-width: 0;
  -webkit-appearance: none;
  appearance: none;
  height: 1px;
  background: rgba(255, 255, 255, 0.5);
  outline: none;
  margin: 0;
  padding: 0;
}
#gui input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 10px;
  height: 10px;
  background: #fff;
  border: 0;
  cursor: pointer;
}
#gui input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  background: #fff;
  border: 0;
  cursor: pointer;
  border-radius: 0;
}
#gui input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 14px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  background: transparent;
  padding: 0;
  cursor: pointer;
}
#gui input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
#gui input[type="color"]::-webkit-color-swatch { border: 0; }
#gui select {
  width: 100%;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.35);
  font-family: inherit;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  padding: 4px 6px;
  outline: none;
  cursor: pointer;
}
#gui button {
  width: 100%;
  height: 22px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.5);
  font-family: inherit;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  padding: 0 6px;
  cursor: pointer;
  outline: none;
  transition: background 0.12s ease;
}
#gui button:hover { background: rgba(255,255,255,0.12); }

/* ---------- start overlay ---------- */
#startOverlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
#startOverlay.hidden { display: none; }
#startBtn {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 20px;
  letter-spacing: 0.4em;
  text-transform: uppercase;
  color: #fff;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.6);
  padding: 14px 32px;
  cursor: pointer;
  outline: none;
  transition: background 0.15s ease, border-color 0.15s ease;
}
#startBtn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.95);
}
#gui button.playing {
  background: rgba(255,255,255,0.85);
  color: #000;
  border-color: rgba(255,255,255,0.95);
}

/* Hide bottom-corner HUD on mobile */
@media (max-width: 768px) {
  .hud.bl, .hud.br { display: none !important; }
}
#gui .section {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}
#guiBody > .section:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}
#gui .section.collapsed .row { display: none; }
