/* ===== RESET & BASE ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #121213;
  --fg: #ffffff;
  --border: #3a3a3c;

  --green: #538d4e;
  --yellow: #b59f3b;
  --gray: #3a3a3c;
  --white: #ffffff;
  --active-border: #565758;
  --key-bg: #818384;

  /* Base sizes — responsive breakpoints override these */
  --tile-size: 62px;
  --tile-gap: 5px;
  --tile-font: 28px;
  --key-height: 58px;
  --key-font: 14px;
  --key-gap: 6px;
  --key-border-radius: 4px;
  --header-pad: 8px 16px;
  --header-font: 32px;
  --board-pad: 20px 0;
}

html, body {
  height: 100%;
  height: 100dvh;       /* mobile viewport fix */
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: 'Helvetica Neue', Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 520px;
  height: 100%;
  padding: 0 8px;
}

/* ===== HEADER ===== */
header {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: var(--header-pad);
  border-bottom: 1px solid var(--border);
  position: relative;
  flex-shrink: 0;
}

header h1 {
  font-size: var(--header-font);
  font-weight: 700;
  letter-spacing: 0.15em;
}

.header-left, .header-right {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 4px;
}

.header-left { left: 12px; }
.header-right { right: 12px; }

header button {
  background: none;
  border: none;
  color: var(--fg);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

header button:hover { background: var(--border); }

/* ===== BOARD ===== */
#board {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--tile-gap);
  padding: var(--board-pad);
  flex: 1 1 auto;
  min-height: 0;
  justify-content: center;
}

.row {
  display: flex;
  gap: var(--tile-gap);
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-font);
  font-weight: 700;
  text-transform: uppercase;
  color: var(--white);
  transform-origin: center;
  will-change: transform, background-color, border-color;
}

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

.tile.green { background: var(--green); border-color: var(--green); }
.tile.yellow { background: var(--yellow); border-color: var(--yellow); }
.tile.gray  { background: var(--gray); border-color: var(--gray); }

@keyframes pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.15); }
  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.3s ease-in both; }
.tile.flip-up   { animation: flip-up 0.3s ease-out both; }

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

@keyframes bounce {
  0%   { transform: translateY(0); }
  20%  { transform: translateY(-20px); }
  35%  { transform: translateY(0); }
  50%  { transform: translateY(-10px); }
  65%  { transform: translateY(0); }
  100% { transform: translateY(0); }
}

.row.shake       { animation: shake 0.4s ease-in-out; }
.tile.win-bounce { animation: bounce 0.65s ease-out; }

/* ===== KEYBOARD ===== */
#keyboard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--key-gap);
  padding: 8px 0 0;
  width: 100%;
  max-width: 500px;
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom, 12px);
}

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

.key {
  height: var(--key-height);
  padding: 0 4px;
  background: var(--key-bg);
  color: var(--white);
  border: none;
  border-radius: var(--key-border-radius);
  font-size: var(--key-font);
  font-weight: 700;
  cursor: pointer;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  max-width: 44px;
  transition: background 0.15s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.key:active { opacity: 0.7; }

.key.wide {
  flex: 1.3;
  max-width: 68px;
  font-size: 11px;
  letter-spacing: 0.04em;
}

.key.green  { background: var(--green); }
.key.yellow { background: var(--yellow); }
.key.gray   { background: var(--gray); opacity: 0.5; }

/* ===== TOASTS ===== */
#toaster {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--fg);
  color: var(--bg);
  padding: 14px 24px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  animation: toast-in 0.3s, toast-out 0.3s 1.7s forwards;
  pointer-events: auto;
}

@keyframes toast-in  { from { opacity:0; transform:translateY(-10px); } to { opacity:1; transform:translateY(0); } }
@keyframes toast-out { from { opacity:1; } to { opacity:0; } }

/* ===== MODALS ===== */
.modal {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.7);
  z-index: 2000;
  justify-content: center;
  align-items: center;
}
.modal.open { display: flex; }

.modal-content {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  max-width: 400px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  color: var(--fg);
}

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

.modal-close {
  background: none; border: none;
  color: var(--fg); font-size: 28px;
  cursor: pointer; line-height: 1;
}

.stats-row {
  display: flex;
  justify-content: space-around;
  margin-bottom: 24px;
}
.stat-box {
  display: flex; flex-direction: column;
  align-items: center; font-size: 14px;
}
.stat-number { font-size: 36px; font-weight: 700; }

#guess-dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.dist-bar {
  background: var(--green); color: var(--white);
  height: 22px; min-width: 22px;
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px;
}

.help-content ul { margin: 12px 0 12px 20px; font-size: 14px; line-height: 1.6; }
.example-box { margin: 16px 0; }
.example-row { display: flex; gap: 4px; margin-bottom: 6px; }
.example-row .tile {
  width: 42px; height: 42px; font-size: 20px;
  animation: none !important;
}
.example-box p { font-size: 14px; line-height: 1.5; }

.result-content { text-align: center; }
.result-content h2 { font-size: 28px; margin-bottom: 10px; }
.result-content p  { font-size: 16px; margin-bottom: 20px; line-height: 1.5; }
.result-actions   { display: flex; flex-direction: column; gap: 10px; }
.result-actions button {
  background: var(--green); color: var(--white);
  border: none; padding: 14px; border-radius: 8px;
  font-size: 16px; font-weight: 700; cursor: pointer;
}
.result-actions button.secondary { background: var(--key-bg); }

/* ============================================================
   RESPONSIVE — scale everything to fit the phone screen
   ============================================================ */

/* Narrow phones (≤ 390 px) */
@media (max-width: 390px) {
  :root {
    --tile-size: 54px;
    --tile-gap: 4px;
    --tile-font: 24px;
    --key-height: 50px;
    --key-font: 13px;
    --key-gap: 4px;
    --header-font: 26px;
  }
  .key { max-width: 36px; }
  .key.wide { max-width: 60px; }
}

/* Small phones / landscape (≤ 360 px or short height) */
@media (max-width: 360px), (max-height: 640px) {
  :root {
    --tile-size: 46px;
    --tile-gap: 3px;
    --tile-font: 20px;
    --key-height: 44px;
    --key-font: 12px;
    --key-gap: 3px;
    --header-font: 22px;
    --board-pad: 8px 0;
  }
  .key { max-width: 32px; border-radius: 3px; }
  .key.wide { max-width: 52px; }
  header { padding: 4px 10px; }
  header button { font-size: 17px; padding: 3px 5px; }
}

/* Very short landscape phones */
@media (max-height: 500px) and (orientation: landscape) {
  :root {
    --tile-size: 40px;
    --tile-gap: 2px;
    --tile-font: 18px;
    --key-height: 38px;
    --key-font: 11px;
    --key-gap: 2px;
    --header-font: 20px;
    --board-pad: 4px 0;
  }
  .key { max-width: 30px; border-radius: 2px; }
  .key.wide { max-width: 48px; }
  header { padding: 2px 10px; border-bottom-width: 1px; }
  header button { font-size: 16px; }
}

/* iPhone notch / dynamic island safe area */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #keyboard {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}
