/* ============================================
   CSS Variables & Theme
   ============================================ */
:root {
  --bg-primary: #0a0e14;
  --bg-secondary: #111827;
  --bg-tertiary: #1f2937;
  --text-primary: #f0f6fc;
  --text-secondary: #8b949e;
  --text-muted: #484f58;
  --accent: #3b82f6;
  --accent-glow: rgba(59, 130, 246, 0.35);
  --border: #30363d;
  --btn-bg: #1f2937;
  --btn-hover: #2d3748;
  --overlay-bg: rgba(0, 0, 0, 0.88);

  /* Pipe game specifics */
  --tile-bg: #161d2b;
  --tile-border: #1e2a3a;
  --pipe-color: #475569;
  --pipe-width: 30%;
  --source-color: #22c55e;
  --source-glow: rgba(34, 197, 94, 0.4);
  --drain-color: #ef4444;
  --drain-glow: rgba(239, 68, 68, 0.4);
  --water-color: #3b82f6;
  --water-glow: rgba(59, 130, 246, 0.5);

  --transition-rotate: 0.18s ease;
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Pretendard', 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ============================================
   App Shell
   ============================================ */
#app {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ============================================
   Screens
   ============================================ */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.screen.hidden {
  display: none;
}

/* ============================================
   Start Screen
   ============================================ */
.screen-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 32px 24px;
  width: 100%;
  max-width: 400px;
}

/* Logo */
.game-logo {
  display: flex;
  align-items: center;
  gap: 0;
  margin-bottom: 4px;
}

.logo-pipe {
  width: 48px;
  height: 48px;
  position: relative;
  background: var(--bg-tertiary);
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* Horizontal logo pipe */
.logo-h::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 30%;
  transform: translateY(-50%);
  background: var(--accent);
  border-radius: 2px;
}

/* Cross logo pipe */
.logo-cross::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 30%;
  transform: translateX(-50%);
  background: var(--accent);
  border-radius: 2px;
}
.logo-cross::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 30%;
  transform: translateY(-50%);
  background: var(--accent);
  border-radius: 2px;
}

/* Titles */
.game-title {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text-primary);
}

.game-desc {
  font-size: 0.9rem;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.5;
}

/* Difficulty buttons */
.difficulty-group {
  display: flex;
  gap: 10px;
  width: 100%;
}

.diff-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 14px 8px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.diff-btn:hover {
  border-color: var(--accent);
  background: var(--btn-hover);
  color: var(--text-primary);
}

.diff-btn.active {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.15);
  color: var(--text-primary);
}

.diff-label {
  font-size: 0.95rem;
  font-weight: 600;
}

.diff-grid {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.diff-btn.active .diff-grid {
  color: var(--accent);
}

/* Primary / Secondary buttons */
.primary-btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}

.primary-btn:hover { opacity: 0.9; }
.primary-btn:active { transform: scale(0.98); }

.secondary-btn {
  flex: 1;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--btn-bg);
  color: var(--text-primary);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

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

/* Best records */
.best-records {
  display: flex;
  gap: 16px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.best-item span {
  color: var(--text-secondary);
}

/* ============================================
   Game Screen
   ============================================ */
#game-screen {
  flex-direction: column;
}

/* Header */
#game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 540px;
  padding: 12px 16px;
  flex-shrink: 0;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text-primary);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

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

.stats {
  display: flex;
  gap: 24px;
}

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

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* Grid wrapper */
#grid-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 8px 16px;
  min-height: 0;
}

/* Grid */
#grid {
  display: grid;
  gap: 3px;
  /* columns / rows set via JS */
}

/* Hint bar */
#hint-bar {
  padding: 10px 16px;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-align: center;
  flex-shrink: 0;
}

/* ============================================
   Pipe Tiles
   ============================================ */
.tile {
  position: relative;
  background: var(--tile-bg);
  border: 1px solid var(--tile-border);
  border-radius: 6px;
  cursor: pointer;
  overflow: hidden;
  /* size set via JS */
  transition: background 0.3s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.tile:hover {
  border-color: rgba(59, 130, 246, 0.4);
}

.tile:active {
  background: #1a2436;
}

/* Pipe segments - four possible openings: top, right, bottom, left */
/* Each segment is a CSS element positioned at the edge */

/* Shared pipe arm style via data attributes */
.tile .pipe-arm {
  position: absolute;
  background: var(--pipe-color);
  border-radius: 2px;
  transition: background 0.4s, transform 0.18s ease;
}

/* Horizontal arm (left or right) */
.tile .arm-left {
  top: 50%;
  left: 0;
  width: 50%;
  height: var(--pipe-width);
  transform: translateY(-50%);
}

.tile .arm-right {
  top: 50%;
  right: 0;
  width: 50%;
  height: var(--pipe-width);
  transform: translateY(-50%);
}

/* Vertical arm (top or bottom) */
.tile .arm-top {
  left: 50%;
  top: 0;
  height: 50%;
  width: var(--pipe-width);
  transform: translateX(-50%);
}

.tile .arm-bottom {
  left: 50%;
  bottom: 0;
  height: 50%;
  width: var(--pipe-width);
  transform: translateX(-50%);
}

/* Center dot */
.tile .pipe-center {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--pipe-width);
  height: var(--pipe-width);
  transform: translate(-50%, -50%);
  background: var(--pipe-color);
  border-radius: 50%;
  transition: background 0.4s;
}

/* ---- State: source tile ---- */
.tile.source {
  background: rgba(34, 197, 94, 0.08);
  border-color: var(--source-color);
}

.tile.source .pipe-arm,
.tile.source .pipe-center {
  background: var(--source-color);
}

/* Source indicator triangle on left edge */
.tile.source::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 8px solid var(--source-color);
  z-index: 2;
}

/* ---- State: drain tile ---- */
.tile.drain {
  background: rgba(239, 68, 68, 0.08);
  border-color: var(--drain-color);
}

.tile.drain .pipe-arm,
.tile.drain .pipe-center {
  background: var(--drain-color);
}

/* Drain indicator on right edge */
.tile.drain::after {
  content: '';
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-right: 8px solid var(--drain-color);
  z-index: 2;
}

/* ---- State: water flowing ---- */
.tile.water {
  background: rgba(59, 130, 246, 0.12);
  border-color: rgba(59, 130, 246, 0.6);
}

.tile.water .pipe-arm,
.tile.water .pipe-center {
  background: var(--water-color);
}

.tile.water.source {
  background: rgba(59, 130, 246, 0.18);
  border-color: var(--water-color);
}

.tile.water.drain {
  background: rgba(59, 130, 246, 0.18);
  border-color: var(--water-color);
}

.tile.water.source .pipe-arm,
.tile.water.source .pipe-center,
.tile.water.drain .pipe-arm,
.tile.water.drain .pipe-center {
  background: var(--water-color);
}

/* ---- Rotation animation ---- */
.tile.rotating {
  animation: tile-rotate 0.18s ease;
}

@keyframes tile-rotate {
  0%   { transform: rotate(0deg); }
  100% { transform: rotate(90deg); }
}

/* ---- Water pulse glow on solved ---- */
.tile.water {
  animation: water-pulse 2s ease-in-out infinite;
}

@keyframes water-pulse {
  0%, 100% { box-shadow: none; }
  50%       { box-shadow: 0 0 8px var(--water-glow) inset; }
}

/* ---- Source / Drain don't animate after solve ---- */
.tile.water.source,
.tile.water.drain {
  animation: none;
  box-shadow: 0 0 12px var(--water-glow);
}

/* ============================================
   Victory Overlay
   ============================================ */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fade-in 0.25s ease;
}

.overlay.hidden {
  display: none;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.overlay-box {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  animation: slide-up 0.3s ease;
}

@keyframes slide-up {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

.victory-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: rgba(59, 130, 246, 0.15);
  border: 2px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  color: var(--accent);
}

.victory-title {
  font-size: 1.6rem;
  font-weight: 700;
}

.result-rows {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 14px 0;
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-label {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.result-value {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.new-record {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.05em;
  padding: 6px 14px;
  border: 1px solid var(--accent);
  border-radius: 20px;
  background: rgba(59, 130, 246, 0.12);
}

.new-record.hidden { display: none; }

.overlay-btns {
  display: flex;
  gap: 10px;
  width: 100%;
}

.overlay-btns .primary-btn {
  flex: 1.4;
}

/* ============================================
   Responsive sizing
   ============================================ */
@media (max-width: 400px) {
  .screen-content {
    padding: 20px 16px;
    gap: 16px;
  }

  .game-title { font-size: 1.6rem; }
  .best-records { flex-direction: column; gap: 4px; align-items: center; }
}

@media (max-height: 600px) {
  .screen-content { gap: 12px; }
  .game-logo { display: none; }
  #hint-bar { display: none; }
}
