:root {
  --bg: #ffffff;
  --fg: #1a1a1b;
  --border: #d3d6da;
  --border-dark: #878a8c;
  --key-bg: #d3d6da;
  --key-fg: #1a1a1b;
  --correct: #6aaa64;
  --present: #c9b458;
  --absent: #787c7e;
  --tile-empty: #ffffff;
  --modal-bg: rgba(0, 0, 0, 0.5);
  --modal-content: #ffffff;
}

[data-theme="dark"] {
  --bg: #121213;
  --fg: #ffffff;
  --border: #3a3a3c;
  --border-dark: #565758;
  --key-bg: #818384;
  --key-fg: #ffffff;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --tile-empty: #121213;
  --modal-bg: rgba(0, 0, 0, 0.7);
  --modal-content: #1e1e1f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  height: 100%;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 1rem;
  padding-top: calc(0.5rem + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

header h1 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.15rem;
  text-align: center;
  flex: 1;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--fg);
  padding: 0.25rem 0.5rem;
}

main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
  gap: 0.4rem;
  overflow: hidden;
}

.board {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 10px;
  width: 100%;
  max-width: 350px;
  flex-shrink: 0;
  margin-bottom: 1.25rem;
}

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

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--border);
  font-size: clamp(1.6rem, 8vw, 2.25rem);
  font-weight: 700;
  text-transform: uppercase;
  background: var(--tile-empty);
  color: var(--fg);
  user-select: none;
  transition: transform 0.1s;
  aspect-ratio: 1 / 1;
  min-width: 0;
}

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

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

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

.row.shake { animation: shake 0.5s; }
.row.bounce .tile { animation: bounce 0.6s; }

@keyframes pop {
  from { transform: scale(0.9); }
  to { transform: scale(1); }
}

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

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

@keyframes bounce {
  0%, 20%   { transform: translateY(0); }
  40%       { transform: translateY(-20px); }
  50%       { transform: translateY(5px); }
  60%       { transform: translateY(-10px); }
  80%       { transform: translateY(2px); }
  100%      { transform: translateY(0); }
}

.message {
  min-height: 1.5rem;
  font-weight: 600;
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  text-align: center;
  flex-shrink: 0;
  margin-bottom: 0.5rem;
}

.message.show {
  background: var(--fg);
  color: var(--bg);
}

.answer-reveal {
  font-weight: 700;
  letter-spacing: 0.2rem;
  text-transform: uppercase;
  padding: 0.4rem 0.8rem;
  flex-shrink: 0;
  font-size: 1.5rem;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  justify-items: center;
  width: 100%;
  max-width: 500px;
  column-gap: 0.5rem;
}
.answer-reveal.hidden { display: none; }
.answer-reveal .word { color: #000; grid-column: 2; }
.answer-reveal .reveal-spacer { display: flex; align-items: center; }
.answer-reveal .reveal-spacer:first-child { justify-self: end; }
.answer-reveal .reveal-spacer:last-child { justify-self: start; }
[data-theme="dark"] .answer-reveal .word { color: #fff; }

.def-btn {
  width: 2.4rem;
  height: 2.4rem;
  min-width: 44px;
  min-height: 44px;
  border-radius: 50%;
  border: 2px solid var(--border-dark);
  background: transparent;
  color: var(--fg);
  font-weight: 700;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  letter-spacing: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.def-btn.hidden { visibility: hidden; }

.def-word {
  text-align: center;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.2rem;
  margin-bottom: 0.75rem;
}
.def-body {
  font-size: 1rem;
  line-height: 1.5;
  text-align: center;
  padding: 0 0.25rem;
}

.keyboard {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0 4px 8px;
  flex-shrink: 0;
}

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

.key {
  flex: 1;
  min-width: 0;
  height: 58px;
  padding: 0 2px;
  border-radius: 4px;
  border: none;
  background: var(--key-bg);
  color: var(--key-fg);
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  text-transform: uppercase;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
}

.key.wide { flex: 1.6; font-size: 0.8rem; }

@media (max-width: 380px) {
  .key { height: 54px; font-size: 0.95rem; }
  .key.wide { flex: 1.8; font-size: 0.75rem; }
  header h1 { font-size: 1.4rem; letter-spacing: 0.1rem; }
  .board { padding: 6px; }
}
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent  { background: var(--absent);  color: #fff; }

.modal {
  position: fixed;
  inset: 0;
  background: var(--modal-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--modal-content);
  color: var(--fg);
  border-radius: 8px;
  padding: 1.5rem;
  max-width: 500px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 0.25rem;
  right: 0.5rem;
  background: none;
  border: none;
  font-size: 1.75rem;
  cursor: pointer;
  color: var(--fg);
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.modal h2 {
  text-align: center;
  margin: 0 0 1rem;
  letter-spacing: 0.1rem;
}

.modal h3 {
  text-transform: uppercase;
  font-size: 0.9rem;
  letter-spacing: 0.05rem;
  margin: 1.25rem 0 0.5rem;
  text-align: center;
}

.stat-tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  text-align: center;
}

.stat-num { font-size: 2rem; font-weight: 700; }
.stat-label { font-size: 0.7rem; text-transform: uppercase; }

.distribution {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 0.5rem;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

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

.dist-bar {
  background: var(--absent);
  color: #fff;
  padding: 2px 8px;
  min-width: 24px;
  text-align: right;
  transition: width 0.3s;
}

.dist-bar.highlight { background: var(--correct); }

.modal-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.countdown {
  font-size: 0.85rem;
  text-align: left;
}

.countdown .cd-label {
  text-transform: uppercase;
  font-size: 0.7rem;
  display: block;
}

.countdown .cd-time {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.share-btn {
  background: var(--correct);
  color: #fff;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  text-transform: uppercase;
}

.archive-launcher {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  text-align: center;
}

.archive-btn {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border-dark);
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
}

.archive-hint {
  font-size: 0.85rem;
  opacity: 0.75;
  text-align: center;
  margin-top: -0.5rem;
  margin-bottom: 1rem;
}

.archive-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 50vh;
  overflow-y: auto;
  padding: 0.25rem;
}

.archive-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  cursor: pointer;
  font-size: 0.95rem;
}

.archive-row:hover:not(.played) { border-color: var(--correct); }

.archive-row.played {
  cursor: pointer;
  opacity: 0.75;
  background: transparent;
}
.archive-row.played:hover { border-color: var(--present); }

.archive-row .date-label { font-weight: 600; }

.archive-row .status-label {
  font-size: 0.85rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--border);
  color: var(--fg);
}

.archive-row.played .status-label.win { background: var(--correct); color: #fff; }
.archive-row.played .status-label.loss { background: var(--absent); color: #fff; }

.archive-row.in-progress { border-color: var(--present); }
.archive-row .status-label.resume { background: var(--present); color: #fff; font-weight: 700; }

.archive-actions {
  text-align: center;
  margin-top: 0.75rem;
}

.archive-end {
  text-align: center;
  font-size: 0.8rem;
  opacity: 0.6;
  padding: 0.75rem 0.5rem;
  font-style: italic;
}

.replay-meta {
  text-align: center;
  margin-bottom: 1rem;
  font-size: 0.95rem;
}

.replay-meta strong { display: block; font-size: 1.1rem; margin-bottom: 0.25rem; }

.replay-board {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-width: 280px;
  width: 100%;
  margin: 0 auto;
}

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

.replay-board .tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--border);
  background: var(--tile-empty);
  color: var(--fg);
  font-weight: 700;
  font-size: 1.25rem;
  text-transform: uppercase;
  aspect-ratio: 1 / 1;
  min-width: 0;
}

.replay-board .tile.correct { background: var(--correct); border-color: var(--correct); color: #fff; }
.replay-board .tile.present { background: var(--present); border-color: var(--present); color: #fff; }
.replay-board .tile.absent  { background: var(--absent);  border-color: var(--absent);  color: #fff; }

.replay-empty {
  text-align: center;
  padding: 1rem;
  font-size: 0.9rem;
  opacity: 0.75;
}

.replay-empty.hidden { display: none; }

.archive-more {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}

.archive-banner {
  padding: 0.5rem 1rem;
  background: var(--present);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.9rem;
  font-weight: 600;
  flex-shrink: 0;
}
#archiveBannerText { flex: 1; min-width: 0; }

.archive-banner.hidden { display: none; }

.update-banner {
  padding: 0.5rem 1rem;
  background: var(--correct);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.9rem;
  font-weight: 600;
  flex-shrink: 0;
  gap: 0.75rem;
}
#updateBannerText { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.update-banner.hidden { display: none; }
.reload-update {
  background: rgba(255,255,255,0.25);
  color: #fff;
  border: none;
  padding: 0.55rem 1rem;
  border-radius: 4px;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.9rem;
  min-height: 44px;
  flex-shrink: 0;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.back-to-today {
  background: rgba(255,255,255,0.25);
  color: #fff;
  border: none;
  padding: 0.55rem 0.9rem;
  border-radius: 4px;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.9rem;
  min-height: 44px;
  flex-shrink: 0;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
