* {
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #121213;
  color: #f2f2f2;
  margin: 0;
  display: flex;
  justify-content: center;
  /* Respect iOS safe-area insets (Dynamic Island / status bar / home
     indicator) in the packaged app, while keeping the original padding as a
     floor on the web where the insets are 0. */
  padding: max(32px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(32px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.app {
  width: 100%;
  max-width: 640px;
}

h1 {
  text-align: center;
  letter-spacing: 0.05em;
  border-bottom: 1px solid #3a3a3c;
  padding-bottom: 16px;
}

.subtitle {
  color: #b0b0b0;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-start;
}

.screen.hidden {
  display: none;
}

/* Pre-game screens (sign in, pick a mode, session-taken-over notice) read
   better centered, like the app's modal overlays -- unlike the pick/game
   screens, which keep left-aligned form/board layouts. */
.screen-landing {
  align-items: center;
  text-align: center;
}

.screen-landing .overlay-actions {
  justify-content: center;
}

.hidden {
  display: none !important;
}

.divider {
  width: 100%;
  border: none;
  border-top: 1px solid #3a3a3c;
  margin: 10px 0;
}

.link-button {
  background: transparent;
  color: #8ab4f8;
  padding: 0;
  margin-top: 0;
  font-weight: 600;
}

.link-button:hover {
  filter: none;
  text-decoration: underline;
}

.mode-badge {
  font-size: 0.7em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #b0b0b0;
  border: 1px solid #565758;
  border-radius: 3px;
  padding: 1px 6px;
  vertical-align: middle;
}

label {
  font-size: 0.85em;
  color: #b0b0b0;
  margin-top: 8px;
}

input[type='text'],
input[type='email'],
input[type='password'] {
  width: 100%;
  padding: 10px 12px;
  font-size: 1.1em;
  background: #1e1e20;
  border: 1px solid #565758;
  border-radius: 4px;
  color: #f2f2f2;
}

/* The 5-letter game word inputs (secret word, guesses) get the Wordle-tile
   treatment; plain text fields like username/password don't. */
.word-input {
  text-transform: uppercase;
  letter-spacing: 0.15em;
}

select {
  padding: 8px 10px;
  font-size: 1em;
  background: #1e1e20;
  border: 1px solid #565758;
  border-radius: 4px;
  color: #f2f2f2;
}

button {
  padding: 10px 18px;
  font-size: 1em;
  font-weight: 600;
  background: #538d4e;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 4px;
}

button:hover {
  filter: brightness(1.1);
}

button:disabled {
  background: #3a3a3c;
  cursor: not-allowed;
}

button.secondary {
  background: #3a3a3c;
}

.status {
  min-height: 1.2em;
}

.status.error {
  color: #ff6961;
}

.game-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 1.05em;
  margin-bottom: 8px;
}

.boards {
  display: flex;
  /* Fluid so the two boards stay side-by-side (core to the "watch your
     opponent" duel UX) even on a narrow phone, without overflowing. Capped
     at 24px, so desktop is unchanged. */
  gap: clamp(10px, 3vw, 24px);
  width: 100%;
}

.board-column {
  flex: 1;
}

.board-column h2 {
  font-size: 1em;
  margin-bottom: 2px;
}

.board-caption {
  font-size: 0.8em;
  color: #b0b0b0;
  margin-top: 0;
}

.board {
  display: flex;
  flex-direction: column;
  gap: clamp(3px, 1vw, 5px);
}

.tile-row {
  display: flex;
  justify-content: center;
  gap: clamp(3px, 1vw, 5px);
  perspective: 400px;
}

.tile {
  /* Fluid size: 10 tiles (two 5-wide boards) span the viewport on a phone,
     capped at 42px so desktop and tablet are unchanged. Scales down to ~23px
     on a 320px screen, where two boards still fit comfortably. */
  width: clamp(23px, 7.2vw, 42px);
  height: clamp(23px, 7.2vw, 42px);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(0.85rem, 3.6vw, 1.1rem);
  border: 2px solid #3a3a3c;
  border-radius: 3px;
  text-transform: uppercase;
}

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

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

.tile.correct {
  background: #538d4e;
  border-color: #538d4e;
}

.tile.present {
  background: #b59f3b;
  border-color: #b59f3b;
}

.tile.absent {
  background: #3a3a3c;
  border-color: #3a3a3c;
}

#guess-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-top: 16px;
}

.overlay-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 10;
}

.overlay-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay-panel {
  position: relative;
  background: #1e1e20;
  border: 1px solid #3a3a3c;
  border-radius: 8px;
  padding: 28px 32px;
  max-width: 420px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  transform: translateY(16px) scale(0.97);
  transition: transform 0.25s ease;
}

.overlay-backdrop.visible .overlay-panel {
  transform: translateY(0) scale(1);
}

.overlay-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: #b0b0b0;
  font-size: 1.4em;
  line-height: 1;
  border-radius: 4px;
}

.overlay-close:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #f2f2f2;
  filter: none;
}

.overlay-actions {
  display: flex;
  gap: 10px;
}

#rematch-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#game-post-actions {
  display: none;
  flex-direction: column;
  align-items: center;
  margin-top: 20px;
}

#game-post-actions.visible {
  display: flex;
}

.elo-detail {
  font-size: 1.1em;
  font-weight: 700;
  margin: 0;
}

.elo-detail.positive {
  color: #6aaa64;
}

.elo-detail.negative {
  color: #ff6961;
}

.elo-detail.neutral {
  color: #b0b0b0;
}

#elo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.elo-graph {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.elo-graph-caption {
  font-size: 0.75em;
  color: #b0b0b0;
  margin: 2px 0 0;
}

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  margin-top: 20px;
  user-select: none;
}

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

.key {
  flex: 1;
  max-width: 42px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #818384;
  color: #f2f2f2;
  border: none;
  border-radius: 4px;
  font-size: 0.85em;
  font-weight: 600;
  text-transform: uppercase;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

.key.wide {
  flex: 1.5;
  max-width: 64px;
  font-size: 0.7em;
}

.key.correct {
  background: #538d4e;
}

.key.present {
  background: #b59f3b;
}

.key.absent {
  background: #3a3a3c;
  color: #8a8a8a;
}

#over-title {
  font-size: 1.8em;
}

.overlay-panel.wide {
  max-width: 480px;
}

.history-list,
.leaderboard-list {
  width: 100%;
  max-height: 340px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.history-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #1e1e20;
  border-radius: 4px;
  font-size: 0.9em;
}

.history-opponent {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-elo {
  font-weight: 700;
  min-width: 44px;
  text-align: right;
}

.history-elo.positive {
  color: #6aaa64;
}

.history-elo.negative {
  color: #ff6961;
}

.history-elo.neutral {
  color: #b0b0b0;
}

.result-badge {
  font-size: 0.75em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-radius: 3px;
  padding: 3px 7px;
  color: #121213;
  flex-shrink: 0;
}

.result-badge.win {
  background: #6aaa64;
}

.result-badge.lose {
  background: #ff6961;
}

.result-badge.neutral {
  background: #b0b0b0;
}

.history-empty {
  color: #b0b0b0;
  padding: 8px 0;
}

.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 4px;
}

.leaderboard-row.you {
  background: #2a3f28;
  font-weight: 700;
}

.lb-rank-badge {
  width: 132px;
  flex-shrink: 0;
  justify-content: center;
}

.lb-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-rating {
  font-weight: 700;
}

.identity-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* Rank badges: icon + label, used on the join screen and in the
   leaderboard. Icon SVG markup lives in RANK_ICON_SVG (client.js); shared
   gradients/filters are defined once in index.html. */
.rank-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.85em;
  font-weight: 700;
  padding: 3px 8px 3px 5px;
  border-radius: 4px;
  background: #1e1e20;
  border: 1px solid #3a3a3c;
  white-space: nowrap;
}

.rank-icon {
  width: 27px;
  height: 27px;
  flex-shrink: 0;
  overflow: visible;
}

.rank-unranked .rank-label {
  color: #808080;
}

.rank-bronze {
  border-color: #8a5a34;
}
.rank-bronze .rank-label {
  color: #cd7f32;
}

.rank-silver {
  border-color: #9a9a9a;
}
.rank-silver .rank-label {
  color: #c0c0c0;
}

.rank-gold {
  border-color: #b8972f;
}
.rank-gold .rank-label {
  color: #ffd700;
}

.rank-platinum {
  border-color: #4fb8b3;
}
.rank-platinum .rank-label {
  color: #7fe5df;
}

.rank-diamond {
  border-color: #3ba3e0;
}
.rank-diamond .rank-label {
  color: #66c9ff;
}

.rank-transcendent {
  border-color: #9a5fd6;
  background: linear-gradient(135deg, #1e1e20, #2a1e3a);
}
.rank-transcendent .rank-label {
  color: #c77dff;
}

.rank-top20 {
  border-color: #ffd700;
  background: linear-gradient(135deg, #2a2410, #1e1e20);
}
.rank-top20 .rank-label {
  color: #ffd700;
}

.visually-hidden-defs {
  position: absolute;
}

/* Phone-sized screens: the board tiles already scale fluidly (clamp above);
   this tightens the surrounding chrome so more fits without scrolling and
   the overlays/keyboard don't feel cramped. */
@media (max-width: 480px) {
  body {
    padding: max(12px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(12px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  h1 {
    font-size: 1.5em;
    padding-bottom: 12px;
  }

  .keyboard {
    margin-top: 12px;
    gap: 6px;
  }

  .keyboard-row {
    gap: 4px;
  }

  #guess-form {
    margin-top: 12px;
  }

  .overlay-backdrop {
    padding: 8px;
  }

  .overlay-panel {
    padding: 22px 18px;
  }

  #over-title {
    font-size: 1.5em;
  }

  /* The fixed-width rank badge eats too much of a narrow leaderboard row;
     let it size to its content so the name/rating keep room. */
  .lb-rank-badge {
    width: auto;
    min-width: 96px;
  }
}

/* --- Play with a Friend (private invite-code matches) --- */
.friend-match {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid #3a3a3c;
}

.friend-match-title {
  font-weight: 600;
  margin-bottom: 2px;
}

.friend-match-note {
  font-size: 0.8em;
  color: #b0b0b0;
  margin-bottom: 10px;
}

.invite-code-label {
  font-size: 0.85em;
  color: #d0d0d0;
  margin-bottom: 6px;
}

/* The rows hug their content (the screen aligns items to the start), so the
   fixed-width fields + equal-width buttons below sit together compactly rather
   than stretching across the screen -- a 6-char code doesn't need the width. */
.invite-code-row,
.join-invite-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.invite-code-row button,
.join-invite-row button {
  flex-shrink: 0;
  margin-top: 0;
  min-width: 84px; /* keeps Copy and Join the same width so the fields align */
}

/* Sized to match the "enter their code" input below (same padding / font /
   letter-spacing) so the two fields line up; kept green + bold so it still
   reads as your own shareable code. */
.invite-code {
  flex: none;
  width: 150px; /* sized for a 6-char code; matches the join input below */
  padding: 10px 12px;
  font-size: 1.1em;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: #6aaa64;
  font-family: 'Courier New', monospace;
  text-align: center;
  background: #1e1e20;
  border: 1px solid #565758;
  border-radius: 4px;
}

.invite-divider {
  font-size: 0.8em;
  color: #b0b0b0;
  text-align: center;
  margin: 14px 0 8px;
}

.join-invite-row {
  margin-top: 8px;
}

.join-invite-row input {
  flex: none;
  width: 150px; /* same fixed width as the invite-code box above */
  text-transform: uppercase;
  letter-spacing: 0.15em;
}
