* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

:root {
  --bg: #f2f2f7;
  --white: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #8e8e93;
  --accent: #007aff;
  --green: #34c759;
  --red: #ff3b30;
  --orange: #ff9500;
  --tile-size: min(70px, calc((100vw - 80px) / 4));
  --tile-gap: 8px;
  --radius: 14px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.12);
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 420px;
  height: 100%;
  position: relative;
  overflow: hidden;
}

/* Screens */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
}

/* Start Screen */
#start-screen {
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px;
}

.logo h1 {
  font-size: 42px;
  font-weight: 800;
  text-align: center;
  letter-spacing: -1px;
  background: linear-gradient(135deg, #007aff, #5856d6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  text-align: center;
  color: var(--text-secondary);
  font-size: 17px;
  margin-top: 4px;
  margin-bottom: 24px;
}

/* Buttons */
.btn {
  width: 100%;
  max-width: 280px;
  padding: 16px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.2s ease;
}

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

.btn-primary {
  background: linear-gradient(135deg, #007aff, #5856d6);
  color: white;
  box-shadow: var(--shadow-lg);
}

.btn-secondary {
  background: var(--white);
  color: var(--accent);
  box-shadow: var(--shadow);
}

/* Game Header */
.game-header {
  padding: 12px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: max(12px, env(safe-area-inset-top));
}

.score-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.score-label {
  font-size: 15px;
  color: var(--text-secondary);
  font-weight: 500;
}

.score-value {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.timer-container {
  width: 100%;
  height: 6px;
  background: #e5e5ea;
  border-radius: 3px;
  overflow: hidden;
}

.timer-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--green), var(--accent));
  border-radius: 3px;
  transition: width 0.25s linear;
}

.timer-bar.warning {
  background: linear-gradient(90deg, var(--orange), var(--red));
}

.timer-text {
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* Current Word */
.current-word {
  text-align: center;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 3px;
  min-height: 40px;
  padding: 4px 16px;
  color: var(--text);
  text-transform: uppercase;
}

.current-word.valid {
  color: var(--green);
}

.current-word.invalid {
  color: var(--text-secondary);
}

/* Grid */
.grid-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  touch-action: none;
}

.line-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.line-svg line {
  stroke: rgba(0, 122, 255, 0.5);
  stroke-width: 8;
  stroke-linecap: round;
  filter: drop-shadow(0 1px 2px rgba(0, 122, 255, 0.3));
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, var(--tile-size));
  grid-template-rows: repeat(4, var(--tile-size));
  gap: var(--tile-gap);
  position: relative;
  z-index: 2;
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.45);
  font-weight: 700;
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-transform: uppercase;
  transition: transform 0.1s ease, background 0.15s ease, box-shadow 0.15s ease;
  cursor: pointer;
}

.tile.selected {
  background: var(--accent);
  color: white;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.3), var(--shadow-lg);
  transform: scale(1.08);
}

.tile.flash-valid {
  background: var(--green);
  color: white;
  transform: scale(1.12);
}

.tile.flash-invalid {
  background: var(--red);
  color: white;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.shake {
  animation: shake 0.4s ease;
}

/* Found Words */
.words-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 8px 16px;
  min-height: 0;
  overflow: hidden;
}

.words-header {
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 600;
  padding-bottom: 6px;
}

.found-words {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom);
}

.word-chip {
  padding: 5px 10px;
  background: var(--white);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.word-chip .word-pts {
  color: var(--accent);
  font-weight: 700;
  margin-left: 4px;
  font-size: 11px;
}

@keyframes chipAppear {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.word-chip {
  animation: chipAppear 0.2s ease;
}

/* Results Screen */
#results-screen {
  align-items: center;
  padding: 40px 24px;
  gap: 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.results-title {
  font-size: 32px;
  font-weight: 800;
}

.results-score {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.results-score-label {
  font-size: 15px;
  color: var(--text-secondary);
}

.results-score-value {
  font-size: 56px;
  font-weight: 800;
  background: linear-gradient(135deg, #007aff, #5856d6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.results-stats {
  display: flex;
  gap: 32px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
}

.stat-label {
  font-size: 13px;
  color: var(--text-secondary);
}

.results-words {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  max-width: 100%;
  padding: 8px 0;
}

.results-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 280px;
  margin-top: 8px;
}

/* Haptic pulse for valid word */
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.5); }
  100% { box-shadow: 0 0 0 20px rgba(52, 199, 89, 0); }
}

/* Responsive */
@media (max-height: 650px) {
  :root {
    --tile-size: min(60px, calc((100vw - 80px) / 4));
  }
  .current-word { font-size: 22px; min-height: 32px; }
  .score-value { font-size: 24px; }
  .game-header { padding: 8px 16px 4px; gap: 4px; }
}

@media (min-width: 500px) {
  :root {
    --tile-size: 80px;
    --tile-gap: 10px;
  }
}
