:root {
  --color-bg: #ffffff;
  --color-text: #1a1a1b;
  --color-border: #d3d6da;
  --color-border-hover: #878a8c;
  --tile-empty: #ffffff;
  --tile-filled-border: #878a8c;
  --key-bg: #d3d6da;
  --key-text: #1a1a1b;
  --color-correct: #6aaa64;
  --color-present: #c9b458;
  --color-absent: #787c7f;
  --color-accent: #6aaa64;
  --modal-bg: #ffffff;
  --overlay-bg: rgba(0, 0, 0, 0.5);
  --toast-bg: #1a1a1b;
  --toast-text: #ffffff;
}

:root[data-theme="dark"] {
  --color-bg: #121213;
  --color-text: #d7dadc;
  --color-border: #3a3a3c;
  --color-border-hover: #565758;
  --tile-empty: #121213;
  --tile-filled-border: #565758;
  --key-bg: #818384;
  --key-text: #ffffff;
  --color-correct: #538d4e;
  --color-present: #b59f3b;
  --color-absent: #3a3a3c;
  --color-accent: #538d4e;
  --modal-bg: #1a1a1b;
  --overlay-bg: rgba(0, 0, 0, 0.7);
  --toast-bg: #ffffff;
  --toast-text: #1a1a1b;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #121213;
    --color-text: #d7dadc;
    --color-border: #3a3a3c;
    --color-border-hover: #565758;
    --tile-empty: #121213;
    --tile-filled-border: #565758;
    --key-bg: #818384;
    --key-text: #ffffff;
    --color-correct: #538d4e;
    --color-present: #b59f3b;
    --color-absent: #3a3a3c;
    --color-accent: #538d4e;
    --modal-bg: #1a1a1b;
    --overlay-bg: rgba(0, 0, 0, 0.7);
    --toast-bg: #ffffff;
    --toast-text: #1a1a1b;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: "Helvetica Neue", Arial, "Segoe UI", sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
  transition: background 0.2s ease, color 0.2s ease;
}

/* ---------- Header ---------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: clamp(6px, 2.5vw, 10px) clamp(8px, 3.5vw, 16px);
  border-bottom: 1px solid var(--color-border);
  gap: clamp(3px, 1.5vw, 8px);
}

.app-title {
  font-size: clamp(1.05rem, 5.2vw, 1.9rem);
  font-weight: 800;
  letter-spacing: 0.03em;
  margin: 0;
  text-align: center;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: clamp(2px, 1.2vw, 6px);
  flex-shrink: 0;
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  width: clamp(26px, 7.5vw, 34px);
  height: clamp(26px, 7.5vw, 34px);
  padding: clamp(3px, 1.2vw, 6px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--color-border);
}

.icon-btn svg {
  width: clamp(15px, 4.5vw, 22px);
  height: clamp(15px, 4.5vw, 22px);
}

.mode-toggle {
  display: flex;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  overflow: hidden;
  flex-shrink: 0;
}

.mode-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: clamp(3px, 1vw, 5px) clamp(5px, 2vw, 10px);
  font-size: clamp(0.56rem, 2.4vw, 0.72rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--color-text);
  white-space: nowrap;
}

.mode-btn.active {
  background: var(--color-accent);
  color: #fff;
}

/* ---------- Score bar ---------- */

.score-bar {
  position: relative;
  display: flex;
  justify-content: center;
  gap: 28px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--color-border);
}

.hard-badge {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--color-present);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 3px 8px;
  border-radius: 999px;
}

.score-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 64px;
}

.score-block.muted { opacity: 0.75; }

.score-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.7;
}

.score-value {
  font-size: 1.6rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  transition: transform 0.15s ease;
}

.score-value.bump {
  transform: scale(1.25);
  color: var(--color-correct);
}

.of6 {
  font-size: 0.9rem;
  font-weight: 500;
  opacity: 0.6;
}

/* ---------- Board ---------- */

main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  position: relative;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  width: min(330px, 90vw);
  aspect-ratio: 5 / 6;
}

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

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--color-border);
  color: var(--color-text);
  user-select: none;
  background: var(--tile-empty);
}

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

.tile.small {
  width: 40px;
  height: 40px;
  font-size: 1.3rem;
  display: inline-flex;
  border-width: 2px;
}

.example-row {
  display: flex;
  gap: 4px;
  margin: 8px 0 4px;
}

@keyframes pop {
  0% { transform: scale(0.9); }
  40% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

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

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

.tile.revealed.correct {
  background: var(--color-correct);
  border-color: var(--color-correct);
  color: #fff;
}

.tile.revealed.present {
  background: var(--color-present);
  border-color: var(--color-present);
  color: #fff;
}

.tile.revealed.absent {
  background: var(--color-absent);
  border-color: var(--color-absent);
  color: #fff;
}

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

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

.board-row.bounce .tile {
  animation: bounce 0.3s ease;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  30% { transform: translateY(-16px); }
  50% { transform: translateY(0); }
}

/* score pop-up */
.score-pop {
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translate(-50%, 0);
  font-size: 0.85rem;
  font-weight: 800;
  pointer-events: none;
  animation: floatUp 0.9s ease forwards;
  z-index: 5;
}

.score-pop.gold { color: #ffb700; text-shadow: 0 0 4px rgba(0,0,0,0.35); }
.score-pop.yellow { color: var(--color-present); text-shadow: 0 0 4px rgba(0,0,0,0.35); }
.score-pop.dim { color: var(--color-absent); }

@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, 6px) scale(0.7); }
  25% { opacity: 1; transform: translate(-50%, -6px) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -34px) scale(1); }
}

/* ---------- Keyboard ---------- */

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 8px 16px;
  width: min(500px, 100%);
  margin: 0 auto;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key {
  flex: 1;
  max-width: 44px;
  height: 58px;
  border: none;
  border-radius: 4px;
  background: var(--key-bg);
  color: var(--key-text);
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}

.key:active { opacity: 0.7; }

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

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

.key svg { width: 20px; height: 20px; }

/* ---------- Toast ---------- */

.toast-container {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--toast-bg);
  color: var(--toast-text);
  padding: 10px 16px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 0.85rem;
  animation: toastIn 0.15s ease, toastOut 0.2s ease 1.3s forwards;
  white-space: nowrap;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toastOut {
  to { opacity: 0; transform: translateY(-8px); }
}

/* ---------- Modals ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  position: relative;
  background: var(--modal-bg);
  color: var(--color-text);
  border-radius: 10px;
  padding: 24px;
  width: min(420px, 100%);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}

.modal h2 {
  margin-top: 0;
  font-size: 1.3rem;
  text-align: center;
}

.modal h3 {
  font-size: 0.95rem;
  margin-bottom: 6px;
}

.modal p {
  font-size: 0.9rem;
  line-height: 1.4;
}

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

.scoring-rules {
  list-style: none;
  padding: 0;
  margin: 10px 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.scoring-rules li {
  font-size: 0.88rem;
  line-height: 1.4;
}

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  color: #fff;
  margin-right: 4px;
}

.pill.green { background: var(--color-correct); }
.pill.yellow { background: var(--color-present); }
.pill.gray { background: var(--color-absent); }

.strategy-tip {
  background: rgba(106, 170, 100, 0.12);
  border: 1px solid var(--color-correct);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.85rem;
}

.hard-mode-note {
  background: rgba(201, 180, 88, 0.14);
  border: 1px solid var(--color-present);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.85rem;
  margin-top: 10px;
}

/* ---------- Settings ---------- */

.setting-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0;
}

.setting-text {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setting-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.setting-desc {
  font-size: 0.78rem;
  opacity: 0.75;
  line-height: 1.35;
}

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

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}

.switch-track {
  position: absolute;
  inset: 0;
  background: var(--color-border);
  border-radius: 999px;
  transition: background 0.15s ease;
}

.switch-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  transition: transform 0.15s ease;
}

.switch input:checked ~ .switch-track {
  background: var(--color-correct);
}

.switch input:checked ~ .switch-track .switch-thumb {
  transform: translateX(18px);
}

hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 16px 0;
}

/* ---------- Stats modal ---------- */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin-bottom: 16px;
}

.stat-num {
  display: block;
  font-size: 1.6rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  opacity: 0.7;
}

.dist-chart {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
}

.dist-bar-wrap {
  flex: 1;
  background: var(--color-border);
  border-radius: 3px;
  overflow: hidden;
}

.dist-bar {
  background: var(--color-absent);
  color: #fff;
  text-align: right;
  padding: 2px 6px;
  font-weight: 700;
  font-size: 0.78rem;
  min-width: 20px;
  white-space: nowrap;
}

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

.next-daily {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--color-border);
  padding-top: 12px;
  margin-top: 8px;
  font-size: 0.85rem;
}

.countdown {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- End modal ---------- */

.final-score {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(106, 170, 100, 0.12);
  border: 1px solid var(--color-correct);
  border-radius: 10px;
  padding: 14px;
  margin: 14px 0;
}

.final-score-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  opacity: 0.75;
}

.final-score-value {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--color-correct);
}

.end-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  border: none;
  border-radius: 6px;
  padding: 12px;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  background: var(--key-bg);
  color: var(--key-text);
}

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

@media (max-width: 380px) {
  .tile { font-size: 1.6rem; }
}
