/*
  2048 PWA stylesheet.
  Design tokens mirror the davidkaing.com theme so the app feels like the site.
*/

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

:root {
  --fg: #1a1a1a;
  --fg-dim: #66645f;
  --bg: #f8f7f4;
  --surface: #f1efe9;
  --border: #dcd8cf;
  --accent: #1f4335;
  --accent-contrast: #f7fbf8;
  --shadow: 0 24px 60px rgba(45, 40, 32, 0.08);
  --cell: #e7e2d8;
  /*
    Board metrics. The app scales the board to fill the card on phones and
    caps it on desktop; the homepage keeps its own fixed 254px board.
  */
  --pad: 6px;
  --gap: 6px;
  --tile: clamp(56px, calc(25vw - 23.5px), 76px);
  --board: calc(var(--pad) * 2 + var(--tile) * 4 + var(--gap) * 3);
  color-scheme: light dark;
}

@font-face {
  font-family: 'DM Mono';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/fonts/dm-mono-400-latin-v16.woff2') format('woff2');
}

@font-face {
  font-family: 'DM Mono';
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url('/fonts/dm-mono-500-latin-v16.woff2') format('woff2');
}

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--bg);
  color: var(--fg);
  font-family: 'DM Mono', ui-monospace, monospace;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

a { color: inherit; }
button, input { font: inherit; }

button:focus-visible,
a:focus-visible,
input:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.shell {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(100% - 2rem, 430px);
  margin-inline: auto;
  padding-block: 1.25rem 2rem;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 44px;
}

.app-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.app-nav {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  font-size: 0.7rem;
  color: var(--fg-dim);
}

.app-nav a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  text-underline-offset: 0.2rem;
}

.install-button {
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  font-size: 0.7rem;
}

.install-button:hover { border-color: var(--accent); }

.notice {
  margin: 0;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--surface);
  color: var(--fg-dim);
  font-size: 0.68rem;
  line-height: 1.5;
}

.game {
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.game-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  width: var(--board);
  margin: 0 auto 0.75rem;
}

.score-box {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin-left: auto;
  text-align: right;
}

.score-label {
  color: var(--fg-dim);
  font-size: 0.6rem;
  letter-spacing: 0.08em;
}

#score {
  font-size: 1.1rem;
  font-weight: 500;
  line-height: 1;
}

.best-line {
  margin: 0;
  align-self: center;
  color: var(--fg-dim);
  font-size: 0.66rem;
}

.best-line span { color: var(--fg); }

.board {
  display: grid;
  position: relative;
  grid-template-columns: repeat(4, var(--tile));
  grid-template-rows: repeat(4, var(--tile));
  gap: var(--gap);
  width: var(--board);
  margin-inline: auto;
  padding: var(--pad);
  border-radius: 3px;
  background: var(--border);
  touch-action: none;
  user-select: none;
}

.board:focus-visible { outline-offset: 6px; }

.cell-bg {
  width: var(--tile);
  height: var(--tile);
  border-radius: 2px;
  background: var(--cell);
}

.tile {
  position: absolute;
  z-index: 10;
  top: 0;
  left: 0;
  width: var(--tile);
  height: var(--tile);
  transition: transform 120ms cubic-bezier(0.25, 0.46, 0.45, 0.94);
  touch-action: none;
  will-change: transform;
}

.tile-face {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 2px;
  background: var(--cell);
  color: var(--fg);
  font-size: calc(var(--tile) * 0.2857);
  font-weight: 500;
}

@keyframes tile-appear {
  from { opacity: 0; transform: scale(0); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes tile-pop {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.4); }
}

.tile-new .tile-face { animation: tile-appear 120ms cubic-bezier(0.175, 0.885, 0.32, 1.275) both; }
.tile-pop .tile-face { animation: tile-pop 180ms ease-in-out; }

.tile-face[data-v="2"] { background: #eee8da; }
.tile-face[data-v="4"] { background: #ede0c8; }
.tile-face[data-v="8"] { background: #f2b179; color: #f8f7f4; }
.tile-face[data-v="16"] { background: #f59563; color: #f8f7f4; font-size: calc(var(--tile) * 0.25); }
.tile-face[data-v="32"] { background: #f67c5f; color: #f8f7f4; font-size: calc(var(--tile) * 0.25); }
.tile-face[data-v="64"] { background: #f65e3b; color: #f8f7f4; font-size: calc(var(--tile) * 0.25); }
.tile-face[data-v="128"] { background: #edcf72; color: #f8f7f4; font-size: calc(var(--tile) * 0.2286); }
.tile-face[data-v="256"] { background: #edcc61; color: #f8f7f4; font-size: calc(var(--tile) * 0.2286); }
.tile-face[data-v="512"] { background: #edc850; color: #f8f7f4; font-size: calc(var(--tile) * 0.2286); }
.tile-face[data-v="1024"] { background: #edc53f; color: #f8f7f4; font-size: calc(var(--tile) * 0.2); }
.tile-face[data-v="2048"] { background: #edc22e; color: #f8f7f4; font-size: calc(var(--tile) * 0.2); }

.game-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: var(--board);
  margin: 0.75rem auto 0;
}

.game-status {
  margin: 0;
  color: var(--fg-dim);
  font-size: 0.66rem;
  line-height: 1.5;
}

.text-button {
  min-height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  font-size: 0.67rem;
  text-decoration: underline;
  text-underline-offset: 0.2rem;
  white-space: nowrap;
}

.name-entry {
  width: var(--board);
  margin: 1rem auto 0;
  color: var(--fg-dim);
  font-size: 0.7rem;
}

.name-entry > label {
  display: block;
  margin-bottom: 0.45rem;
}

.name-entry-controls {
  display: flex;
  gap: 0.45rem;
}

.name-entry input,
.name-entry button {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.72rem;
}

.name-entry input {
  min-width: 0;
  flex: 1;
  padding: 0.6rem 0.65rem;
}

.name-entry button {
  padding: 0.6rem 0.75rem;
  cursor: pointer;
}

.name-err {
  min-height: 1.2em;
  margin: 0.4rem 0 0;
  color: #a92d24;
  font-size: 0.65rem;
}

.scoreboard {
  width: var(--board);
  margin: 1.5rem auto 0;
  font-size: 0.7rem;
}

.scoreboard table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.7rem;
}

.scoreboard caption {
  margin-bottom: 0.45rem;
  color: var(--fg-dim);
  letter-spacing: 0.06em;
  text-align: left;
}

.scoreboard th,
.scoreboard td {
  padding: 0.2rem 0.3rem;
  color: var(--fg-dim);
  font-weight: 400;
  text-align: left;
}

.scoreboard th {
  border-bottom: 1px solid var(--border);
  font-size: 0.6rem;
}

.scoreboard td:first-child,
.scoreboard th:first-child {
  width: 2rem;
  color: var(--fg);
  text-align: right;
}

.scoreboard td:nth-child(2) { color: var(--fg); }

.scoreboard td:last-child,
.scoreboard th:last-child { text-align: right; }

.scoreboard tr.me td {
  color: var(--fg);
  font-weight: 500;
}

.scoreboard-empty {
  margin: 0;
  color: var(--fg-dim);
  font-style: italic;
}

.app-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  color: var(--fg-dim);
  font-size: 0.66rem;
}

.app-footer a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  text-underline-offset: 0.2rem;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}

/* Dark mode must stay last so it wins the cascade. */
@media (prefers-color-scheme: dark) {
  :root {
    --fg: #e5eaf0;
    --fg-dim: #9aa7b7;
    --bg: #0d1117;
    --surface: #141a22;
    --border: #2d3748;
    --accent: #7fc6a8;
    --accent-contrast: #07120d;
    --shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
    --cell: #303844;
  }

  .tile-face[data-v="2"] { background: #45423e; }
  .tile-face[data-v="4"] { background: #584f43; }
  .name-err { color: #ff9d94; }
}
