/* ─── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ─── Layout / non-themed variables ─────────────────────────────────────────── */
:root {
  --header-h:  56px;
  --tile-size: 62px;
  --tile-gap:  5px;
  --font:      'Noto Sans', 'Segoe UI', Arial, sans-serif;
}

/* ─── Dark theme variables ───────────────────────────────────────────────────── */
html[data-theme="dark"] {
  --bg-primary:   #121213;
  --bg-secondary: #1a1a1b;
  --bg-tertiary:  #272729;

  --tile-border-empty:  #3a3a3c;
  --tile-border-filled: #565758;
  --tile-bg-empty:      transparent;
  --tile-bg-revealed:   transparent;

  --text-primary:   #ffffff;
  --text-secondary: #818384;

  --key-bg:   #818384;
  --key-text: #ffffff;

  --color-correct:      #538d4e;
  --color-present:      #b59f3b;
  --color-absent:       #3a3a3c;
  --color-correct-text: #ffffff;
  --color-present-text: #ffffff;
  --color-absent-text:  #ffffff;

  --header-border: #3a3a3c;
  --modal-bg:      #1a1a1b;
  --toast-bg:      #ffffff;
  --toast-text:    #000000;

  --input-bg:     #121213;
  --input-border: #565758;
  --input-text:   #ffffff;

  --button-bg:   #538d4e;
  --button-text: #ffffff;
  --link-color:  #538d4e;

  /* Derived / compound values */
  --nav-hover-bg:         rgba(255, 255, 255, 0.07);
  --table-row-border:     rgba(255, 255, 255, 0.05);
  --table-row-hover:      rgba(255, 255, 255, 0.03);
  --today-row-bg:         rgba(83, 141, 78, 0.06);
  --alert-error-bg:       rgba(200, 50, 50, 0.15);
  --alert-error-border:   rgba(200, 50, 50, 0.40);
  --alert-error-text:     #f87171;
  --alert-success-bg:     rgba(83, 141, 78, 0.15);
  --alert-success-border: rgba(83, 141, 78, 0.40);
  --alert-success-text:   #6ee7b7;
}

/* ─── Light theme variables ──────────────────────────────────────────────────── */
html[data-theme="light"] {
  --bg-primary:   #ffffff;
  --bg-secondary: #f6f7f8;
  --bg-tertiary:  #edeff1;

  --tile-border-empty:  #d3d6da;
  --tile-border-filled: #878a8c;
  --tile-bg-empty:      transparent;
  --tile-bg-revealed:   transparent;

  --text-primary:   #1a1a1b;
  --text-secondary: #787c7e;

  --key-bg:   #d3d6da;
  --key-text: #1a1a1b;

  --color-correct:      #6aaa64;
  --color-present:      #c9b458;
  --color-absent:       #787c7e;
  --color-correct-text: #ffffff;
  --color-present-text: #ffffff;
  --color-absent-text:  #ffffff;

  --header-border: #d3d6da;
  --modal-bg:      #ffffff;
  --toast-bg:      #1a1a1b;
  --toast-text:    #ffffff;

  --input-bg:     #ffffff;
  --input-border: #d3d6da;
  --input-text:   #1a1a1b;

  --button-bg:   #6aaa64;
  --button-text: #ffffff;
  --link-color:  #6aaa64;

  /* Derived / compound values */
  --nav-hover-bg:         rgba(0, 0, 0, 0.06);
  --table-row-border:     rgba(0, 0, 0, 0.06);
  --table-row-hover:      rgba(0, 0, 0, 0.03);
  --today-row-bg:         rgba(106, 170, 100, 0.10);
  --alert-error-bg:       rgba(200, 50, 50, 0.08);
  --alert-error-border:   rgba(200, 50, 50, 0.30);
  --alert-error-text:     #c0392b;
  --alert-success-bg:     rgba(106, 170, 100, 0.10);
  --alert-success-border: rgba(106, 170, 100, 0.40);
  --alert-success-text:   #27ae60;
}

/* ─── Base ───────────────────────────────────────────────────────────────────── */
html {
  font-size: 16px;
}

body {
  background:     var(--bg-primary);
  color:          var(--text-primary);
  font-family:    var(--font);
  min-height:     100vh;
  display:        flex;
  flex-direction: column;
  overflow-x:     hidden;
  max-width:      100vw;
  /* Smooth transition only for user-triggered toggles, not page load */
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ─── Header ─────────────────────────────────────────────────────────────────── */
header {
  position:   sticky;
  top:        0;
  z-index:    100;
  background: var(--bg-primary);
  transition: background-color 0.2s ease;
}

.header-top {
  display:         flex;
  align-items:     center;
  justify-content: space-between;
  height:          var(--header-h);
  max-width:       900px;
  margin:          0 auto;
  padding:         0 16px;
}

.header-controls {
  display:     flex;
  align-items: center;
  gap:         4px;
}

.header-border {
  border-bottom: 1px solid var(--header-border);
}

.logo {
  font-size:    24px;
  font-weight:  800;
  letter-spacing: 4px;
  color:        var(--text-primary);
  user-select:  none;
  flex-shrink:  0;
}

/* ─── Navigation ─────────────────────────────────────────────────────────────── */

/* Desktop: nav links shown as a horizontal row below the title */
.main-nav {
  display:     flex;
  align-items: center;
  flex-wrap:   wrap;
  gap:         4px;
  max-width:   900px;
  margin:      0 auto;
  padding:     0 10px 8px;
}

/* Pushes logout (and username on index) to the far right on desktop */
.nav-right-group {
  margin-left: auto;
}

/* Hamburger — hidden on desktop */
.hamburger {
  display:      none;
  background:   none;
  border:       none;
  cursor:       pointer;
  padding:      0;
  width:        34px;
  height:       34px;
  border-radius: 50%;
  align-items:  center;
  justify-content: center;
  font-size:    20px;
  line-height:  1;
  color:        var(--text-primary);
  flex-shrink:  0;
  transition:   background-color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.hamburger:hover {
  background: var(--nav-hover-bg);
}

.hamburger:active {
  filter: brightness(0.85);
}

.nav-link {
  color:           var(--text-secondary);
  text-decoration: none;
  font-size:       13px;
  font-weight:     700;
  padding:         6px 10px;
  border-radius:   4px;
  transition:      color 0.15s, background-color 0.15s;
  white-space:     nowrap;
}

.nav-link:hover {
  color:      var(--text-primary);
  background: var(--nav-hover-bg);
}

.nav-link.active {
  color: var(--text-primary);
}

.nav-admin {
  color: var(--color-present);
}

.nav-user {
  font-size: 13px;
  color:     var(--text-secondary);
  padding:   6px 8px;
}

/* ─── Theme toggle button ────────────────────────────────────────────────────── */
.theme-toggle {
  background:  none;
  border:      none;
  cursor:      pointer;
  padding:     0;
  width:       34px;
  height:      34px;
  border-radius: 50%;
  display:     flex;
  align-items: center;
  justify-content: center;
  font-size:   18px;
  line-height: 1;
  flex-shrink: 0;
  margin-left: 6px;
  transition:  background-color 0.15s;
  /* Remove tap highlight on mobile */
  -webkit-tap-highlight-color: transparent;
}

.theme-toggle:hover {
  background: var(--nav-hover-bg);
}

.theme-toggle:active {
  filter: brightness(0.85);
}

/* ─── Main ───────────────────────────────────────────────────────────────────── */
main {
  flex:            1;
  display:         flex;
  flex-direction:  column;
  align-items:     center;
  padding:         16px 16px 40px;
}

.no-word-msg {
  margin-top: 60px;
  color:      var(--text-secondary);
  font-size:  18px;
  text-align: center;
}

/* ─── Toast ──────────────────────────────────────────────────────────────────── */
.toast {
  position:    fixed;
  top:         108px; /* below 2-row desktop header; overridden to 72px on mobile */
  left:        50%;
  transform:   translateX(-50%) translateY(-12px);
  background:  var(--toast-bg);
  color:       var(--toast-text);
  font-weight: 700;
  font-size:   14px;
  padding:     10px 18px;
  border-radius: 6px;
  pointer-events: none;
  opacity:     0;
  transition:  opacity 0.2s, transform 0.2s;
  z-index:     200;
  white-space: nowrap;
  max-width:   calc(100vw - 32px);
  text-align:  center;
}

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

/* ─── Game Board ─────────────────────────────────────────────────────────────── */
#board-container {
  display:         flex;
  justify-content: center;
  align-items:     center;
  flex-grow:       1;
  width:           100%;
  max-width:       350px;
  margin:          16px auto 8px;
}

#board {
  display:               grid;
  grid-template-rows:    repeat(6, var(--tile-size));
  gap:                   var(--tile-gap);
  width:                 calc(5 * var(--tile-size) + 4 * var(--tile-gap));
}

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

.tile {
  width:           var(--tile-size);
  height:          var(--tile-size);
  border:          2px solid var(--tile-border-empty);
  background:      var(--tile-bg-empty);
  display:         flex;
  align-items:     center;
  justify-content: center;
  font-size:       2rem;
  font-weight:     800;
  text-transform:  uppercase;
  user-select:     none;
  color:           var(--text-primary);
  transition:      border-color 0.05s;
}

/* Letter typed but not yet submitted */
.tile[data-state="tbd"] {
  border-color: var(--tile-border-filled);
  animation:    pop 0.1s ease;
}

/* Revealed states */
.tile[data-state="correct"] {
  background:   var(--color-correct);
  border-color: var(--color-correct);
  color:        var(--color-correct-text);
}

.tile[data-state="present"] {
  background:   var(--color-present);
  border-color: var(--color-present);
  color:        var(--color-present-text);
}

.tile[data-state="absent"] {
  background:   var(--color-absent);
  border-color: var(--color-absent);
  color:        var(--color-absent-text);
}

/* ─── Tile animations ────────────────────────────────────────────────────────── */
@keyframes pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

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

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

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

.tile.flip-up {
  animation: flip-up 0.25s ease-out forwards;
}

@keyframes shake {
  0%, 100%      { transform: translateX(0); }
  10%, 50%, 90% { transform: translateX(-4px); }
  30%, 70%      { transform: translateX(4px); }
}

.board-row.shake {
  animation: shake 0.5s ease;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  40%      { transform: translateY(-22px); }
  60%      { transform: translateY(-10px); }
}

.tile.bounce {
  animation: bounce 0.7s ease forwards;
}

/* ─── Keyboard ───────────────────────────────────────────────────────────────── */
#keyboard {
  width:       100%;
  max-width:   500px;
  margin:      0 auto;
  padding:     0 8px;
  user-select: none;
}

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

.key {
  height:       58px;
  min-width:    43px;
  padding:      0 6px;
  border:       none;
  border-radius: 4px;
  background:   var(--key-bg);
  color:        var(--key-text);
  font-family:  var(--font);
  font-size:    13px;
  font-weight:  700;
  cursor:       pointer;
  display:      flex;
  align-items:  center;
  justify-content: center;
  flex:         1;
  max-width:    43px;
  transition:   background-color 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.key-wide {
  max-width: 65px;
  font-size: 12px;
}

.key:active {
  filter: brightness(0.85);
}

/* Keyboard key states after a guess */
.key[data-state="correct"] {
  background: var(--color-correct);
  color:      var(--color-correct-text);
}

.key[data-state="present"] {
  background: var(--color-present);
  color:      var(--color-present-text);
}

.key[data-state="absent"] {
  background: var(--color-absent);
  color:      var(--color-absent-text);
}

/* ─── Auth pages ─────────────────────────────────────────────────────────────── */
.auth-page {
  display:         flex;
  align-items:     flex-start;
  justify-content: center;
  padding-top:     40px;
  width:           100%;
}

.auth-card {
  background:    var(--bg-secondary);
  border:        1px solid var(--tile-border-empty);
  border-radius: 10px;
  padding:       36px 40px;
  width:         100%;
  max-width:     420px;
}

.auth-card h2 {
  font-size:    22px;
  font-weight:  800;
  margin-bottom: 8px;
}

.auth-sub {
  color:         var(--text-secondary);
  font-size:     14px;
  margin-bottom: 24px;
  line-height:   1.5;
}

/* ─── Forms ──────────────────────────────────────────────────────────────────── */
.form-group {
  margin-bottom: 18px;
}

.form-group label {
  display:        block;
  font-size:      13px;
  font-weight:    700;
  color:          var(--text-secondary);
  margin-bottom:  6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.optional {
  font-weight:    400;
  text-transform: none;
  letter-spacing: 0;
  color:          var(--text-secondary);
}

.form-group input[type="email"],
.form-group input[type="text"],
.form-group input[type="date"] {
  width:         100%;
  padding:       11px 14px;
  background:    var(--input-bg);
  border:        1px solid var(--input-border);
  border-radius: 6px;
  color:         var(--input-text);
  font-family:   var(--font);
  font-size:     15px;
  outline:       none;
  transition:    border-color 0.15s;
}

.form-group input:focus {
  border-color: var(--text-primary);
}

.form-group input::placeholder {
  color: var(--text-secondary);
}

.btn-primary {
  width:          100%;
  padding:        13px;
  background:     var(--button-bg);
  color:          var(--button-text);
  border:         none;
  border-radius:  6px;
  font-family:    var(--font);
  font-size:      15px;
  font-weight:    700;
  cursor:         pointer;
  transition:     filter 0.15s;
  letter-spacing: 0.5px;
}

.btn-primary:hover  { filter: brightness(1.1); }
.btn-primary:active { filter: brightness(0.9); }

/* ─── Alerts ─────────────────────────────────────────────────────────────────── */
.alert {
  padding:       12px 16px;
  border-radius: 6px;
  font-size:     14px;
  margin-bottom: 18px;
  line-height:   1.5;
}

.alert-error {
  background: var(--alert-error-bg);
  border:     1px solid var(--alert-error-border);
  color:      var(--alert-error-text);
}

.alert-success {
  background: var(--alert-success-bg);
  border:     1px solid var(--alert-success-border);
  color:      var(--alert-success-text);
}

/* ─── Leaderboard ────────────────────────────────────────────────────────────── */
.leaderboard-page,
.admin-page {
  width:     100%;
  max-width: 860px;
  margin:    0 auto;
  padding:   24px 16px 60px;
}

.lb-section {
  margin-bottom: 48px;
}

.lb-title {
  font-size:      18px;
  font-weight:    800;
  letter-spacing: 1px;
  text-transform: uppercase;
  border-bottom:  1px solid var(--header-border);
  padding-bottom: 12px;
  margin-bottom:  20px;
  display:        flex;
  align-items:    baseline;
  gap:            12px;
}

.lb-date {
  font-size:      13px;
  font-weight:    400;
  color:          var(--text-secondary);
  text-transform: none;
  letter-spacing: 0;
}

.lb-empty {
  color:     var(--text-secondary);
  font-size: 14px;
  padding:   16px 0;
}

.table-wrap {
  overflow-x:                auto;
  -webkit-overflow-scrolling: touch;
}

.lb-table {
  width:           100%;
  border-collapse: collapse;
  font-size:       14px;
}

.lb-table th {
  text-align:     left;
  padding:        10px 14px;
  color:          var(--text-secondary);
  font-size:      12px;
  font-weight:    700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom:  1px solid var(--header-border);
  white-space:    nowrap;
}

.lb-table td {
  padding:       12px 14px;
  border-bottom: 1px solid var(--table-row-border);
  vertical-align: middle;
}

.lb-table tr:last-child td {
  border-bottom: none;
}

.lb-table tr:hover td {
  background: var(--table-row-hover);
}

.lb-rank {
  color:     var(--text-secondary);
  font-size: 12px;
  width:     32px;
}

.lb-result {
  font-weight: 800;
  font-size:   16px;
}

.result-win  { color: var(--link-color); }
.result-loss { color: var(--text-secondary); }

.lb-pct    { font-weight: 700; }
.lb-streak { color: var(--color-present); font-weight: 700; }

.row-dnf td   { opacity: 0.7; }
.row-today td { background: var(--today-row-bg); }

/* ─── Admin ──────────────────────────────────────────────────────────────────── */
.admin-heading {
  font-size:      26px;
  font-weight:    800;
  letter-spacing: 2px;
  text-transform: uppercase;
  margin-bottom:  32px;
  color:          var(--color-present);
}

.admin-section {
  background:    var(--bg-secondary);
  border:        1px solid var(--tile-border-empty);
  border-radius: 8px;
  padding:       24px 28px;
  margin-bottom: 28px;
}

.admin-section h2 {
  font-size:      15px;
  font-weight:    800;
  text-transform: uppercase;
  letter-spacing: 1px;
  color:          var(--text-secondary);
  margin-bottom:  18px;
  border-bottom:  1px solid var(--header-border);
  padding-bottom: 10px;
}

.admin-form .form-row {
  display:     flex;
  gap:         16px;
  align-items: flex-end;
  flex-wrap:   wrap;
}

.admin-form .form-group {
  margin-bottom: 0;
  flex:          1;
  min-width:     140px;
}

.form-group-action {
  flex: 0 0 auto;
}

.form-group-action .btn-primary {
  width:   auto;
  padding: 11px 24px;
}

.admin-word {
  font-weight:    800;
  letter-spacing: 2px;
  color:          var(--link-color);
}

.badge-today {
  background:    var(--color-correct);
  color:         var(--color-correct-text);
  font-size:     11px;
  font-weight:   700;
  padding:       2px 7px;
  border-radius: 10px;
  margin-left:   6px;
  vertical-align: middle;
}

.badge-admin {
  background:    var(--color-present);
  color:         var(--bg-primary);
  font-size:     11px;
  font-weight:   700;
  padding:       2px 7px;
  border-radius: 10px;
}

/* ─── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  :root {
    --tile-size: 52px;
    --tile-gap:  4px;
  }

  .logo {
    font-size:      18px;
    letter-spacing: 2px;
  }

  /* ── Hamburger ─────────────────────────────────────────────── */
  .hamburger {
    display: flex;
  }

  /* ── Mobile nav dropdown ───────────────────────────────────── */
  .main-nav {
    display:        none;
    flex-direction: column;
    align-items:    stretch;
    gap:            0;
    padding:        0;
    max-width:      100%;
    background:     var(--bg-secondary);
    border-top:     1px solid var(--header-border);
  }

  .main-nav.open {
    display: flex;
  }

  /* Nav links / username in dropdown */
  .main-nav .nav-link,
  .main-nav .nav-user {
    padding:       0 16px;
    min-height:    44px;
    display:       flex;
    align-items:   center;
    font-size:     15px;
    border-radius: 0;
    border-bottom: 1px solid var(--table-row-border);
    white-space:   nowrap;
  }

  .main-nav > :last-child {
    border-bottom: none;
  }

  /* Show nav-user inside the dropdown (was globally hidden on mobile) */
  .main-nav .nav-user {
    display: flex;
    color:   var(--text-secondary);
  }

  /* Remove right-push on mobile (items stack vertically) */
  .nav-right-group {
    margin-left: 0;
  }

  /* ── Toast: single-row header on mobile ────────────────────── */
  .toast {
    top: 72px;
  }

  /* ── Game board / keyboard ─────────────────────────────────── */
  .tile {
    font-size: 1.6rem;
  }

  .key {
    height:    50px;
    min-width: 32px;
    max-width: 36px;
    font-size: 12px;
  }

  .key-wide {
    max-width: 52px;
    font-size: 11px;
  }

  .keyboard-row {
    gap: 4px;
  }

  /* ── Auth / forms ──────────────────────────────────────────── */
  .auth-card {
    padding: 24px 20px;
  }

  .admin-form .form-row {
    flex-direction: column;
  }

  .form-group-action .btn-primary {
    width: 100%;
  }

  /* ── Leaderboard table fixes ───────────────────────────────── */
  .leaderboard-page {
    max-width: 100%;
    padding:   16px 16px 60px;
    box-sizing: border-box;
  }

  /* Fixed layout so columns don't blow out */
  .leaderboard-page .lb-table {
    table-layout: fixed;
    width:        100%;
  }

  /* Player column: 35% wide, truncate with ellipsis */
  .leaderboard-page .lb-table th:nth-child(2) {
    width: 35%;
  }

  .leaderboard-page .lb-table td:nth-child(2) {
    overflow:      hidden;
    text-overflow: ellipsis;
    white-space:   nowrap;
  }

  /* Hide Avg (col 6) and Best (col 8) from All-Time Stats table only */
  .leaderboard-page .lb-stats th:nth-child(6),
  .leaderboard-page .lb-stats td:nth-child(6),
  .leaderboard-page .lb-stats th:nth-child(8),
  .leaderboard-page .lb-stats td:nth-child(8) {
    display: none;
  }
}

@media (max-width: 380px) {
  :root {
    --tile-size: 46px;
    --tile-gap:  3px;
  }

  .key {
    min-width: 28px;
    max-width: 32px;
  }
}
