:root {
  color-scheme: dark;
  --bg: #12151c;
  --panel: #1a1e27;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --sub: #8b92a3;
  --accent: #5b8cff;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic", sans-serif;
}

.app {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
}

.panel {
  width: 320px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
  max-height: 100vh;
  max-height: 100dvh;
}

.panel h1 {
  font-size: 18px;
  margin: 0 0 2px;
}

.panel .sub {
  font-size: 12px;
  color: var(--sub);
  margin: 0 0 18px;
}

.panel section {
  border-top: 1px solid var(--border);
  padding: 14px 0;
}

.panel section:first-of-type {
  border-top: none;
  padding-top: 0;
}

.panel h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--sub);
  margin: 0 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel label {
  display: block;
  font-size: 13px;
  margin-bottom: 12px;
}

.panel label:last-child {
  margin-bottom: 0;
}

.panel input[type="range"] {
  width: 100%;
  margin-top: 4px;
  accent-color: var(--accent);
}

.panel input[type="number"],
.panel input[type="color"],
.panel select {
  width: 100%;
  margin-top: 4px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
}

.panel input[type="color"] {
  padding: 2px;
  height: 32px;
}

.variant-row {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

.variant-row select {
  margin-top: 0;
  flex: 1;
  min-width: 0;
}

.variant-row button {
  flex: none;
  width: auto;
  padding: 6px 10px;
  font-size: 12px;
  white-space: nowrap;
}

.val {
  float: right;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.row {
  display: flex;
  gap: 8px;
}

button {
  flex: 1;
  background: #262c38;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 9px 10px;
  font-size: 13px;
  cursor: pointer;
}

button:hover { background: #2e3543; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

button.primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #08101f;
  font-weight: 600;
  margin-top: 4px;
}

button.primary:hover { background: #6f9aff; }

.status {
  font-size: 12px;
  color: var(--sub);
  min-height: 16px;
  margin: 8px 0 0;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100vh;
  height: 100dvh;
}

.stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  min-height: 0;
}

/* height:100% (not max-height) so it always fills whatever room .stage
   actually has left after the toolbar - #scene's own max-height:100% then
   has a real number to resolve against instead of a hardcoded vh guess that
   goes stale whenever the toolbar's height changes. Centers its own content
   since a portrait canvas capped by height won't otherwise fill this box. */
.canvas-wrap {
  position: relative;
  max-width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

#scene {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  border-radius: 4px;
  box-shadow: 0 0 0 1px var(--border);
  display: block;
}

.toolbar {
  flex-shrink: 0;
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px 20px;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.tbtn {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: auto;
  white-space: nowrap;
}


.tb-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--sub);
  margin: 0;
  white-space: nowrap;
}

.tb-field select,
.tb-field input[type="range"] {
  width: auto;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 6px;
  font-size: 12px;
}

.tb-field input[type="range"] {
  border: none;
  padding: 0;
  width: 100px;
  accent-color: var(--accent);
}


.inline-check {
  display: flex;
  align-items: center;
  gap: 6px;
}

.inline-check input {
  width: auto;
  margin: 0;
}

.ball-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ball-card {
  border: 1px solid var(--border);
  border-left: 3px solid var(--card-accent, var(--accent));
  border-radius: 6px;
  padding: 10px;
  background: #161a22;
}

.ball-card.is-off {
  opacity: 0.45;
}

.ball-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.ball-card-head .name {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
}

.ball-card-head .name input[type="checkbox"] {
  width: auto;
}

.ball-swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.ball-card-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 8px;
}

.ball-card-grid label {
  font-size: 11px;
  color: var(--sub);
  margin-bottom: 0;
}

.ball-card-grid input,
.ball-card-grid select {
  width: 100%;
  margin-top: 2px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 6px;
  font-size: 12px;
}

.ball-card-grid input[type="number"] {
  cursor: ns-resize;
}

body.dragging-number,
body.dragging-number * {
  cursor: ns-resize !important;
  user-select: none !important;
}

.ball-card-grid .full {
  grid-column: 1 / -1;
}

.ball-advanced {
  margin-top: 8px;
}

.ball-advanced summary {
  font-size: 11px;
  color: var(--sub);
  cursor: pointer;
  user-select: none;
}

.ball-advanced summary:hover {
  color: var(--text);
}

.ball-advanced .ball-card-grid {
  margin-top: 8px;
}

.dice-btn {
  flex: none;
  width: auto;
  padding: 2px 8px;
  font-size: 13px;
  line-height: 1.4;
}


.app-version {
  font-size: 11px;
  color: var(--sub);
  margin: 18px 0 4px;
  text-align: center;
}

.app-version a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  padding: 6px 2px; /* a comfortable tap target on phones */
}
.app-version a:hover { color: var(--text); }

/* ---- Phones / narrow screens ------------------------------------------------
   The canvas + transport bar stay pinned to the top while the settings scroll
   underneath, so you can tweak things while watching the result. */
@media (max-width: 800px) {
  .app {
    flex-direction: column;
  }

  .main {
    order: -1;
    position: sticky;
    top: 0;
    z-index: 10;
    height: auto;
    flex: none;
    background: var(--bg);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45);
  }

  .stage {
    flex: none;
    height: 42vh;
    height: 42svh; /* svh, not dvh: must not change when a mobile address bar slides away */
    padding: 8px;
  }

  .toolbar {
    padding: 8px 12px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .toolbar-row {
    gap: 8px;
  }

  .tb-field input[type="range"] {
    width: 90px;
  }

  .panel {
    width: 100%;
    max-height: none;
    overflow-y: visible;
    border-right: none;
    padding: 16px 14px calc(24px + env(safe-area-inset-bottom));
  }

  /* touch-sized controls; 16px text keeps iOS Safari from zooming in on focus */
  button,
  .panel select,
  .panel input[type="number"],
  .tb-field select {
    min-height: 44px;
    font-size: 16px;
  }

  .ball-card-grid label {
    font-size: 12px;
  }

  .ball-card-grid input,
  .ball-card-grid select {
    font-size: 16px;
    padding: 8px;
    min-height: 44px;
  }

  .panel input[type="range"] {
    height: 32px;
  }

  .dice-btn {
    min-height: 36px;
    padding: 4px 12px;
  }
}

/* +/- steppers wrapped around number inputs on touch devices, where
   drag-to-scrub and mouse-wheel adjusting aren't available */
.stepper {
  display: flex;
  gap: 4px;
  margin-top: 2px;
}

.stepper input {
  flex: 1;
  min-width: 0;
  margin-top: 0 !important;
  text-align: center;
}

.stepper button {
  flex: none;
  width: 44px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
}

.tbtn.is-on {
  border-color: var(--accent);
  color: var(--accent);
}

/* display:flex etc. on a class would otherwise beat the hidden attribute */
[hidden] { display: none !important; }

/* the canvas is a touch surface: no page scroll/zoom/long-press menus on it */
#scene {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.tap-hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
  animation: hint-pulse 2.4s ease-in-out infinite;
}

@keyframes hint-pulse {
  0%, 100% { opacity: 0.95; }
  50% { opacity: 0.55; }
}

/* text for search engines, AI crawlers and screen readers only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* "Tap to start" cover - the balls keep moving (silently) underneath it */
.start-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: rgba(8, 12, 22, 0.38);
  color: #fff;
  font: inherit;
  line-height: 1.4;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.6s ease, visibility 0.6s;
}
.start-overlay.is-gone { opacity: 0; visibility: hidden; pointer-events: none; }
.start-overlay .play {
  display: grid;
  place-items: center;
  width: 76px;
  height: 76px;
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  font-size: 28px;
  padding-left: 5px; /* the triangle looks off-centre otherwise */
  animation: start-pulse 2.2s ease-in-out infinite;
}
.start-overlay .big {
  font-size: clamp(22px, 5.5vw, 34px);
  font-weight: 700;
  letter-spacing: 0.1em;
  text-shadow: 0 2px 14px rgba(0, 0, 0, 0.55);
}
.start-overlay .small { font-size: clamp(12px, 2.8vw, 15px); opacity: 0.8; letter-spacing: 0.08em; }
@keyframes start-pulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.35); }
  50% { transform: scale(1.07); box-shadow: 0 0 0 14px rgba(255, 255, 255, 0); }
}
@media (prefers-reduced-motion: reduce) {
  .start-overlay .play, .tap-hint { animation: none; }
}

/* ---- Toolbar buttons: icon + label (label hidden on phones) ------------------ */
.tbtn .ico { font-size: 1.1em; line-height: 1; }

/* ---- Mood picker ------------------------------------------------------------- */
.mood-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
  max-width: 560px; /* don't stretch five pills across a wide desktop toolbar */
}

.mood-btn {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 2px;
  border-radius: 10px;
  font-size: 11px;
  line-height: 1.2;
  white-space: nowrap;
}

.mood-btn .ico { font-size: 20px; line-height: 1; }

.mood-btn.is-active {
  border-color: var(--accent);
  background: rgba(91, 140, 255, 0.2);
}

/* in auto mode, the mood it currently picked gets a lighter outline */
.mood-btn.is-current {
  box-shadow: inset 0 0 0 1px var(--accent);
}

@media (max-width: 800px) {
  .tbtn .lbl { display: none; }
  .tbtn { width: 48px; padding: 0; justify-content: center; }
  .toolbar-row { flex-wrap: nowrap; }
  .tb-volume { flex: 1; min-width: 0; }
  .tb-volume .val { display: none; }
  .tb-volume input[type="range"] { flex: 1; width: auto; min-width: 60px; }
  .mood-btn { min-height: 54px; }
}

