:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #eef2f5;
  --ink: #17202a;
  --muted: #66727f;
  --line: #d8e0e7;
  --primary: #1867c0;
  --primary-dark: #104e93;
  --danger: #c73737;
  --shadow: 0 18px 50px rgba(24, 35, 48, 0.12);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(243, 245, 247, 0.9)),
    var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  transition: background-color 0.18s ease, transform 0.18s ease, box-shadow 0.18s ease;
}

button:hover {
  background: var(--primary-dark);
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.game-shell {
  display: grid;
  grid-template-columns: minmax(300px, 520px) minmax(320px, 420px);
  gap: 24px;
  align-items: start;
  width: min(1120px, calc(100% - 32px));
  margin: 28px auto;
}

.play-section,
.side-panel {
  min-width: 0;
}

.game-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.game-header h1 {
  margin: 0;
  font-size: clamp(28px, 5vw, 42px);
  line-height: 1.1;
  letter-spacing: 0;
}

.game-header p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 15px;
}

.board-wrap {
  position: relative;
  width: min(100%, 420px);
  aspect-ratio: 1 / 2;
  margin: 0 auto;
  border-radius: 8px;
  background: #111820;
  box-shadow: var(--shadow);
  overflow: hidden;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #111820;
}

.status-layer {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(13, 19, 26, 0.52);
  padding: 20px;
}

.status-card {
  width: min(240px, 100%);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--ink);
  padding: 20px;
  text-align: center;
  box-shadow: var(--shadow);
}

.status-card strong,
.status-card span {
  display: block;
}

.status-card strong {
  font-size: 24px;
  margin-bottom: 8px;
}

.status-card span {
  color: var(--muted);
}

.mobile-controls {
  display: none;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 16px;
}

.mobile-controls button {
  min-height: 48px;
  padding: 10px 8px;
  background: #243447;
  font-size: 15px;
}

.side-panel {
  display: grid;
  gap: 16px;
}

.panel-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 10px 28px rgba(24, 35, 48, 0.07);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  overflow: hidden;
}

.stats-grid div {
  padding: 18px 14px;
  border-right: 1px solid var(--line);
}

.stats-grid div:last-child {
  border-right: 0;
}

.stats-grid span {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
}

.stats-grid strong {
  display: block;
  font-size: 26px;
  line-height: 1;
}

.next-block,
.help-block,
.leaderboard-block {
  padding: 16px;
}

.block-title {
  color: var(--ink);
  font-size: 16px;
  font-weight: 800;
}

.next-block {
  display: grid;
  grid-template-columns: 1fr 120px;
  align-items: center;
  gap: 16px;
}

#nextCanvas {
  width: 120px;
  height: 120px;
  border-radius: 8px;
  background: var(--surface-2);
}

.help-block dl {
  display: grid;
  gap: 8px;
  margin: 14px 0 0;
}

.help-block div {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 12px;
}

.help-block dt,
.help-block dd {
  margin: 0;
}

.help-block dt {
  color: var(--ink);
  font-weight: 800;
}

.help-block dd {
  color: var(--muted);
}

.leaderboard-head {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.leaderboard-head span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.ghost-button,
.text-button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

.ghost-button:hover,
.text-button:hover {
  background: var(--surface-2);
}

.game-header .ghost-button,
.text-button {
  padding: 9px 12px;
  white-space: nowrap;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 360px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 8px;
  text-align: left;
  font-size: 14px;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

td:nth-child(1),
td:nth-child(3) {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

td:nth-child(4) {
  color: var(--muted);
  white-space: nowrap;
}

tr:last-child td {
  border-bottom: 0;
}

.message {
  min-height: 20px;
  margin: 10px 0 0;
  color: var(--danger);
  font-size: 14px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(13, 19, 26, 0.58);
}

.score-modal {
  width: min(420px, 100%);
  border-radius: 8px;
  background: #fff;
  padding: 24px;
  box-shadow: var(--shadow);
}

.score-modal h2 {
  margin: 0 0 12px;
  font-size: 28px;
  letter-spacing: 0;
}

.score-modal p {
  margin: 0 0 16px;
  color: var(--muted);
}

.score-modal p strong {
  color: var(--ink);
}

.score-modal label {
  display: block;
  margin-bottom: 8px;
  font-weight: 800;
}

.score-modal input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  outline: 0;
}

.score-modal input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(24, 103, 192, 0.16);
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 16px;
}

.modal-actions button {
  min-height: 44px;
  padding: 10px 12px;
}

@media (max-width: 900px) {
  .game-shell {
    grid-template-columns: 1fr;
    width: min(720px, calc(100% - 24px));
    margin: 18px auto 28px;
  }

  .board-wrap {
    width: min(100%, 380px);
  }

  .mobile-controls {
    display: grid;
  }
}

@media (max-width: 520px) {
  .game-header {
    align-items: start;
  }

  .game-header .ghost-button {
    display: none;
  }

  .board-wrap {
    width: min(100%, 340px);
  }

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

  .stats-grid strong {
    font-size: 22px;
  }

  .next-block {
    grid-template-columns: 1fr auto;
  }

  .help-block div {
    grid-template-columns: 78px 1fr;
  }

  .modal-actions {
    grid-template-columns: 1fr;
  }
}

body.embed-mode {
  background: var(--bg);
}

body.embed-mode .game-shell {
  grid-template-columns: minmax(300px, 420px) minmax(260px, 320px);
  gap: 16px;
  width: min(960px, calc(100% - 20px));
  margin: 16px auto 22px;
}

body.embed-mode .game-header h1 {
  font-size: clamp(26px, 4vw, 36px);
}

body.embed-mode .board-wrap {
  width: min(100%, 380px);
}

body.embed-mode .leaderboard-block {
  display: none;
}

body.embed-mode .panel-block {
  box-shadow: 0 8px 22px rgba(24, 35, 48, 0.06);
}

@media (max-width: 820px) {
  body.embed-mode .game-shell {
    grid-template-columns: 1fr;
    width: min(620px, calc(100% - 20px));
  }

  body.embed-mode .mobile-controls {
    display: grid;
  }
}
