:root {
  --color-bg: #121213;
  --color-text: #ffffff;
  --color-correct: #538d4e;
  --color-present: #b59f3b;
  --color-absent: #3a3a3c;
  --color-border-empty: #3a3a3c;
  --color-border-filled: #565758;
  --color-key-bg: #818384;
  --color-overlay: rgba(0, 0, 0, 0.75);
  
  --font-family: 'Inter', 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
  --header-height: 50px;
}

body[data-theme='light'] {
  --color-bg: #ffffff;
  --color-text: #000000;
  --color-border-empty: #d3d6da;
  --color-key-bg: #d3d6da;
  --color-overlay: rgba(255, 255, 255, 0.85);
}

body[data-hc='true'] {
  --color-correct: #f5793a;
  --color-present: #85c0f9;
}

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

body {
  background-color: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-family);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  touch-action: manipulation;
}

header {
  height: var(--header-height);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid var(--color-border-empty);
  font-weight: bold;
  font-size: 30px;
  letter-spacing: 1px;
}

header > div:first-child, header > div:last-child {
  flex: 1;
  display: flex;
}

header > div:last-child {
  justify-content: flex-end;
}

header > div:nth-child(2) {
  flex: 2;
  text-align: center;
}

.settings-btn {
  background: none;
  border: none;
  color: var(--color-text);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

main {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  align-items: center;
  justify-content: center;
  padding: 10px;
  max-width: 500px;
  margin: 0 auto;
  width: 100%;
}

.board-container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-grow: 1;
  width: 100%;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 5px;
  width: min(350px, calc((100dvh - 280px) * 5 / 6));
  aspect-ratio: 5/6;
}

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

.tile {
  width: 100%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  line-height: 2rem;
  font-weight: bold;
  vertical-align: middle;
  text-transform: uppercase;
  border: 2px solid var(--color-border-empty);
  user-select: none;
  transition: all 0.3s ease;
}

.tile[data-state='tbd'] {
  border-color: var(--color-border-filled);
  animation: popIn 0.1s ease-in-out;
}

.tile[data-state='correct'] {
  background-color: var(--color-correct);
  border-color: var(--color-correct);
  color: white;
}

.tile[data-state='present'] {
  background-color: var(--color-present);
  border-color: var(--color-present);
  color: white;
}

.tile[data-state='absent'] {
  background-color: var(--color-absent);
  border-color: var(--color-absent);
  color: white;
}

@keyframes flip {
  0% { transform: rotateX(0); background-color: var(--color-bg); border-color: var(--color-border-filled); color: var(--color-text); }
  49.99% { transform: rotateX(90deg); background-color: var(--color-bg); border-color: var(--color-border-filled); color: var(--color-text); }
  50% { transform: rotateX(90deg); background-color: var(--final-bg); border-color: var(--final-bg); color: white; }
  100% { transform: rotateX(0); background-color: var(--final-bg); border-color: var(--final-bg); color: white; }
}

.tile.flip[data-state='correct'] {
  --final-bg: var(--color-correct);
  animation: flip 0.5s ease both;
}
.tile.flip[data-state='present'] {
  --final-bg: var(--color-present);
  animation: flip 0.5s ease both;
}
.tile.flip[data-state='absent'] {
  --final-bg: var(--color-absent);
  animation: flip 0.5s ease both;
}

@keyframes popIn {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

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

.tile.bounce {
  animation: bounce 1s ease-in-out !important;
}

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

.row.shake {
  animation: shake 0.6s cubic-bezier(.36,.07,.19,.97) both;
}

/* Keyboard */
.keyboard {
  height: auto;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  user-select: none;
  padding: 0 4px 8px;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  width: 100%;
  margin: 0 auto 6px;
  touch-action: manipulation;
}

.key {
  font-family: inherit;
  font-weight: bold;
  border: 0;
  padding: 0;
  margin: 0 2px;
  height: 58px;
  border-radius: 4px;
  cursor: pointer;
  background-color: var(--color-key-bg);
  color: var(--color-text);
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  text-transform: uppercase;
  transition: transform 0.2s;
}

@media (max-width: 400px), (max-height: 600px) {
  .key {
    height: 45px;
  }
}

.key:active {
  transform: scale(0.95);
}

.key.large {
  flex: 1.5;
  font-size: 12px;
}

.key[data-state='correct'] {
  background-color: var(--color-correct);
}
.key[data-state='present'] {
  background-color: var(--color-present);
}
.key[data-state='absent'] {
  background-color: var(--color-absent);
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--color-overlay);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.modal-content {
  background: var(--color-bg);
  border: 1px solid var(--color-border-filled);
  padding: 20px;
  border-radius: 8px;
  width: 90%;
  max-width: 400px;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  text-align: center;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 15px;
  background: none;
  border: none;
  color: var(--color-text);
  font-size: 24px;
  cursor: pointer;
}

.blurred {
  filter: blur(5px);
  cursor: pointer;
  transition: filter 0.3s;
}

.blurred:hover, .blurred.revealed {
  filter: blur(0);
}

.btn {
  background-color: var(--color-correct);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  margin: 10px 5px;
  transition: background 0.2s;
}

.btn:hover {
  background-color: #43713f;
}

.btn-secondary {
  background-color: var(--color-key-bg);
}

.btn-secondary:hover {
  background-color: #616263;
}

/* Inputs */
.slider-container {
  margin: 20px 0;
}

input[type=range] {
  width: 100%;
  margin: 10px 0;
}

input[type=text] {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  background: var(--color-absent);
  border: 1px solid var(--color-border-filled);
  color: white;
  border-radius: 4px;
}

/* Toast Messages */
.toast-container {
  position: fixed;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2000;
  pointer-events: none;
}

/* Settings Toggles */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: var(--color-key-bg);
  transition: .2s;
  border-radius: 24px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 18px; width: 18px;
  left: 3px; bottom: 3px;
  background-color: white;
  transition: .2s;
  border-radius: 50%;
}
input:checked + .slider {
  background-color: var(--color-correct);
}
input:checked + .slider:before {
  transform: translateX(20px);
}
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid var(--color-border-empty);
}
.setting-row:first-of-type {
  padding-top: 0;
}
.setting-row:last-of-type {
  border-bottom: none;
}
.setting-text {
  flex: 1;
  padding-right: 15px;
}
.setting-title {
  font-size: 18px;
}
.setting-desc {
  font-size: 12px;
  color: #888;
  margin-top: 4px;
}

.toast {
  background-color: var(--color-text);
  color: var(--color-bg);
  padding: 12px 20px;
  border-radius: 4px;
  font-weight: bold;
  margin-bottom: 10px;
  opacity: 1;
  transition: opacity 0.5s ease;
}

.toast.fade-out {
  opacity: 0;
}

/* Stats */
.stat-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 60px;
}
.stat-num {
    font-size: 36px;
    font-weight: bold;
}
.stat-label {
    font-size: 12px;
    text-align: center;
}
.dist-row {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
    font-size: 14px;
}
.dist-label {
    width: 15px;
    font-weight: bold;
}
.dist-bar {
    background: #3a3a3c;
    color: white;
    padding: 2px 5px;
    text-align: right;
    font-weight: bold;
    min-width: 20px;
}
.dist-bar.highlight {
    background: var(--color-correct);
}

.auth-panel {
    display: none;
}
.auth-panel.active {
    display: block;
}
