/*
 * Product chrome: startup overlay, control bar, paint-mode panel.
 * (The admin tuning panel is a dev tool and deliberately not styled here.)
 *
 * The chrome is mood-tinted: main.js writes the selected scale's mood hue
 * into --mood-hue, and every border, accent, and surface tint derives from
 * it, so the chrome reads as part of the organism rather than a UI slapped
 * on top. Everything else stays a dark, low-contrast glass so the canvas
 * keeps the lead.
 *
 * Corner radius rule: panels 12px, controls inside them 8px.
 */

/* Sofia Sans (SIL Open Font License), self-hosted so the page stays a
 * static bundle with no third-party requests. Variable weight 200-500;
 * latin-ext carries the Polish diacritics the title needs. */
@font-face {
  font-family: 'Sofia Sans';
  font-style: normal;
  font-weight: 200 500;
  font-display: swap;
  src: url('../../assets/fonts/sofia-sans-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193,
    U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
  font-family: 'Sofia Sans';
  font-style: normal;
  font-weight: 200 500;
  font-display: swap;
  src: url('../../assets/fonts/sofia-sans-latin-ext.woff2') format('woff2');
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF,
    U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020,
    U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

:root {
  /* Fallback hue for the moment before main.js applies the selected scale's
   * mood. Matches palette.js's DEFAULT_HUE so chrome and canvas agree. */
  --mood-hue: 180;

  --chrome-font: 'Sofia Sans', system-ui, sans-serif;
  --chrome-surface: hsla(var(--mood-hue), 30%, 7%, 0.6);
  --chrome-line: hsla(var(--mood-hue), 45%, 75%, 0.22);
  --chrome-line-strong: hsla(var(--mood-hue), 50%, 75%, 0.45);
  --chrome-accent: hsl(var(--mood-hue), 55%, 68%);
  --chrome-text: hsl(var(--mood-hue), 12%, 92%);
  --chrome-text-dim: hsla(var(--mood-hue), 10%, 88%, 0.65);
  --chrome-hover: hsla(var(--mood-hue), 45%, 80%, 0.1);

  --radius-panel: 12px;
  --radius-control: 8px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--chrome-font);
  background: #000;
  color: var(--chrome-text);
}

#p5-container {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
}

.ui-icon {
  width: 17px;
  height: 17px;
  display: block;
  flex: none;
}

/* ---------------------------------------------------------------- overlay */

#audio-unlock-overlay {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at center, hsla(var(--mood-hue), 35%, 14%, 0.55), transparent 70%),
    hsla(var(--mood-hue), 25%, 3%, 0.96);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9000;
  backdrop-filter: blur(10px);
  animation: overlay-in 0.6s ease-out;
}

#audio-unlock-overlay.hidden {
  opacity: 0;
  pointer-events: none;
  animation: overlay-out 0.6s ease-out forwards;
}

@keyframes overlay-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes overlay-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

.overlay-content {
  text-align: center;
  padding: 0 24px;
  animation: overlay-content-in 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes overlay-content-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.overlay-content h1 {
  font-size: clamp(2.75rem, 9vw, 4.5rem);
  font-weight: 200;
  letter-spacing: 0.06em;
  text-transform: lowercase;
  color: var(--chrome-text);
  margin-bottom: 0.75rem;
}

.overlay-content p {
  font-size: 1rem;
  font-weight: 400;
  letter-spacing: 0.04em;
  color: var(--chrome-text-dim);
  margin-bottom: 3rem;
}

/* The overlay's Enter button and the paint panel's action button are the same
   component at two sizes; only type scale and padding differ. */
.overlay-content button,
.panel-btn {
  font: inherit;
  font-weight: 500;
  text-transform: uppercase;
  color: var(--chrome-text);
  background: hsla(var(--mood-hue), 40%, 60%, 0.1);
  border: 1px solid var(--chrome-line-strong);
  border-radius: var(--radius-control);
  cursor: pointer;
  transition: background 0.3s ease, border-color 0.3s ease, transform 0.15s ease;
}

.overlay-content button:hover,
.panel-btn:hover {
  background: hsla(var(--mood-hue), 45%, 65%, 0.18);
  border-color: var(--chrome-accent);
}

.overlay-content button:active,
.panel-btn:active {
  transform: scale(0.98);
}

.overlay-content button {
  font-size: 0.9rem;
  letter-spacing: 0.14em;
  padding: 13px 44px;
}

/* ------------------------------------------------------------ control bar */

#controls-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 5000;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--chrome-surface);
  border: 1px solid var(--chrome-line);
  border-radius: var(--radius-panel);
  backdrop-filter: blur(12px);
}

.ctrl-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.ctrl-sep {
  width: 1px;
  height: 18px;
  background: var(--chrome-line);
  margin: 0 5px;
  flex: none;
}

.ctrl-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-control);
  color: var(--chrome-text-dim);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, transform 0.1s ease;
}

.ctrl-btn:hover {
  background: var(--chrome-hover);
  color: var(--chrome-text);
}

.ctrl-btn:active {
  transform: scale(0.94);
}

/* Focus ring: one treatment for every control in the chrome, keyboard only.
   Two pixels of accent on a dark glass surface is the least it can be and
   still be unmissable. */
.ctrl-btn:focus-visible,
.ctrl-range:focus-visible,
.ctrl-select:focus-visible,
.panel-btn:focus-visible,
.overlay-content button:focus-visible {
  outline: 2px solid var(--chrome-accent);
  outline-offset: 2px;
}

/* Hide-UI: the bar collapses to the bare tuning toggle. That button is a dev
   tool rather than part of the visualization, so it deliberately outlives the
   chrome around it - everything else in the bar goes. The controls are hidden
   one by one rather than by their groups, so the touch rule further down can
   bring one of them back. */
#controls-container.is-ui-hidden {
  gap: 0;
  background: transparent;
  border-color: transparent;
  backdrop-filter: none;
}

#controls-container.is-ui-hidden .ctrl-sep,
#controls-container.is-ui-hidden .ctrl-range,
#controls-container.is-ui-hidden .ctrl-select,
#controls-container.is-ui-hidden .ctrl-btn:not(.ctrl-admin) {
  display: none;
}

/* Toggles in their "off" state (muted, interface hidden) */
.ctrl-btn.is-off {
  color: hsla(var(--mood-hue), 10%, 88%, 0.35);
}

/* Volume slider: native input[type=range], repainted */
.ctrl-range {
  appearance: none;
  -webkit-appearance: none;
  width: 84px;
  height: 32px;
  background: transparent;
  cursor: pointer;
  margin: 0 4px;
}

.ctrl-range::-webkit-slider-runnable-track {
  height: 2px;
  border-radius: 1px;
  background: var(--chrome-line-strong);
}

.ctrl-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  margin-top: -5px;
  border: none;
  border-radius: 50%;
  background: var(--chrome-accent);
}

.ctrl-range::-moz-range-track {
  height: 2px;
  border-radius: 1px;
  background: var(--chrome-line-strong);
}

.ctrl-range::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border: none;
  border-radius: 50%;
  background: var(--chrome-accent);
}

/* Scale select: native select, repainted; the caret is an inline SVG
   background because a <select> can't contain markup */
.ctrl-select {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-size: 12.5px;
  min-width: 118px;
  padding: 6px 26px 6px 10px;
  color: var(--chrome-text);
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 256 256' fill='%23ffffff99'%3E%3Cpath d='M213.66,101.66l-80,80a8,8,0,0,1-11.32,0l-80-80A8,8,0,0,1,53.66,90.34L128,164.69l74.34-74.35a8,8,0,0,1,11.32,11.32Z'/%3E%3C/svg%3E")
    no-repeat right 9px center / 11px 11px;
  background-color: hsla(var(--mood-hue), 35%, 55%, 0.09);
  border: 1px solid var(--chrome-line);
  border-radius: var(--radius-control);
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}

.ctrl-select:hover {
  border-color: var(--chrome-line-strong);
}

/* The dropped-down list itself (browser-drawn, colors only) */
.ctrl-select option {
  background: hsl(var(--mood-hue), 20%, 8%);
  color: var(--chrome-text);
}

/* ------------------------------------------------------- paint-mode panel */

#game-controls {
  position: fixed;
  bottom: 16px;
  left: 16px;
  z-index: 5000;
  width: 264px;
  padding: 14px;
  background: var(--chrome-surface);
  border: 1px solid var(--chrome-line);
  border-radius: var(--radius-panel);
  backdrop-filter: blur(12px);
  font-size: 12px;
  color: var(--chrome-text-dim);
  display: none; /* Hidden by default */
}

#game-controls.is-ui-hidden {
  display: none;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--chrome-text);
  margin-bottom: 12px;
}

.panel-btn {
  letter-spacing: 0.06em;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px;
  margin-bottom: 10px;
}

.panel-btn .ui-icon {
  width: 13px;
  height: 13px;
}

.panel-status {
  text-align: center;
  padding: 6px;
  margin-bottom: 12px;
  font-size: 11.5px;
  color: var(--chrome-accent);
  background: hsla(var(--mood-hue), 40%, 60%, 0.07);
  border-radius: var(--radius-control);
}

.panel-label {
  display: block;
  font-size: 11.5px;
  letter-spacing: 0.02em;
  margin-bottom: 6px;
}

.panel-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.panel-row .ctrl-range {
  flex: 1;
  width: auto;
  margin: 0;
}

.ctrl-range:disabled {
  opacity: 0.4;
  cursor: default;
}

.panel-value {
  width: 26px;
  text-align: right;
  color: var(--chrome-accent);
  font-variant-numeric: tabular-nums;
}

.panel-help {
  font-size: 11.5px;
  line-height: 1.7;
  padding-top: 10px;
  border-top: 1px solid var(--chrome-line);
}

.panel-help kbd {
  font: inherit;
  font-size: 10.5px;
  padding: 1px 5px;
  border: 1px solid var(--chrome-line);
  border-radius: 4px;
  color: var(--chrome-text);
}

/* ----------------------------------------------------------------- mobile */

@media (max-width: 640px) {
  /* Seven controls won't fit one row on a phone. Rather than let a wrapping
     flex row stretch edge to edge, lay them out as a fixed 4-column grid:
     the block then sizes to its content and stays a small panel in the
     corner instead of a bar across the top of the canvas. `display: contents`
     dissolves the desktop groups so the buttons become grid items directly. */
  #controls-container {
    top: 8px;
    right: 8px;
    display: grid;
    grid-template-columns: repeat(4, auto);
    align-items: center;
    justify-items: center;
    gap: 2px;
    max-width: calc(100vw - 16px);
  }

  .ctrl-group {
    display: contents;
  }

  .ctrl-sep {
    display: none;
  }

  .ctrl-range {
    width: 72px;
  }

  #game-controls {
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
  }
}

/* Touch: icon buttons grow to a comfortable tap target. */
@media (pointer: coarse) {
  .ctrl-btn {
    width: 40px;
    height: 40px;
  }

  /* No keyboard means no H, so the hide toggle has to survive the collapse it
     causes - otherwise hiding the interface on a phone is a one-way trip with
     a page reload as the only way out. */
  #controls-container.is-ui-hidden .ctrl-btn.ctrl-hide {
    display: grid;
  }

  .ctrl-select {
    padding-top: 9px;
    padding-bottom: 9px;
  }
}

/* --------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  #audio-unlock-overlay,
  #audio-unlock-overlay.hidden,
  .overlay-content {
    animation: none;
  }

  .overlay-content button,
  .ctrl-btn,
  .panel-btn {
    transition: none;
  }
}
