:root {
  /* Colors */
  --primary-color: #3b82f6;
  --primary-dark: #2563eb;
  --accent-color: #f59e0b;
  --bg-color: #f8fafc;
  --surface-color: #ffffff;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --border-color: #e2e8f0;
  --grid-border: #334155;
  --cell-bg: #ffffff;
  --cell-active: #bae6fd;
  --cell-focus: #ffedd5;
  --cell-correct: #dcfce7;

  /* Dimensions */
  --header-height: 60px;
  --grid-gap: 1px;
  --cell-size: 36px; /* Responsive base */
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: "Tajawal", sans-serif;
  background-color: var(--bg-color);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden; /* App style */
  direction: rtl;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Header */
.game-header {
  height: var(--header-height);
  background: var(--surface-color);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  z-index: 10;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.game-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary-dark);
}

.game-stats {
  display: flex;
  gap: 1rem;
  font-family: monospace;
  font-size: 1.1rem;
}

/* Main Area */
.game-area {
  flex: 1;
  display: flex;
  overflow: hidden;
  padding: 1rem;
  gap: 1.5rem;
  justify-content: center;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

/* Crossword Grid */
.crossword-container {
  background: var(--grid-border);
  padding: 2px;
  display: grid;
  gap: var(--grid-gap);
  /* grid-template-columns set by JS */
  width: fit-content;
  height: fit-content;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  align-self: flex-start;
  position: relative; /* For indices if needed */
  /* Constrain large puzzles and allow scrolling */
  max-width: calc(100% - 340px);
  max-height: calc(100vh - 140px);
  overflow: auto;
}

/* Indices */
.grid-index {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    color: var(--text-secondary);
    background: #f1f5f9;
    font-weight: bold;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--cell-bg);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.36);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.1s;
}

.cell.black {
  background-color: var(--grid-border);
  cursor: default;
}

.cell.active {
  background-color: var(--cell-active);
}

.cell.focus {
  background-color: var(--cell-focus);
}

.cell.correct {
    background-color: var(--cell-correct);
    color: #166534;
}

.cell.error {
    background-color: #fecaca;
    color: #991b1b;
}

.cell-number {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 0.6rem;
  color: var(--text-secondary);
  line-height: 1;
}

.cell input {
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  background: transparent;
  font-family: inherit;
  font-size: inherit;
  font-weight: inherit;
  color: inherit;
  padding: 0;
  outline: none;
  caret-color: var(--primary-color);
}

/* Clues */
.clues-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow-y: auto;
  max-width: 300px;
  background: var(--surface-color);
  padding: 1rem;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.clue-section h3 {
  font-size: 1rem;
  color: var(--primary-dark);
  margin-bottom: 0.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}

.clue-list {
  list-style: none;
}

.clue-item {
  padding: 0.5rem;
  font-size: 0.95rem;
  cursor: pointer;
  border-radius: 4px;
  margin-bottom: 0.25rem;
  transition: all 0.2s;
  white-space: normal; /* Allow long/translated clues to wrap */
  line-height: 1.3;
}

.clue-hint {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 0.25rem;
  font-style: italic;
}

.clue-item:hover {
  background-color: var(--bg-color);
}

.clue-item.active {
  background-color: var(--cell-active);
  color: var(--primary-dark);
  font-weight: 500;
}

.clue-number {
  font-weight: 700;
  margin-left: 0.5rem;
}

/* Control Bar (Mobile sticky) */
.input-controls {
  background: var(--surface-color);
  padding: 0.75rem;
  box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1);
}

.current-clue-bar {
  text-align: center;
  font-weight: 500;
  color: var(--text-primary);
}

/* Utilities */
.hidden {
  display: none !important;
}

/* Responsive */
@media (max-width: 768px) {
    :root {
        --cell-size: 30px;
    }
    
    .game-area {
    flex-direction: column;
    align-items: center;
    overflow-y: auto;
    padding-bottom: 80px; /* Space for controls */
  }

  .clues-container {
    width: 100%;
    max-width: none;
    max-height: 200px;
  }

  .crossword-container {
    margin: 0 auto;
  }
}

/* Make crossword use more available space on mid-size screens and allow hiding clues */
@media (max-width: 900px) {
  .crossword-container { max-width: calc(100% - 20px); }
  .clues-container.hidden { display: none !important; }
}

/* Home Screen Styles */
#home-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    height: 100%;
    background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    overflow-y: auto;
}

.home-header {
    text-align: center;
    margin-bottom: 2rem;
}

.main-title {
    font-size: 3rem;
    color: var(--primary-dark);
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1.2rem;
}

.levels-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 1rem;
    width: 100%;
    max-width: 600px;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.8); /* increased opacity */
    border-radius: 12px;
    min-height: 200px; /* Ensure visibility */
}

.level-btn {
    aspect-ratio: 1;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    background: white;
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    position: relative;
    overflow: hidden;
}

.level-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    border-color: var(--primary-color);
}

.level-btn.active {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-dark);
}

.level-btn.locked {
    background: #e2e8f0;
    color: #94a3b8;
    cursor: not-allowed;
    border-color: transparent;
}

.level-btn.completed {
    background: #dcfce7;
    color: #166534;
    border-color: #86efac;
}

.level-btn .status-icon {
    font-size: 1rem;
    margin-top: 5px;
}

.menu-btn {
    cursor: pointer;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
}

.menu-btn:hover {
    background: rgba(0,0,0,0.05);
}

.screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow: hidden;
}

@media (min-width: 600px) {
    .levels-grid {
        grid-template-columns: repeat(5, 1fr);
    }
}
