/* System fonts only: the classic serif + the OS native sans that the page
   shows instantly. No webfont download, so there's no flash-of-unstyled-text. */

/* ─── PineTone — deep blue, clean ───────────────────────────────────────────
   The original Mosi blues (sky-cyan on deep navy), executed quietly: matte
   surfaces, hairline borders, one accent used as signal — no glassmorphism,
   no glow washes, no shimmer/pulse. Humanist serif (Newsreader) for the voice,
   humanist sans (Hanken Grotesk) for the hands. */
:root {
  --bg:           #060b12;   /* deep navy-black field */
  --surface:      #0f1c2e;   /* lifted card */
  --surface-2:    #0a1622;   /* recessed wells / insets */
  --surface-3:    #142539;   /* hover wells */

  --ink:          #eaf2fb;   /* cool near-white */
  --ink-2:        #9fb3cd;   /* secondary / labels */
  --ink-3:        #6f859f;   /* muted (large / decorative only) */

  --line:         rgba(125, 170, 220, 0.13);
  --line-strong:  rgba(125, 170, 220, 0.22);

  --accent:       #38bdf8;   /* sky-cyan — actions & active state */
  --accent-2:     #7dd3fc;   /* hover / bright */
  --accent-deep:  #0ea5e9;   /* pressed */
  --accent-ink:   #04141f;   /* ink on a cyan surface */
  --accent-soft:  rgba(56, 189, 248, 0.15);
  --accent-line:  rgba(56, 189, 248, 0.55);

  --shadow-card:  0 1px 2px rgba(0,0,0,0.45), 0 26px 60px -28px rgba(0,0,0,0.85);
  --shadow-chip:  0 1px 2px rgba(0,0,0,0.5), 0 12px 30px -14px rgba(0,0,0,0.7);

  --radius:       16px;
  --radius-sm:    10px;

  --serif: Georgia, 'Times New Roman', serif;
  --sans:  system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --ease:  cubic-bezier(0.22, 1, 0.36, 1);

  --z-chrome: 90;
  --z-overlay: 100;
  --z-toast: 200;
}

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

body, html {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}

/* ─── Field (settings world) ────────────────────────────────────────────── */
.bg-layer {
  position: fixed; inset: 0; z-index: -2;
  background:
    radial-gradient(135% 100% at 50% -8%, #0b1626 0%, var(--bg) 55%),
    var(--bg);
}
.bg-pattern, .hex-decor { display: none; }

/* ─── Settings overlay host ─────────────────────────────────────────────── */
#landing-page {
  position: fixed; inset: 0;
  z-index: var(--z-overlay);
  /* Hidden on load so the instrument is the only thing that ever paints first;
     back() sets display:flex when the gear opens settings (no FOUC flash). */
  display: none; justify-content: center; align-items: center;
  padding: 1.75rem;
}
.drawer-scrim {
  position: absolute; inset: 0;
  background: rgba(4, 8, 14, 0.7);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  transition: opacity 0.26s var(--ease);
  cursor: pointer;
}

/* ─── The card ──────────────────────────────────────────────────────────── */
.settings-drawer {
  position: relative;
  width: min(512px, 100%);
  max-height: calc(100dvh - 3.5rem);
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  overflow-y: auto;
  overscroll-behavior: contain;
  animation: card-in 0.32s var(--ease) both;
}
@keyframes card-in {
  from { transform: translateY(12px) scale(0.985); opacity: 0; }
  to   { transform: translateY(0) scale(1);         opacity: 1; }
}
#landing-page.landing-out .settings-drawer {
  transform: translateY(8px) scale(0.99); opacity: 0;
  transition: transform 0.26s var(--ease), opacity 0.26s var(--ease);
}
#landing-page.landing-out .drawer-scrim { opacity: 0; }

/* Desktop: settings is a live LEFT side panel — the instrument stays visible
   and interactive on the right (the host is click-through except the panel),
   so slider/colour/key changes preview in real time. Mobile keeps full-screen. */
@media (hover: hover) and (pointer: fine) {
  body.settings-open #landing-page {
    justify-content: flex-start;
    padding: 0;
    pointer-events: none;
    background: none;
  }
  body.settings-open .drawer-scrim { display: none; }
  body.settings-open .settings-drawer {
    pointer-events: auto;
    width: min(420px, 42vw);
    max-height: 100dvh; height: 100dvh;
    border-radius: 0;
    border: none; border-right: 1px solid var(--line-strong);
    box-shadow: 12px 0 48px -24px rgba(0,0,0,0.85);
    animation: panel-slide-in 0.24s var(--ease) both;
  }
}
@keyframes panel-slide-in { from { transform: translateX(-100%); } to { transform: translateX(0); } }

/* ─── Header / wordmark ─────────────────────────────────────────────────── */
.drawer-head {
  display: flex; align-items: center; gap: 0.85rem;
  padding: 2rem 2.1rem 0;
}
.drawer-head .mark {
  width: 40px; height: 40px; object-fit: contain; flex-shrink: 0;
}
.wm-name {
  font-family: var(--serif);
  font-size: 1.75rem; font-weight: 500; letter-spacing: -0.01em;
  line-height: 1; color: var(--ink);
}
.drawer-close {
  margin-left: auto; width: 36px; height: 36px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: transparent; color: var(--ink-3);
  border: 1px solid var(--line); border-radius: 50%;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.drawer-close:hover { color: var(--ink); border-color: var(--line-strong); background: var(--surface-3); }
.drawer-close:focus-visible { outline: none; border-color: var(--accent-line); color: var(--accent); }
.drawer-close svg { width: 15px; height: 15px; }

/* Welcoming intro — editorial serif */
.welcome {
  margin: 0.85rem 2.1rem 0;
  font-family: var(--serif);
  font-size: 1.06rem; line-height: 1.5; color: var(--ink-2);
  text-wrap: pretty; max-width: 42ch;
}

/* ─── Form / sections ───────────────────────────────────────────────────── */
#settingsForm { padding: 1.5rem 2.1rem 2rem; }
.module { padding: 1.15rem 0; border-bottom: 1px solid var(--line); }
.module:first-of-type { padding-top: 0; }
.module:last-of-type { border-bottom: none; }
.module-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.9rem 1.25rem; }
.form-group { display: flex; flex-direction: column; gap: 0.5rem; }
.full-width { grid-column: 1 / -1; }

.module-label {
  display: block; margin-bottom: 0.85rem;
  font-family: var(--sans); font-size: 0.82rem; font-weight: 600;
  letter-spacing: 0; color: var(--ink);
}
label {
  font-family: var(--sans); font-size: 0.78rem; font-weight: 500;
  color: var(--ink-2); transition: color 0.15s;
}
.form-group:focus-within > label { color: var(--accent); }

/* ─── Controls ──────────────────────────────────────────────────────────── */
input[type="number"], select, textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--ink);
  padding: 0.66rem 0.85rem;
  font-family: var(--sans); font-size: 0.95rem; line-height: 1.4;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
  outline: none; -webkit-appearance: none; appearance: none;
}
input[type="number"]::placeholder, textarea::placeholder { color: var(--ink-3); }
input[type="number"]:hover, select:hover, textarea:hover { border-color: var(--line-strong); }
input[type="number"]:focus, select:focus, textarea:focus {
  border-color: var(--accent-line);
  background: #0c1828;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select {
  cursor: pointer; padding-right: 2.25rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='7' viewBox='0 0 10 7'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%239fb3cd' stroke-width='1.4' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 0.9rem center;
}
select option { background: var(--surface); color: var(--ink); }
textarea {
  resize: vertical; min-height: 80px;
  font-size: 0.82rem; line-height: 1.6; color: var(--ink-2);
}

/* Range — cool track + cyan detent */
input[type="range"] {
  width: 100%; -webkit-appearance: none; appearance: none;
  height: 4px; border-radius: 4px; background: var(--surface-3);
  outline: none; cursor: pointer; margin: 0.7rem 0;
  border: 1px solid var(--line);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface);
  box-shadow: 0 1px 3px rgba(0,0,0,0.5);
  cursor: pointer; transition: transform 0.12s, box-shadow 0.12s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.12); }
input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px var(--accent-soft); }
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px; border: 2px solid var(--surface);
  border-radius: 50%; background: var(--accent); cursor: pointer;
}

/* ─── Buttons ───────────────────────────────────────────────────────────── */
.actions { display: flex; flex-direction: column; gap: 0.65rem; padding-top: 1.6rem; }
.btn-primary {
  display: flex; align-items: center; justify-content: center; gap: 0.55rem;
  width: 100%;
  background: var(--accent); color: var(--accent-ink);
  border: none; border-radius: var(--radius-sm);
  padding: 0.92rem 1.25rem;
  font-family: var(--sans); font-weight: 600; font-size: 0.98rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}
.btn-primary svg { width: 15px; height: 15px; }
.btn-primary:hover { background: var(--accent-2); }
.btn-primary:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.btn-primary:active { transform: translateY(1px); background: var(--accent-deep); }
.btn-secondary {
  display: block; width: 100%;
  background: transparent; color: var(--ink-2);
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  padding: 0.82rem 1.25rem;
  font-family: var(--sans); font-weight: 500; font-size: 0.92rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.btn-secondary:hover { color: var(--ink); border-color: var(--ink-3); background: var(--surface-3); }
.btn-secondary:focus-visible { outline: none; border-color: var(--accent-line); color: var(--accent); }

/* Quiet, low-emphasis text button for the destructive-ish reset. */
.btn-reset {
  width: 100%; margin-top: 0.1rem;
  background: none; border: none; cursor: pointer;
  font-family: var(--sans); font-size: 0.8rem; color: var(--ink-3);
  padding: 0.5rem; transition: color 0.15s;
}
.btn-reset:hover { color: var(--ink-2); text-decoration: underline; }
.btn-reset:focus-visible { outline: none; color: var(--accent); }

/* ─── Advanced disclosure ───────────────────────────────────────────────── */
details.advanced-settings { padding: 1.15rem 0 0; }
details summary {
  list-style: none; cursor: pointer;
  font-family: var(--sans); font-size: 0.82rem; font-weight: 600; color: var(--ink-2);
  display: flex; align-items: center; gap: 0.55rem;
  transition: color 0.15s; user-select: none;
}
details summary::-webkit-details-marker { display: none; }
details summary::before {
  content: '+'; font-size: 1rem; line-height: 1; color: var(--accent); transition: color 0.15s;
  width: 1em; text-align: center;
}
details[open] summary::before { content: '\2212'; }
details summary::after { content: ''; flex: 1; height: 1px; background: var(--line); }
details summary:hover { color: var(--ink); }
.advanced-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.9rem 1.25rem; margin-top: 1.2rem; }
.chk-inline {
  display: flex; align-items: center; gap: 0.55rem; cursor: pointer;
  font-family: var(--sans); font-size: 0.85rem; color: var(--ink-2);
}
.chk-inline input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; }
.chk-inline:hover { color: var(--ink); }

/* ─── Layout tuning sliders (Advanced) ──────────────────────────────────── */
.layout-sliders { display: flex; flex-direction: column; gap: 0.85rem; margin-bottom: 0.5rem; }
.layout-sliders .sub-label {
  font-family: var(--sans); font-size: 0.78rem; font-weight: 500;
  letter-spacing: 0; color: var(--ink-2);
}
.slider-row { display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 0 0.6rem; }
.slider-row > label { font-family: var(--sans); font-size: 0.85rem; color: var(--ink-2); }
.slider-row > .slider-val {
  font-family: var(--sans); font-size: 0.82rem; font-weight: 600;
  font-variant-numeric: tabular-nums; color: var(--accent);
  min-width: 2.6rem; text-align: right;
}
.slider-row > input[type="range"] { grid-column: 1 / -1; width: 100%; margin: 0.45rem 0 0; cursor: pointer; }

/* ─── Footer — quiet maker's credit ─────────────────────────────────────── */
.credits {
  margin-top: auto;
  padding: 1.2rem 2.1rem 1.6rem;
  border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 0.3rem;
  font-family: var(--sans); font-size: 0.72rem; line-height: 1.6; color: var(--ink-3);
}
.credits .serial {
  font-family: var(--serif); font-style: italic; font-size: 0.82rem; color: var(--ink-2);
}

/* ─── Loading toast (on the instrument) ─────────────────────────────────── */
#loading-toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  z-index: var(--z-toast); display: flex; align-items: center; gap: 0.6rem;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 50px;
  padding: 0.55rem 1.1rem 0.55rem 0.95rem;
  font-family: var(--sans); font-size: 0.82rem; font-weight: 500; color: var(--ink-2);
  box-shadow: var(--shadow-chip);
  transition: opacity 0.3s, transform 0.3s;
}
#loading-toast.toast-hide { opacity: 0; transform: translateX(-50%) translateY(8px); }
.loading-spinner {
  width: 12px; height: 12px; flex-shrink: 0;
  border: 2px solid var(--surface-3); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Keyboard canvas ───────────────────────────────────────────────────── */
#keyboard {
  display: none; position: fixed; top: 50%; left: 50%;
  z-index: 5; touch-action: none; background: transparent;
}
#keyboard.keyboard-in { animation: fade-in 0.24s ease-out both; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }


/* ─── Gear / settings trigger (on the instrument) ───────────────────────── */
#backButton {
  position: fixed; left: 1.5rem; top: 1.5rem; z-index: var(--z-chrome);
  width: 44px; height: 44px; padding: 10px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 50%; cursor: pointer; color: var(--ink-2);
  display: none; align-items: center; justify-content: center;
  box-shadow: var(--shadow-chip);
  transition: color 0.15s, background 0.15s, border-color 0.15s, transform 0.15s;
}
#backButton svg { width: 100%; height: 100%; display: block; transition: transform 0.4s var(--ease); }
#backButton:hover { color: var(--accent); transform: scale(1.05); border-color: var(--accent-line); }
#backButton:hover svg { transform: rotate(35deg); }
#backButton:focus-visible { outline: none; border-color: var(--accent-line); color: var(--accent); }

/* ─── Help trigger (sits beside the gear) ───────────────────────────────── */
#helpButton {
  position: fixed; top: 1.5rem; z-index: var(--z-chrome);
  left: calc(max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)) + 54px);
  width: 44px; height: 44px; padding: 11px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 50%; cursor: pointer; color: var(--ink-2);
  display: none; align-items: center; justify-content: center;
  box-shadow: var(--shadow-chip);
  transition: color 0.15s, background 0.15s, border-color 0.15s, transform 0.15s;
}
#helpButton svg { width: 100%; height: 100%; display: block; }
#helpButton:hover { color: var(--accent); transform: scale(1.05); border-color: var(--accent-line); }
#helpButton:focus-visible { outline: none; border-color: var(--accent-line); color: var(--accent); }

/* ─── Center + fullscreen (continue the top-left chrome row) ─────────────── */
#centerButton, #fullscreenButton {
  position: fixed; top: 1.5rem; z-index: var(--z-chrome);
  width: 44px; height: 44px; padding: 11px;
  background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 50%; cursor: pointer; color: var(--ink-2);
  display: none; align-items: center; justify-content: center;
  box-shadow: var(--shadow-chip);
  transition: color 0.15s, background 0.15s, border-color 0.15s, transform 0.15s;
}
#centerButton     { left: calc(max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)) + 108px); }
#fullscreenButton { left: calc(max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)) + 162px); }
#centerButton svg, #fullscreenButton svg { width: 100%; height: 100%; display: block; }
#centerButton:hover, #fullscreenButton:hover { color: var(--accent); transform: scale(1.05); border-color: var(--accent-line); }
#centerButton:focus-visible, #fullscreenButton:focus-visible { outline: none; border-color: var(--accent-line); color: var(--accent); }

/* ─── Desktop legend (key reference; "?" opens it as a dropdown; hidden on touch) ── */
#legend {
  position: fixed; z-index: var(--z-chrome);
  left: max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem));
  top: calc(1.5rem + 56px); /* drops just below the chrome button row */
  display: none; flex-direction: column; gap: 0.28rem;
  padding: 0.65rem 0.75rem;
  background: rgba(10, 18, 30, 0.9); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow-chip);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  transform-origin: top left;
}
body.keyboard-active #legend { display: flex; animation: legend-drop 0.16s var(--ease); }
body.keyboard-active.legend-hidden #legend { display: none; }
@keyframes legend-drop { from { opacity: 0; transform: translateY(-6px) scale(0.98); } to { opacity: 1; transform: none; } }
.legend-row { display: flex; align-items: center; gap: 0.5rem; font-family: var(--sans); font-size: 0.74rem; color: var(--ink-2); }
.legend-row kbd {
  font-family: var(--sans); font-size: 0.66rem; font-weight: 600;
  background: var(--surface-2); color: var(--accent);
  border: 1px solid var(--line-strong); border-radius: 5px;
  padding: 0.1rem 0.4rem; white-space: nowrap; min-width: 64px; text-align: center;
}
.legend-row span { flex: 1; }

/* Hovering a row pops a guide out to its right explaining that control. */
.legend-row[data-tip] { position: relative; cursor: help; }
.legend-row[data-tip]::after {
  content: attr(data-tip);
  position: absolute; left: calc(100% + 14px); top: 50%;
  transform: translateY(-50%) translateX(-6px);
  width: max-content; max-width: 230px;
  padding: 0.5rem 0.65rem;
  background: rgba(14, 28, 46, 0.98); color: var(--ink);
  border: 1px solid var(--line-strong); border-radius: 9px;
  box-shadow: var(--shadow-chip);
  font-family: var(--sans); font-size: 0.72rem; line-height: 1.45; font-weight: 400;
  text-align: left; white-space: normal;
  opacity: 0; pointer-events: none;
  transition: opacity 0.14s var(--ease), transform 0.14s var(--ease);
  z-index: var(--z-overlay);
}
/* Little caret joining the guide to its row. */
.legend-row[data-tip]::before {
  content: ""; position: absolute; left: calc(100% + 8px); top: 50%;
  transform: translateY(-50%) translateX(-6px);
  border: 6px solid transparent; border-right-color: var(--line-strong);
  opacity: 0; pointer-events: none;
  transition: opacity 0.14s var(--ease), transform 0.14s var(--ease);
  z-index: var(--z-overlay);
}
.legend-row[data-tip]:hover::after,
.legend-row[data-tip]:hover::before { opacity: 1; transform: translateY(-50%) translateX(0); }
@media (prefers-reduced-motion: reduce) {
  .legend-row[data-tip]::after, .legend-row[data-tip]::before { transition: none; }
}
@media (hover: none) and (pointer: coarse) { #legend { display: none !important; } }

/* ─── Current-key indicator (top center) ────────────────────────────────── */
#key-indicator {
  position: fixed; top: 1.5rem; left: 50%; transform: translateX(-50%);
  z-index: var(--z-chrome); display: none;
  padding: 0.4rem 0.9rem; border-radius: 999px;
  background: rgba(10, 18, 30, 0.72); border: 1px solid var(--line-strong);
  box-shadow: var(--shadow-chip); backdrop-filter: blur(8px);
  font-family: var(--sans); font-size: 0.82rem; font-weight: 600;
  color: var(--ink); white-space: nowrap; pointer-events: none;
}

/* ─── On-instrument label-layer toggles ─────────────────────────────────── */
#layer-toggles {
  position: fixed; z-index: var(--z-chrome); display: none;
  right: max(1.5rem, calc(env(safe-area-inset-right) + 0.5rem));
  bottom: max(1.5rem, calc(env(safe-area-inset-bottom) + 0.5rem));
  flex-direction: column; gap: 0.3rem; padding: 0.35rem;
  background: rgba(10,18,30,0.72); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: var(--shadow-chip);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.layer-toggle {
  font-family: var(--sans); font-size: 0.76rem; font-weight: 500;
  color: var(--ink-3); background: transparent; border: none;
  padding: 0.4rem 0.9rem; border-radius: 9px; cursor: pointer; text-align: center;
  transition: color 0.15s, background 0.15s;
}
.layer-toggle:hover { color: var(--ink); }
.layer-toggle.active { color: var(--accent-ink); background: var(--accent); }
.layer-toggle:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--accent-line); }
@media (max-width: 560px) {
  .layer-toggle { padding: 0.35rem 0.7rem; font-size: 0.72rem; }
}

/* ─── Move handles (touch only) ──────────────────────────────────────────────
   On touch devices the instrument position stays locked; you drag one of these
   side grips to move/rotate it. The body itself is for playing (glissando). */
#move-handles { display: none; }
@media (hover: none) and (pointer: coarse) {
  body.keyboard-active #move-handles { display: block; }
}
.move-handle {
  position: fixed; top: 50%; transform: translateY(-50%);
  z-index: var(--z-chrome);
  width: 44px; height: 92px; border-radius: 16px;
  background: rgba(10, 18, 30, 0.6);
  border: 1px solid var(--line-strong);
  box-shadow: var(--shadow-chip);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  touch-action: none; cursor: grab;
}
.move-handle::before {
  content: ''; width: 4px; height: 44px; border-radius: 2px;
  background: repeating-linear-gradient(to bottom, var(--ink-3) 0 3px, transparent 3px 8px);
}
.move-handle:active { cursor: grabbing; border-color: var(--accent-line); }
.move-handle.left  { left:  max(0.5rem, env(safe-area-inset-left)); }
.move-handle.right { right: max(0.5rem, env(safe-area-inset-right)); }
/* Top/bottom grips are horizontal bars centred on the screen width. */
.move-handle.top, .move-handle.bottom {
  top: auto; left: 50%; transform: translateX(-50%);
  width: 92px; height: 44px;
}
.move-handle.top    { top:    max(0.5rem, env(safe-area-inset-top)); }
.move-handle.bottom { bottom: max(0.5rem, env(safe-area-inset-bottom)); }
.move-handle.top::before, .move-handle.bottom::before {
  width: 44px; height: 4px;
  background: repeating-linear-gradient(to right, var(--ink-3) 0 3px, transparent 3px 8px);
}
/* Portrait: grips on top & bottom. Landscape: grips on the sides. */
@media (orientation: portrait)  { .move-handle.left, .move-handle.right { display: none; } }
@media (orientation: landscape) { .move-handle.top,  .move-handle.bottom { display: none; } }

/* ─── How-to-play overlay ───────────────────────────────────────────────── */
#help-overlay {
  position: fixed; inset: 0; z-index: var(--z-overlay);
  display: flex; justify-content: center; align-items: center; padding: 1.75rem;
}
.help-scrim {
  position: absolute; inset: 0;
  background: rgba(4, 8, 14, 0.7);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  cursor: pointer;
}
.help-card {
  position: relative;
  width: min(480px, 100%); max-height: calc(100dvh - 4rem);
  display: flex; flex-direction: column; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow-card);
  animation: card-in 0.3s var(--ease) both;
}
.help-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.6rem 1.8rem 0.5rem;
}
.help-head h2 {
  margin: 0; font-family: var(--serif); font-weight: 500; font-size: 1.5rem; color: var(--ink);
}
.help-list {
  margin: 0; padding: 0.75rem 1.8rem 1.8rem; list-style: none;
  display: flex; flex-direction: column; gap: 0.85rem;
}
.help-list li {
  font-family: var(--sans); font-size: 0.92rem; line-height: 1.5; color: var(--ink-2);
  text-wrap: pretty;
}
.help-list b { color: var(--ink); font-weight: 600; }
.help-list kbd {
  font-family: var(--sans); font-size: 0.78rem; font-weight: 600;
  background: var(--surface-2); color: var(--accent);
  border: 1px solid var(--line-strong); border-radius: 5px;
  padding: 0.05rem 0.38rem; white-space: nowrap;
}

/* ─── Responsive ────────────────────────────────────────────────────────── */
/* Phone (portrait): settings + help go full-bleed, single column. */
@media (max-width: 560px) {
  #landing-page, #help-overlay { padding: 0; }
  .settings-drawer, .help-card {
    width: 100%; max-height: 100dvh; min-height: 100dvh;
    border: none; border-radius: 0;
  }
  .module-row, .advanced-grid { grid-template-columns: 1fr; }
  .drawer-head { padding-top: max(2rem, env(safe-area-inset-top)); }
  .help-head { padding-top: max(1.6rem, env(safe-area-inset-top)); }
}

/* Touch devices (phone / iPad / tablet): larger tap targets; the Keyboard
   layer is QWERTY-only so it's hidden where there's no physical keyboard. */
@media (hover: none) and (pointer: coarse) {
  .layer-toggle { padding: 0.62rem 1.05rem; font-size: 0.82rem; min-height: 44px; }
  .layer-toggle[data-layer="keys"] { display: none; }
  #backButton, #helpButton, #centerButton, #fullscreenButton, .drawer-close { width: 46px; height: 46px; }
  input[type="number"], select, textarea { font-size: 16px; } /* prevents iOS zoom-on-focus */
  /* Stack the chrome buttons DOWN the left edge so they don't sit under the
     top move-handle (which lives top-center in portrait). */
  #helpButton     { left: max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)); top: calc(max(1.5rem, calc(env(safe-area-inset-top) + 0.5rem)) + 58px); }
  #centerButton   { left: max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)); top: calc(max(1.5rem, calc(env(safe-area-inset-top) + 0.5rem)) + 116px); }
  #fullscreenButton { left: max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem)); top: calc(max(1.5rem, calc(env(safe-area-inset-top) + 0.5rem)) + 174px); }
  /* Key indicator drops below the top handle. */
  #key-indicator { top: calc(max(1.5rem, env(safe-area-inset-top)) + 58px); }
}
#backButton {
  left: max(1.5rem, calc(env(safe-area-inset-left) + 0.5rem));
  top:  max(1.5rem, calc(env(safe-area-inset-top)  + 0.5rem));
}

/* ─── Reduced motion ────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .loading-spinner { animation: none; }
  #backButton:hover svg { transform: none; }
}
