:root {
  --bg: #1a0f07;
  --bg2: #2a170a;
  --ink: #f6e7cc;
  --muted: #c9ae86;
  --accent: #ffd37a;
  --btn: rgba(255, 225, 170, 0.08);
  --btn-hi: rgba(255, 225, 170, 0.18);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: radial-gradient(ellipse at 50% 30%, var(--bg2), var(--bg) 70%);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

/* Northern Lights spell: a video layer behind everything, faded in with body.aurora.
   The gradient on top keeps the header and footer text readable. */
.sky {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  overflow: hidden;
  opacity: 0;
  transition: opacity 2.5s ease;
}

.sky-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 15% 40%; /* the brightest band is on the left; matters on tall phone screens */
}

.sky::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(6, 8, 18, 0.72), rgba(6, 8, 18, 0.25) 28%, rgba(6, 8, 18, 0.3) 70%, rgba(6, 8, 18, 0.8));
}

body.aurora .sky {
  opacity: var(--aurora, 0.8);
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: max(10px, env(safe-area-inset-top)) 16px 8px;
}

h1 {
  font-size: 18px;
  margin: 0;
  letter-spacing: 0.02em;
}

h1 span {
  font-weight: 400;
  color: var(--muted);
  font-size: 13px;
  margin-left: 6px;
}

#status {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  flex: 1;
}

nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

button {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--btn);
  border: 1px solid rgba(255, 225, 170, 0.2);
  border-radius: 8px;
  padding: 6px 11px;
  cursor: pointer;
}

button:hover {
  background: var(--btn-hi);
}

button:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button[aria-pressed='true'],
button[aria-checked='true'] {
  background: rgba(255, 211, 122, 0.22);
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.75;
  cursor: default;
}

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 28px;
  padding: 0 16px 8px;
}

/* Control groups (Demo, Minipeli, Taiat): a heading and its controls, kept together */
.group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.group .row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.spells .row {
  gap: 6px 26px;
}

select {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--bg2);
  border: 1px solid rgba(255, 225, 170, 0.2);
  border-radius: 8px;
  padding: 5px 8px;
}

.group h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.spell {
  display: flex;
  align-items: center;
  gap: 8px;
}

.difficulty {
  display: flex;
  align-items: center;
  gap: 8px;
}

.difficulty input[type='range'] {
  width: 64px;
  accent-color: #8fe9ff;
}

.difficulty input:disabled {
  opacity: 0.5;
}

.difficulty output {
  min-width: 7.5em;
  font-size: 13px;
  color: #bfefff;
}

.spell input[type='range'] {
  width: 96px;
  accent-color: var(--accent);
  opacity: 0.45;
  transition: opacity 0.2s;
}

.spell button[aria-pressed='true'] + input {
  opacity: 1;
}

.spell button[aria-pressed='true'] {
  box-shadow: 0 0 14px rgba(255, 211, 122, 0.35);
}

main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.stage {
  flex: 1;
  min-height: 0;
  position: relative;
}

canvas {
  position: absolute;
  inset: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Lever switches for keyboard / screen-reader users; the canvas mirrors their state.
 */
.levers {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 0 16px;
  height: 0;
  overflow: hidden;
}

.levers[hidden] {
  display: none;
}

.levers:focus-within {
  height: auto;
  padding: 6px 16px max(10px, env(safe-area-inset-bottom));
}

footer {
  padding: 4px 16px max(6px, env(safe-area-inset-bottom));
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

footer a {
  color: var(--accent);
  text-decoration-color: rgba(255, 211, 122, 0.45);
  text-underline-offset: 2px;
}

/* on narrow screens the coffee line gets its own row */
@media (max-width: 640px) {
  footer .coffee {
    display: block;
  }
  /* the line break separates the two sentences visually */
  footer .coffee .sep {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

footer a:hover {
  text-decoration-color: var(--accent);
}

#swearing[aria-checked='false'] {
  color: #c8ffd9;
  border-color: rgba(150, 255, 190, 0.45);
}

.start {
  position: fixed;
  inset: 0;
  border: 0;
  border-radius: 0;
  background: rgba(16, 9, 4, 0.82);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  font-size: 22px;
  letter-spacing: 0.03em;
}

.start[hidden] {
  display: none;
}

.start:hover {
  background: rgba(16, 9, 4, 0.78);
}

.start .bar {
  width: min(280px, 70vw);
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 225, 170, 0.15);
  overflow: hidden;
}

.start .bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.2s;
}

.menu-toggle {
  display: none;
}

/* Small screens (phones, either way round): one compact header row, the controls
   behind a menu button, so the instrument gets nearly the whole screen. */
@media (max-width: 640px), (max-height: 500px) {
  header {
    flex-wrap: nowrap;
    gap: 10px;
    padding: max(6px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  }
  h1 {
    font-size: 16px;
    flex: none;
  }
  h1 span {
    display: none;
  }
  #status {
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 12px;
  }
  .menu-toggle {
    display: inline-block;
    flex: none;
  }
  body:not(.menu-open) #controls-nav,
  body:not(.menu-open) #controls-bar {
    display: none;
  }
  body.menu-open header {
    flex-wrap: wrap;
  }
  body.menu-open #controls-nav {
    flex-basis: 100%;
  }
  .bar {
    padding: 0 max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
    gap: 8px 20px;
  }
  footer {
    padding: 1px 12px max(3px, env(safe-area-inset-bottom));
    font-size: 11px;
  }
}

/* Phones held upright: each group is a block with its heading on top and all of its
   controls on one row; each spell's button sits above its own slider. */
@media (max-width: 640px) {
  .bar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .group {
    display: block;
  }
  .group h2 {
    margin-bottom: 5px;
  }
  .group .row {
    display: grid;
    gap: 6px;
  }
  .demo .row {
    grid-template-columns: 1fr auto;
  }
  .game .row {
    grid-template-columns: auto minmax(0, 1fr) auto;
  }
  .difficulty {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    min-width: 0;
  }
  .difficulty input[type='range'] {
    width: 100%;
    margin: 0;
  }
  .difficulty output {
    min-width: 0;
    font-size: 12px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .spells .row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .spell {
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
  }
  .spell button {
    padding: 6px 4px;
  }
  .spell input[type='range'] {
    width: 100%;
    margin: 0;
  }
}

/* ------------------------------------------------------------------ Loitsu minigame */
.rite-canvas {
  pointer-events: none;
  width: 100%;
  height: 100%;
}

.rite {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.rite[hidden] {
  display: none;
}

.rite-hud {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: min(560px, calc(100% - 32px));
  text-align: center;
}

.rite-round {
  display: block;
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #bfefff;
  text-shadow: 0 0 8px rgba(120, 220, 255, 0.8);
  margin-bottom: 6px;
}

.rite-bar {
  position: relative;
  height: 14px;
  border-radius: 7px;
  background: rgba(10, 20, 40, 0.75);
  border: 1px solid rgba(170, 240, 255, 0.45);
  box-shadow: 0 0 18px rgba(120, 220, 255, 0.25), inset 0 0 8px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.rite-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #3ad6a0, #8fe9ff 55%, #e9c2ff);
  background-size: 200% 100%;
  animation: rite-flow 2.2s linear infinite;
  box-shadow: 0 0 14px rgba(150, 255, 220, 0.9);
  transition: width 0.25s ease-out;
}

.rite-bar.full {
  border-color: #fff3c4;
  box-shadow: 0 0 26px rgba(255, 236, 160, 0.9);
}

.rite-bar.hurt {
  animation: rite-hurt 0.35s ease-out;
}

@keyframes rite-flow {
  to {
    background-position: -200% 0;
  }
}

@keyframes rite-hurt {
  0% {
    border-color: #ff6a50;
    box-shadow: 0 0 22px rgba(255, 90, 60, 0.9);
    transform: translateX(-4px);
  }
  50% {
    transform: translateX(4px);
  }
  100% {
    transform: none;
  }
}

.rite-msg {
  position: absolute;
  top: 9%;
  left: 0;
  right: 0;
  text-align: center;
}

.rite-word {
  display: inline-block;
  font-family: Georgia, 'Times New Roman', serif;
  font-weight: 700;
  color: #eafcff;
  text-shadow: 0 0 12px rgba(120, 220, 255, 0.95), 0 0 34px rgba(120, 255, 200, 0.6);
  animation: rite-word 1.6s ease-out forwards;
}

.rite-word.count {
  font-size: 64px;
  animation-duration: 0.6s;
}

.rite-word.title,
.rite-word.round {
  font-size: 38px;
  letter-spacing: 0.06em;
}

.rite-word.lever {
  font-size: 30px;
  color: #c8ffd9;
  animation-duration: 3.2s;
}

.rite-word.win {
  font-size: 52px;
  color: #fff6d0;
  text-shadow: 0 0 16px #ffe08a, 0 0 50px #8fffc0;
  animation-duration: 3.2s;
}

.rite-word.lose {
  font-size: 44px;
  color: #ffd0c0;
  text-shadow: 0 0 14px rgba(255, 90, 60, 0.9);
  animation-duration: 3s;
}

@keyframes rite-word {
  0% {
    opacity: 0;
    transform: scale(0.6);
    filter: blur(6px);
  }
  18% {
    opacity: 1;
    transform: scale(1.05);
    filter: blur(0);
  }
  75% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(1.15) translateY(-12px);
    filter: blur(4px);
  }
}

.stage.shake {
  animation: shake 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes shake {
  10%,
  90% {
    transform: translateX(-2px);
  }
  20%,
  80% {
    transform: translateX(5px);
  }
  30%,
  50%,
  70% {
    transform: translateX(-9px);
  }
  40%,
  60% {
    transform: translateX(9px);
  }
}

/* thought bubbles */
.bubbles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.bubble {
  position: absolute;
  padding: 10px 18px;
  border-radius: 50% / 46%;
  background: #fffaf0;
  color: #2a1a0a;
  font: 700 18px/1.15 'Comic Sans MS', 'Chalkboard SE', 'Segoe Print', system-ui, sans-serif;
  white-space: nowrap;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  transform-origin: 0% 100%;
  animation: bubble-life 2.2s ease-out forwards;
}

.bubble .trail {
  position: absolute;
  border-radius: 50%;
  background: #fffaf0;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.35);
}

.bubble .t0 {
  width: 14px;
  height: 14px;
  left: 10px;
  bottom: -14px;
}

.bubble .t1 {
  width: 9px;
  height: 9px;
  left: 0;
  bottom: -26px;
}

.bubble .t2 {
  width: 5px;
  height: 5px;
  left: -8px;
  bottom: -34px;
}

.bubble.tier1 {
  font-size: 21px;
}

.bubble.tier2 {
  font-size: 25px;
  background: #fff2d6;
}

.bubble.tier3 {
  font-size: 29px;
  background: #ffe1c8;
  color: #6a1408;
}

.bubble.tier4 {
  font-size: 34px;
  background: #ffcab8;
  color: #8a0a00;
  animation: bubble-life 2.4s ease-out forwards, bubble-rage 0.12s linear 0.25s 8;
}

.bubble.tier3 .trail,
.bubble.tier4 .trail {
  background: inherit;
}

@keyframes bubble-life {
  0% {
    opacity: 0;
    transform: scale(0.2) rotate(var(--tilt));
  }
  12% {
    opacity: 1;
    transform: scale(1.12) rotate(var(--tilt));
  }
  20% {
    transform: scale(1) rotate(var(--tilt));
  }
  80% {
    opacity: 1;
    transform: translateY(-14px) rotate(var(--tilt));
  }
  100% {
    opacity: 0;
    transform: translateY(-30px) scale(0.95) rotate(var(--tilt));
  }
}

@keyframes bubble-rage {
  50% {
    margin-left: 3px;
    margin-top: -2px;
  }
}

/* video of the completed working */
.yt-modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(40, 90, 80, 0.55), rgba(3, 6, 14, 0.94));
  animation: yt-in 0.6s ease-out;
}

.yt-modal.closing {
  animation: yt-out 0.4s ease-in forwards;
}

.yt-frame {
  width: min(960px, calc(100vw - 32px));
  aspect-ratio: 16 / 9;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 0 60px rgba(143, 255, 192, 0.45);
}

.yt-frame iframe,
.yt-slot {
  width: 100%;
  height: 100%;
  border: 0;
  display: grid;
  place-items: center;
  color: var(--ink);
}

.yt-close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 16px;
  font-size: 18px;
}

@keyframes yt-in {
  from {
    opacity: 0;
  }
}

@keyframes yt-out {
  to {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .stage.shake,
  .bubble.tier4,
  .rite-bar.hurt {
    animation: none;
  }
  .bubble {
    animation: bubble-life 2.2s linear forwards;
  }
}

/* thought bubbles on phones: same escalation, about two thirds the size */
@media (max-width: 640px), (max-height: 500px) {
  .bubble {
    font-size: 13px;
    padding: 7px 13px;
  }
  .bubble.tier1 {
    font-size: 14px;
  }
  .bubble.tier2 {
    font-size: 16px;
  }
  .bubble.tier3 {
    font-size: 19px;
  }
  .bubble.tier4 {
    font-size: 22px;
  }
}
