/* Falaj. The whole look is one idea: dry sand until the water arrives, and
   water is the only bright thing on the screen. */

:root {
  --night: #0d1b24;
  --night-deep: #081219;
  --line: rgba(173, 203, 219, 0.16);
  --ink: #e7f0f5;
  --muted: #8ea7b4;
  --sand: #c9a46b;
  --sand-soft: rgba(201, 164, 107, 0.22);
  --dry: #51606a;
  --water: #4fc3df;
  --water-deep: #1d7f9c;
  --spill: #d98a4a;
  --palm: #5fae72;
  --radius: 14px;
  --tile: 56px;
  --gap: 2px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(120% 80% at 50% 0%, #11242f 0%, var(--night) 46%, var(--night-deep) 100%);
  color: var(--ink);
  font: 500 16px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

[hidden] { display: none !important; }

/* Header */

.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  flex: none;
}

.home {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--line);
  color: var(--muted);
  text-decoration: none;
  flex: none;
}

.home:hover, .home:focus-visible { color: var(--ink); border-color: var(--sand); }

.bar h1 {
  font-size: 18px;
  letter-spacing: 0.02em;
  margin: 0;
  flex: 1;
}

.stats {
  display: flex;
  gap: 18px;
  margin: 0;
  text-align: right;
}

.stats div { display: block; }
.stats dt {
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.stats dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
  font-size: 17px;
  font-weight: 700;
}

/* Size picker */

.sizes {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px 4px;
  flex: none;
}

.sizes button {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 16px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}

.sizes button[aria-pressed="true"] {
  color: var(--sand);
  border-color: var(--sand);
  background: var(--sand-soft);
}

.sizes button:hover { color: var(--ink); }

/* Board */

#stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 10px;
}

#board {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(var(--cols), var(--tile));
  grid-auto-rows: var(--tile);
  touch-action: manipulation;
}

.hint {
  margin: 0;
  max-width: 44ch;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* Tiles. The drawing inside is always in its finished orientation; turning a
   piece turns the whole drawing, which is what makes the movement read as one
   solid thing swinging round rather than lines redrawing themselves. */

.tile {
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  background: none;
  width: var(--tile);
  height: var(--tile);
  cursor: pointer;
  position: relative;
  border-radius: 4px;
}

.tile svg {
  width: 100%;
  height: 100%;
  display: block;
  transition: transform 160ms cubic-bezier(0.22, 0.9, 0.3, 1);
}

.tile:focus-visible {
  outline: 2px solid var(--sand);
  outline-offset: -2px;
}

.channel {
  fill: none;
  stroke: var(--dry);
  stroke-width: 15;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke 220ms ease;
}

.hub { fill: var(--dry); transition: fill 220ms ease; }

.tile.wet .channel { stroke: var(--water); }
.tile.wet .hub { fill: var(--water); }

/* Water running out into the sand. Marked at the exact opening it escapes
   from rather than by colouring the whole piece: the player needs to know
   which way it is going, not merely that something is wrong somewhere. */
.cap {
  fill: var(--spill);
  opacity: 0;
  transition: opacity 180ms ease;
}
.cap.on { opacity: 1; }

.well-mark { fill: var(--water-deep); stroke: var(--water); stroke-width: 6; }
.tile.wet .well-mark { fill: var(--water); }

/* The plot the palm is planted in, drawn over the channel so the tree is
   never crossed by it. */
.plot {
  fill: #16222a;
  stroke: var(--sand-soft);
  stroke-width: 3;
  transition: fill 260ms ease, stroke 260ms ease;
}
.tile.wet .plot { fill: #142a24; stroke: rgba(95, 174, 114, 0.45); }

/* Turned back against the tile's own rotation. transform-box makes the
   origin the middle of the drawing rather than the middle of its own bounding
   box, which would swing the tree off to one side. */
.upright {
  transform-box: view-box;
  transform-origin: 50px 50px;
  transition: transform 160ms cubic-bezier(0.22, 0.9, 0.3, 1);
}

.palm-mark {
  fill: none;
  stroke: var(--dry);
  stroke-width: 5.5;
  stroke-linecap: round;
  transition: stroke 260ms ease;
}
.palm-mark.trunk { stroke-width: 6.5; }
.tile.wet .palm-mark { stroke: var(--palm); }

.tile.solved svg { animation: settle 420ms ease; }

@keyframes settle {
  0% { transform: scale(1); }
  45% { transform: scale(1.07); }
  100% { transform: scale(1); }
}

/* Tools */

.tools {
  display: flex;
  gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  flex: none;
}

.tools button {
  appearance: none;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--ink);
  border-radius: 12px;
  padding: 11px 10px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}

.tools button:hover:not(:disabled) { border-color: var(--sand); color: var(--sand); }
.tools button:disabled { opacity: 0.38; cursor: default; }
.tools button[aria-pressed="true"] { border-color: var(--sand); color: var(--sand); background: var(--sand-soft); }

/* Win panel */

.panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(6, 12, 17, 0.72);
  z-index: 20;
  padding: 20px;
}

.panel-inner {
  width: min(380px, 100%);
  background: #11232e;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
}

.panel-inner h2 { margin: 0 0 8px; font-size: 20px; }
.panel-inner p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }

.panel-actions { display: flex; gap: 8px; }

.panel-actions button {
  appearance: none;
  flex: 1;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--ink);
  border-radius: 10px;
  padding: 11px;
  font: inherit;
  cursor: pointer;
}

.panel-actions .primary {
  background: var(--sand);
  border-color: var(--sand);
  color: #22160a;
  font-weight: 700;
}

@media (max-width: 420px) {
  .stats { gap: 12px; }
  .bar h1 { font-size: 16px; }
  .hint { font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile svg, .channel, .hub, .palm-mark, .upright { transition: none; }
  .tile.solved svg { animation: none; }
}
