:root {
  color-scheme: light;
  --ink: #162033;
  --muted: #647184;
  --green: #068c6f;
  --green-dark: #05624f;
  --coral: #e95c4b;
  --red: #dc3d35;
  --orange: #ff7a00;
  --yellow: #f3c33b;
  --blue: #058fc5;
  --glass-border: rgba(255, 255, 255, 0.62);
  --glass-fill: rgba(255, 255, 255, 0.52);
  --chip-fill: rgba(255, 255, 255, 0.58);
  --shadow: 0 18px 48px rgba(35, 53, 78, 0.16);
  --board-cols: 5;
  --card-gap: 10px;
  --card-size: 120px;
}

body[data-theme="dark"] {
  color-scheme: dark;
  --ink: #eef6ff;
  --muted: #adc1d5;
  --green: #28c8a2;
  --green-dark: #77ead4;
  --coral: #ff8b7e;
  --red: #ff6158;
  --orange: #ff9c38;
  --yellow: #ffd84c;
  --blue: #49c7ff;
  --glass-border: rgba(255, 255, 255, 0.18);
  --glass-fill: rgba(13, 25, 39, 0.6);
  --chip-fill: rgba(255, 255, 255, 0.1);
  --shadow: 0 22px 62px rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 12%, rgba(255, 122, 0, 0.22), transparent 24%),
    radial-gradient(circle at 83% 18%, rgba(5, 143, 197, 0.2), transparent 26%),
    radial-gradient(circle at 52% 88%, rgba(6, 140, 111, 0.16), transparent 28%),
    linear-gradient(135deg, #f8fbff 0%, #eef8f5 48%, #fff7ec 100%);
}

body[data-theme="dark"] {
  background:
    radial-gradient(circle at 16% 12%, rgba(255, 122, 0, 0.2), transparent 24%),
    radial-gradient(circle at 82% 18%, rgba(73, 199, 255, 0.18), transparent 26%),
    radial-gradient(circle at 52% 88%, rgba(40, 200, 162, 0.14), transparent 28%),
    linear-gradient(135deg, #08111d 0%, #102332 50%, #1a1c23 100%);
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  height: 100dvh;
  padding: 12px;
}

.topbar {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: clamp(32px, 4vw, 72px);
  align-items: center;
  justify-content: stretch;
  padding: 10px;
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.6), rgba(255, 255, 255, 0.18)),
    var(--glass-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.62),
    var(--shadow);
  backdrop-filter: blur(26px) saturate(170%);
  -webkit-backdrop-filter: blur(26px) saturate(170%);
}

body[data-theme="dark"] .topbar {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.04)),
    var(--glass-fill);
}

.brand-block {
  display: grid;
  justify-items: center;
  justify-self: start;
  min-width: 0;
  padding: 0 8px;
  text-align: center;
}

.menu-cluster {
  grid-column: 2;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  min-width: 0;
  width: 100%;
}

.stage-kicker,
.score-label {
  color: var(--green-dark);
  font-size: 0.68rem;
  font-weight: 900;
  letter-spacing: 0;
  line-height: 1;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  overflow: hidden;
  max-width: 100%;
  font-size: clamp(1.35rem, 2.15vw, 2.2rem);
  line-height: 0.94;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage-kicker {
  color: var(--muted);
  font-size: 0.66rem;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(82px, 1fr));
  gap: 8px;
  align-items: center;
  padding-right: 10px;
  border-right: 1px solid rgba(22, 32, 51, 0.18);
}

body[data-theme="dark"] .controls {
  border-right-color: rgba(255, 255, 255, 0.22);
}

.control {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 82px;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid rgba(22, 32, 51, 0.14);
  border-radius: 999px;
  color: var(--ink);
  background: var(--chip-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.58),
    0 8px 20px rgba(43, 59, 84, 0.08);
  cursor: pointer;
  line-height: 1;
  text-align: center;
  transition:
    transform 140ms ease,
    border-color 140ms ease,
    background 140ms ease;
}

body[data-theme="dark"] .control {
  border-color: rgba(255, 255, 255, 0.14);
}

.control:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: rgba(6, 140, 111, 0.36);
}

.control:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.control.primary {
  border-color: rgba(6, 140, 111, 0.46);
  color: #ffffff;
  background: linear-gradient(135deg, var(--green), #0aa7a5);
}

.control.warning {
  border-color: rgba(243, 195, 59, 0.55);
  color: #3b2b00;
  background: linear-gradient(135deg, var(--yellow), #f0b830);
}

.control.danger {
  color: #ffffff;
  border-color: rgba(220, 61, 53, 0.46);
  background: linear-gradient(135deg, var(--red), #f06a5f);
}

body[data-theme="dark"] .control.danger {
  color: #ffffff;
}

.control-icon {
  width: 16px;
  text-align: center;
  line-height: 1;
}

.scoreboard {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  justify-self: start;
  min-width: 0;
}

.score-group {
  position: relative;
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  min-width: 0;
}

.score-group:not(.score-group-meta),
.score-group-meta {
  padding-left: 8px;
  padding-right: 10px;
  border-right: 1px solid rgba(22, 32, 51, 0.18);
}

body[data-theme="dark"] .score-group:not(.score-group-meta),
body[data-theme="dark"] .score-group-meta {
  border-right-color: rgba(255, 255, 255, 0.22);
}

.score-group-meta {
  flex: 0 0 auto;
}

.scoreboard .score-group > div {
  display: grid;
  align-content: center;
  justify-items: center;
  min-width: 66px;
  min-height: 42px;
  padding: 6px 11px;
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  background: var(--chip-fill);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.score-label {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 0.58rem;
}

.scoreboard strong {
  display: block;
  overflow: hidden;
  font-size: clamp(0.82rem, 1.1vw, 1.05rem);
  line-height: 1.05;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scoreboard strong.is-low-time {
  color: var(--red);
  animation: low-time-blink 620ms steps(2, jump-none) infinite;
}

.theme-control {
  flex: 0 0 auto;
  min-width: 86px;
}

.best-score-pill {
  display: grid;
  align-content: center;
  justify-items: center;
  flex: 0 0 auto;
  min-width: 118px;
  min-height: 42px;
  padding: 6px 14px;
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  background: var(--chip-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.58),
    0 8px 20px rgba(43, 59, 84, 0.08);
}

.glass {
  border: 1px solid var(--glass-border);
  border-radius: 30px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0.24)),
    var(--glass-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.72),
    inset 0 -18px 36px rgba(255, 255, 255, 0.1),
    var(--shadow);
  backdrop-filter: blur(28px) saturate(170%);
  -webkit-backdrop-filter: blur(28px) saturate(170%);
}

body[data-theme="dark"] .glass {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0.04)),
    var(--glass-fill);
}

.best-score-pill strong {
  display: block;
  overflow: hidden;
  max-width: 100%;
  font-size: clamp(0.82rem, 1.1vw, 1.05rem);
  line-height: 1.05;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

main {
  min-height: 0;
}

.board {
  display: flex;
  flex-wrap: wrap;
  gap: var(--card-gap);
  align-content: center;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.card {
  position: relative;
  flex: 0 0 var(--card-size);
  width: var(--card-size);
  min-width: 0;
  max-height: 100%;
  aspect-ratio: 1 / 1.18;
  padding: 0;
  border: 0;
  border-radius: clamp(10px, 10%, 22px);
  background: transparent;
  cursor: pointer;
  perspective: 900px;
}

.card:disabled {
  cursor: default;
}

.card-inner {
  position: absolute;
  inset: 0;
  border-radius: clamp(10px, 10%, 22px);
  transform-style: preserve-3d;
  transition: transform 240ms ease;
}

.card.is-flipped .card-inner,
.card.is-matched .card-inner {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: clamp(10px, 10%, 22px);
  backface-visibility: hidden;
}

.card-back {
  border: 1px solid var(--glass-border);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0.2)),
    var(--chip-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.72),
    0 10px 24px rgba(21, 47, 61, 0.12);
  backdrop-filter: blur(18px) saturate(170%);
  -webkit-backdrop-filter: blur(18px) saturate(170%);
}

.card-front {
  transform: rotateY(180deg);
  border: 1px solid var(--glass-border);
  background:
    radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.96), transparent 37%),
    linear-gradient(155deg, #fff6dc 0%, #e8f6f3 50%, #eaf0ff 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    inset 0 -10px 18px rgba(22, 32, 51, 0.06);
}

body[data-theme="dark"] .card-front {
  background:
    radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.16), transparent 37%),
    linear-gradient(155deg, #203044 0%, #123d43 52%, #182338 100%);
}

.animal {
  display: block;
  font-size: clamp(1rem, calc(var(--card-size) * 0.56), 5rem);
  filter: drop-shadow(0 8px 0 rgba(255, 255, 255, 0.58));
  line-height: 1;
  transform: translateY(-1px);
}

body[data-theme="dark"] .animal {
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, 0.28));
}

.rc-logo {
  display: block;
  width: clamp(22px, 58%, 112px);
  max-height: 72%;
  object-fit: contain;
  filter: drop-shadow(0 8px 14px rgba(255, 122, 0, 0.14));
}

.card.is-matched {
  animation: matched 480ms ease;
}

.card.is-matched .card-front {
  border-color: rgba(6, 140, 111, 0.34);
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.82), transparent 38%),
    linear-gradient(155deg, rgba(242, 188, 46, 0.36), rgba(6, 140, 111, 0.18));
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(241, 247, 250, 0.42);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

body[data-theme="dark"] .dialog-backdrop {
  background: rgba(3, 10, 18, 0.55);
}

.dialog-backdrop.is-visible {
  display: grid;
}

.dialog {
  position: relative;
  display: grid;
  gap: 14px;
  width: min(520px, calc(100vw - 32px));
  padding: 24px;
}

.dialog-close {
  position: absolute;
  top: 14px;
  right: 14px;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  color: var(--ink);
  background: var(--chip-fill);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.56),
    0 8px 18px rgba(43, 59, 84, 0.12);
  cursor: pointer;
  font-size: 1.45rem;
  font-weight: 800;
  line-height: 1;
}

.dialog-close:hover {
  border-color: rgba(255, 122, 0, 0.48);
  color: var(--orange);
}

.dialog h2 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: clamp(2rem, 6vw, 3.1rem);
  line-height: 1;
}

.dialog-copy {
  max-width: 46ch;
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
  text-wrap: balance;
}

.level-dialog {
  text-align: center;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.level-dialog .dialog-actions {
  justify-self: center;
}

.dialog-action {
  justify-self: start;
  min-width: 132px;
}

@keyframes matched {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.04);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes low-time-blink {
  0%,
  100% {
    opacity: 1;
    text-shadow: 0 0 14px rgba(220, 61, 53, 0.42);
  }
  50% {
    opacity: 0.28;
    text-shadow: none;
  }
}

@media (max-width: 1260px) {
  .topbar {
    column-gap: 10px;
  }

  .brand-block {
    max-width: 190px;
  }

  .menu-cluster {
    gap: 8px;
  }

  .control,
  .best-score-pill {
    min-width: 72px;
    padding: 0 10px;
  }

  .controls {
    grid-template-columns: repeat(3, minmax(72px, 1fr));
  }

  .scoreboard {
    gap: 6px;
  }

  .scoreboard .score-group > div {
    min-width: 58px;
    padding: 6px 9px;
  }
}

@media (max-width: 1200px) {
  .app {
    gap: 8px;
    padding: 8px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr;
    align-items: stretch;
    border-radius: 30px;
    row-gap: 8px;
  }

  .brand-block {
    justify-self: center;
    max-width: none;
  }

  .brand-block,
  .menu-cluster,
  .controls,
  .scoreboard {
    width: 100%;
  }

  .menu-cluster {
    grid-column: auto;
    display: grid;
    grid-template-columns: minmax(0, max-content) minmax(0, max-content);
    gap: 8px 14px;
    justify-content: center;
  }

  .controls {
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, 1fr);
    justify-self: center;
    width: min(560px, 100%);
  }

  .scoreboard {
    display: contents;
  }

  .scoreboard .score-group:nth-child(1) {
    grid-column: 1;
    grid-row: 2;
    justify-self: end;
    padding-right: 14px;
    border-right: 1px solid rgba(22, 32, 51, 0.18);
  }

  .scoreboard .score-group:nth-child(2) {
    grid-column: 2;
    grid-row: 2;
    justify-self: start;
  }

  .score-group-meta {
    grid-column: 1;
    grid-row: 3;
    justify-self: end;
    padding-right: 14px;
    border-right: 1px solid rgba(22, 32, 51, 0.18);
  }

  .controls,
  .score-group:not(.score-group-meta),
  .score-group-meta {
    padding-left: 0;
    padding-right: 0;
    border-right: 0;
  }

  .scoreboard .score-group:nth-child(1),
  .score-group-meta {
    padding-right: 14px;
    border-right: 1px solid rgba(22, 32, 51, 0.18);
  }

  body[data-theme="dark"] .scoreboard .score-group:nth-child(1),
  body[data-theme="dark"] .score-group-meta {
    border-right-color: rgba(255, 255, 255, 0.22);
  }

  .best-score-pill {
    grid-column: 2;
    grid-row: 3;
    justify-self: start;
  }

  .theme-control {
    min-width: 120px;
  }

  .best-score-pill {
    min-width: 210px;
  }
}

@media (max-width: 520px) {
  .menu-cluster {
    grid-template-columns: 1fr;
  }

  .controls,
  .scoreboard,
  .best-score-pill {
    grid-column: 1;
  }

  .scoreboard {
    display: grid;
    gap: 6px;
    justify-items: center;
  }

  .scoreboard {
    gap: 6px;
  }

  .scoreboard .score-group:nth-child(1),
  .scoreboard .score-group:nth-child(2),
  .score-group-meta,
  .best-score-pill {
    grid-column: 1;
    grid-row: auto;
    justify-self: stretch;
    width: 100%;
  }

  .scoreboard .score-group:nth-child(1),
  .score-group-meta {
    padding-right: 0;
    border-right: 0;
  }

  .scoreboard .score-group > div,
  .control,
  .best-score-pill {
    min-height: 36px;
  }

  .score-group {
    justify-content: center;
    flex-wrap: wrap;
  }

  .theme-control,
  .best-score-pill {
    width: 100%;
  }

  .control {
    min-width: 0;
    padding: 0 8px;
  }

  .control-icon {
    display: none;
  }
}
