/* Wordle clone — responsive, mobile-first */

:root {
  --bg: #121213;
  --text: #ffffff;
  --border: #3a3a3c;
  --key-bg: #818384;
  --key-text: #ffffff;
  --absent: #3a3a3c;
  --present: #b59f3b;
  --correct: #538d4e;
  --toast-bg: #ffffff;
  --toast-text: #121213;
  --modal-bg: #1a1a1b;
  --header-h: 52px;
  --keyboard-gap: 6px;
  --tile-gap: 5px;
  --font: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

body {
  /* Safe areas for notched phones */
  padding:
    env(safe-area-inset-top, 0)
    env(safe-area-inset-right, 0)
    env(safe-area-inset-bottom, 0)
    env(safe-area-inset-left, 0);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

.app {
  min-height: 100dvh;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: 0 auto;
}

/* —— Header —— */
.header {
  height: var(--header-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 8px 0 4px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 4px;
}

.header-left {
  justify-self: start;
  display: flex;
  align-items: center;
}

.header-right {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 4px;
}

.title {
  margin: 0;
  font-size: clamp(1.35rem, 5vw, 1.75rem);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  text-align: center;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 4px;
  color: var(--text);
  opacity: 0.9;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  background: rgba(255, 255, 255, 0.08);
  outline: none;
}

.new-word-btn {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 8px 12px;
  border-radius: 4px;
  background: var(--correct);
  color: #fff;
  white-space: nowrap;
}

.new-word-btn:hover,
.new-word-btn:focus-visible {
  filter: brightness(1.1);
  outline: none;
}

.new-word-btn:active {
  transform: scale(0.97);
}

.new-word-btn:disabled,
.new-word-btn.is-loading {
  opacity: 0.65;
  cursor: wait;
  filter: none;
}

/* —— Main / board —— */
.main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 0;
  padding: 8px 0;
  width: 100%;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: var(--tile-gap);
  /* Size board to fit available space while staying square-ish tiles */
  width: min(100%, 350px);
  /* Prefer viewport-based height so phone keyboards leave room */
  height: min(
    calc(100dvh - var(--header-h) - 220px - env(safe-area-inset-bottom, 0px)),
    calc((min(100vw, 500px) - 24px) * 6 / 5)
  );
  max-height: 420px;
  min-height: 240px;
  padding: 0 8px;
  aspect-ratio: 5 / 6.15;
}

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

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.25rem, 6vw, 2rem);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text);
  user-select: none;
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
  perspective: 1000px;
}

.tile.filled {
  border-color: #565758;
  animation: pop 0.1s ease-in-out;
}

.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;
}

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

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

/* Wordle win bounce (after reveal) */
.tile.win {
  animation: win-bounce 0.5s ease;
}

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

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-8px); }
  40%, 60% { transform: translateX(8px); }
}

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

@keyframes win-bounce {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-12px); }
  60% { transform: translateY(2px); }
}

/* —— Status + legend —— */
.status-hint {
  margin: 0 12px 8px;
  min-height: 1.25em;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 600;
  color: #d3d6da;
  letter-spacing: 0.01em;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 14px;
  margin: 10px 8px 0;
  padding: 0;
  font-size: 0.7rem;
  color: #a1a1a1;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 2px;
  display: inline-block;
}

.legend-swatch.correct { background: var(--correct); }
.legend-swatch.present { background: var(--present); }
.legend-swatch.absent { background: var(--absent); border: 1px solid #565758; }

/* —— Toast (fixed so it never gets clipped) —— */
.toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top, 12px));
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: var(--toast-bg);
  color: var(--toast-text);
  font-weight: 700;
  font-size: 0.9rem;
  padding: 14px 18px;
  border-radius: 4px;
  opacity: 0;
  pointer-events: none;
  z-index: 200;
  transition: opacity 0.15s ease, transform 0.15s ease;
  max-width: min(360px, calc(100% - 32px));
  text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
}

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

/* —— Keyboard —— */
.keyboard {
  flex-shrink: 0;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding:
    0 6px
    max(8px, env(safe-area-inset-bottom, 8px))
    6px;
  display: flex;
  flex-direction: column;
  gap: var(--keyboard-gap);
  user-select: none;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: var(--keyboard-gap);
  width: 100%;
}

.key {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  max-width: 43px;
  border-radius: 4px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 2px;
  touch-action: manipulation;
}

.key.wide {
  flex: 1.6 1 0;
  max-width: 68px;
  font-size: 0.7rem;
}

.key:active:not(:disabled) {
  opacity: 0.75;
}

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

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

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

/* Glow ENTER when a full 5-letter guess is ready to submit */
.key.key-ready {
  background: var(--correct) !important;
  color: #fff !important;
  box-shadow: 0 0 0 2px #fff3, 0 0 12px #538d4eaa;
  animation: enter-pulse 1s ease-in-out infinite;
}

@keyframes enter-pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.15); }
}

/* Larger phones / tablets */
@media (min-width: 400px) {
  .key {
    height: 58px;
    font-size: 0.85rem;
  }

  .key.wide {
    font-size: 0.75rem;
  }
}

/* Short viewports (landscape phones) */
@media (max-height: 560px) {
  .board {
    min-height: 180px;
    max-height: 260px;
    height: min(260px, calc(100dvh - var(--header-h) - 160px));
  }

  .key {
    height: 42px;
  }

  .tile {
    font-size: 1.1rem;
  }
}

/* Desktop comfort */
@media (min-width: 600px) {
  .key {
    height: 58px;
    max-width: 48px;
  }

  .key.wide {
    max-width: 72px;
  }

  .board {
    width: 350px;
    height: 420px;
    max-height: 420px;
  }
}

/* —— Modal —— */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.modal-panel {
  position: relative;
  background: var(--modal-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 28px 24px 24px;
  max-width: 420px;
  width: 100%;
  max-height: min(90dvh, 90vh);
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  animation: modal-in 0.2s ease;
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  font-size: 1.75rem;
  line-height: 1;
  opacity: 0.7;
  width: 36px;
  height: 36px;
}

.modal-close:hover {
  opacity: 1;
}

.modal-panel h2 {
  margin: 0 0 12px;
  font-size: 1.25rem;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.modal-panel h3 {
  margin: 16px 0 8px;
  font-size: 0.95rem;
}

.modal-panel p,
.modal-panel li {
  font-size: 0.9rem;
  line-height: 1.45;
  color: #d3d6da;
}

.modal-panel ul {
  padding-left: 1.2em;
  margin: 0 0 8px;
}

.example {
  margin: 12px 0;
}

.example-row {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.example-row .tile {
  width: 40px;
  height: 40px;
  font-size: 1.25rem;
  aspect-ratio: auto;
}

.help-note {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 0.85rem !important;
}

.result-panel {
  text-align: center;
}

.result-panel .answer-reveal {
  margin: 8px 0 20px;
}

.primary-btn {
  display: inline-block;
  background: var(--correct);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 12px 28px;
  border-radius: 4px;
}

.primary-btn:hover,
.primary-btn:focus-visible {
  filter: brightness(1.1);
  outline: none;
}

/* —— Settings —— */
.settings-panel .setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}

.setting-text {
  min-width: 0;
}

.setting-label {
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 4px;
}

.setting-desc {
  font-size: 0.8rem;
  color: #a1a1a1;
  line-height: 1.35;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
  cursor: pointer;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--absent);
  border-radius: 26px;
  transition: background 0.15s ease;
}

.switch-slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s ease;
}

.switch input:checked + .switch-slider {
  background: var(--correct);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(18px);
}

.switch input:focus-visible + .switch-slider {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .tile.filled,
  .row.shake,
  .tile.flip,
  .tile.win,
  .modal-panel {
    animation: none;
  }

  .toast {
    transition: none;
  }
}
