:root {
  color-scheme: dark;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  --board-r: 0;
  --board-g: 0;
  --board-b: 0;
  --flash-r: 255;
  --flash-g: 255;
  --flash-b: 255;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; background: #000; }
body { overscroll-behavior: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button { font: inherit; }

#app {
  position: fixed;
  inset: 0;
  min-height: 100dvh;
  background: rgb(var(--board-r), var(--board-g), var(--board-b));
  transition: background-color 420ms cubic-bezier(.2,.75,.25,1);
  isolation: isolate;
}
#app.ending { transition-duration: 1600ms; transition-timing-function: ease-in-out; }

#app::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background:
    radial-gradient(ellipse 110% 48% at 50% 50%, rgba(255,255,255,.018), transparent 70%),
    linear-gradient(to bottom, rgba(255,255,255,.018), transparent 14%, transparent 86%, rgba(255,255,255,.02));
  mix-blend-mode: screen;
}

.edge-flash {
  position: absolute;
  left: 0;
  right: 0;
  height: 70%;
  opacity: 0;
  pointer-events: none;
  z-index: 4;
  filter: saturate(1.18);
  will-change: opacity, transform;
}

.edge-flash.top {
  top: -10%;
  transform-origin: top center;
  background: radial-gradient(
    ellipse 76% 90% at 50% 0%,
    rgba(var(--flash-r), var(--flash-g), var(--flash-b), 1) 0%,
    rgba(var(--flash-r), var(--flash-g), var(--flash-b), .78) 24%,
    rgba(var(--flash-r), var(--flash-g), var(--flash-b), .3) 55%,
    transparent 88%
  );
}

.edge-flash.bottom {
  bottom: -10%;
  transform-origin: bottom center;
  background: radial-gradient(ellipse 76% 90% at 50% 100%, rgba(var(--flash-r), var(--flash-g), var(--flash-b), 1) 0%, rgba(var(--flash-r), var(--flash-g), var(--flash-b), .78) 24%, rgba(var(--flash-r), var(--flash-g), var(--flash-b), .3) 55%, transparent 88%);
}

.edge-flash.fire {
  animation: flare 760ms cubic-bezier(.16,.76,.28,1) both;
}

@keyframes flare {
  0% { opacity: 0; transform: scaleY(.35); }
  18% { opacity: .98; transform: scaleY(1); }
  55% { opacity: .58; }
  100% { opacity: 0; transform: scaleY(1.14); }
}

.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(110px, calc(env(safe-area-inset-bottom) + 96px));
  display: grid;
  grid-template-columns: repeat(3, 62px);
  justify-content: center;
  gap: clamp(28px, 9vw, 48px);
  padding: 0 18px;
  z-index: 8;
}

.color-button {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 999px;
  border: 0;
  outline: 0;
  -webkit-tap-highlight-color: transparent;
  box-shadow: none;
  background-image: none;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity 160ms ease, transform 110ms ease;
}

.color-button:disabled { cursor: default; }
.color-button:not(:disabled):active { transform: scale(.94); }
.color-slot[data-slot="R"] { --slot-color: #ff414b; }
.color-slot[data-slot="G"] { --slot-color: #33e976; }
.color-slot[data-slot="B"] { --slot-color: #4384ff; }
.color-button { background: var(--slot-color); }
.color-slot { position: relative; min-width: 0; }
.orbit { position: absolute; inset: -8px; border: 2px solid var(--slot-color); border-radius: 50%; transform: scale(.7); opacity: 0; pointer-events: none; transition: opacity 410ms ease, transform 410ms cubic-bezier(.2,.75,.3,1); }
.color-slot.won .orbit { opacity: 1; transform: scale(1); }
.orbit i { position: absolute; top: -12px; left: calc(50% + var(--marker-offset, 0px)); width: 9px; height: 9px; border: 1.5px solid var(--slot-color); border-radius: 50%; background: transparent; transform: translate(-50%, -50%); }
.color-slot[data-slot="G"] .orbit i:first-child { --marker-offset: -8px; }
.color-slot[data-slot="G"] .orbit i:nth-child(2) { --marker-offset: 8px; }
.color-slot[data-slot="B"] .orbit i:first-child { --marker-offset: -16px; }
.color-slot[data-slot="B"] .orbit i:nth-child(3) { --marker-offset: 16px; }

.restart-button {
  position: absolute;
  z-index: 9;
  left: 50%;
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 8px));
  transform: translateX(-50%);
  width: 48px;
  min-height: 48px;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: white;
  opacity: .96;
  mix-blend-mode: difference;
  cursor: pointer;
}
.restart-button svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
button:focus { outline: none; }
button:focus-visible { outline: 1px solid rgba(255,255,255,.8); outline-offset: 5px; }
.color-button:focus-visible { outline: none; }
[hidden] { display: none !important; }
.minimal-dialog { position: fixed; margin: auto; width: min(320px, calc(100% - 32px)); max-height: calc(100dvh - 32px); padding: 36px 20px 24px; border: 1px solid #252525; border-radius: 0; background: #000; color: #fff; }
.minimal-dialog::backdrop { background: rgba(0,0,0,.86); }
.dialog-close { position: absolute; top: 0; right: 0; width: 36px; height: 36px; padding: 0; display: grid; place-items: center; border: 0; background: transparent; color: #aaa; font-size: 23px; line-height: 1; cursor: pointer; }
.clear-dialog { padding: 30px 24px 16px; text-align: center; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.clear-visual { position: relative; width: 100%; margin: 0 auto 10px; overflow: hidden; }
.clear-image { display: block; width: 100%; height: auto; margin: 0; }
.clear-save-note { margin: 0 0 12px; color: #aaa; font-size: 11px; line-height: 1.5; }
.clear-dialog h1 { margin: 0; font-size: 34px; font-weight: 500; letter-spacing: .28em; text-indent: .28em; }
.clear-label { margin: 8px 0 24px; color: #aaa; font-size: 12px; letter-spacing: .3em; text-indent: .3em; }
.modal-actions { display: grid; }
.modal-actions button, .modal-actions a { display: grid; place-items: center; min-height: 46px; padding: 10px; border: 0; border-top: 1px solid #252525; border-radius: 0; background: transparent; color: white; font: inherit; font-size: 14px; text-decoration: none; cursor: pointer; }
.modal-actions button:hover, .modal-actions a:hover { color: #aaa; }
#rules-action { display: block; width: 100%; min-height: 48px; margin-top: 14px; border: 0; border-top: 1px solid #333; background: transparent; color: #fff; font: inherit; font-size: 15px; cursor: pointer; }
.sound-note { margin: 18px 0 0; text-align: center; color: #bbb; font-size: 12px; line-height: 1.4; }
#explanation-dialog { width: min(520px, calc(100% - 32px)); padding: 40px 20px 24px; overflow: hidden; }
#explanation-content p:last-child { margin-bottom: 0; }
.remaining-sequence { text-align: center; font-variant-numeric: tabular-nums; }
.color-mapping { margin: 0 0 20px; padding-left: 24px; }
.rules-lines { display: grid; gap: 14px; }
.rules-lines p { display: flex; align-items: center; gap: 12px; margin: 0; font-size: 16px; line-height: 1.6; }
.rule-number { flex: 0 0 12px; font-weight: 600; }
.rule-number.red { color: #ff414b; }
.rule-number.green { color: #33e976; }
.rule-number.blue { color: #4384ff; }
.retry-inline { display: inline-block; vertical-align: -.18em; width: 1.15em; height: 1.15em; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.stats-dialog { width: min(330px, calc(100% - 32px)); padding: 52px 24px 36px; }
.stats-dialog pre { margin: 0; color: white; font: 14px/2 ui-sans-serif, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif; white-space: pre-wrap; }

.debug {
  display: none;
  position: absolute;
  left: 12px;
  top: 12px;
  z-index: 30;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(0,0,0,.55);
  color: white;
  font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre;
}
.debug.on { display: block; }

@media (max-width: 390px) {
  .controls { grid-template-columns: repeat(3, 58px); gap: 28px; }
}

/* Optional aids stay invisible until enabled in Settings. */
.text-info .color-button::after { content: attr(data-color); color: #fff; font-size: 20px; font-weight: 600; }
.text-info .color-button[data-color="G"]::after { color: #000; }
.move-letter { position: absolute; left: 50%; transform: translateX(-50%); z-index: 7; color: white; mix-blend-mode: difference; font-size: 24px; font-weight: 600; pointer-events: none; }
.move-letter.top { top: 16%; }
.move-letter.bottom { bottom: 28%; }
.note-ball { position: absolute; top: 0; left: 50%; width: 7px; height: 7px; border-radius: 50%; background: currentColor; box-shadow: 0 0 9px 2px currentColor; z-index: 6; pointer-events: none; }
.toggle-state { border: 1px solid #555; border-radius: 20px; min-width: 42px; padding: 4px 7px; font-size: 10px; text-align: center; color: #aaa; }
.toggle-state::after { content: 'OFF'; }
[aria-checked="true"] .toggle-state { background: #fff; border-color: #fff; color: #000; }
[aria-checked="true"] .toggle-state::after { content: 'ON'; }

#explanation-content { max-height: calc(100dvh - 112px); overflow-y: auto; overscroll-behavior: contain; font-size: 16px; }

/* v5: compact Rules / Settings UI */
.settings-button {
  position: absolute;
  z-index: 9;
  top: max(14px, calc(env(safe-area-inset-top) + 8px));
  right: 16px;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: #fff;
  opacity: .96;
  mix-blend-mode: difference;
  cursor: pointer;
  line-height: 1;
}
.info-icon { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.dialog-info-button { position: absolute; z-index: 2; top: 3px; right: 3px; width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border: 0; background: transparent; color: #aaa; cursor: pointer; }
.dialog-info-button:hover, .dialog-info-button:focus-visible { color: #fff; }
.dialog-info-button .info-icon { width: 21px; height: 21px; }
.dialog-title { margin: 0 0 20px; font-size: 16px; font-weight: 500; text-align: center; letter-spacing: .04em; }
#rules-dialog { width: min(340px, calc(100% - 32px)); padding: 28px 18px 20px; }
.auxiliary-settings { margin-top: 16px; border-top: 1px solid #252525; border-bottom: 1px solid #252525; }
.auxiliary-settings summary { position: relative; list-style: none; padding: 13px 26px 13px 0; cursor: pointer; }
.auxiliary-settings summary::-webkit-details-marker { display: none; }
.auxiliary-settings summary::after { content: '⌄'; position: absolute; top: 13px; right: 2px; color: #888; transform-origin: center; transition: transform 160ms ease; }
.auxiliary-settings[open] summary::after { transform: rotate(180deg); }
.aux-title { display: block; color: #eee; font-size: 13px; line-height: 1.4; }
.aux-summary { display: block; margin-top: 2px; color: #888; font-size: 10px; line-height: 1.45; }
.auxiliary-panel { padding: 0 0 9px; }
.aux-warning { margin: 0 0 8px; color: #999; font-size: 10px; line-height: 1.45; }
.setting-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px; min-height: 54px; }
.setting-copy strong, .setting-copy small { display: block; }
.setting-copy strong { color: #ddd; font-size: 13px; font-weight: 500; }
.setting-copy small { margin-top: 2px; color: #888; font-size: 10px; line-height: 1.45; }
.setting-toggle { display: flex; align-items: center; justify-content: center; width: auto; min-width: 50px; min-height: 40px; padding: 4px; border: 0; background: transparent; color: #ddd; cursor: pointer; }
.language-setting { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 52px; color: #ddd; font-size: 13px; }
.language-setting select { max-width: 155px; border: 1px solid #333; border-radius: 0; padding: 7px 28px 7px 9px; background: #080808; color: #fff; font: inherit; font-size: 12px; }
.translation-note { margin: -7px 0 13px; color: #777; font-size: 9px; line-height: 1.45; text-align: right; }
.translation-note[hidden] { display: none; }
#explanation-content { line-height: 1.72; }
#explanation-content p { margin: 0 0 13px; }
#explanation-content strong { font-weight: 600; }
.red-text { color: #ff414b; }
.green-text { color: #33e976; }
.blue-text { color: #4384ff; }
