:root {
  --bg: #0d1117;
  --bg-raised: #161b22;
  --bg-sunken: #010409;
  --border: #30363d;
  --border-soft: #21262d;
  --fg: #e6edf3;
  --fg-dim: #8b949e;
  --fg-dimmer: #6e7681;
  --accent: #e3b341;
  --accent-fg: #1c1206;
  --cell-bg: #1c2129;
  --mono: "Menlo", "Monaco", "Cascadia Code", "Consolas", "Liberation Mono", monospace;
  --ui: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

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

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
}

body {
  font-family: var(--ui);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

kbd {
  font-family: var(--mono);
  font-size: 0.78em;
  background: var(--bg-sunken);
  border: 1px solid var(--border-soft);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--fg-dim);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  padding: 28px 32px 18px;
  border-bottom: 1px solid var(--border-soft);
  flex-wrap: wrap;
}

.brand h1 {
  margin: 0 0 6px;
  font-size: 1.6rem;
  letter-spacing: -0.02em;
  font-weight: 700;
}

.tag {
  margin: 0;
  max-width: 46ch;
  color: var(--fg-dim);
  font-size: 0.92rem;
  line-height: 1.5;
}

.scores {
  display: flex;
  gap: 10px;
}

.score-box {
  background: var(--bg-raised);
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 8px 16px;
  min-width: 74px;
  text-align: center;
}

.score-label {
  display: block;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-dimmer);
  margin-bottom: 2px;
}

.score-value {
  display: block;
  font-family: var(--mono);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
}

.layout {
  flex: 1;
  display: flex;
  gap: 0;
  position: relative;
  align-items: flex-start;
  justify-content: center;
  padding: 28px 20px 40px;
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: 100%;
  max-width: 500px;
}

.deck-picker {
  display: flex;
  gap: 6px;
  width: 100%;
  max-width: 460px;
  background: var(--bg-raised);
  border: 1px solid var(--border-soft);
  border-radius: 9px;
  padding: 4px;
}

.deck-btn {
  flex: 1;
  font-family: var(--ui);
  font-size: 0.82rem;
  font-weight: 600;
  background: none;
  color: var(--fg-dim);
  border: none;
  border-radius: 6px;
  padding: 8px 10px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.deck-btn:hover { color: var(--fg); }
.deck-btn.active {
  background: var(--accent);
  color: var(--accent-fg);
}

.board-toolbar {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 460px;
  justify-content: flex-end;
}

.btn {
  font-family: var(--ui);
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 7px;
  padding: 9px 16px;
  cursor: pointer;
  transition: filter 0.12s ease, transform 0.05s ease;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.35; cursor: not-allowed; filter: none; transform: none; }
.btn:disabled:active { transform: none; }

.btn-ghost {
  background: var(--bg-raised);
  color: var(--fg);
  border: 1px solid var(--border);
}
.btn-ghost:hover { filter: none; background: var(--border-soft); }

#codexCount {
  color: var(--fg-dim);
  font-weight: 400;
  font-family: var(--mono);
  font-size: 0.8em;
}

.board-wrap {
  position: relative;
  width: 100%;
  max-width: 460px;
  aspect-ratio: 1 / 1;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--bg-sunken);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  padding: 10px;
  touch-action: none;
  user-select: none;
}

.board-bg {
  position: absolute;
  inset: 10px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 10px;
}

.board-bg .cell {
  background: var(--cell-bg);
  border-radius: 8px;
}

.tiles {
  position: absolute;
  inset: 10px;
}

.tile {
  position: absolute;
  width: calc(25% - 7.5px);
  height: calc(25% - 7.5px);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 4px;
  transition: left 0.11s ease-in-out, top 0.11s ease-in-out;
  box-shadow: 0 1px 0 rgba(0,0,0,0.25) inset;
}

.tile .tile-lang {
  font-weight: 700;
  font-size: clamp(0.62rem, 2.6vw, 0.95rem);
  line-height: 1.15;
  word-break: break-word;
}

.tile .tile-year {
  font-family: var(--mono);
  font-size: clamp(0.5rem, 1.8vw, 0.68rem);
  opacity: 0.75;
  margin-top: 2px;
}

.tile.spawn {
  animation: spawnIn 0.16s ease-out;
}

.tile.merged {
  animation: mergePop 0.16s ease-out;
}

@keyframes spawnIn {
  from { transform: scale(0.3); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes mergePop {
  0% { transform: scale(1); }
  45% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(1, 4, 9, 0.86);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease-out;
}

.overlay[hidden] {
  display: none;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.overlay-card { text-align: center; padding: 20px; }

.overlay-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 8px;
}

.overlay-sub {
  color: var(--fg-dim);
  max-width: 32ch;
  margin: 0 auto 18px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.hint {
  color: var(--fg-dimmer);
  font-size: 0.8rem;
}

.hint-mobile { display: none; }

@media (hover: none) and (pointer: coarse) {
  .hint-desktop { display: none; }
  .hint-mobile { display: inline; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%) translateY(0);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  max-width: min(480px, 88vw);
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--fg);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 50;
  animation: toastIn 0.18s ease-out;
}

.toast strong { color: var(--accent); }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.codex-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: min(360px, 90vw);
  background: var(--bg-raised);
  border-left: 1px solid var(--border-soft);
  padding: 24px;
  overflow-y: auto;
  z-index: 60;
  box-shadow: -12px 0 32px rgba(0,0,0,0.35);
  animation: slideIn 0.18s ease-out;
}

@keyframes slideIn {
  from { transform: translateX(24px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.codex-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.codex-header h2 { margin: 0; font-size: 1.15rem; }

.codex-close {
  background: none;
  border: none;
  color: var(--fg-dim);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}
.codex-close:hover { color: var(--fg); }

.codex-sub {
  color: var(--fg-dim);
  font-size: 0.82rem;
  margin: 8px 0 18px;
  line-height: 1.5;
}

.codex-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.codex-item {
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.codex-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex-shrink: 0;
  margin-top: 3px;
}

.codex-item.locked .codex-swatch { background: var(--border) !important; }

.codex-body { flex: 1; min-width: 0; }

.codex-name {
  font-weight: 700;
  font-size: 0.88rem;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.codex-year {
  font-family: var(--mono);
  color: var(--fg-dimmer);
  font-weight: 400;
  font-size: 0.8rem;
}

.codex-fact {
  color: var(--fg-dim);
  font-size: 0.78rem;
  line-height: 1.45;
  margin-top: 3px;
}

.codex-item.locked .codex-name,
.codex-item.locked .codex-year { color: var(--fg-dimmer); }

.codex-item.locked .codex-fact { color: var(--fg-dimmer); }

.footer {
  text-align: center;
  padding: 18px;
  font-size: 0.8rem;
  color: var(--fg-dimmer);
}

.footer a { color: var(--fg-dim); text-decoration: none; }
.footer a:hover { color: var(--fg); text-decoration: underline; }

@media (max-width: 560px) {
  .topbar { padding: 20px 16px 14px; }
  .layout { padding: 18px 12px 28px; }
  .brand h1 { font-size: 1.35rem; }
}
