@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Marck+Script&family=PT+Serif:wght@400;700&display=swap');

:root {
  color-scheme: dark;
  font-family: 'DM Sans', system-ui, sans-serif;
  background: #100f20;
  color: #fff6e8;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-text-size-adjust: 100%;
  --shell: rgba(18, 19, 39, .69);
  --gold: #f7d795;
  --gold-soft: #e7c18a;
  --board-size: min(74svh, 560px, calc(100vw - 44px));
}

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

body {
  min-inline-size: 0;
  min-block-size: 100vh;
  min-block-size: 100svh;
  margin: 0;
  padding: 22px clamp(12px, 4vw, 46px) 14px;
  display: grid;
  place-items: center;
  position: relative;
  overflow-x: hidden;
  background: #100f20;
  isolation: isolate;
}

.atmosphere,
.grain {
  position: fixed;
  inset: 0;
  z-index: -2;
  pointer-events: none;
}

.atmosphere {
  background-image:
    linear-gradient(180deg, rgba(9, 11, 25, .22) 0%, rgba(9, 11, 24, .13) 37%, rgba(9, 10, 22, .53) 100%),
    linear-gradient(90deg, rgba(10, 11, 24, .14), transparent 28%, transparent 72%, rgba(10, 11, 24, .18)),
    radial-gradient(ellipse at 50% 47%, transparent 6%, rgba(10, 11, 24, .23) 84%),
    url('assets/lukomorye-bg.png');
  background-size: cover, cover, cover, cover;
  background-position: center;
  transition: opacity 550ms ease;
  animation: scene-arrive 900ms both;
}

.story-world[data-world="firebird"] .atmosphere {
  background-image:
    linear-gradient(180deg, rgba(6, 12, 34, .20), rgba(7, 11, 28, .36)),
    radial-gradient(ellipse at center, transparent 0%, rgba(6, 11, 29, .25) 82%),
    url('assets/firebird-bg.png');
}

.story-world[data-world="ocean"] .atmosphere {
  background-image:
    linear-gradient(180deg, rgba(7, 20, 36, .22), rgba(5, 20, 36, .40)),
    radial-gradient(ellipse at 52% 49%, transparent 4%, rgba(6, 19, 35, .32) 82%),
    url('assets/ocean-bg.png');
}

.story-world[data-world="baba-yaga"] .atmosphere {
  background-image:
    linear-gradient(180deg, rgba(16, 11, 31, .25), rgba(13, 10, 28, .42)),
    radial-gradient(ellipse at center, transparent 0%, rgba(13, 10, 28, .33) 82%),
    url('assets/baba-yaga-bg.png');
}

.grain {
  z-index: -1;
  opacity: .12;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.82' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.24'/%3E%3C/svg%3E");
  mix-blend-mode: screen;
}

.game {
  inline-size: min(100%, 820px);
  min-inline-size: 0;
  margin-inline: auto;
  animation: game-enter 700ms cubic-bezier(.2, .7, .2, 1) both;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-block-end: 12px;
  padding-inline: clamp(2px, 2vw, 12px);
}

.title, .toolbar, .score-value { display: flex; align-items: center; }
.title { min-inline-size: 0; gap: 9px; }
.title-mark { inline-size: clamp(34px, 6vw, 48px); flex: none; filter: drop-shadow(0 0 13px #a77dff90); }
.title-mark img { display: block; inline-size: 100%; block-size: auto; }
.title-copy { min-inline-size: 0; }

.eyebrow, #score-label, .chapter-number, .frame-ornament, .chapter-dot, .game-footer {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: .13em;
}

.eyebrow {
  margin: 0 0 3px;
  color: #e9c77e;
  font-size: .62rem;
  letter-spacing: .21em;
}

h1, h2 { font-family: 'PT Serif', Georgia, serif; }
h1 { margin: 0; font-size: clamp(1.05rem, 3vw, 1.52rem); line-height: 1.16; letter-spacing: .015em; }
h1 span { color: var(--gold); }

.toolbar { flex: none; justify-content: flex-end; gap: clamp(7px, 1.7vw, 14px); }

.score-card {
  min-inline-size: 102px;
  padding: 5px 14px 7px;
  display: grid;
  justify-items: center;
  border: 1px solid #e9cb904a;
  border-radius: 13px;
  background: linear-gradient(155deg, #31283bdb, #18182ae6);
  box-shadow: 0 8px 24px #08091439, inset 0 1px #fff5dc1a;
  backdrop-filter: blur(14px);
}

#score-label { color: #e8ce9d; font-size: .57rem; letter-spacing: .18em; }
.score-value { min-height: 26px; gap: 4px; color: #fff6e8; font-size: 1.16rem; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; text-shadow: 0 1px #fff5; }
.score-value img { inline-size: 24px; block-size: 24px; object-fit: contain; filter: drop-shadow(0 0 5px #ffc56c5c); }
#score { overflow-wrap: anywhere; }

.restart {
  min-block-size: 40px;
  padding: 8px 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid #f7dcaa9c;
  border-radius: 12px;
  background: linear-gradient(145deg, #f4ddad, #e6c38c 58%, #d9ad74);
  color: #382842;
  box-shadow: 0 6px 20px #09091755, inset 0 1px #fffdf3e0, inset 0 -1px #9e734e5c;
  font: 700 .82rem/1.2 'DM Sans', sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 160ms ease, box-shadow 160ms ease, filter 160ms ease;
}

.restart:disabled { opacity: .65; cursor: default; }
.restart-icon { color: #946c40; font-size: 1rem; }
.restart:active:not(:disabled) { transform: translateY(1px); box-shadow: inset 0 1px 5px #70503a4f; }
@media (hover: hover) {
  .restart:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 9px 24px #09091775, inset 0 1px #fffdf3e0; filter: brightness(1.04); }
}

.stage { display: grid; justify-items: center; }
.chapter-intro { inline-size: min(100%, 550px); min-block-size: 78px; margin: 0 auto 5px; padding-inline: 8px; text-align: center; animation: story-reveal 500ms both; }
.chapter-number { color: var(--gold-soft); font-size: .58rem; letter-spacing: .23em; text-shadow: 0 2px 12px #07131ccc; }
h2 { margin: 2px 0 1px; font-size: clamp(1.25rem, 3vw, 1.75rem); font-weight: 700; letter-spacing: .015em; line-height: 1.13; text-shadow: 0 2px 15px #07131ccc; }
.chapter-intro p { margin: 0; color: #f2e9df; font-size: clamp(.72rem, 1.7vw, .84rem); line-height: 1.35; text-shadow: 0 2px 10px #080b16; }

.board-frame {
  --board-size: min(65svh, 560px, calc(100vw - 56px));
  inline-size: var(--board-size);
  flex: none;
  padding: clamp(8px, 1.8vw, 15px);
  position: relative;
  isolation: isolate;
  border: 1px solid #f1dcb36e;
  border-radius: clamp(15px, 3vw, 24px);
  background: linear-gradient(145deg, #86704bdb, #342e39f5 22%, #191a2bef 54%, #756247eb 100%);
  box-shadow: 0 24px 80px #0507168c, 0 0 58px #ddbc782e, inset 0 0 0 2px #ffffff0c, inset 0 2px 3px #fff4d364, inset 0 -3px 7px #090915a3;
  animation: frame-glow 7s ease-in-out infinite alternate;
}

.board-frame::before {
  content: '';
  position: absolute;
  z-index: -1;
  inset: 5px;
  border: 1px solid #e0c68f36;
  border-radius: inherit;
  background: linear-gradient(135deg, #fff2d609, transparent 42%, #b5c1ff08);
  pointer-events: none;
}

.board-frame::after {
  content: '';
  position: absolute;
  z-index: -2;
  inset: -8px;
  border-radius: 32px;
  background: radial-gradient(ellipse, #b998ff1d 0, #e4c68f0b 48%, transparent 76%);
  filter: blur(10px);
  pointer-events: none;
}

.board {
  display: grid;
  grid-template: repeat(8, minmax(0, 1fr)) / repeat(8, minmax(0, 1fr));
  gap: clamp(3px, .68vw, 5px);
  position: relative;
  aspect-ratio: 1;
  padding: clamp(7px, 1.3vw, 11px);
  overflow: hidden;
  border: 1px solid #9da6cc33;
  border-radius: clamp(10px, 2.4vw, 17px);
  background: linear-gradient(145deg, #14182bea, #101424ed 55%, #111426eb);
  box-shadow: inset 0 0 35px #a3b7ff12, inset 0 2px 5px #040613c7, 0 4px 12px #07081273;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.cell {
  display: grid;
  place-items: center;
  min-inline-size: 0;
  min-block-size: 0;
  padding: 0;
  position: relative;
  border: 1px solid #d2daff0c;
  border-radius: clamp(4px, 1.15vw, 10px);
  background: linear-gradient(145deg, #27304a9c, #171c30c4);
  box-shadow: inset 0 1px #ffffff12, inset 0 -3px 5px #03051224;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 130ms ease, border-color 130ms ease, box-shadow 130ms ease;
}

.cell.alt { background: linear-gradient(145deg, #30334f9c, #1b1d35c9); }
@media (hover: hover) { .board[aria-busy="false"] .cell:hover { background: #8273a459; border-color: #ead8ff38; } }
.cell[aria-pressed="true"] { background: #9276b161; border-color: #f8dfa1bd; box-shadow: inset 0 0 0 1px #fff0d252, 0 0 14px #e8ca8b46; }
.cell[aria-pressed="true"] .gem { filter: drop-shadow(0 3px 4px #030311aa) drop-shadow(0 0 8px #ffe6a6a6); }
.cell:focus-visible { outline: 2px solid #fff0c8; outline-offset: -2px; }
.restart:focus-visible, .chapter-dot:focus-visible { outline: 2px solid #fff0c8; outline-offset: 3px; }
.board[aria-busy="true"] .cell { cursor: default; }

.gem {
  display: block;
  inline-size: 96%;
  block-size: 96%;
  object-fit: contain;
  filter: drop-shadow(0 2px 3px #03031191) drop-shadow(0 0 4px #ffffff24);
  pointer-events: none;
}

.gem[hidden] { display: none; }

.cell[aria-pressed="true"], .cell.is-moving, .cell.is-matching { z-index: 2; }
.cell.is-matching {
  border-color: #fff0bcb3;
  background: #a88b5066;
  box-shadow: inset 0 0 14px #ffe6aa45, 0 0 10px #f9d88355;
}
.cell.is-matching .gem { filter: brightness(1.25) drop-shadow(0 0 6px #ffe8b888); }

.sound-toggle {
  min-block-size: 40px;
  padding: 8px 12px;
  border: 1px solid #e7c18a80;
  border-radius: 12px;
  background: #181b30db;
  color: #f7d795;
  font: 600 .8rem/1.2 system-ui, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
}
.sound-toggle[aria-pressed="false"] { color: #c6c3cf; border-color: #b7b0c450; }
.sound-toggle:focus-visible { outline: 2px solid #fff0c8; outline-offset: 3px; }

.reaction {
  height: 42px;
  padding: 4px 0;
  opacity: 0;
  text-align: center;
  color: #ffe5a9;
  font: 700 clamp(1.15rem, 4vw, 1.6rem)/1.3 'PT Serif', Georgia, serif;
  text-shadow: 0 2px 10px #110e20, 0 0 14px #e9ae5455;
  pointer-events: none;
}

.match-sparkle {
  position: absolute;
  z-index: 5;
  left: var(--sparkle-x);
  top: var(--sparkle-y);
  inline-size: var(--sparkle-size);
  block-size: var(--sparkle-size);
  border-radius: 50%;
  background: hsl(var(--sparkle-hue) 100% 87%);
  box-shadow: 0 0 5px 2px hsl(var(--sparkle-hue) 100% 78% / .9), 0 0 13px 5px hsl(var(--sparkle-hue) 100% 69% / .45);
  pointer-events: none;
}

.match-sparkle:nth-child(3n) { border-radius: 1px; transform: rotate(45deg); }

@media (prefers-reduced-motion: reduce) {
  .cell[aria-pressed="true"] .gem { filter: drop-shadow(0 0 5px #ffe6a6a6); }
}

.frame-ornament {
  position: absolute;
  z-index: 1;
  inset-inline: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #f4dda9ba;
  text-shadow: 0 1px 5px #090915;
  pointer-events: none;
}

.ornament-top { inset-block-start: 1px; block-size: 10px; font-size: .55rem; letter-spacing: .24em; }
.ornament-bottom { inset-block-end: 2px; gap: 12px; font-size: .48rem; letter-spacing: .2em; }
.ornament-bottom i { color: #ffe6a9; font-size: .75rem; font-style: normal; text-shadow: 0 0 8px #f6d48fa8; }

.stage-bottom { inline-size: min(100%, 590px); min-block-size: 62px; display: grid; align-content: start; justify-items: center; }
.game-status { min-block-size: 1.5em; margin: 8px 0 4px; color: #f4e8d5; font-size: .8rem; line-height: 1.4; text-align: center; text-shadow: 0 2px 8px #0b0b1c; }

.chapter-nav { display: flex; align-items: center; justify-content: center; gap: 0; }
.chapter-dot { inline-size: 32px; block-size: 32px; display: grid; place-items: center; padding: 0; border: 1px solid #ecd6a64d; border-radius: 50%; background: #151629bf; color: #fff0d3b5; box-shadow: inset 0 1px #ffffff14, 0 3px 12px #08091954; font-family: 'DM Sans', sans-serif; font-size: .6rem; letter-spacing: .08em; cursor: pointer; touch-action: manipulation; transition: color 170ms ease, border-color 170ms ease, background 170ms ease, box-shadow 170ms ease, transform 170ms ease; }
.chapter-dot.is-active { border-color: #f4db9f; background: linear-gradient(145deg, #edd293, #c59c69); color: #32253d; box-shadow: 0 0 16px #f1ce8080, inset 0 1px #fff8da; }
@media (hover: hover) { .chapter-dot:hover:not(.is-active) { border-color: #eed29b; background: #514365e6; transform: translateY(-1px); } }
.chapter-connector { inline-size: clamp(18px, 5vw, 38px); block-size: 1px; background: linear-gradient(90deg, #e8cd9354, #f1d8a98c, #e8cd9354); }

.game-footer { margin-block-start: 4px; display: flex; align-items: center; justify-content: center; gap: 11px; color: #eedbbca3; font-size: .62rem; font-weight: 500; letter-spacing: .055em; text-align: center; text-shadow: 0 1px 8px #090a18; }
.footer-sparkle { color: #f3d190; font-size: .85rem; }

@keyframes game-enter { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes story-reveal { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
@keyframes scene-arrive { from { opacity: .65; } to { opacity: 1; } }
@keyframes frame-glow { from { box-shadow: 0 24px 70px #05071680, 0 0 38px #ddbc781b, inset 0 0 0 2px #ffffff0c, inset 0 2px 3px #fff4d364, inset 0 -3px 7px #090915a3; } to { box-shadow: 0 24px 80px #0507168c, 0 0 50px #d8bd8340, inset 0 0 0 2px #ffffff10, inset 0 2px 3px #fff4d364, inset 0 -3px 7px #090915a3; } }

@media (max-width: 520px) {
  :root { --board-size: min(68svh, calc(100vw - 34px)); }
  body { padding: 14px 9px 9px; }
  .game-header { padding-inline: 2px; margin-block-end: 10px; gap: 7px; }
  .title { gap: 5px; }
  .title-mark { inline-size: 34px; }
  .eyebrow { font-size: .52rem; letter-spacing: .15em; }
  h1 { font-size: clamp(.91rem, 4.1vw, 1.2rem); }
  .toolbar { gap: 6px; }
  .score-card { min-inline-size: 75px; padding: 4px 8px 5px; border-radius: 11px; }
  #score-label { font-size: .51rem; }
  .score-value { min-height: 23px; font-size: 1rem; }
  .score-value img { inline-size: 21px; block-size: 21px; }
  .restart { min-block-size: 38px; padding: 7px 9px; gap: 5px; border-radius: 10px; font-size: .7rem; }
  .restart-icon { font-size: .87rem; }
  .chapter-intro { min-block-size: 69px; margin-block-end: 4px; }
  .chapter-number { font-size: .52rem; }
  h2 { font-size: 1.24rem; }
  .chapter-intro p { max-inline-size: 350px; margin-inline: auto; font-size: .69rem; }
  .board-frame { --board-size: min(63svh, calc(100vw - 34px)); padding: 8px; }
  .board { gap: 3px; padding: 7px; }
  .ornament-top { font-size: .44rem; }
  .ornament-bottom { gap: 8px; font-size: .4rem; }
  .stage-bottom { min-block-size: 59px; }
  .game-status { margin-block-start: 7px; font-size: .72rem; }
  .chapter-dot { inline-size: 30px; block-size: 30px; }
  .chapter-connector { inline-size: clamp(16px, 6vw, 27px); }
  .game-footer { margin-block-start: 2px; gap: 7px; font-size: .55rem; }
}

@media (max-width: 350px) {
  .title-mark { display: none; }
  h1 { font-size: .9rem; }
  .restart { font-size: .66rem; padding-inline: 7px; }
  .score-card { min-inline-size: 66px; padding-inline: 5px; }
  .game-footer { gap: 5px; font-size: .51rem; }
}



@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}

/* Keep the reactions outside the board and the controls within small screens. */
.game-header { flex-wrap: wrap; }
.toolbar { flex-wrap: wrap; }
.board-frame { --board-size: min(560px, calc(100svh - 190px), calc(100vw - 40px)); }
.stage-bottom { min-block-size: 0; }
.game-status { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
@media (max-width: 520px) {
  .game-header { justify-content: center; gap: 12px; }
  .title { justify-content: center; width: 100%; }
  .toolbar { justify-content: center; width: 100%; }
  .sound-toggle { font-size: .72rem; min-height: 38px; padding: 7px 9px; }
  .board-frame { --board-size: min(560px, calc(100svh - 190px), calc(100vw - 30px)); }
}
@media (orientation: landscape) and (max-height: 550px) {
  body { padding: 10px; }
  .game { display: grid; grid-template-columns: minmax(130px, 1fr) minmax(0, min(560px, calc(100svh - 24px))); align-items: center; gap: 16px; }
  .game-header { flex-direction: column; justify-content: center; margin: 0; }
  .title { width: 100%; justify-content: center; }
  .toolbar { width: 100%; justify-content: center; }
  .title-mark { display: none; }
  .stage { position: relative; }
  .reaction { position: absolute; bottom: 100%; width: 100%; height: 28px; font-size: 1rem; }
  .board-frame { --board-size: min(560px, calc(100svh - 60px), calc(100vw - 180px)); }
}
