:root {
  --bg-1: #0f2744;
  --bg-2: #1a4f75;
  --ink: #113249;
  --ink-soft: #3e657f;
  --line: #ffffff55;
  --surface: #f8fcffec;
  --surface-strong: #ffffff;
  --primary: #ff7a18;
  --primary-deep: #dd5e09;
  --danger: #d64545;
  --success: #199b59;
  --shadow: 0 18px 48px #0821324a;
}

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

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

body {
  font-family: "Trebuchet MS", "Segoe UI", Tahoma, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 8%, #ffffff1a 0%, transparent 36%),
    radial-gradient(circle at 90% 18%, #c6fdff3d 0%, transparent 42%),
    linear-gradient(155deg, var(--bg-1), var(--bg-2));
}

body.game-over-active {
  overflow: hidden;
}

body.game-menu-active {
  min-height: 100dvh;
  overflow-y: auto;
}

.page-wrap {
  width: min(1220px, 100%);
  margin: 0 auto;
  padding: 1rem;
}

.game-brandbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.7rem;
  margin-bottom: 0.75rem;
  padding: 0.5rem 0.65rem;
  border-radius: 10px;
  border: 1px solid #ffffff8e;
  background: #f5fcffb8;
  box-shadow: 0 10px 22px #0c304229;
}

.game-brandlink {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  color: var(--ink);
  min-width: 0;
}

.game-brand-logo {
  width: 32px !important;
  height: 32px !important;
  min-width: 32px;
  min-height: 32px;
  max-width: 32px !important;
  max-height: 32px !important;
  flex: 0 0 32px;
  border-radius: 7px;
  object-fit: cover;
  border: 1px solid #8ec9e9;
  background: #fff;
  display: block;
}

.game-brand-name {
  font-size: 0.96rem;
  font-weight: 800;
}

.game-home-link {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  justify-content: center;
  min-height: 38px;
  padding: 0.38rem 0.76rem;
  border-radius: 999px;
  text-decoration: none;
  color: #ffffff;
  border: 1px solid #68c8f6;
  background: linear-gradient(180deg, #21bfff, #1095db);
  font-size: 0.8rem;
  font-weight: 800;
  box-shadow: 0 8px 16px #1183c846, inset 0 1px 0 #9be7ff;
  transition: transform 0.14s ease, box-shadow 0.14s ease, filter 0.14s ease;
}

.game-home-link:hover {
  transform: translateY(-1px);
  filter: brightness(1.04);
  box-shadow: 0 12px 20px #0f7fc255, inset 0 1px 0 #c2f4ff;
}

.game-home-link:active {
  transform: translateY(0);
}

.home-icon {
  display: inline-grid;
  place-items: center;
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 999px;
  background: #ffffffde;
  color: #0a5f91;
  font-size: 0;
  font-weight: 900;
  line-height: 1;
}

.home-icon::before {
  content: "\2302";
  font-size: 0.72rem;
}

.layout-grid {
  display: grid;
  grid-template-columns: minmax(0, 760px) minmax(280px, 1fr);
  gap: 1rem;
  align-items: start;
}

.game-column,
.board-column {
  width: 100%;
}

.game-column {
  display: flex;
  flex-direction: column;
}

.canvas-shell {
  position: relative;
  border: 2px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #08314f;
  box-shadow: var(--shadow);
}

.game-menu-active .canvas-shell {
  min-height: calc(100dvh - 6.4rem);
  overflow: visible;
  background:
    radial-gradient(circle at 18% 0%, #ffffff38 0%, transparent 42%),
    linear-gradient(160deg, #dff4ff, #f8fcff);
}

.game-menu-active #gameCanvas,
.game-menu-active .state-badge {
  display: none;
}

.game-menu-active .overlay {
  position: relative;
  inset: auto;
  min-height: calc(100dvh - 6.4rem);
  align-items: start;
  place-items: start center;
  overflow: visible;
  padding: clamp(0.85rem, 3vw, 1.4rem);
}

.game-menu-active .overlay-card {
  width: min(100%, 680px);
  max-height: none;
  overflow: visible;
  margin: 0 auto;
}

#gameCanvas {
  display: block;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 1 / 1;
  background: transparent;
  touch-action: none;
}

.state-badge {
  position: absolute;
  top: 0.7rem;
  left: 0.7rem;
  padding: 0.3rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  background: #ffffffd9;
  border: 1px solid #ffffff;
  z-index: 4;
}

.hud {
  position: static;
  z-index: 5;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: flex-end;
  gap: 0.6rem;
  margin-top: 0.68rem;
  padding: 0.6rem;
  border-radius: 10px;
  border: 1px solid #ffffff75;
  background: #eaf7ffdb;
  box-shadow: 0 10px 24px #06263d2c;
}

.hud-left,
.hud-right {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.hud-left {
  min-width: 0;
  align-items: center;
}

.hud-right {
  justify-content: flex-end;
}

.hud-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.33rem;
  padding: 0.35rem 0.62rem;
  border-radius: 999px;
  background: #ffffffe8;
  border: 1px solid #ffffff;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 700;
}

.icon-btn {
  border: 1px solid #ffffff;
  background: #ffffffdf;
  color: var(--ink);
  border-radius: 999px;
  min-height: 38px;
  padding: 0.42rem 0.72rem;
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0.75rem;
  background: #0a294164;
  z-index: 6;
}

.fullscreen-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  padding: clamp(0.55rem, 2.2vw, 1.2rem);
  background:
    radial-gradient(circle at 20% 0%, #95ddff2e 0%, transparent 48%),
    linear-gradient(165deg, #031629db, #05233ad4);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.overlay-card {
  width: min(100%, 520px);
  background: var(--surface);
  border: 1px solid #ffffff;
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 1rem;
  max-height: calc(100dvh - 2rem);
  overflow: auto;
}

.fullscreen-overlay .overlay-card {
  width: min(100%, 980px);
  max-height: calc(100dvh - clamp(1rem, 3vh, 2rem));
  border-radius: 16px;
  padding: clamp(0.85rem, 2vw, 1.25rem);
}

.overlay-card.small {
  width: min(100%, 320px);
  text-align: center;
}

.title {
  margin: 0 0 0.25rem;
  font-size: clamp(1.8rem, 3.8vw, 2.5rem);
  line-height: 1.05;
  text-transform: uppercase;
  animation: titlePulse 2.2s ease-in-out infinite;
}

@keyframes titlePulse {
  0%, 100% {
    transform: translateY(0);
    text-shadow: 0 0 0 #ffffff;
  }
  50% {
    transform: translateY(-2px);
    text-shadow: 0 4px 14px #ffffffd3;
  }
}

.subtitle {
  margin: 0 0 0.8rem;
  color: var(--ink-soft);
}

.field-label {
  display: inline-block;
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 0.35rem;
}

.text-input {
  width: 100%;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid #a6c6da;
  background: var(--surface-strong);
  padding: 0.62rem 0.72rem;
  font-size: 1rem;
  color: var(--ink);
}

.text-input:focus-visible,
.btn:focus-visible,
.icon-btn:focus-visible,
.control-btn:focus-visible {
  outline: 3px solid #8fd5ff;
  outline-offset: 1px;
}

.help-text {
  margin: 0.4rem 0 0;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.error-text {
  min-height: 1.24rem;
  margin: 0.25rem 0 0.8rem;
  color: #b3261e;
  font-size: 0.8rem;
  font-weight: 700;
}

.menu-stats {
  margin-bottom: 0.8rem;
  font-weight: 700;
}

.btn-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
  margin-bottom: 0.8rem;
}

.btn-grid.tight {
  margin-top: 0.5rem;
}

.btn {
  min-height: 46px;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0.6rem 0.8rem;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 800;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.btn:hover {
  transform: translateY(-1px);
  filter: brightness(1.02);
}

.btn:active {
  transform: translateY(0);
}

.btn.primary {
  background: linear-gradient(180deg, var(--primary), var(--primary-deep));
  color: #ffffff;
  box-shadow: 0 8px 18px #ef762755;
}

.btn.ghost {
  background: #ffffff;
  border-color: #b8d2e2;
  color: var(--ink);
}

.btn.danger {
  background: #ffe8e8;
  border-color: #e8b2b2;
  color: #7c1f1f;
}

.controls-note {
  margin: 0 0 0.8rem;
  color: var(--ink-soft);
  font-size: 0.84rem;
  text-align: center;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.65rem;
  margin-bottom: 0.5rem;
}

.card-header h2,
.card-header h3 {
  margin: 0;
}

.card-header h2 {
  font-size: 1.03rem;
}

.card-header h3 {
  font-size: 0.94rem;
}

.status-text {
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 700;
}

.inline-board {
  border-top: 1px solid #d4e5f1;
  padding-top: 0.75rem;
}

.board-card {
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #fcffffea;
  padding: 1rem;
  box-shadow: var(--shadow);
}

.leaderboard-wrap {
  overflow-x: auto;
  border-radius: 8px;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  background: #ffffffc8;
}

.leaderboard-table th,
.leaderboard-table td {
  padding: 0.46rem 0.4rem;
  text-align: left;
  font-size: 0.8rem;
  border-bottom: 1px solid #d4e5f0;
  vertical-align: top;
}

.leaderboard-table th {
  position: sticky;
  top: 0;
  background: #e9f6ff;
  z-index: 1;
}

.leaderboard-table tr.rank-1 {
  background: #ffe9a8;
}

.leaderboard-table tr.rank-2 {
  background: #f0f3f7;
}

.leaderboard-table tr.rank-3 {
  background: #ffe8d0;
}

.rank-badge {
  display: inline-block;
  min-width: 1.6rem;
  text-align: center;
  padding: 0.16rem 0.36rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 800;
  background: #cfe7f7;
  color: #12486f;
}

.rank-1 .rank-badge {
  background: #ffce3d;
  color: #784f00;
}

.rank-2 .rank-badge {
  background: #dde4ea;
  color: #3b505f;
}

.rank-3 .rank-badge {
  background: #ffc188;
  color: #7a3f0b;
}

.game-over-card {
  background:
    radial-gradient(circle at 14% 0%, #ffffffea 0%, #f7fbffea 55%),
    linear-gradient(160deg, #f8fbff, #f2f9ff);
  border: 1px solid #ffffff;
}

.game-over-hero {
  text-align: center;
  margin-bottom: 0.7rem;
}

.game-over-badge {
  display: inline-block;
  padding: 0.24rem 0.62rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 800;
  color: #8a4d00;
  background: #ffe4ad;
  border: 1px solid #ffd08a;
  margin-bottom: 0.42rem;
}

.game-over-title {
  margin: 0;
  font-size: clamp(1.55rem, 3.6vw, 2.2rem);
  line-height: 1;
  color: #0f3854;
  text-transform: uppercase;
}

.game-over-subtitle {
  margin: 0.34rem 0 0;
  color: #4a6980;
  font-size: 0.86rem;
}

.game-over-video-wrap {
  width: min(100%, 840px);
  aspect-ratio: 16 / 9;
  max-height: 40dvh;
  margin: 0.16rem auto 0.78rem;
  border-radius: 11px;
  overflow: hidden;
  border: 2px solid #ffffff;
  box-shadow: 0 12px 24px #1939513b;
  background: #061e31;
  pointer-events: none;
  user-select: none;
}

.game-over-video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000000;
  pointer-events: none;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
  margin-bottom: 0.7rem;
}

.result-card {
  padding: 0.58rem 0.46rem;
  border-radius: 9px;
  border: 1px solid #cce4f3;
  background: #ffffff;
  text-align: center;
  box-shadow: inset 0 -4px 0 #e9f5ff;
}

.score-card {
  background: linear-gradient(180deg, #fff2d8, #ffe7b9);
  border-color: #ffd99b;
}

.best-card {
  background: linear-gradient(180deg, #def9e8, #cdf3de);
  border-color: #b6eacb;
}

.result-label {
  display: block;
  font-size: 0.74rem;
  color: #56748a;
  margin-bottom: 0.16rem;
  font-weight: 700;
}

.result-value {
  font-size: 1.03rem;
  color: #0f3854;
}

.submit-status {
  margin: 0.2rem 0 0.64rem;
  text-align: center;
  font-size: 0.79rem;
  color: #41657d;
  font-weight: 700;
}

.submit-status.ok {
  color: #15824d;
}

.submit-status.error {
  color: #a12b2b;
}

.touch-controls {
  margin-top: 0.65rem;
  display: grid;
  place-items: center;
  gap: 0.45rem;
}

.high-score-toast {
  position: fixed;
  left: 50%;
  top: max(0.7rem, env(safe-area-inset-top));
  transform: translate(-50%, -125%);
  opacity: 0;
  min-width: min(90vw, 320px);
  padding: 0.72rem 0.95rem;
  border-radius: 12px;
  border: 1px solid #9afad0;
  background: linear-gradient(180deg, #1ac676f5, #0f9c5ef2);
  color: #ffffff;
  font-weight: 800;
  text-align: center;
  letter-spacing: 0;
  box-shadow: 0 14px 34px #0a5d3896;
  transition: transform 0.22s ease, opacity 0.22s ease;
  z-index: 90;
  pointer-events: none;
}

.high-score-toast.show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.confetti-layer {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 85;
}

.confetti-piece {
  position: absolute;
  top: -8vh;
  border-radius: 2px;
  opacity: 0.96;
  animation: confettiFall var(--fall, 2400ms) linear forwards;
}

@keyframes confettiFall {
  from {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
  to {
    transform: translate3d(var(--drift, 0px), 115vh, 0) rotate(var(--spin, 520deg));
    opacity: 0.15;
  }
}

.middle-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.45rem;
}

.control-btn {
  min-width: 64px;
  min-height: 52px;
  border-radius: 9px;
  border: 1px solid #9bd2ef;
  background: #f0f9ff;
  color: #0b3858;
  font-size: 1.2rem;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 8px 14px #02304b23;
}

.hidden {
  display: none !important;
}

.score-pop {
  animation: scorePulse 0.24s ease;
}

@keyframes scorePulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.24); color: var(--success); }
  100% { transform: scale(1); }
}

@media (max-width: 980px) {
  .layout-grid {
    grid-template-columns: 1fr;
  }

  .board-column {
    display: none;
  }
}

@media (max-width: 640px) {
  .page-wrap {
    padding: 0.65rem;
  }

  .game-brandbar {
    padding: 0.42rem 0.5rem;
  }

  .game-brand-name {
    font-size: 0.86rem;
  }

  .game-brand-logo {
    width: 28px !important;
    height: 28px !important;
    min-width: 28px;
    min-height: 28px;
    max-width: 28px !important;
    max-height: 28px !important;
    flex: 0 0 28px;
  }

  .game-home-link {
    min-height: 34px;
    padding: 0.3rem 0.56rem;
    font-size: 0.76rem;
  }

  .home-icon {
    width: 1rem;
    height: 1rem;
    font-size: 0;
  }

  .home-icon::before {
    font-size: 0.66rem;
  }

  .btn-grid {
    grid-template-columns: 1fr;
  }

  .result-grid {
    grid-template-columns: 1fr;
  }

  .overlay {
    padding: 0.45rem;
  }

  .overlay-card {
    padding: 0.85rem;
  }

  .state-badge {
    top: 0.45rem;
    left: 0.45rem;
  }

  .hud {
    gap: 0.42rem;
    grid-template-columns: 1fr;
    justify-items: stretch;
    order: 3;
    margin-top: 0.62rem;
    padding: 0.58rem;
  }

  .hud-left {
    justify-content: center;
  }

  .hud-right {
    justify-content: center;
  }

  .hud-pill {
    font-size: 0.75rem;
    padding: 0.3rem 0.52rem;
  }

  .icon-btn {
    min-height: 34px;
    padding: 0.35rem 0.66rem;
    font-size: 0.74rem;
  }

  .fullscreen-overlay {
    padding: 0.5rem;
  }

  .game-over-video-wrap {
    width: min(100%, 94vw);
    max-height: 30dvh;
    margin-bottom: 0.48rem;
  }

  .control-btn {
    min-width: 58px;
    min-height: 48px;
  }
}

@media (max-width: 420px) {
  .game-over-video-wrap {
    width: min(100%, 94vw);
    max-height: 27dvh;
    margin-bottom: 0.35rem;
  }

  .high-score-toast {
    min-width: min(94vw, 320px);
    font-size: 0.88rem;
    padding: 0.66rem 0.84rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .confetti-piece {
    animation-duration: 1ms;
  }

  .high-score-toast {
    transition: none;
  }
}
