:root {
  color-scheme: dark;
  --bg: #101114;
  --panel: #181b20;
  --panel-2: #22262d;
  --line: #3f4652;
  --text: #f5efe2;
  --muted: #9ba8b6;
  --gold: #f2b84b;
  --cyan: #54d7c7;
  --red: #ef6461;
  --green: #7bd66f;
  --card-w: 74px;
  --card-h: 104px;
  --stack-gap: 28px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background:
    linear-gradient(rgba(255,255,255,.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.03) 1px, transparent 1px),
    var(--bg);
  background-size: 24px 24px;
  color: var(--text);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  -webkit-text-size-adjust: 100%;
}

button,
input {
  font: inherit;
}

a {
  color: var(--cyan);
}

a:hover {
  color: var(--gold);
}

button {
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

.hidden {
  display: none !important;
}

.app-shell {
  width: min(1180px, calc(100vw - 24px));
  margin: 0 auto;
  padding: 18px 0;
}

.login-view {
  min-height: calc(100vh - 36px);
  display: grid;
  align-content: start;
  justify-items: stretch;
  gap: 18px;
  padding: 34px 0;
}

.public-hero {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 18px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 92px;
  height: 92px;
  border: 4px solid var(--gold);
  background: #0b0c0f;
  color: var(--cyan);
  box-shadow: 8px 8px 0 #000;
  font-size: 26px;
  font-weight: 900;
}

.login-view h1,
.top-bar h1 {
  margin: 0;
  font-size: clamp(28px, 5vw, 54px);
  letter-spacing: 0;
}

.hero-copy {
  max-width: 760px;
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.7;
}

.public-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 420px);
  gap: 16px;
  align-items: start;
}

.public-content,
.home-panel,
.lore-panel {
  border: 2px solid var(--line);
  background: var(--panel);
}

.public-content {
  padding: 18px;
}

.public-content h2,
.home-panel h2,
.lore-panel h3 {
  margin: 0 0 12px;
}

.public-content p,
.home-panel p,
.lore-panel p {
  line-height: 1.75;
  color: var(--muted);
}

.intro-grid,
.how-to-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}

.intro-grid article,
.how-to-grid article {
  min-height: 126px;
  padding: 12px;
  background: #0b0c0f;
  border: 1px solid var(--line);
}

.intro-grid h3,
.how-to-grid h3 {
  margin: 0 0 8px;
  color: var(--gold);
}

.intro-grid p,
.how-to-grid p {
  margin: 0;
}

.login-form {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  padding: 22px;
  border: 2px solid var(--line);
  background: var(--panel);
  box-shadow: 8px 8px 0 #000;
}

.login-form h2 {
  margin: 0;
}

.site-footer {
  width: min(1180px, calc(100vw - 24px));
  margin: 0 auto 24px;
  padding: 14px;
  border: 2px solid var(--line);
  background: var(--panel);
}

.site-footer nav {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
}

.site-footer a,
.static-nav a {
  color: var(--muted);
  text-decoration: none;
}

.site-footer a:hover,
.static-nav a:hover {
  color: var(--cyan);
}

.static-page {
  width: min(960px, calc(100vw - 24px));
  margin: 0 auto;
  padding: 22px 0 34px;
}

.static-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  padding: 12px 0 20px;
}

.static-hero,
.static-section {
  border: 2px solid var(--line);
  background: var(--panel);
}

.static-hero {
  padding: 24px;
  margin-bottom: 14px;
}

.static-hero h1 {
  margin: 0;
  font-size: clamp(32px, 6vw, 56px);
}

.static-hero p,
.static-section p,
.static-section li {
  color: var(--muted);
  line-height: 1.75;
}

.static-section {
  padding: 20px;
  margin-bottom: 14px;
}

.static-section h2,
.static-section h3 {
  margin: 0 0 10px;
  color: var(--gold);
}

.static-section ul {
  margin: 0;
  padding-left: 22px;
}

.static-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.static-card {
  padding: 14px;
  border: 1px solid var(--line);
  background: #0b0c0f;
}

.static-card h2,
.static-card h3 {
  color: var(--gold);
}

.static-card a {
  color: var(--cyan);
  text-decoration: none;
}

.static-section time {
  color: var(--cyan);
  font-weight: 900;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
}

input {
  width: 100%;
  padding: 13px 14px;
  border: 2px solid var(--line);
  border-radius: 0;
  background: #0b0c0f;
  color: var(--text);
  outline: none;
}

input:focus {
  border-color: var(--cyan);
}

.login-form button,
#startBtn,
.fullscreen-btn {
  min-height: 46px;
  background: var(--gold);
  color: #161006;
  font-weight: 900;
  box-shadow: 4px 4px 0 #000;
}

.fullscreen-btn {
  background: var(--cyan);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 0 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--cyan);
}

.player-panel {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  min-width: 280px;
  padding: 10px;
  border: 2px solid var(--line);
  background: var(--panel);
}

#coinCount::before {
  content: "$ ";
  color: var(--gold);
}

#logoutBtn {
  min-height: 36px;
  padding: 0 12px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
}

.content-grid {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 260px;
  gap: 14px;
  align-items: start;
}

.game-menu,
.leaderboard,
.play-area {
  border: 2px solid var(--line);
  background: var(--panel);
}

.game-menu {
  display: grid;
  align-content: start;
  gap: 8px;
  padding: 10px;
}

.game-group {
  display: grid;
  gap: 7px;
  padding: 8px;
  background: #11151a;
  border: 1px solid var(--line);
}

.game-group summary {
  cursor: pointer;
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--cyan);
  font-weight: 900;
  list-style: none;
}

.game-group summary::-webkit-details-marker {
  display: none;
}

.game-group summary::after {
  content: "+";
  color: var(--gold);
  font-size: 20px;
}

.game-group[open] summary::after {
  content: "-";
}

.game-group[open] summary {
  margin-bottom: 6px;
}

.game-tile {
  display: grid;
  gap: 5px;
  min-height: 64px;
  padding: 10px;
  text-align: left;
  background: var(--panel-2);
  color: var(--text);
  border-left: 4px solid transparent;
}

.game-tile.active {
  border-left-color: var(--cyan);
  background: #102b2c;
}

.game-tile small {
  color: var(--muted);
}

.play-area {
  min-width: 0;
  padding: 12px;
}

.hud {
  display: grid;
  grid-template-columns: 1fr 1fr 120px;
  gap: 10px;
  margin-bottom: 12px;
}

.play-area.has-fullscreen-control .hud {
  grid-template-columns: 1fr 1fr 120px 140px;
}

.hud > div {
  display: grid;
  gap: 3px;
  min-height: 54px;
  padding: 8px 10px;
  background: #0b0c0f;
  border: 1px solid var(--line);
}

.hud span {
  color: var(--muted);
  font-size: 13px;
}

.hud strong {
  font-size: 24px;
  line-height: 1;
}

canvas {
  display: block;
  width: 100%;
  max-height: min(62vh, 620px);
  aspect-ratio: 3 / 2;
  image-rendering: pixelated;
  background: #07090d;
  border: 2px solid #000;
  touch-action: none;
}

canvas.is-square-board {
  max-width: min(100%, 720px);
  margin: 0 auto;
  aspect-ratio: 1 / 1;
}

.play-area:fullscreen,
.play-area.is-gomoku-expanded {
  width: 100vw;
  height: 100vh;
  max-width: none;
  padding: 10px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background:
    linear-gradient(rgba(255,255,255,.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.03) 1px, transparent 1px),
    var(--panel);
  background-size: 24px 24px;
}

.play-area.is-gomoku-expanded {
  position: fixed;
  inset: 0;
  z-index: 1000;
}

.play-area:fullscreen .hud,
.play-area.is-gomoku-expanded .hud {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 112px 150px;
}

.play-area:fullscreen canvas,
.play-area.is-gomoku-expanded canvas {
  align-self: stretch;
  justify-self: center;
  width: min(100%, calc(100vh - 92px));
  height: auto;
  max-height: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
}

.play-area:fullscreen .touch-controls,
.play-area.is-gomoku-expanded .touch-controls {
  display: none !important;
}

.touch-controls {
  display: none;
  grid-template-columns: repeat(4, 64px);
  grid-template-areas:
    ". up . attack"
    "left down right attack";
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}

.touch-controls button {
  display: grid;
  place-items: center;
  width: 64px;
  height: 54px;
  border: 2px solid var(--line);
  background: #0b0c0f;
  color: var(--text);
  font-size: 20px;
  box-shadow: 3px 3px 0 #000;
}

.touch-controls button:active,
.touch-controls button.is-pressed {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #000;
  border-color: var(--cyan);
}

.touch-controls [data-key="arrowup"] {
  grid-area: up;
}

.touch-controls [data-key="arrowleft"] {
  grid-area: left;
}

.touch-controls [data-key="arrowdown"] {
  grid-area: down;
}

.touch-controls [data-key="arrowright"] {
  grid-area: right;
}

.touch-controls [data-action="attack"] {
  grid-area: attack;
  height: 116px;
  color: #161006;
  background: var(--gold);
  font-weight: 900;
}

.game-panel {
  min-height: 480px;
}

.mini-panel {
  min-height: 480px;
  padding: 16px;
  background: #0b0c0f;
  border: 2px solid var(--line);
}

.mini-head {
  padding: 16px;
  border-left: 4px solid var(--gold);
  background:
    linear-gradient(90deg, rgba(242,184,75,.12), transparent 50%),
    #12161b;
}

.mini-head h2 {
  margin: 0 0 10px;
  font-size: clamp(26px, 4vw, 42px);
}

.mini-head p {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
}

.mini-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.mini-stats span {
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--cyan);
  font-weight: 900;
}

.mini-log {
  min-height: 132px;
  max-height: 210px;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 8px;
  padding: 12px;
  background: #101114;
  border: 1px solid var(--line);
}

.mini-log p,
.mini-prompt {
  margin: 0;
  color: var(--muted);
  line-height: 1.7;
}

.mini-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.mini-prompt {
  grid-column: 1 / -1;
  padding: 12px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
}

.mini-actions button {
  min-height: 48px;
  padding: 10px;
  background: var(--gold);
  color: #161006;
  font-weight: 900;
  box-shadow: 3px 3px 0 #000;
}

.home-panel {
  min-height: 480px;
  padding: 16px;
}

.home-lead {
  padding: 16px;
  margin-bottom: 12px;
  background: #0b0c0f;
  border-left: 4px solid var(--cyan);
}

.home-lead h2 {
  font-size: clamp(24px, 4vw, 38px);
}

.lore-panel {
  padding: 16px;
  margin-top: 16px;
  background:
    linear-gradient(90deg, rgba(242,184,75,.12), transparent 42%),
    #11151a;
}

.chapter-tabs {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.chapter-tabs button {
  min-height: 40px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
}

.chapter-tabs button.active {
  color: #07100f;
  background: var(--cyan);
  font-weight: 900;
}

.story-log {
  height: 340px;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 10px;
  padding: 12px;
  background: #0b0c0f;
  border: 1px solid var(--line);
}

.story-entry {
  display: grid;
  gap: 7px;
  padding: 10px;
  border-left: 4px solid var(--gold);
  background: #12161b;
}

.story-entry p {
  margin: 0;
  line-height: 1.7;
}

.story-entry .move {
  color: var(--gold);
}

.story-entry .ai {
  color: var(--text);
}

.story-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 100px;
  gap: 8px;
  margin-top: 12px;
}

.story-form button {
  min-height: 46px;
  background: var(--gold);
  color: #161006;
  font-weight: 900;
}

.cards-panel {
  min-height: 540px;
  padding: 10px;
  background: #0b4a2d;
  border: 5px solid #3d2b1f;
}

.cards-top {
  display: grid;
  grid-template-columns: var(--card-w) var(--card-w) minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  margin-bottom: 18px;
}

.foundation-row {
  display: grid;
  grid-template-columns: repeat(4, var(--card-w));
  justify-content: end;
  gap: 10px;
}

.tableau-row {
  display: grid;
  grid-template-columns: repeat(7, minmax(var(--card-w), 1fr));
  gap: 8px;
}

.tableau-pile {
  position: relative;
  min-height: 290px;
  border: 1px dashed rgba(255,255,255,.28);
  transition: min-height .12s ease;
}

.card-slot,
.playing-card {
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 5px;
  border: 2px solid rgba(255,255,255,.42);
}

.card-slot {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  background: rgba(255,255,255,.08);
  color: rgba(255,255,255,.78);
}

.playing-card {
  display: grid;
  align-content: space-between;
  padding: 7px;
  background: #f8f4e8;
  color: #101114;
  box-shadow: 3px 3px 0 rgba(0,0,0,.38);
  text-align: left;
  font-weight: 900;
  font-size: 16px;
}

.tableau-pile .playing-card {
  position: absolute;
}

.card-slot .playing-card {
  position: static;
}

.playing-card.red {
  color: #c83a32;
}

.playing-card.back {
  background:
    radial-gradient(#314b68 18%, transparent 20%),
    #1f334c;
  background-size: 9px 9px;
  color: transparent;
}

.playing-card.selected {
  outline: 3px solid var(--gold);
  transform: translateY(-4px);
}

.playing-card .corner:last-child {
  justify-self: end;
  transform: rotate(180deg);
}

.card-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 28px;
  position: relative;
  z-index: 2;
}

.card-actions button {
  min-height: 42px;
  padding: 0 14px;
  background: var(--red);
  color: #fff;
  font-weight: 900;
}

.leaderboard {
  padding: 14px;
}

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

.leaderboard h2 {
  margin: 0;
  font-size: 20px;
}

.leaderboard-head a {
  padding: 7px 9px;
  background: #0b0c0f;
  border: 1px solid var(--line);
  color: var(--cyan);
  text-decoration: none;
}

.leaderboard ol {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 24px;
}

.leaderboard li {
  padding: 8px;
  background: #0b0c0f;
  border: 1px solid var(--line);
}

.leaderboard b {
  color: var(--gold);
}

@media (max-width: 1080px) {
  .content-grid {
    grid-template-columns: minmax(0, 1fr) 280px;
  }

  .game-menu {
    grid-column: 1 / -1;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .play-area {
    grid-column: 1;
  }

  .leaderboard {
    grid-column: 2;
  }
}

@media (max-width: 820px) {
  .top-bar {
    display: grid;
  }

  .player-panel {
    min-width: 0;
  }

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

  .game-menu {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .public-layout {
    grid-template-columns: 1fr;
  }

  .game-menu,
  .play-area,
  .leaderboard {
    grid-column: auto;
  }

  .leaderboard ol {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding-left: 0;
    list-style-position: inside;
  }
}

@media (max-width: 620px) {
  .app-shell {
    width: min(100vw - 14px, 1180px);
    padding-top: 8px;
  }

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

  .play-area.has-fullscreen-control .hud,
  .play-area:fullscreen .hud,
  .play-area.is-gomoku-expanded .hud {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .game-menu {
    grid-template-columns: 1fr;
    max-height: 330px;
    overflow: auto;
  }

  .public-hero {
    grid-template-columns: 1fr;
    justify-items: start;
  }

  .intro-grid,
  .how-to-grid,
  .static-grid {
    grid-template-columns: 1fr;
  }

  .public-content,
  .home-panel {
    padding: 12px;
  }

  .chapter-tabs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mini-stats,
  .mini-actions {
    grid-template-columns: 1fr;
  }

  .story-form {
    grid-template-columns: 1fr;
  }

  .cards-panel {
    --card-w: 46px;
    --card-h: 68px;
    --stack-gap: 18px;
    padding: 7px;
    overflow-x: hidden;
  }

  .cards-top {
    grid-template-columns: var(--card-w) var(--card-w) minmax(0, 1fr);
    gap: 5px;
    margin-bottom: 12px;
  }

  .foundation-row {
    grid-template-columns: repeat(4, var(--card-w));
    gap: 5px;
  }

  .tableau-row {
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 4px;
  }

  .tableau-pile {
    min-width: 0;
  }

  .card-slot,
  .playing-card {
    border-width: 1px;
    border-radius: 4px;
  }

  .card-slot {
    font-size: 12px;
  }

  .playing-card {
    padding: 4px;
    font-size: 13px;
    box-shadow: 2px 2px 0 rgba(0,0,0,.38);
  }

  .playing-card.selected {
    outline-width: 2px;
    transform: translateY(-2px);
  }

  .card-actions {
    margin-top: 16px;
  }

  .login-view {
    align-content: start;
    padding-top: 42px;
  }

  .brand-mark {
    width: 72px;
    height: 72px;
    font-size: 21px;
  }

  .login-form {
    padding: 16px;
    box-shadow: 5px 5px 0 #000;
  }

  .top-bar h1,
  .login-view h1 {
    font-size: 30px;
  }

  .player-panel {
    grid-template-columns: 1fr auto;
  }

  #logoutBtn {
    grid-column: 1 / -1;
  }

  .game-tile {
    min-height: 62px;
  }

  .hud strong {
    font-size: 20px;
  }

  canvas {
    max-height: none;
  }

  .touch-controls {
    display: grid;
  }

  .leaderboard ol {
    grid-template-columns: 1fr;
  }
}

@media (hover: none) and (pointer: coarse) {
  .touch-controls {
    display: grid;
  }
}
