*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  --ink: rgba(255, 255, 255, .92);
  --ink-dim: rgba(255, 255, 255, .5);
  --bar: rgba(22, 22, 24, .82);
  --pop: #16161a;
  --hover: rgba(255, 255, 255, .1);
  --on: rgba(255, 255, 255, .16);
  --edge: rgba(255, 255, 255, .14);
  transition: background .25s ease;
}
body[data-background="white"] {
  background: #fff;
  --ink: rgba(0, 0, 0, .88);
  --ink-dim: rgba(0, 0, 0, .48);
  --bar: rgba(252, 252, 253, .86);
  --pop: #fbfbfc;
  --hover: rgba(0, 0, 0, .07);
  --on: rgba(0, 0, 0, .11);
  --edge: rgba(0, 0, 0, .12);
}

#app { position: fixed; inset: 0; }
canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
canvas.dragging { cursor: grabbing; }

/* ---------- bottom HUD ---------- */
.hud {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  z-index: 10;
  max-width: calc(100vw - 24px);
}

.status {
  height: 14px;
  line-height: 14px;
  font-size: 11.5px;
  letter-spacing: .01em;
  color: var(--ink-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  pointer-events: none;
  transition: opacity .2s ease;
}
.status.flash { animation: status-in .32s ease; }
@keyframes status-in { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

.bar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  border-radius: 11px;
  background: var(--bar);
  border: 1px solid var(--edge);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, .28);
  max-width: 100%;
  overflow: hidden;
}
.seg { display: flex; align-items: center; gap: 2px; }
.div { width: 1px; align-self: stretch; margin: 3px 4px; background: var(--edge); flex: none; }

.bar button {
  appearance: none;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 12.5px;
  line-height: 1;
  padding: 7px 11px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .11s ease, opacity .11s ease;
}
.bar button:hover:not(:disabled) { background: var(--hover); }
.bar button:disabled { opacity: .3; cursor: default; }
.bar button[aria-expanded="true"] { background: var(--on); }
.bar button:focus-visible, .row:focus-visible, .choices button:focus-visible {
  outline: 2px solid var(--ink-dim);
  outline-offset: -2px;
}
.bar button.icon {
  padding: 7px 9px;
  font-size: 14px;
  display: inline-grid;
  place-items: center;
  min-width: 30px;
  height: 27px;
}
.bar button.icon svg { display: block; }
.bar button.size {
  font-variant-numeric: tabular-nums;
  padding-right: 21px;
  position: relative;
}
.bar button.size::after {
  content: '';
  position: absolute;
  right: 9px;
  top: 50%;
  width: 4.5px;
  height: 4.5px;
  border-right: 1.4px solid currentColor;
  border-bottom: 1.4px solid currentColor;
  transform: translateY(-70%) rotate(45deg);
  opacity: .55;
}

#soundBtn .i-off, #soundBtn[aria-pressed="true"] .i-on { display: none; }
#soundBtn[aria-pressed="true"] .i-off { display: block; }
#playBtn .i-play, #playBtn.paused .i-pause { display: none; }
#playBtn.paused .i-play { display: block; }

.counter, .move {
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-dim);
  padding: 0 4px;
  white-space: nowrap;
}
.move { color: var(--ink); min-width: 26px; text-align: center; }
.counter { min-width: 46px; text-align: center; }
.speed { width: 70px; margin: 0 4px; accent-color: var(--ink); cursor: pointer; }

/* ---------- popovers ---------- */
.pop {
  position: absolute;
  bottom: calc(100% + 7px);
  padding: 5px;
  border-radius: 11px;
  background: var(--pop);
  border: 1px solid var(--edge);
  box-shadow: 0 8px 28px rgba(0, 0, 0, .36);
  display: flex;
  flex-direction: column;
  gap: 1px;
  max-height: min(56vh, 360px);
  overflow-y: auto;
  transform-origin: bottom center;
  animation: pop-in .14s cubic-bezier(.2, .8, .3, 1);
}
@keyframes pop-in { from { opacity: 0; transform: translateY(4px) scale(.98); } to { opacity: 1; transform: none; } }
.pop-left { left: 0; transform-origin: bottom left; }
.pop-right { right: 0; transform-origin: bottom right; }
.pop-label {
  font-size: 10px;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-dim);
  padding: 5px 8px 3px;
}
.row {
  appearance: none;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  text-align: left;
  padding: 7px 14px 7px 9px;
  cursor: pointer;
  white-space: nowrap;
}
.row:hover { background: var(--hover); }
.row.on { background: var(--on); }

.choices { display: flex; gap: 4px; padding: 2px 4px 4px; }
.choices button {
  appearance: none;
  flex: 1;
  border: 1px solid var(--edge);
  border-radius: 7px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 12.5px;
  padding: 6px 12px;
  cursor: pointer;
}
.choices button:hover { background: var(--hover); }
.choices button.on { background: var(--on); }

.keys {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 5px 10px;
  align-items: center;
  padding: 9px 11px;
}
.keys kbd {
  font: inherit;
  font-size: 11px;
  color: var(--ink);
  background: var(--hover);
  border: 1px solid var(--edge);
  border-radius: 5px;
  padding: 3px 6px;
  white-space: nowrap;
}
.keys span { font-size: 12px; color: var(--ink-dim); white-space: nowrap; }

.fatal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--ink);
  font-size: 14px;
  text-align: center;
  line-height: 1.5;
  z-index: 20;
}

@media (max-width: 620px) {
  .bar button { padding: 7px 8px; font-size: 12px; }
  .bar button.size { padding-right: 19px; }
  .bar button.size::after { right: 8px; }
  .bar button.icon { padding: 7px 6px; min-width: 26px; }
  .speed { width: 52px; }
  .keys span { white-space: normal; }
  .pop-right { max-width: calc(100vw - 24px); }
}
@media (max-width: 420px) {
  .bar { gap: 0; padding: 3px; }
  .bar button { padding: 7px 7px; }
  .bar button.icon { padding: 7px 4px; min-width: 24px; }
  .div { margin: 3px 2px; }
  .move { min-width: 22px; padding: 0 2px; }
  .counter { min-width: 38px; padding: 0 2px; }
  .speed { width: 40px; margin: 0 2px; }
}
@media (prefers-reduced-motion: reduce) {
  .pop, .status.flash { animation: none; }
}
