/* NYT Wordle–inspired theme (light default + dark/high-contrast) */
:root {
  --bg: #ffffff;
  --text: #1a1a1b;
  --muted: #787c7e;
  --border: #d3d6da;
  --border-filled: #878a8c;
  --key: #d3d6da;
  --key-text: #1a1a1b;
  --correct: #6aaa64;
  --present: #c9b458;
  --absent: #787c7e;
  --toast-bg: #1a1a1b;
  --toast-text: #ffffff;
  --modal-bg: #ffffff;
  --modal-overlay: rgba(255, 255, 255, 0.5);
  --header-border: #d3d6da;
  --icon: #1a1a1b;
  --share: #6aaa64;
  --tile-shadow: none;
  --font: "Clear Sans", "Helvetica Neue", Arial, system-ui, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --header-h: 56px;
  --key-h: 62px;
  --key-gap: 7px;
  --board-w: min(350px, calc(100vw - 28px - var(--safe-left) - var(--safe-right)));
  --board-h: min(420px, calc(100dvh - var(--header-h) - var(--safe-top) - var(--safe-bottom) - 230px));
  --tile-font: clamp(1.2rem, 5.5vw, 2rem);
  color-scheme: light;
}

html.dark {
  --bg: #121213;
  --text: #ffffff;
  --muted: #818384;
  --border: #3a3a3c;
  --border-filled: #565758;
  --key: #818384;
  --key-text: #ffffff;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --toast-bg: #ffffff;
  --toast-text: #121213;
  --modal-bg: #121213;
  --modal-overlay: rgba(0, 0, 0, 0.55);
  --header-border: #3a3a3c;
  --icon: #ffffff;
  --share: #538d4e;
  color-scheme: dark;
}

html.high-contrast {
  --correct: #f5793a;
  --present: #85c0f9;
  --absent: #787c7e;
  --share: #f5793a;
}
html.dark.high-contrast {
  --correct: #f5793a;
  --present: #85c0f9;
  --absent: #3a3a3c;
}

* { box-sizing: border-box; }

html {
  height: 100%;
  height: -webkit-fill-available;
  background: var(--bg);
  overscroll-behavior: none;
}

body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin-left: auto;
  margin-right: auto;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  overflow-x: hidden;
}

/* Header — NYT style */
.top {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: var(--header-h);
  padding: calc(4px + var(--safe-top)) 8px 4px;
  border-bottom: 1px solid var(--header-border);
}
.top-left { display: flex; justify-content: flex-start; gap: 2px; }
.top-right { display: flex; justify-content: flex-end; gap: 2px; }
.brand-wrap { text-align: center; }
.brand {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1.1;
  font-family: "Times New Roman", Times, Georgia, serif;
}
.meta {
  margin: 2px 0 0;
  font-size: 0.7rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--icon);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  padding: 0;
}
.icon-btn:active { opacity: 0.6; }

main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px 6px;
  gap: 10px;
  width: 100%;
}

/* Board — sizes driven by --board-w / --board-h (set by JS from screen size) */
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  width: var(--board-w);
  height: var(--board-h);
  min-height: 160px;
  max-height: min(440px, 58dvh);
  flex: 0 1 auto;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
  min-height: 0;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-font);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text);
  min-width: 0;
  min-height: 0;
  height: 100%;
  line-height: 1;
  perspective: 1000px;
}
.tile.filled {
  border-color: var(--border-filled);
  animation: pop 0.1s ease-in-out;
}
.tile.correct,
.tile.present,
.tile.absent {
  border-color: transparent;
  color: #fff;
}
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }

/* Flip: half-way swap color (JS toggles class mid animation) */
.tile.flip {
  animation: flip 0.5s ease forwards;
}
.tile.win {
  animation: bounce 0.5s ease;
}

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

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}
@keyframes flip {
  0% { transform: rotateX(0); }
  50% { transform: rotateX(-90deg); }
  100% { transform: rotateX(0); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-18px); }
  50% { transform: translateY(3px); }
  60% { transform: translateY(-8px); }
  80% { transform: translateY(0); }
  100% { transform: translateY(0); }
}

/* Toast */
.toast {
  position: fixed;
  top: calc(72px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(90vw, 300px);
  text-align: center;
  background: var(--toast-bg);
  color: var(--toast-text);
  font-weight: 700;
  font-size: 0.88rem;
  padding: 12px 16px;
  border-radius: 4px;
  z-index: 50;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  animation: toast-in 0.12s ease;
}
.toast.hidden { display: none; }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, -6px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* Keyboard */
.keyboard {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: var(--key-gap);
  margin-top: auto;
  padding: 0 4px calc(6px + var(--safe-bottom));
  flex-shrink: 0;
}
.kb-row {
  display: flex;
  justify-content: center;
  gap: 6px;
  width: 100%;
}
.key {
  border: 0;
  border-radius: 5px;
  background: var(--key);
  color: var(--key-text);
  font-weight: 700;
  font-size: clamp(0.78rem, 3vw, 0.92rem);
  height: var(--key-h);
  min-height: 52px;
  min-width: 0;
  flex: 1 1 0;
  max-width: 48px;
  cursor: pointer;
  text-transform: uppercase;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
  transition: background 0.1s;
}
.key.wide {
  max-width: 72px;
  flex: 1.55 1 0;
  font-size: clamp(0.72rem, 2.6vw, 0.8rem);
}
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent { background: var(--absent); color: #fff; }
.key:active { filter: brightness(0.92); }

.foot {
  flex-shrink: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 6px 14px calc(10px + var(--safe-bottom));
  font-size: 0.68rem;
  color: var(--muted);
  border-top: 1px solid var(--header-border);
}
.foot a { color: var(--muted); text-decoration: underline; }

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: max(12px, var(--safe-top)) 12px max(12px, var(--safe-bottom));
  background: var(--modal-overlay);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.modal.hidden { display: none; }
.modal-card {
  position: relative;
  width: min(480px, 100%);
  background: var(--modal-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 28px 22px 22px;
  box-shadow: 0 4px 40px rgba(0, 0, 0, 0.18);
  max-height: min(92dvh, 100%);
  overflow: auto;
  animation: modal-in 0.18s ease;
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
.modal-card h2 {
  margin: 0 0 14px;
  font-size: 1rem;
  text-align: center;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 800;
}
.modal-card .lead { font-size: 0.95rem; margin: 0 0 10px; line-height: 1.45; }
.modal-card p { margin: 0 0 10px; line-height: 1.45; font-size: 0.9rem; }
.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.rules {
  margin: 0 0 14px;
  padding-left: 1.15em;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text);
}
.examples-label { margin: 12px 0 8px !important; font-size: 0.9rem; }
.ex-block { margin-bottom: 14px; }
.ex-block p { margin: 6px 0 0; font-size: 0.88rem; }
.ex-row { display: flex; gap: 5px; }
.ex-row .tile {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  font-size: 1.2rem;
  animation: none;
}
.muted { color: var(--muted); font-size: 0.84rem !important; }
.footer-note { margin-top: 16px !important; border-top: 1px solid var(--border); padding-top: 12px; }
.footer-note a { color: var(--text); }

/* Stats */
.stats-body {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 18px;
  text-align: center;
}
.stat .n {
  font-size: 2rem;
  font-weight: 500;
  line-height: 1;
}
.stat .l {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text);
  margin-top: 4px;
}
.dist-title {
  font-size: 0.8rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
  text-align: center;
}
.dist-row {
  display: grid;
  grid-template-columns: 12px 1fr;
  gap: 6px;
  align-items: center;
  margin-bottom: 4px;
  font-size: 0.8rem;
  font-weight: 700;
}
.bar {
  background: var(--absent);
  color: #fff;
  padding: 2px 8px;
  min-width: 18px;
  text-align: right;
  font-size: 0.78rem;
  font-weight: 700;
  height: 20px;
  line-height: 16px;
  box-sizing: content-box;
}
.bar.hi { background: var(--correct); }

.share-box { margin-top: 8px; }
.next-row {
  display: flex;
  align-items: stretch;
  gap: 12px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
.next-block {
  flex: 1;
  text-align: center;
  border-right: 1px solid var(--border);
  padding-right: 8px;
}
.next-label {
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.countdown {
  font-size: 1.7rem;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  margin-top: 4px;
}
.btn-share {
  flex: 1;
  border: 0;
  border-radius: 4px;
  background: var(--share);
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  touch-action: manipulation;
}
.btn-share:active { filter: brightness(0.95); }
.result-msg {
  text-align: center;
  font-weight: 600;
  margin: 12px 0 0 !important;
  font-size: 0.92rem !important;
}
.btn-practice {
  width: 100%;
  margin-top: 14px;
  border: 0;
  border-radius: 4px;
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  font-size: 1rem;
  min-height: 48px;
  cursor: pointer;
  touch-action: manipulation;
}
html.dark .btn-practice {
  background: #fff;
  color: #121213;
}
.btn-practice:active { filter: brightness(0.92); }
.practice-note {
  text-align: center;
  margin: 10px 0 0 !important;
  font-size: 0.75rem !important;
  line-height: 1.35;
}

/* Layout size classes applied by JS from measured screen */
html.layout-phone {
  --key-h: 64px;
  --key-gap: 8px;
  --tile-font: clamp(1.15rem, 6.2vw, 1.75rem);
}
html.layout-phone .key {
  min-height: 56px;
  max-width: 52px;
  font-size: 0.9rem;
}
html.layout-phone .key.wide {
  max-width: 78px;
  font-size: 0.78rem;
}
html.layout-phone-sm {
  --key-h: 58px;
  --tile-font: clamp(1rem, 5.5vw, 1.5rem);
}
html.layout-phone-sm .key {
  min-height: 50px;
  max-width: 48px;
}
html.layout-phone-sm .key.wide { max-width: 70px; }
html.layout-tablet {
  --key-h: 60px;
  --tile-font: 1.85rem;
}
html.layout-desktop {
  --key-h: 58px;
  --tile-font: 2rem;
}
html.layout-short {
  --header-h: 48px;
}
html.layout-short .meta { font-size: 0.65rem; }
html.layout-short .foot { display: none; }
html.layout-tiny .meta { display: none; }
html.layout-tiny .foot { display: none; }

.meta.practice {
  color: var(--present);
  font-weight: 700;
}

/* Settings */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.setting-name { font-weight: 700; font-size: 0.95rem; }
.setting-desc { font-size: 0.78rem; color: var(--muted); margin-top: 2px; line-height: 1.3; }
.switch {
  position: relative;
  width: 48px;
  height: 28px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: 0.2s;
}
.slider::before {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.switch input:checked + .slider { background: var(--correct); }
.switch input:checked + .slider::before { transform: translateX(20px); }

/* Phones: larger touch targets */
@media (max-width: 520px) {
  :root {
    --key-h: 64px;
    --key-gap: 8px;
  }
  .keyboard {
    padding-left: 6px;
    padding-right: 6px;
    gap: var(--key-gap);
  }
  .kb-row { gap: 7px; }
  .key {
    min-height: 56px;
    max-width: 52px;
    font-size: 0.9rem;
    border-radius: 6px;
  }
  .key.wide {
    max-width: 78px;
    font-size: 0.78rem;
  }
  /* Free a bit of board height so taller keys still fit */
  .board {
    height: min(
      360px,
      calc(100dvh - var(--header-h) - var(--safe-top) - var(--safe-bottom) - 250px)
    );
  }
}

@media (max-height: 700px) {
  :root { --key-h: 54px; --header-h: 50px; }
  .brand { font-size: 1.45rem; }
  .board {
    height: min(300px, calc(100dvh - 210px));
    min-height: 160px;
  }
  .key { min-height: 50px; }
}
@media (max-height: 560px) {
  :root { --key-h: 48px; }
  .meta { display: none; }
  .board { height: min(200px, calc(100dvh - 180px)); min-height: 140px; }
  .foot { display: none; }
  .key { min-height: 46px; max-width: 46px; }
  .key.wide { max-width: 68px; }
}
@media (max-width: 360px) {
  .key { max-width: 48px; font-size: 0.85rem; }
  .key.wide { max-width: 68px; font-size: 0.7rem; }
  .brand { font-size: 1.35rem; }
}
@media (prefers-reduced-motion: reduce) {
  .tile.filled, .tile.flip, .tile.win, .row.shake, .modal-card, .toast { animation: none !important; }
}
