:root {
  --bg: #121213;
  --surface: #1a1a1b;
  --border: #3a3a3c;
  --border-light: #565758;
  --text: #ffffff;
  --text-dim: #818384;
  --green: #538d4e;
  --orange: #c9852a;
  --grey: #3a3a3c;
  --accent: #538d4e;
  --danger: #b3413c;
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button {
  font-family: inherit;
  cursor: pointer;
}

.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  flex-direction: column;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.topbar-row1 {
  display: grid;
  grid-template-columns: 30px 1fr 30px;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
}

.topbar-spacer {
  width: 30px;
  height: 1px;
}

.logo-img {
  justify-self: center;
  height: 26px;
  width: auto;
  max-width: 100%;
  display: block;
}

.tabs {
  display: flex;
  gap: 4px;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  justify-content: space-between;
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 7px 6px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  flex: 1;
}

.tab-btn.active {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-light);
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-weight: 700;
  flex-shrink: 0;
  justify-self: end;
}

@media (min-width: 480px) {
  .logo-img { height: 34px; }
  .tab-btn { flex: none; padding: 6px 14px; font-size: 0.85rem; }
  .tabs { justify-content: center; gap: 8px; }
}

/* ---------- Main / views ---------- */
#app {
  max-width: 500px;
  margin: 0 auto;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  min-height: calc(100% - 54px);
}

.view {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
}

.game-meta {
  display: flex;
  justify-content: space-between;
  width: 100%;
  max-width: 350px;
  padding: 6px 4px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

#mode-label { font-weight: 700; color: var(--text); }

/* ---------- Hard mode toggle ---------- */
.hard-mode-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 350px;
  padding: 0 4px;
  margin-bottom: 4px;
  font-size: 0.78rem;
}

.hard-mode-label {
  font-weight: 600;
  color: var(--text);
}

.lock-note {
  color: var(--text-dim);
  font-style: italic;
}

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

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  inset: 0;
  background: var(--grey);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.switch-slider::before {
  content: "";
  position: absolute;
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s ease;
}

.switch input:checked + .switch-slider {
  background: var(--green);
}

.switch input:checked + .switch-slider::before {
  transform: translateX(16px);
}

.switch input:disabled + .switch-slider {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Board ---------- */
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  width: min(330px, 82vw);
  aspect-ratio: 5 / 6;
  margin: 8px auto 4px;
}

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

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.2rem, 6vw, 1.8rem);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  user-select: none;
  transition: transform 0.15s ease;
}

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

.tile.correct { background: var(--green); border-color: var(--green); }
.tile.present { background: var(--orange); border-color: var(--orange); }
.tile.absent { background: var(--grey); border-color: var(--grey); color: var(--text-dim); }

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

.tile.shake {
  animation: shake 0.4s ease;
}

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

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

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

/* ---------- Message / toast ---------- */
.message {
  min-height: 0;
  font-weight: 700;
  font-size: 0.95rem;
  text-align: center;
}

.toast {
  position: fixed;
  top: calc(70px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  color: #111;
  padding: 10px 16px;
  border-radius: 6px;
  font-weight: 700;
  font-size: 0.9rem;
  z-index: 999;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

/* ---------- Keyboard ---------- */
.keyboard {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
  padding-top: 0;
}

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

.key {
  flex: 1;
  max-width: 43px;
  height: 52px;
  background: #818384;
  color: var(--text);
  border: none;
  border-radius: 4px;
  font-weight: 700;
  font-size: 0.8rem;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  justify-content: center;
}

.key.wide {
  max-width: 66px;
  font-size: 0.65rem;
}

.key.correct { background: var(--green); }
.key.present { background: var(--orange); }
.key.absent { background: var(--grey); color: var(--text-dim); }

.key:active { filter: brightness(1.2); }

.kb-extra-row {
  margin-bottom: -2px;
}

.spacer-key {
  visibility: hidden;
}

.key.enter-extra {
  background: var(--accent);
  color: #fff;
  font-size: 1.3rem;
}

/* ---------- Result actions ---------- */
.result-actions {
  display: flex;
  gap: 10px;
  margin: 10px 0;
}

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
}

.btn.secondary {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text);
}

/* ---------- Leaderboard ---------- */
.lb-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  margin-top: 6px;
}

.lb-header h2 { margin: 0; }
.week-range { color: var(--text-dim); font-size: 0.8rem; }

.section-title {
  margin-top: 22px;
  align-self: flex-start;
  padding-left: 4px;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

.lb-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 0.88rem;
}

.lb-table th, .lb-table td {
  padding: 8px 6px;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.lb-table th {
  color: var(--text-dim);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.lb-table td:nth-child(2) {
  text-align: left;
  font-weight: 700;
}

.lb-table tr.me { background: rgba(83, 141, 78, 0.15); }

.empty-state {
  color: var(--text-dim);
  text-align: center;
  padding: 20px 0;
  font-size: 0.85rem;
}

#refresh-lb-btn {
  margin: 20px 0 10px;
}

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

.modal {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 22px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}

.modal h2 { margin-top: 0; }
.modal p { color: var(--text-dim); font-size: 0.9rem; }

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

.help-row {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  margin: 10px 0;
}

.help-row .tile {
  width: 40px;
  height: 40px;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.help-row span { font-size: 0.85rem; color: var(--text); }

@media (min-width: 600px) {
  .board { width: 340px; }
  .key { height: 58px; }
}
