:root {
  color-scheme: dark;
  --bg: #121213;
  --surface: #1A1A1D;
  --border: #2C2C30;
  --border-strong: #3A3A3C;
  --text: #F4F4F5;
  --muted: #8E8E93;
  --correct: #538D4E;
  --present: #B59F3B;
  --absent: #3A3A3C;
  --accent: #6E6E77;
  --tile-size: min(16vw, 62px);
  --gap: 6px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  min-height: 100vh;
}

.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  height: 100dvh;
  height: 100vh;
  padding: env(safe-area-inset-top, 0) 12px env(safe-area-inset-bottom, 0);
  position: relative;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 2px 10px;
  flex-shrink: 0;
}

.logo {
  font-family: Inter, sans-serif;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 1.6px;
  color: var(--text);
}

.header-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 10px;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}

.icon-btn:active {
  background: var(--surface);
  color: var(--text);
}

/* Mode switch */
.mode-switch {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 8px;
  flex-shrink: 0;
}

.mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  font-family: Inter, sans-serif;
  font-weight: 600;
  font-size: 13px;
  padding: 9px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.mode-btn.active {
  background: #2A2A2E;
  color: var(--text);
}

/* Status */
.status-line {
  text-align: center;
  margin-bottom: 12px;
  min-height: 18px;
  flex-shrink: 0;
}

.status-text {
  font-family: "JetBrains Mono", monospace;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.3px;
}

/* Panels */
.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.game-panel {
  align-items: center;
  justify-content: flex-start;
  padding-top: 4px;
}

.overlay-panel {
  position: absolute;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  padding: 16px 12px;
  overflow-y: auto;
}

.overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.overlay-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }

.hidden { display: none !important; }

/* Board */
.board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile-size));
  gap: var(--gap);
  margin: 0 auto 12px;
}

.row {
  display: grid;
  grid-template-columns: repeat(4, var(--tile-size));
  gap: var(--gap);
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border: 1.5px solid var(--border-strong);
  border-radius: 4px;
  display: grid;
  place-items: center;
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  font-size: calc(var(--tile-size) * 0.48);
  color: var(--text);
  background: transparent;
  transition: border-color 0.12s, transform 0.08s;
  user-select: none;
}

.tile.filled {
  border-color: var(--accent);
  animation: pop 0.08s ease-out;
}

.tile.active {
  border-color: #8A8A93;
  transform: scale(1.04);
}

.tile.correct {
  background: var(--correct);
  border-color: var(--correct);
  color: #fff;
}

.tile.present {
  background: var(--present);
  border-color: var(--present);
  color: #fff;
}

.tile.absent {
  background: var(--absent);
  border-color: var(--absent);
  color: #fff;
}

.tile.flip {
  animation: flip 0.5s ease forwards;
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes flip {
  0% { transform: rotateX(0); }
  50% { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

.row.shake {
  animation: shake 0.45s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-6px); }
  30% { transform: translateX(6px); }
  45% { transform: translateX(-5px); }
  60% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
  90% { transform: translateX(3px); }
}

/* Message & new game */
.message {
  min-height: 20px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  margin: 4px 0 2px;
}

.text-btn {
  background: none;
  border: none;
  color: var(--text);
  font-family: Inter, sans-serif;
  font-weight: 600;
  font-size: 13px;
  padding: 10px 16px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* Keyboard */
.keyboard {
  flex-shrink: 0;
  padding: 8px 0 16px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key {
  min-width: 48px;
  height: 52px;
  border: none;
  border-radius: 6px;
  background: #56565C;
  color: #fff;
  font-family: "JetBrains Mono", monospace;
  font-weight: 600;
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s;
  user-select: none;
}

.key:active {
  transform: scale(0.94);
}

.key-wide {
  min-width: 72px;
  font-family: Inter, sans-serif;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.4px;
  background: #3A3A3C;
}

.key.correct {
  background: var(--correct);
}

.key.present {
  background: var(--present);
}

.key.absent {
  background: #2A2A2C;
  color: #6E6E77;
}

/* Leaderboard */
.leaderboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 12px;
}

.rank-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 13px;
}

.rank-row .rank {
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  color: var(--muted);
  width: 22px;
  flex-shrink: 0;
}

.rank-row .points {
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  color: var(--text);
  margin-left: auto;
}

.rank-row small {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 32px 12px;
}

/* Profile */
.profile-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 16px 0;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
}

.stat b {
  display: block;
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  font-size: 22px;
  color: var(--text);
  margin-bottom: 4px;
}

.stat span {
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.4px;
  text-transform: uppercase;
}

.badge-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0 20px;
}

.badge {
  font-size: 11px;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 5px 10px;
  border-radius: 20px;
}

.primary-btn {
  width: 100%;
  border: none;
  border-radius: 10px;
  padding: 14px;
  background: var(--text);
  color: var(--bg);
  font-family: Inter, sans-serif;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  transition: opacity 0.15s;
}

.primary-btn:active {
  opacity: 0.85;
}

.primary-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.text-link {
  display: block;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin-top: 14px;
  text-decoration: none;
}

.text-link:hover {
  color: var(--text);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.72);
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-card {
  position: relative;
  width: min(100%, 360px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px 22px;
}

.modal-card .eyebrow {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 1.2px;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 6px;
}

.modal-card h2 {
  margin: 0 0 16px;
  font-size: 20px;
  font-weight: 700;
}

.modal-body {
  font-size: 14px;
  line-height: 1.65;
  color: #C4C4C8;
  margin-bottom: 22px;
}

.modal-body p {
  margin: 0 0 12px;
}

.modal-body .example {
  display: flex;
  gap: 6px;
  margin: 10px 0;
}

.modal-body .ex-tile {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  display: grid;
  place-items: center;
  font-family: "JetBrains Mono", monospace;
  font-weight: 700;
  font-size: 16px;
  color: #fff;
}

.close {
  position: absolute;
  right: 14px;
  top: 12px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 4px;
}

/* Toast */
.toast {
  position: fixed;
  z-index: 60;
  top: calc(env(safe-area-inset-top, 0px) + 56px);
  left: 50%;
  transform: translateX(-50%) translateY(-12px);
  background: #2A2A2E;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
  max-width: 90vw;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Small screens */
@media (max-height: 700px) {
  :root {
    --tile-size: min(14vw, 52px);
  }
  .key {
    height: 46px;
    font-size: 16px;
  }
  .keyboard {
    padding-bottom: 10px;
  }
}

@media (max-height: 620px) {
  :root {
    --tile-size: min(13vw, 46px);
  }
  .header {
    padding-top: 8px;
    padding-bottom: 6px;
  }
  .key {
    height: 42px;
  }
}
