:root {
  --bg: #ffffff;
  --text: #1a1a1b;
  --border: #d3d6da;
  --tile-border-filled: #878a8c;
  --key-bg: #d3d6da;
  --key-text: #1a1a1b;
  --correct: #6aaa64;
  --present: #c9b458;
  --absent: #787c7e;
  --modal-bg: #ffffff;
  --banner-bg: #fff3cd;
  --banner-text: #664d03;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --text: #f8f8f8;
    --border: #3a3a3c;
    --tile-border-filled: #565758;
    --key-bg: #818384;
    --key-text: #f8f8f8;
    --absent: #3a3a3c;
    --modal-bg: #1f1f20;
    --banner-bg: #3a3320;
    --banner-text: #e8d48b;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* Author display values (flex/grid) override the hidden attribute's UA
   default; this keeps the attribute authoritative. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
}

/* ---- header ---- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding: 6px 0;
}

header h1 {
  font-size: 1.6rem;
  letter-spacing: 0.15em;
  font-weight: 800;
}

.menu-wrap { position: relative; width: 44px; }

#menu-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 4px 10px;
}

#menu {
  position: absolute;
  top: 110%;
  left: 0;
  background: var(--modal-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  min-width: 160px;
  z-index: 20;
  overflow: hidden;
}

#menu a, #menu button {
  background: none;
  border: none;
  color: var(--text);
  text-decoration: none;
  font-size: 1rem;
  text-align: left;
  padding: 12px 16px;
  cursor: pointer;
}

#menu a:hover, #menu button:hover { background: var(--border); }

/* ---- banner (repeated word note) ---- */
#banner {
  background: var(--banner-bg);
  color: var(--banner-text);
  border-radius: 6px;
  padding: 8px 12px;
  margin-top: 8px;
  font-size: 0.85rem;
  text-align: center;
}

/* ---- layout ---- */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 8px;
  gap: 12px;
  min-height: 0;
}

#message {
  padding: 24px 12px;
  text-align: center;
  font-size: 1.05rem;
}

#message button { margin-top: 12px; }

/* ---- board ---- */
/* Width-driven sizing: tiles get their width from the 1fr columns and
   their height from aspect-ratio. Never stretch the board to fill the
   viewport — a forced row height would push tile width past max-width
   via aspect-ratio min-content. */
#board {
  display: grid;
  gap: 5px;
  width: 100%;
  max-width: calc(var(--cols, 5) * 68px);
  margin: auto;
}

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

.tile {
  aspect-ratio: 1;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1rem, 7.5vw, 2rem);
  font-weight: 700;
  text-transform: uppercase;
  user-select: none;
  transition: background-color 0.3s, border-color 0.3s, color 0.3s;
}

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

.tile.correct, .tile.present, .tile.absent {
  border-color: transparent;
  color: #fff;
  animation: flip 0.7s ease backwards;
}

.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }

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

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

.row.shake { animation: shake 0.4s; }

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

/* ---- keyboard ---- */
#keyboard {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 4px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.key {
  flex: 1;
  max-width: 44px;
  height: 54px;
  border: none;
  border-radius: 4px;
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 0.95rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.2s;
}

.key.wide { max-width: 68px; flex: 1.5; font-size: 0.75rem; }

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

.kb-spacer { flex: 0.5; }

/* ---- toast ---- */
#toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 6px;
  font-weight: 600;
  z-index: 50;
  white-space: nowrap;
}

/* ---- modals ---- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  padding: 16px;
}

.modal-card {
  background: var(--modal-bg);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 380px;
  position: relative;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

.modal-card h2 { margin-bottom: 8px; }
.modal-card p { margin-bottom: 12px; font-size: 0.95rem; }

.modal-card .close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.6rem;
  cursor: pointer;
  line-height: 1;
}

.modal-card input {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  margin-bottom: 12px;
}

button.primary {
  background: var(--correct);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 24px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

/* ---- reveal + stats ---- */
.reveal-label { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.7; }
.reveal-answer { font-size: 2rem; font-weight: 800; letter-spacing: 0.1em; margin-bottom: 2px !important; }
.reveal-submitter { opacity: 0.8; }

.stat-row {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 16px 0;
}

.stat { display: flex; flex-direction: column; }
.stat-num { font-size: 1.8rem; font-weight: 700; }
.stat-label { font-size: 0.75rem; opacity: 0.75; }

#stats-modal h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
}

#histogram {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 16px;
  text-align: left;
}

.hist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.hist-guess { width: 3.2em; text-align: right; font-weight: 600; flex-shrink: 0; }

.hist-bar-wrap { flex: 1; }

.hist-bar {
  background: var(--absent);
  color: #fff;
  min-width: 1.4em;
  padding: 2px 6px;
  text-align: right;
  font-weight: 700;
  border-radius: 2px;
}

.hist-bar.today { background: var(--correct); }
