/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #ffffff;
  --bg-surface: #f5f5f5;
  --text: #1a1a1b;
  --text-muted: #787c7e;
  --accent: #c23616;

  /* Tile colors (warm, vibrant on white) */
  --rust: #c4704a;
  --sand: #d4b87a;
  --sage: #7a9a58;

  /* Terrain colors (soft, light) */
  --terrain-empty: #ffffff;
  --terrain-rock: #cdc4b0;
  --terrain-tree: #b8d8b0;
  --terrain-river: #a0cce8;

  /* Grid -- width accounts for side panels (~22px each + gaps) */
  /* Height: 12 = 10 grid rows + 2 preview rows */
  --grid-gap: 3px;
  --border-color: #d3d6da;
  --radius: 6px;
  --cell-size: min(calc((100vw - 94px) / 7), calc((100vh - 250px) / 12));
}

@supports (height: 1dvh) {
  :root {
    --cell-size: min(calc((100vw - 94px) / 7), calc((100dvh - 220px) / 12));
  }
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  max-width: 500px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

#header h1 {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.header-right {
  position: relative;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  transition: color 0.15s, background 0.15s;
}

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

/* ===== Header Menu (dropdown) ===== */
.header-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(0, 0, 0, 0.06);
  padding: 4px 0;
  min-width: 180px;
  z-index: 50;
  animation: fade-in 0.12s ease-out;
}

.header-menu.hidden {
  display: none;
}

.menu-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  text-align: left;
  background: none;
  border: none;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
}

.menu-item:active {
  background: rgba(0, 0, 0, 0.05);
}

.menu-item--danger {
  color: var(--accent);
}

/* ===== Game Area ===== */
#game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 4px 10px 8px;
  gap: 4px;
  overflow: hidden;
}

/* ===== Score Bar ===== */
#score-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  flex-shrink: 0;
  padding: 2px 2px;
}

#score-display {
  color: var(--text);
  font-size: 15px;
}

/* ===== Grid ===== */
#grid-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 0;
  overflow: hidden;
}

#grid {
  display: grid;
  grid-template-columns: repeat(7, var(--cell-size));
  grid-template-rows: repeat(10, var(--cell-size));
  gap: var(--grid-gap);
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 4px;
  border: 2px solid var(--border-color);
  touch-action: manipulation;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell-emoji {
  font-size: calc(var(--cell-size) * 0.5);
  line-height: 1;
  pointer-events: none;
}

/* Terrain */
.cell--empty { background: var(--terrain-empty); border-color: var(--border-color); }
.cell--rock  { background: var(--terrain-rock);  border-color: #bab0a0; }
.cell--tree  { background: var(--terrain-tree);  border-color: #98c098; }
.cell--river { background: var(--terrain-river); border-color: #80b4d0; }

/* Tile colors */
.cell--rust { background: var(--rust); border-color: #a85838; color: white; }
.cell--sand { background: var(--sand); border-color: #c0a060; color: white; }
.cell--sage { background: var(--sage); border-color: #608040; color: white; }

/* Ghost preview */
.cell--preview {
  opacity: 0.7;
  animation: pulse-preview 1s ease-in-out infinite;
}

.cell--invalid {
  opacity: 0.4;
  background: var(--accent) !important;
  border-color: var(--accent) !important;
}

@keyframes pulse-preview {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 0.85; }
}

/* ===== Tile Panel (below grid) ===== */
#tile-panel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px 0 2px;
  flex-shrink: 0;
}

/* Tile preview: compact strip */
#tile-preview-area {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-surface);
  border-radius: var(--radius);
  padding: 6px 12px;
  /* Fixed height for tallest shape (bar=4 cells × 0.5 + gaps) */
  height: calc(var(--cell-size) * 2 + 18px);
}

#tile-preview {
  display: grid;
  gap: 2px;
}

.preview-cell {
  width: calc(var(--cell-size) * 0.5);
  height: calc(var(--cell-size) * 0.5);
  border-radius: 2px;
}

.preview-cell--empty { background: transparent; }

/* Side panels: remaining tile inventory */
.seq-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 24px;
  flex-shrink: 0;
}

.seq-shape {
  display: grid;
  gap: 0;
}

.seq-cell {
  width: 5px;
  height: 5px;
}

.seq-cell--empty {
  background: transparent;
}

.seq-shape--done {
  opacity: 0.15;
}

.seq-shape--done .seq-cell {
  background: var(--border-color) !important;
}

/* Controls: single horizontal row */
#tile-controls {
  display: flex;
  gap: 6px;
}

.control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 52px;
  touch-action: manipulation;
  background: var(--bg);
  border: 2px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
  flex: 1;
}

.control-btn:active {
  transform: scale(0.96);
}

.control-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

/* Place button -- wider and prominent */
.place-btn {
  flex: 2;
  background: var(--sage);
  border-color: var(--sage);
  color: white;
  font-size: 15px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.place-btn:disabled {
  background: var(--bg-surface);
  border-color: var(--border-color);
  color: var(--text-muted);
  font-weight: 700;
}

/* Skip button */
.skip-btn {
  flex-direction: column;
  gap: 0;
  line-height: 1;
  color: var(--accent);
  border-color: rgba(194, 54, 22, 0.3);
  font-size: 13px;
}

.skip-cost {
  font-size: 10px;
  opacity: 0.6;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  background: var(--bg);
  border-radius: 12px;
  padding: 24px;
  max-width: 360px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  animation: fade-in 0.2s ease-out;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.modal h2 {
  font-size: 20px;
  margin-bottom: 16px;
}

.modal p {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-muted);
  margin-bottom: 12px;
}

/* ===== Game Over ===== */
.game-over-title {
  font-size: 24px;
  text-align: center;
  margin-bottom: 4px;
}

.game-over-score {
  font-size: 48px;
  font-weight: 800;
  text-align: center;
  margin-bottom: 4px;
}

.game-over-stars {
  text-align: center;
  font-size: 28px;
  margin-bottom: 16px;
}

.score-breakdown {
  font-size: 13px;
  margin-bottom: 20px;
}

.score-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid #e8e8e8;
}

.score-row:last-child {
  border-bottom: none;
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
}

.score-row .label { color: var(--text-muted); }
.score-row .value { color: var(--text); font-weight: 600; }
.score-row .value.negative { color: var(--accent); }
.score-row .value.positive { color: var(--sage); }

.btn-share {
  display: block;
  width: 100%;
  padding: 14px;
  background: var(--sage);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: opacity 0.15s;
}

.btn-share:active {
  opacity: 0.8;
}

/* ===== Stats Modal ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.stat-box { text-align: center; }
.stat-value { font-size: 28px; font-weight: 800; }
.stat-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; }

/* ===== Help Modal ===== */
.help-section { margin-bottom: 16px; }

.help-section h3 {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.building-legend {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.legend-swatch {
  width: 24px;
  height: 24px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ===== Animations ===== */
@keyframes place-tile {
  0% { transform: scale(1.15); opacity: 0.7; }
  100% { transform: scale(1); opacity: 1; }
}

.cell--just-placed {
  animation: place-tile 0.2s ease-out;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  z-index: 200;
  animation: fade-in 0.15s ease-out;
}
