:root {
  --bg: #ffffff;
  --bg-secondary: #f5f5f7;
  --text: #1a1a2e;
  --text-secondary: #666;
  --border: #d0d0d0;
  --cell-bg: #ffffff;
  --cell-selected: #a8d8ea;
  --cell-highlighted: #e8f4f8;
  --cell-correct: #d4edda;
  --cell-wrong: #f8d7da;
  --black-cell: #1a1a2e;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --header-bg: #1a1a2e;
  --header-text: #ffffff;
  --clue-active: #e8f4f8;
  --modal-bg: rgba(0,0,0,0.5);
  --toast-bg: #333;
  --toast-text: #fff;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
}

[data-theme="dark"] {
  --bg: #121212;
  --bg-secondary: #1e1e1e;
  --text: #e0e0e0;
  --text-secondary: #999;
  --border: #333;
  --cell-bg: #1e1e1e;
  --cell-selected: #1a4a6e;
  --cell-highlighted: #1a2a3e;
  --cell-correct: #1a3a1a;
  --cell-wrong: #3a1a1a;
  --black-cell: #000000;
  --accent: #60a5fa;
  --accent-hover: #3b82f6;
  --header-bg: #0a0a1e;
  --clue-active: #1a2a3e;
  --modal-bg: rgba(0,0,0,0.7);
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
}

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

html {
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  background: var(--header-bg);
  color: var(--header-text);
  position: sticky;
  top: 0;
  z-index: 100;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

#header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

#timer {
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
  font-weight: 600;
  opacity: 0.9;
}

.icon-btn {
  background: none;
  border: none;
  color: inherit;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
  opacity: 0.85;
  transition: opacity 0.15s;
}

.icon-btn:hover { opacity: 1; }

/* Menu drawer */
#menu-drawer {
  position: fixed;
  inset: 0;
  z-index: 200;
}

.drawer-overlay {
  position: absolute;
  inset: 0;
  background: var(--modal-bg);
}

.drawer-content {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--bg);
  padding: 1.5rem 1.25rem;
  overflow-y: auto;
  box-shadow: var(--shadow);
  animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

.drawer-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text);
  cursor: pointer;
}

#user-info { margin-bottom: 0.5rem; }

#user-greeting {
  font-weight: 600;
  margin-bottom: 0.75rem;
}

#auth-buttons {
  display: flex;
  gap: 0.5rem;
}

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

#stats-panel h3, #next-puzzle-panel h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 0.35rem 0;
  font-size: 0.95rem;
}

.stat-row span:last-child {
  font-weight: 600;
}

#countdown {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.btn:active { transform: scale(0.97); }

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

.btn-primary:hover { background: var(--accent-hover); }

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

.btn-full {
  width: 100%;
  margin-top: 0.5rem;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-weight: 600;
  font-size: inherit;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: var(--modal-bg);
}

.modal-content {
  position: relative;
  background: var(--bg);
  border-radius: 12px;
  padding: 2rem 1.5rem;
  width: min(400px, 90vw);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  animation: modalIn 0.2s ease-out;
}

@keyframes modalIn {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text);
  cursor: pointer;
}

.modal-content h2 {
  margin-bottom: 1rem;
}

.modal-content form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.modal-content input,
.modal-content select,
.modal-content textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  background: var(--bg-secondary);
  color: var(--text);
}

.modal-content label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.auth-switch {
  text-align: center;
  margin-top: 1rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.error-text {
  color: #dc3545;
  font-size: 0.85rem;
}

.success-text {
  color: #28a745;
  font-size: 0.85rem;
}

.completion-content {
  text-align: center;
}

.completion-content h2 {
  font-size: 1.5rem;
}

#completion-time {
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0.5rem 0;
}

#completion-stats {
  margin: 1rem 0;
}

/* Main content */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* Active clue bar */
#active-clue-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  min-height: 2.5rem;
}

#active-clue-text {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
  text-align: center;
}

.clue-nav-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1.2rem;
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text);
  flex-shrink: 0;
}

/* Grid */
#grid-container {
  position: relative;
  display: flex;
  justify-content: center;
  padding: 0.75rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

#grid {
  display: grid;
  border: 2px solid var(--text);
  gap: 0;
  width: fit-content;
  touch-action: manipulation;
}

.cell {
  position: relative;
  width: var(--cell-size, 36px);
  height: var(--cell-size, 36px);
  border: 0.5px solid var(--border);
  background: var(--cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--cell-font, 16px);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.1s;
}

.cell.black {
  background: var(--black-cell);
  cursor: default;
}

.cell.selected {
  background: var(--cell-selected);
}

.cell.highlighted {
  background: var(--cell-highlighted);
}

.cell.selected.highlighted {
  background: var(--cell-selected);
}

.cell.correct {
  background: var(--cell-correct);
}

.cell.wrong {
  background: var(--cell-wrong);
}

.cell-number {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: calc(var(--cell-size, 36px) * 0.28);
  font-weight: 700;
  line-height: 1;
  color: var(--text-secondary);
}

.cell-value {
  font-size: calc(var(--cell-size, 36px) * 0.5);
  font-weight: 600;
  line-height: 1;
}

#hidden-input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
  top: -100px;
}

/* Puzzle layout */
#puzzle-layout {
  display: flex;
  flex-direction: column;
}

#grid-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  order: -1;
}

/* Clues */
.clues-panel {
  padding: 0.5rem 1rem 2rem;
}

.clue-heading {
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0.75rem 0 0.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  user-select: none;
}

.toggle-arrow {
  font-size: 0.7rem;
  transition: transform 0.2s;
}

.clue-heading.collapsed .toggle-arrow {
  transform: rotate(-90deg);
}

.clue-list {
  list-style: none;
}

.clue-list.collapsed {
  display: none;
}

.clue-item {
  padding: 0.5rem 0.5rem;
  font-size: 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  gap: 0.5rem;
  transition: background 0.1s;
  line-height: 1.35;
}

.clue-item:hover {
  background: var(--bg-secondary);
}

.clue-item.active {
  background: var(--clue-active);
  font-weight: 600;
}

.clue-item.completed {
  color: var(--text-secondary);
}

.clue-number {
  font-weight: 700;
  min-width: 1.5rem;
  color: var(--text-secondary);
}

/* Toast */
#toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--toast-text);
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  z-index: 400;
  animation: toastIn 0.2s ease-out;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

@keyframes toastIn {
  from { transform: translateX(-50%) translateY(1rem); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* Tier badges */
.tier-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}

.tier-free {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.tier-premium {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
}

.btn-accent {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
}

.btn-accent:hover {
  background: linear-gradient(135deg, #d97706, #b45309);
}

/* Pricing modal */
.pricing-content {
  text-align: center;
  width: min(480px, 90vw);
}

.pricing-subtitle {
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
  font-size: 0.95rem;
}

.pricing-cards {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
}

.pricing-card {
  flex: 1;
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem 1rem;
  position: relative;
}

.pricing-card.featured {
  border-color: var(--accent);
}

.pricing-badge {
  position: absolute;
  top: -0.75rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fff;
  padding: 0.15rem 0.75rem;
  border-radius: 20px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  white-space: nowrap;
}

.pricing-card h3 {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.pricing-amount {
  font-size: 1.75rem;
  font-weight: 800;
  margin-bottom: 0.25rem;
}

.pricing-amount span {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.pricing-savings {
  color: #28a745;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.pricing-note {
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin-top: 0.5rem;
}

/* Puzzle info bar */
#puzzle-info-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.3rem 0.5rem;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

#puzzle-date-label {
  font-weight: 600;
}

#free-tier-notice {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #fff;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
}

#subscription-info {
  margin-top: 0.5rem;
}

.hidden { display: none !important; }

/* Desktop layout */
@media (min-width: 768px) {
  #puzzle-layout {
    flex-direction: row;
    align-items: flex-start;
  }

  #grid-column {
    flex: 0 0 auto;
    order: 2;
  }

  #grid-container {
    padding: 1rem;
  }

  .clues-panel {
    flex: 1;
    min-width: 200px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
    padding: 1rem;
  }

  #clues-across-container {
    order: 1;
  }

  #clues-down-container {
    order: 3;
  }

  .cell {
    --cell-size: 42px;
    --cell-font: 18px;
  }
}

@media (min-width: 1024px) {
  .cell {
    --cell-size: 48px;
    --cell-font: 20px;
  }
}

/* Small phones */
@media (max-width: 380px) {
  .cell {
    --cell-size: 30px;
    --cell-font: 13px;
  }

  header h1 { font-size: 0.95rem; }
}

#app-version {
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-secondary);
  padding: 8px 0;
  opacity: 0.6;
}
