/* ============================================================
   KNIGHT WORDS — Game Styles
   ============================================================
   Dark theme, Wordle-inspired tiles, chess-grid letter board.
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Core palette */
  --bg:            #121213;
  --bg-secondary:  #1a1a1b;
  --border:        #3a3a3c;
  --text:          #d7dadc;
  --text-dim:      #818384;
  --text-bright:   #ffffff;
  --font:          'Segoe UI', system-ui, -apple-system, sans-serif;

  /* Wordle colors */
  --correct:       #6aaa64;
  --correct-dark:  #538d4e;
  --present:       #c9b458;
  --present-dark:  #b59f3b;
  --absent:        #3a3a3c;
  --absent-dark:   #2c2c2e;

  /* Game-specific */
  --selected:      #2563eb;
  --selected-light:#3b82f6;
  --valid-dot:     rgba(106, 170, 100, 0.5);
  --pending:       #565758;

  /* Sizing — viewport-relative so the full game fits on one screen */
  --tile-size:     min(58px, calc((100dvh - 250px) / 12));
  --cell-size:     min(54px, calc((100dvh - 250px) / 12), calc((100vw - 80px) / 6));
  --gap:           clamp(2px, 0.4dvh, 4px);
}

html { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overflow: hidden;
}

#app {
  width: 100%;
  max-width: 540px;
  padding: 0 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ═══════════════════════════════════════════════════════════
   HEADER
   ═══════════════════════════════════════════════════════════ */
#header {
  width: 100%;
  border-bottom: 1px solid var(--border);
  padding: clamp(4px, 1dvh, 12px) 0;
  margin-bottom: 2px;
  flex-shrink: 0;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#header h1 {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--text-bright);
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.icon-btn:hover { background: var(--bg-secondary); }

/* ═══════════════════════════════════════════════════════════
   PUZZLE INFO
   ═══════════════════════════════════════════════════════════ */
#puzzle-info {
  padding: 6px 0 2px;
  text-align: center;
}

#puzzle-number {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.08em;
}

/* ═══════════════════════════════════════════════════════════
   GUESS BOARD (6 rows × 4 tiles)
   ═══════════════════════════════════════════════════════════ */
#guess-board {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  margin: clamp(2px, 0.5dvh, 8px) 0 clamp(2px, 0.8dvh, 12px);
  flex-shrink: 0;
}

.guess-row {
  display: flex;
  gap: var(--gap);
  justify-content: center;
}

.guess-tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--text-bright);
  text-transform: uppercase;
  transition: border-color 0.15s;
  perspective: 600px;
}

.guess-tile.filled {
  border-color: var(--pending);
}

.guess-tile.pending {
  border-color: var(--text-dim);
}

/* Feedback colors */
.guess-tile.correct {
  background: var(--correct);
  border-color: var(--correct);
  color: var(--text-bright);
}
.guess-tile.present {
  background: var(--present);
  border-color: var(--present);
  color: var(--text-bright);
}
.guess-tile.absent {
  background: var(--absent);
  border-color: var(--absent);
  color: var(--text-bright);
}

/* ═══════════════════════════════════════════════════════════
   TILE ANIMATIONS
   ═══════════════════════════════════════════════════════════ */

/* Pop when letter typed */
.guess-tile.pop {
  animation: pop 0.1s ease;
}
@keyframes pop {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* Flip to reveal feedback */
.guess-tile.flipping {
  animation: flip 0.5s ease;
}
@keyframes flip {
  0%   { transform: rotateX(0deg); }
  45%  { transform: rotateX(-90deg); }
  55%  { transform: rotateX(-90deg); }
  100% { transform: rotateX(0deg); }
}

/* Shake for invalid */
.guess-row.shake {
  animation: shake 0.5s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 50%, 90% { transform: translateX(-4px); }
  30%, 70% { transform: translateX(4px); }
}

/* Bounce on win */
.guess-tile.bounce {
  animation: bounce 0.6s ease;
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  30%  { transform: translateY(-20px); }
  60%  { transform: translateY(-8px); }
}

/* ═══════════════════════════════════════════════════════════
   LETTER GRID (6×6)
   ═══════════════════════════════════════════════════════════ */
#grid-wrapper {
  margin: clamp(2px, 0.4dvh, 4px) 0 clamp(2px, 0.8dvh, 12px);
  flex-shrink: 0;
}

#letter-grid {
  display: grid;
  grid-template-columns: repeat(6, var(--cell-size));
  grid-template-rows: repeat(6, var(--cell-size));
  gap: 3px;
}

.grid-cell {
  position: relative;
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-secondary);
  border: 2px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.grid-cell.dark {
  background: #161617;
}

.grid-cell:hover {
  border-color: var(--text-dim);
  background: #252527;
}

.grid-cell.startable:hover {
  border-color: var(--selected-light);
  background: rgba(37, 99, 235, 0.15);
}

/* Selected cell (in current path) — must override key-status */
.grid-cell.selected,
.grid-cell.selected.key-correct,
.grid-cell.selected.key-present,
.grid-cell.selected.key-absent {
  background: var(--selected);
  border-color: var(--selected-light);
  box-shadow: 0 0 12px rgba(37, 99, 235, 0.4);
  opacity: 1;
  z-index: 2;
}

.grid-cell.selected .cell-letter {
  color: var(--text-bright);
}

/* Valid next move indicator */
.grid-cell.valid-next {
  border-color: var(--correct);
  background: rgba(106, 170, 100, 0.08);
}

.grid-cell.valid-next::after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--valid-dot);
  pointer-events: none;
}

.grid-cell.valid-next:hover {
  background: rgba(106, 170, 100, 0.2);
  border-color: var(--correct);
}

/* Valid-next on cells with green/yellow status — keep the color, add a pulsing ring */
.grid-cell.valid-next.key-correct {
  background: var(--correct-dark);
  border: 2px solid #fff;
  animation: jumpPulse 1.2s ease-in-out infinite;
}

.grid-cell.valid-next.key-present {
  background: var(--present-dark);
  border: 2px solid #fff;
  animation: jumpPulse 1.2s ease-in-out infinite;
}

.grid-cell.valid-next.key-correct::after,
.grid-cell.valid-next.key-present::after {
  /* Replace the green dot with a white knight dot */
  background: #fff;
  width: 8px;
  height: 8px;
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.9);
}

.grid-cell.valid-next.key-correct:hover,
.grid-cell.valid-next.key-present:hover {
  filter: brightness(1.25);
  animation: none;
  border-color: #fff;
}

/* Valid-next on absent cells — still jumpable, override the dim */
.grid-cell.valid-next.key-absent {
  opacity: 0.7;
  border-color: var(--correct);
}

@keyframes jumpPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.5); }
  50%      { box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.15); }
}

/* Letter status from previous guesses */
.grid-cell.key-correct {
  background: var(--correct-dark);
  border-color: var(--correct-dark);
}
.grid-cell.key-correct .cell-letter { color: var(--text-bright); }

.grid-cell.key-present {
  background: var(--present-dark);
  border-color: var(--present-dark);
}
.grid-cell.key-present .cell-letter { color: var(--text-bright); }

.grid-cell.key-absent {
  opacity: 0.4;
}

/* Cell letter */
.cell-letter {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--text);
  pointer-events: none;
  z-index: 1;
  text-transform: uppercase;
}

/* Path position number */
.path-number {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.6rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.7);
  pointer-events: none;
  z-index: 2;
}

/* Invalid cell shake */
.grid-cell.invalid-shake {
  animation: cell-shake 0.35s ease;
}
@keyframes cell-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

/* ═══════════════════════════════════════════════════════════
   CONTROLS
   ═══════════════════════════════════════════════════════════ */
#controls {
  display: flex;
  gap: 8px;
  margin: clamp(2px, 0.4dvh, 4px) 0;
  width: 100%;
  max-width: 340px;
  justify-content: center;
  flex-shrink: 0;
}

.ctrl-btn {
  flex: 1;
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-secondary);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.ctrl-btn:hover:not(:disabled) {
  background: var(--border);
  color: var(--text-bright);
}

.ctrl-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.ctrl-btn.primary {
  background: var(--correct);
  border-color: var(--correct);
  color: var(--text-bright);
}

.ctrl-btn.primary:hover:not(:disabled) {
  background: var(--correct-dark);
}

.ctrl-btn.primary:disabled {
  background: var(--border);
  border-color: var(--border);
  opacity: 0.4;
}

.ctrl-icon {
  font-size: 0.95rem;
}

/* ═══════════════════════════════════════════════════════════
   TOAST
   ═══════════════════════════════════════════════════════════ */
#toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--text-bright);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 0.85rem;
  font-weight: 700;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}
.toast.show { opacity: 1; }
.toast.fade { opacity: 0; }

/* ═══════════════════════════════════════════════════════════
   MODALS
   ═══════════════════════════════════════════════════════════ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  max-width: 440px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--text-bright);
}

.close-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.close-btn:hover { color: var(--text-bright); background: var(--border); }

/* Modal buttons */
.modal-btn {
  padding: 12px 24px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-secondary);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
}

.modal-btn.primary {
  background: var(--correct);
  border-color: var(--correct);
  color: var(--text-bright);
}
.modal-btn.primary:hover { background: var(--correct-dark); }

.modal-btn.full-width {
  display: block;
  width: 100%;
  margin-top: 16px;
}

.modal-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}

/* ═══════════════════════════════════════════════════════════
   HELP MODAL CONTENT
   ═══════════════════════════════════════════════════════════ */
.help-content {
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text);
}

.help-content h3 {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--text-bright);
  margin: 14px 0 8px;
}

.help-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.help-content p {
  margin: 6px 0;
}

.help-steps {
  padding-left: 20px;
  margin: 8px 0;
}
.help-steps li {
  margin: 6px 0;
}

.help-diagram {
  display: flex;
  justify-content: center;
  margin: 12px 0;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(5, 36px);
  gap: 2px;
}

.mg-cell {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  font-size: 0.8rem;
}
.mg-cell.dot::after {
  content: '●';
  color: var(--correct);
  font-size: 0.7rem;
}
.mg-cell.knight {
  background: var(--selected);
  font-size: 1.2rem;
}

.help-legend {
  margin: 8px 0;
}

.help-example {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0;
}

.ex-tile {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 800;
  border-radius: 3px;
  color: var(--text-bright);
  flex-shrink: 0;
}
.ex-tile.correct { background: var(--correct); }
.ex-tile.present { background: var(--present); }
.ex-tile.absent  { background: var(--absent); }

.help-tip {
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* ═══════════════════════════════════════════════════════════
   STATS MODAL
   ═══════════════════════════════════════════════════════════ */
.stats-grid {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin: 12px 0;
}

.stat-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-big {
  font-size: 2rem;
  font-weight: 800;
  color: var(--text-bright);
}

.stat-small {
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* Distribution */
#distribution {
  margin: 8px 0;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 3px 0;
}

.dist-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text);
  width: 14px;
  text-align: right;
}

.dist-bar {
  min-width: 24px;
  padding: 2px 6px;
  background: var(--absent);
  border-radius: 3px;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-bright);
  text-align: right;
  transition: width 0.4s ease;
}

.dist-bar.highlight {
  background: var(--correct);
}

.toast-inline {
  text-align: center;
  font-size: 0.8rem;
  color: var(--correct);
  font-weight: 600;
  margin-top: 8px;
}

/* ═══════════════════════════════════════════════════════════
   CONFETTI
   ═══════════════════════════════════════════════════════════ */
.confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
  overflow: hidden;
}

.confetti {
  position: absolute;
  top: -10px;
  width: 8px;
  height: 8px;
  opacity: 0.9;
  animation: confettiFall linear forwards;
}

@keyframes confettiFall {
  0%   { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* ═══════════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 380px) {
  :root {
    --cell-size: min(calc((100vw - 64px) / 6), calc((100dvh - 230px) / 12));
  }

  #header h1 { font-size: 1.2rem; }
  .cell-letter { font-size: 1rem; }
  .guess-tile { font-size: 1.3rem; }
}

@media (min-width: 600px) and (min-height: 800px) {
  :root {
    --tile-size: 62px;
    --cell-size: 58px;
  }
}

/* ============================================================
   ITCH.IO / IFRAME MOBILE — Compact Layout
   ============================================================ */

/* Short viewport (landscape mobile or small itch.io frame) */
@media (max-height: 600px) {
  #header h1 { font-size: 1.1rem; }
  .mode-tab { padding: 5px 10px; font-size: 0.78rem; }
  #puzzle-info { font-size: 0.8rem; }
  .guess-tile { font-size: 1.1rem; }
  .ctrl-btn { padding: 5px 10px; font-size: 0.75rem; }
  #app { padding: 0 8px 8px; }
}

@media (max-height: 450px) {
  #header h1 { font-size: 0.95rem; letter-spacing: 0.08em; }
  .icon-btn { width: 28px; height: 28px; font-size: 0.8rem; }
  .mode-tab { padding: 4px 8px; font-size: 0.72rem; }
  .guess-tile { font-size: 0.95rem; }
  .cell-letter { font-size: 0.85rem; }
  .ctrl-btn { padding: 4px 8px; font-size: 0.7rem; }
}

/* ============================================================
   LOADING SCREEN
   ============================================================ */
#loader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.4s, visibility 0.4s;
}
#loader.loaded {
  opacity: 0;
  visibility: hidden;
}
.loader-content {
  text-align: center;
}
.loader-icon {
  font-size: 4rem;
  animation: loader-bounce 1s ease-in-out infinite;
}
.loader-title {
  margin-top: 12px;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--text-bright);
}
.loader-bar {
  margin-top: 20px;
  width: 160px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.loader-fill {
  height: 100%;
  width: 30%;
  background: var(--correct);
  border-radius: 2px;
  animation: loader-progress 1.2s ease-in-out infinite;
}
@keyframes loader-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}
@keyframes loader-progress {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(540%); }
}

/* ============================================================
   MODE TABS (Daily / Practice)
   ============================================================ */
#mode-tabs {
  display: flex;
  justify-content: center;
  gap: 0;
  margin: 4px auto 0;
  max-width: 280px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.mode-tab {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-secondary);
  color: var(--text-dim);
  border: none;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.mode-tab.active {
  background: var(--correct);
  color: var(--text-bright);
}
.mode-tab:hover:not(.active) {
  background: var(--border);
}

/* ============================================================
   SETTINGS
   ============================================================ */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.settings-row {
  display: block;
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.95rem;
  font-family: var(--font);
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}
.settings-row:hover {
  background: var(--border);
}

/* ============================================================
   COLOR-BLIND MODE
   ============================================================ */
body.color-blind .guess-tile.correct,
body.color-blind .grid-cell.key-correct { background: #f5793a !important; }
body.color-blind .guess-tile.present,
body.color-blind .grid-cell.key-present { background: #85c0f9 !important; }

/* Add symbols to tiles in color-blind mode */
body.color-blind .guess-tile.correct::after { content: '✓'; position: absolute; bottom: 2px; right: 3px; font-size: 0.6rem; }
body.color-blind .guess-tile.present::after { content: '○'; position: absolute; bottom: 2px; right: 3px; font-size: 0.6rem; }
body.color-blind .guess-tile.absent::after  { content: '✕'; position: absolute; bottom: 2px; right: 3px; font-size: 0.6rem; }
body.color-blind .guess-tile { position: relative; }

/* ============================================================
   TOUCH POLISH
   ============================================================ */
#letter-grid {
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
#app {
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
