:root {
  --mint: #a6dbc1;
  --mint-dark: #95d2b6;
  --navy: #1d2478;
  --navy-soft: #2e3790;
  --white: #f6fffb;
  --ink: #0f144a;
  --glass: rgba(255, 255, 255, 0.78);
  --glass-strong: rgba(255, 255, 255, 0.94);
  --shadow: 0 16px 40px rgba(29, 36, 120, 0.18);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: "Open Sans", Calibri, Arial, sans-serif;
  color: var(--navy);
  background:
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.42), transparent 38%),
    linear-gradient(180deg, var(--mint), var(--mint-dark));
}

.launcher {
  width: 100%;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: 14px;
}

.launcher-card {
  width: min(100%, 420px);
  background: var(--glass-strong);
  border: 2px solid rgba(29, 36, 120, 0.22);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 22px 18px;
  text-align: center;
  position: relative;
}

.launcher-title {
  margin: 8px 0 0;
  font-size: clamp(2rem, 8vw, 2.6rem);
  font-weight: 800;
  letter-spacing: 0.04em;
}

.launcher-subtitle {
  margin: 6px 0 18px;
  color: var(--navy-soft);
  font-weight: 600;
}

.play-btn,
.btn {
  border: 0;
  border-radius: 18px;
  font-family: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease, background 120ms ease;
}

.play-btn {
  min-height: 54px;
  width: 100%;
  background: var(--navy);
  color: #fff;
  font-size: 1.05rem;
  box-shadow: 0 8px 20px rgba(29, 36, 120, 0.28);
}

.play-btn:active,
.btn:active,
.icon-btn:active {
  transform: scale(0.98);
}

.icon-btn {
  border: 0;
  background: rgba(255, 255, 255, 0.9);
  color: var(--navy);
  width: 42px;
  height: 42px;
  border-radius: 999px;
  font-size: 1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 6px 14px rgba(29, 36, 120, 0.15);
}

.launcher-icon {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 6px 14px rgba(29, 36, 120, 0.1);
}

.app-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  background:
    radial-gradient(circle at 18% 12%, rgba(255, 255, 255, 0.42), transparent 38%),
    linear-gradient(180deg, var(--mint), var(--mint-dark));
  display: grid;
  place-items: center;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

.app-overlay.is-hidden,
.sheet.is-hidden,
.result-overlay.is-hidden,
.loading-overlay.is-hidden {
  display: none;
}

.close-overlay {
  position: fixed;
  top: calc(10px + env(safe-area-inset-top));
  left: calc(10px + env(safe-area-inset-left));
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 0;
  background: rgba(255, 255, 255, 0.92);
  color: var(--navy);
  font-size: 1rem;
  cursor: pointer;
  z-index: 15;
}

.game-shell {
  width: min(100%, 980px);
  height: 100%;
  max-height: 100%;
  border: 2px solid rgba(29, 36, 120, 0.28);
  border-radius: 20px;
  background: rgba(246, 255, 251, 0.72);
  box-shadow: var(--shadow);
  padding: 10px;
  display: grid;
  grid-template-rows: auto auto auto 1fr auto;
  gap: 8px;
  overflow: hidden;
  position: relative;
}

.compact-topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin: 0;
  position: relative;
  z-index: 3;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  position: relative;
  z-index: 4;
}

.title {
  margin: 0;
  color: var(--navy);
  font-size: clamp(1rem, 2vw, 1.5rem);
  font-weight: 800;
  letter-spacing: 0.04em;
}

.subtitle {
  margin: 2px 0 0;
  color: var(--navy-soft);
  font-size: 0.78rem;
}

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

.difficulty-label {
  font-size: 0.85rem;
  color: var(--navy-soft);
  font-weight: 700;
}

.difficulty-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hud {
  display: grid;
  gap: 4px;
}

.hud p {
  margin: 0;
  color: var(--ink);
  font-size: 0.84rem;
  font-weight: 700;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--navy-soft);
  font-size: 0.76rem;
  font-weight: 700;
}

.canvas-wrap {
  min-height: 0;
  height: 100%;
  border-radius: 18px;
  border: 2px solid rgba(29, 36, 120, 0.2);
  background: rgba(255, 255, 255, 0.62);
  padding: 4px;
  overflow: hidden;
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 14px;
  image-rendering: pixelated;
}

.canvas-floating-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: none;
  gap: 8px;
  z-index: 6;
}

.btn {
  min-height: 44px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--navy);
  font-size: 0.9rem;
}

.btn.primary {
  background: var(--navy);
  color: #fff;
}

.btn.ghost {
  background: rgba(255, 255, 255, 0.55);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-btn.is-active {
  background: var(--navy);
  color: #fff;
}

.mode-btn.is-locked {
  opacity: 0.45;
}

.hint {
  margin: 0;
  text-align: center;
  color: var(--navy-soft);
  font-size: 0.72rem;
}

.hint p {
  margin: 0;
}

.sheet,
.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(7, 10, 30, 0.44);
  display: grid;
  place-items: center;
  padding: 12px;
}

.sheet-card,
.result-card {
  width: min(96vw, 520px);
  max-height: min(88dvh, 760px);
  overflow: auto;
  background: var(--glass-strong);
  border: 2px solid rgba(29, 36, 120, 0.25);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 14px;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(7, 10, 30, 0.4);
  display: grid;
  place-items: center;
  padding: 12px;
}

#landscapeHint {
  display: none;
}

.loading-card {
  width: min(88vw, 320px);
  background: rgba(255, 255, 255, 0.96);
  border: 2px solid rgba(29, 36, 120, 0.2);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 16px;
  text-align: center;
}

.loading-card p {
  margin: 10px 0 0;
  color: var(--navy);
  font-weight: 700;
}

.spinner {
  width: 34px;
  height: 34px;
  margin: 0 auto;
  border-radius: 999px;
  border: 3px solid rgba(29, 36, 120, 0.18);
  border-top-color: var(--navy);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

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

.sheet-head h2,
.result-card h2 {
  margin: 0;
  font-size: 1.05rem;
}

.profile,
.auth,
.leaderboard {
  margin-top: 8px;
}

.profile-row,
.auth-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 8px;
}

.profile label {
  font-weight: 700;
  color: var(--navy-soft);
}

.profile p,
.auth p,
.leaderboard-title,
.result-card p {
  margin: 4px 0;
  color: var(--navy-soft);
  font-size: 0.86rem;
}

.profile input,
.auth input {
  min-height: 42px;
  padding: 9px 12px;
  border-radius: 14px;
  border: 1px solid rgba(29, 36, 120, 0.22);
  background: rgba(255, 255, 255, 0.96);
  color: var(--navy);
  font-family: inherit;
  font-size: 0.95rem;
  min-width: min(240px, 100%);
}

.leaderboard ol,
.result-toplist {
  margin: 8px 0 0;
  padding-left: 22px;
  color: var(--navy);
  font-size: 0.9rem;
}

.result-toplist.is-hidden {
  display: none;
}

.result-actions,
.result-auth-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

body.is-playing .hint {
  display: none;
}

body.is-playing .topbar .subtitle {
  display: none;
}

body.is-playing .hud p {
  display: none;
}

@media (max-width: 780px) {
  .launcher-card {
    width: min(100%, 420px);
    border-radius: 24px;
    border-left: 2px solid rgba(29, 36, 120, 0.22);
    border-right: 2px solid rgba(29, 36, 120, 0.22);
    min-height: 0;
    display: block;
  }

  .app-overlay {
    padding: 0;
  }

  .game-shell {
    width: 100%;
    height: 100dvh;
    border-radius: 0;
    border: 0;
    padding: 48px 6px 6px;
    gap: 6px;
    grid-template-rows: auto auto 1fr;
  }

  .compact-topbar {
    display: none;
  }

  .difficulty {
    gap: 6px;
    padding: 2px 0;
  }

  .difficulty-label,
  .subtitle,
  .hint {
    display: none;
  }

  .difficulty-buttons {
    width: 100%;
    gap: 6px;
    flex-wrap: nowrap;
  }

  .difficulty-buttons .btn {
    min-height: 34px;
    padding: 6px 10px;
    border-radius: 14px;
    font-size: 0.82rem;
    flex: 1 1 0;
  }

  .hud {
    gap: 2px;
  }

  .hud p {
    font-size: 0.72rem;
    line-height: 1.15;
  }

  .stats {
    gap: 4px 6px;
    font-size: 0.68rem;
    line-height: 1.1;
  }

  #gridText,
  #movesText {
    display: none;
  }

  .stats span {
    padding: 2px 6px;
    background: rgba(255, 255, 255, 0.65);
    border-radius: 999px;
    border: 1px solid rgba(29, 36, 120, 0.12);
  }

  .canvas-wrap {
    padding: 2px;
    border-radius: 16px;
  }

  .canvas-wrap canvas {
    border-radius: 12px;
  }

  .canvas-floating-actions {
    display: flex;
    top: 8px;
    right: 8px;
  }

  .canvas-floating-actions .icon-btn {
    width: 38px;
    height: 38px;
    background: rgba(255, 255, 255, 0.95);
    box-shadow: 0 8px 18px rgba(29, 36, 120, 0.18);
  }

  .close-overlay {
    width: 34px;
    height: 34px;
    z-index: 16;
  }

  body.is-playing .hud {
    gap: 0;
  }

  body.is-playing .difficulty {
    display: none;
  }

  body.is-playing .game-shell {
    grid-template-rows: auto 1fr;
  }

  body.is-playing .canvas-wrap {
    margin-top: 2px;
  }
}

@media (max-width: 900px) and (orientation: landscape) {
  #landscapeHint {
    display: grid;
    place-items: center;
    position: fixed;
    inset: 0;
    z-index: 999;
    background: rgba(7, 10, 30, 0.96);
    color: #ffffff;
    text-align: center;
    padding: 20px;
    font-weight: 700;
    font-size: 1.1rem;
    line-height: 1.3;
  }
}
