:root {
  --navbar-height: 48px;
  --tile-gap: 5px;

  /* Actual viewport height (set by JavaScript) */
  /* This gives us the REAL visible height, accounting for Safari's URL bar */
  --actual-vh: 1vh;

  /* Calculate sizes based on actual visible viewport */
  --available-height: calc(var(--actual-vh, 1vh) * 100 - 80px);
  --keyboard-height: 156px;
  --board-height: calc(var(--available-height) - var(--keyboard-height));
  --tile-size: calc((var(--board-height) - 35px) / 8); /* 7 gaps of 5px = 35px */
  --score-box-width: calc(var(--tile-size) * 0.8);

  /* Colors */
  --bg-color: #fff;
  --text-color: #333;
  --border-color: #ddd;
  --tile-bg: #fff;
  --tile-empty-border: #ccc;
  --tile-filled-border: #666;
  --score-red: #ff4444;
  --score-yellow: #ffc107;
  --score-green: #4caf50;
  --key-bg: #e0e0e0;
  --key-hover: #d0d0d0;
  --key-active: #bbb;
  --modal-overlay: rgba(0, 0, 0, 0.5);
}

/* Dark mode */
[data-theme="dark"] {
  --bg-color: #1a1a1a;
  --text-color: #e0e0e0;
  --border-color: #444;
  --tile-bg: #2a2a2a;
  --tile-empty-border: #555;
  --tile-filled-border: #999;
  --key-bg: #3a3a3a;
  --key-hover: #4a4a4a;
  --key-active: #555;
  --modal-overlay: rgba(0, 0, 0, 0.7);
}

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg-color);
  color: var(--text-color);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* Prevent double-tap zoom */
  /* Use actual viewport height from JavaScript */
  height: calc(var(--actual-vh, 1vh) * 100);
  position: fixed; /* Prevent Safari from scrolling */
  width: 100%;
  top: 0;
  left: 0;
}

/* Navbar */
.game-navbar {
  position: sticky;
  top: 0;
  top: env(safe-area-inset-top, 0); /* Account for notch on iPhone */
  background: var(--bg-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: var(--navbar-height);
  padding: 0 12px;
  padding-left: max(12px, env(safe-area-inset-left, 12px));
  padding-right: max(12px, env(safe-area-inset-right, 12px));
  z-index: 100;
  border-bottom: 1px solid var(--border-color);
}

.home-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  text-decoration: none;
  color: var(--text-color);
  font-size: 1.2rem;
  border-radius: 6px;
  transition: background 0.15s;
}

.home-btn::before {
  content: '←';
}

.home-btn:active {
  background: var(--key-hover);
}

.nav-title {
  font-weight: bold;
  font-size: 1.1rem;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.nav-actions {
  display: flex;
  gap: 24px;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
  color: var(--text-color);
}

.icon-btn .text-icon {
  font-size: 1.5rem;
  font-weight: 900;
  line-height: 1;
  color: var(--text-color);
}

.icon-btn:active {
  background: var(--key-hover);
}

/* Text icon button in circle */
.text-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 2px solid var(--text-color);
  border-radius: 50%;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  transition: all 0.15s;
  padding: 0;
}

.text-icon-btn .text-icon {
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: relative;
  top: -1px;
}

/* CSS-based plus icon */
.icon-btn-plus::before,
.icon-btn-plus::after {
  content: '';
  position: absolute;
  background: var(--text-color);
}

.icon-btn-plus::before {
  width: 14px;
  height: 2.5px;
}

.icon-btn-plus::after {
  width: 2.5px;
  height: 14px;
}

.text-icon-btn:active {
  background: var(--key-hover);
}

.text-icon-btn:hover {
  background: rgba(0, 0, 0, 0.05);
}

@media (prefers-color-scheme: dark) {
  .text-icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
  }
}

/* Main Game Container */
.game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom, 8px)); /* Account for home indicator */
  gap: 12px;
  /* Use actual viewport height from JavaScript */
  height: calc(var(--actual-vh, 1vh) * 100 - var(--navbar-height));
  overflow: hidden;
  position: relative;
}

/* Game Board */
.board {
  display: flex;
  flex-direction: column;
  gap: var(--tile-gap);
  width: 100%;
  max-width: 340px;
  flex-shrink: 0;
}

.row {
  display: flex;
  gap: var(--tile-gap);
  justify-content: center;
}

/* Tiles */
.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  background: var(--tile-bg);
  border: 2px solid var(--tile-empty-border);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.5); /* Scale font with tile size */
  font-weight: bold;
  text-transform: uppercase;
  position: relative;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.tile.filled {
  border-color: var(--tile-filled-border);
}

/* Tile marks (Hard Mode - borders) */
.tile.mark-incorrect {
  border: 3px solid var(--score-red);
}

.tile.mark-correct {
  border: 3px solid var(--score-green);
}

/* Tile dots (Easy Mode - top-right dots) */
.tile.dot-incorrect::after,
.tile.dot-correct::after {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.tile.dot-incorrect::after {
  background: var(--score-red);
}

.tile.dot-correct::after {
  background: var(--score-green);
}

/* Tile background colors for submitted guesses */
.tile.wrong {
  background: rgba(255, 68, 68, 0.2);
  border-color: var(--score-red);
}

.tile.correct {
  background: rgba(76, 175, 80, 0.2);
  border-color: var(--score-green);
}

/* Row background colors (legacy - may not be used) */
.row.row-red .tile {
  background: rgba(255, 68, 68, 0.2);
  border-color: var(--score-red);
}

.row.row-green .tile {
  background: rgba(76, 175, 80, 0.2);
  border-color: var(--score-green);
}

/* Score Box */
.score-box {
  width: var(--tile-size);
  height: var(--tile-size);
  background: var(--tile-bg);
  border: 2px solid var(--tile-empty-border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.45); /* Scale font with tile size */
  font-weight: bold;
}

.score-box.score-filled,
.score-box.partial {
  background: rgba(255, 193, 7, 0.15);
  border-color: var(--score-yellow);
  border-width: 3px;
  color: var(--text-color);
}

.score-box.score-zero,
.score-box.zero {
  background: rgba(255, 68, 68, 0.15);
  border-color: var(--score-red);
  border-width: 3px;
  color: var(--text-color);
}

.score-box.score-win,
.score-box.winning {
  background: rgba(76, 175, 80, 0.15);
  border-color: var(--score-green);
  border-width: 3px;
  color: var(--text-color);
}

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

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

.key {
  min-width: 28px;
  height: 48px;
  background: var(--key-bg);
  border: none;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-color);
  cursor: pointer;
  transition: background 0.1s, transform 0.05s;
  flex: 1;
  max-width: 38px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.key-enter,
.key-backspace {
  flex: 1.5;
  max-width: 62px;
  font-size: 1.3rem;
  font-weight: 700;
  pointer-events: auto;
}

/* Keyboard color states */
.key.key-incorrect {
  background: var(--score-red);
  color: white;
}

.key.key-correct {
  background: var(--score-green);
  color: white;
}

/* Enter and Backspace buttons - same styling as other keys */
.key-enter,
.key-backspace {
  background: var(--key-bg) !important;
  color: var(--text-color) !important;
  border: none !important;
  box-shadow: none !important;
  font-weight: 800 !important;
}

.key-backspace {
  pointer-events: auto !important;
  z-index: 10;
}

.key-enter:active,
.key-backspace:active {
  background: var(--key-active) !important;
}

/* Keyboard outline states (Hard Mode) */
.key.key-outline-incorrect::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 3px solid var(--score-red);
  border-radius: 6px;
  pointer-events: none;
}

.key.key-outline-correct::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 3px solid var(--score-green);
  border-radius: 6px;
  pointer-events: none;
}

/* Modals */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-color);
  border-radius: 12px;
  max-width: 500px;
  width: 100%;
  max-height: calc(var(--actual-vh, 1vh) * 90);
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
  font-size: 1.4rem;
  margin: 0;
}

.modal-close {
  background: transparent;
  border: none;
  font-size: 2rem;
  cursor: pointer;
  color: var(--text-color);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: background 0.15s;
}

.modal-close:active {
  background: var(--key-hover);
}

.modal-body {
  padding: 20px;
}

.modal-body h3 {
  margin-top: 16px;
  margin-bottom: 8px;
  font-size: 1.1rem;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body p {
  margin-bottom: 12px;
  line-height: 1.5;
}

.modal-body ul {
  margin-left: 20px;
  margin-bottom: 12px;
  line-height: 1.6;
}

/* Settings */
.setting-item {
  padding: 16px 0;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.setting-item:last-child {
  border-bottom: none;
}

.setting-item label {
  font-weight: 500;
}

.setting-item input[type="checkbox"] {
  width: 44px;
  height: 24px;
  cursor: pointer;
}

.link-btn {
  display: inline-block;
  padding: 10px 16px;
  background: var(--key-bg);
  border-radius: 6px;
  text-decoration: none;
  color: var(--text-color);
  transition: background 0.15s;
}

.link-btn:active {
  background: var(--key-active);
}

/* Buttons */
.btn {
  display: inline-block;
  padding: 12px 24px;
  background: var(--key-bg);
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  color: var(--text-color);
  transition: background 0.15s, transform 0.05s;
}

.btn:active {
  background: var(--key-active);
  transform: scale(0.98);
}

/* Colorful Start Playing Button */
.btn-primary {
  background: linear-gradient(135deg, #4caf50 0%, #45a049 100%);
  color: #fff;
  box-shadow: 0 4px 15px rgba(76, 175, 80, 0.4);
}

.btn-primary:hover {
  box-shadow: 0 6px 20px rgba(76, 175, 80, 0.6);
  transform: translateY(-2px);
}

.btn-primary:active {
  background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
  transform: scale(0.98) translateY(0);
  box-shadow: 0 2px 10px rgba(76, 175, 80, 0.4);
}

/* Dark mode adjustments for primary button */
[data-theme="dark"] .btn-primary {
  background: linear-gradient(135deg, #66bb6a 0%, #5cb860 100%);
  box-shadow: 0 4px 15px rgba(102, 187, 106, 0.3);
}

[data-theme="dark"] .btn-primary:hover {
  box-shadow: 0 6px 20px rgba(102, 187, 106, 0.5);
}

[data-theme="dark"] .btn-primary:active {
  background: linear-gradient(135deg, #5cb860 0%, #4caf50 100%);
  box-shadow: 0 2px 10px rgba(102, 187, 106, 0.3);
}

/* Responsive adjustments for larger screens */
@media (min-height: 700px) {
  :root {
    --keyboard-height: 168px; /* Slightly taller keys */
  }

  .key {
    height: 52px;
    font-size: 1rem;
  }

  .key-enter,
  .key-backspace {
    font-size: 1.4rem;
  }
}

@media (min-height: 800px) {
  :root {
    --keyboard-height: 180px;
    --tile-gap: 6px;
    --board-height: calc(var(--available-height) - var(--keyboard-height));
    --tile-size: calc((var(--board-height) - 42px) / 8); /* 7 gaps of 6px = 42px */
  }

  .game-container {
    gap: 16px;
  }

  .key {
    height: 56px;
    font-size: 1.1rem;
    max-width: 48px;
  }

  .key-enter,
  .key-backspace {
    font-size: 1.5rem;
  }
}

@media (min-width: 768px) {
  .board {
    max-width: 400px;
  }

  .keyboard {
    max-width: 600px;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}

/* Touch feedback enhancement */
@media (hover: none) {
  .key:active,
  .icon-btn:active,
  .btn:active {
    opacity: 0.7;
  }
}

/* Notification Toast */
.notification {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--tile-bg);
  color: var(--text-color);
  border: 2px solid var(--border-color);
  padding: 12px 24px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  font-size: 0.95rem;
  font-weight: 600;
  z-index: 2000;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}

.notification.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Shake Animation */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

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

/* Answer word styling in modal */
.answer-word {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: var(--primary-color);
  margin: 16px 0;
}

/* Mode Selection in Info Modal */
.mode-selection {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border-color);
}

.mode-selection h3 {
  margin-bottom: 12px;
  font-size: 1.1rem;
  color: var(--text-color);
}

.mode-buttons {
  display: flex;
  gap: 12px;
}

.mode-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 12px;
  background: var(--tile-bg);
  border: 2px solid var(--border-color);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.mode-btn:hover {
  background: var(--key-hover);
}

.mode-btn.active {
  border-color: var(--score-green);
  background: rgba(76, 175, 80, 0.1);
}

.mode-btn .mode-icon {
  font-size: 2rem;
}

.mode-btn .mode-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-color);
}

.mode-btn .mode-desc {
  font-size: 0.75rem;
  color: var(--text-color);
  opacity: 0.7;
}

.rules-section {
  margin-top: 20px;
}

.rules-section h3 {
  margin-top: 16px;
  margin-bottom: 8px;
  font-size: 1.05rem;
  color: var(--text-color);
}

.rules-section p {
  margin-bottom: 12px;
  line-height: 1.5;
}

.rules-section ul {
  margin-left: 20px;
  margin-bottom: 12px;
}

.rules-section li {
  margin-bottom: 6px;
  line-height: 1.5;
}

.mode-rules {
  background: rgba(76, 175, 80, 0.05);
  border-left: 3px solid var(--score-green);
  padding: 12px 16px;
  border-radius: 4px;
  margin-top: 12px;
}

/* Example Tiles in Modal */
.examples {
  margin-bottom: 24px;
}

.example {
  margin-bottom: 16px;
}

.example-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(40px, 45px)) minmax(40px, 45px);
  gap: 4px;
  margin-bottom: 8px;
  justify-content: flex-start;
}

.example-tile {
  aspect-ratio: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  text-transform: uppercase;
  color: var(--text-color);
  border-radius: 4px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-filled-border);
}

.example-tile.example-red {
  background: rgba(255, 68, 68, 0.2);
  border-color: var(--score-red);
}

.example-tile.example-green {
  background: rgba(76, 175, 80, 0.2);
  border-color: var(--score-green);
}

.example-tile.example-red-mark {
  border: 3px solid var(--score-red) !important;
}

.example-tile.example-green-mark {
  border: 3px solid var(--score-green) !important;
}

/* Example dots for easy mode */
.example-tile.example-red-dot::after,
.example-tile.example-green-dot::after {
  content: '';
  position: absolute;
  top: 2px;
  right: 2px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.example-tile.example-red-dot::after {
  background: var(--score-red);
}

.example-tile.example-green-dot::after {
  background: var(--score-green);
}

.example-tile {
  position: relative;
}

.example-score {
  height: 100%;
  aspect-ratio: 1;
  border: 2px solid var(--tile-empty-border);
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  border-radius: 50%;
  color: var(--text-color);
  background: var(--tile-bg);
}

.example-score.example-score-partial {
  background: rgba(255, 193, 7, 0.15);
  border-color: var(--score-yellow);
  border-width: 3px;
}

.example-score.example-score-zero {
  background: rgba(255, 68, 68, 0.15);
  border-color: var(--score-red);
  border-width: 3px;
}

.example-score.example-score-win {
  background: rgba(76, 175, 80, 0.15);
  border-color: var(--score-green);
  border-width: 3px;
}

.example-description {
  font-size: 14px;
  color: var(--text-color);
  opacity: 0.8;
  margin: 4px 0 0 0;
  line-height: 1.4;
}

/* Responsive adjustments for larger screens */
@media (min-width: 768px) {
  .example-tile,
  .example-score {
    font-size: 24px;
  }

  .example-description {
    font-size: 15px;
  }
}

/* System theme preference */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-color: #1a1a1a;
    --text-color: #e0e0e0;
    --border-color: #444;
    --tile-bg: #2a2a2a;
    --tile-empty-border: #555;
    --tile-filled-border: #999;
    --key-bg: #3a3a3a;
    --key-hover: #4a4a4a;
    --key-active: #555;
    --modal-overlay: rgba(0, 0, 0, 0.7);
  }
}
