/*
 * Global styles for the Wordle‑style game.  This file defines the
 * layout, colours and animations used throughout the application.
 * Modern CSS features such as flexbox and grid are used for
 * responsiveness, ensuring that the board and keyboard look great
 * on both desktop and mobile devices.
 */

/* Reset some base styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Define a CSS variable to track the keyboard height so we can
   reserve space for it when fixed to the bottom of the screen. */
:root {
  --keyboard-height: 200px; /* default keyboard height */
  --header-height: 56px;    /* fixed header height for layout math */
  --row-gap: 8px;           /* vertical gap between rows */
  --col-gap: 8px;           /* horizontal gap between tiles */
  --bottom-banner-height: 12px; /* height of the bottom banner */
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: #f5f5f5;
  color: #222;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 20px;
  /* Add bottom padding equal to the keyboard height so the board
     remains visible above the fixed keyboard. */
  padding-bottom: calc(20px + var(--keyboard-height));
}

header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 600px;
  margin: 0 auto 20px;
  height: var(--header-height);
  /* Add breathing room between title and actions */
  column-gap: 14px;
  row-gap: 8px;
  flex-wrap: wrap;
}

.game-title {
  font-size: 2rem;
  font-weight: 700;
}

/* Nudge the stats button away from the title when on one line */
#stats-button {
  margin-left: 8px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px; /* tighter for icon */
  width: 40px;
  height: 40px;
}

#stats-button svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

/* Buttons */
.primary-btn,
.secondary-btn {
  padding: 10px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
}

.primary-btn {
  background-color: #4caf50;
  color: #fff;
}

.secondary-btn {
  background-color: #2196f3;
  color: #fff;
}

.secondary-btn:hover,
.primary-btn:hover {
  opacity: 0.9;
}

/* Board styles */
.board {
  display: grid;
  gap: var(--row-gap);
  width: 100%;
  /* Ensure the full 6-row board + 1/4 tile gap fits above keyboard.
     Let t be tile size. Board height ≈ 6t + 5*row-gap.
     Desired extra gap g = 0.25t. Fit: 6t + 5r + g <= H.
     Solve t <= (H - 5r) / 6.25; width ≈ 5t + 4c. */
  max-width: calc(
    min(
      314px,
      (
        (
          (100dvh - var(--keyboard-height) - var(--header-height) - env(safe-area-inset-bottom))
          - (5 * var(--row-gap))
        ) * (5 / 6)
        + (4 * var(--col-gap))
      )
    )
  );
  margin: 0 auto;
  /* No gap between board and keyboard */
  margin-bottom: 0;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--col-gap);
}

.tile {
  width: 100%;
  /* Keep tiles perfectly square regardless of content */
  aspect-ratio: 1 / 1;
  position: relative;
  border: 2px solid #d3d6da;
  background-color: #fff;
  text-transform: uppercase;
  font-weight: 700;
  /* Scale tile letter size with viewport while clamping */
  font-size: clamp(1rem, 4.8vw, 2rem);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;         /* prevent line-height from inflating height */
  overflow: hidden;       /* avoid content changing box size */
  min-width: 0;           /* prevent grid item from expanding its track */
  min-height: 0;
  color: #222;
  transition: background-color 0.3s ease, color 0.3s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tile.filled {
  border-color: #86888a;
}

/* Colour classes for feedback */
.tile.correct {
  background-color: #6aaa64;
  border-color: #6aaa64;
  color: #fff;
}

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

.tile.absent {
  background-color: #d3d6da; /* light grey to match keyboard absent */
  border-color: #d3d6da;
  color: #222; /* dark text for contrast */
}

/* Flip animation */
.flip {
  animation: flip 0.5s forwards;
}

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

/* Keyboard styles */
.keyboard {
  position: fixed;
  bottom: calc(var(--bottom-banner-height) + 5px);
  left: 0;
  width: 100%;
  background-color: #f5f5f5;
  z-index: 10;
  height: var(--keyboard-height);
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Slim banner along the very bottom of the screen */
.bottom-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: var(--bottom-banner-height);
  background-color: #e9ecef; /* light, neutral tone */
  z-index: 5; /* below the keyboard */
}

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

/* Centered actions row with a bit more spacing */
.keyboard-row.actions {
  justify-content: center;
  gap: 12px;
}

.key {
  flex: 1 0 8%;
  min-width: 40px;
  max-width: 56px; /* slightly wider default keys */
  padding: 12px;
  margin: 2px;
  background-color: #787c7e; /* dark grey when available */
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 600;
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  touch-action: manipulation; /* prevent double-tap zoom on keys */
}

.key.large {
  flex: 1 0 16%;
  min-width: 70px;
}

/* Wider action keys on the separate actions row */
.key.action {
  flex: 0 0 auto; /* do not stretch; center as a pair */
  min-width: 120px; /* wider than regular keys */
}

/* Invisible spacer roughly one key width to separate action keys */
/* spacer no longer used */

.key.correct {
  background-color: #6aaa64;
  color: #fff;
}

.key.present {
  background-color: #c9b458;
  color: #fff;
}

.key.absent {
  background-color: #d3d6da; /* light grey when wrong */
  color: #222;
}

/* Overlay styles for profile and stats */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.stats-modal,
.endgame-modal {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  max-width: 400px;
  width: 90%;
}

/* profile selection styles removed */

.stats-summary {
  margin-bottom: 20px;
  line-height: 1.4;
}

/* Message container for toasts */
.message-container {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
}

.toast {
  background-color: rgba(0, 0, 0, 0.85);
  color: #fff;
  padding: 12px 20px;
  border-radius: 6px;
  margin-top: 8px;
  animation: fadeinout 3s forwards;
  font-size: 0.9rem;
  text-align: center;
}

@keyframes fadeinout {
  0% {
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

/* Hide overlays by default */
.overlay.hidden,
.overlay.hidden,
#stats-overlay.hidden {
  display: none;
}

@media (max-width: 480px) {
  header {
    margin: 0 auto 24px;
  }

  .game-title {
    font-size: 1.6rem;
  }

  #stats-button {
    width: 36px;
    height: 36px;
    padding: 6px;
  }

  #stats-button svg {
    width: 18px;
    height: 18px;
  }
  /* Slightly reduce keyboard height on small screens to keep board visible */
  :root {
    --keyboard-height: 200px; /* extra row for action keys */
    --row-gap: 4px;
    --col-gap: 4px;
  }

  .board {
    /* On narrow screens, cap by available height rather than width using same 1/4 tile gap logic */
    max-width: calc(
      min(
        270px,
        (
          (
            (100dvh - var(--keyboard-height) - var(--header-height) - env(safe-area-inset-bottom))
            - (5 * var(--row-gap))
          ) * (5 / 6)
          + (4 * var(--col-gap))
        )
      )
    );
    /* No gap on mobile either */
    margin-bottom: 0;
  }

  .board .row {
    gap: var(--col-gap);
  }

  .board .row .tile {
    font-size: clamp(0.95rem, 5.2vw, 1.5rem);
  }

  .keyboard {
    gap: 2px;
    padding: 0 6px 6px; /* no top padding so the measured gap is exactly one tile */
  }

  .key {
    padding: 6px;
    font-size: 0.9rem;      /* a bit bigger */
    line-height: 1;         /* prevent vertical expansion from text */
    white-space: nowrap;    /* keep labels on one line */
    overflow: hidden;       /* guard against overflow */
    text-overflow: ellipsis;
    min-width: 30px;
    max-width: 44px;        /* allow slightly wider keys */
    height: 46px;           /* larger touch target */
  }

  .key.large {
    min-width: 44px;
    font-size: 0.85rem;     /* keep readable but contained */
  }

  .key.action {
    min-width: 132px;       /* 3x typical mobile key */
    height: 48px;
  }

  .key-spacer {
    min-width: 44px;        /* roughly one key gap between actions */
  }
}
