
@property --cell { syntax: "<length>"; inherits: true; initial-value: 64px; }
@property --trace { syntax: "<angle>"; inherits: false; initial-value: 360deg; }

:root {
  --grid-eye: #6a74ff;
  --grid-hue: #4951b6;
  --grid-light: var(--grid-hue);
  --tile-bg: rgba(255, 255, 255, 0.72);
  --tile-edge: rgba(20, 24, 40, 0.14);
  --tile-hot: var(--grid-light);
  --tile-mark: rgba(20, 24, 40, 0.38);
  --hud: rgba(20, 24, 40, 0.5);
  --lattice: rgba(20, 24, 40, 0.07);
}
:root[data-theme="dark"] {
  --tile-bg: rgba(12, 14, 22, 0.62);
  --tile-edge: rgba(150, 170, 255, 0.16);
  --grid-light: var(--grid-eye);
  --tile-hot: var(--grid-light);
  --tile-mark: rgba(220, 228, 255, 0.5);
  --hud: rgba(220, 228, 255, 0.5);
  --lattice: rgba(150, 170, 255, 0.09);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --tile-bg: rgba(12, 14, 22, 0.62);
    --tile-edge: rgba(150, 170, 255, 0.16);
    --grid-light: var(--grid-eye);
    --tile-hot: var(--grid-light);
    --tile-mark: rgba(220, 228, 255, 0.5);
    --hud: rgba(220, 228, 255, 0.5);
    --lattice: rgba(150, 170, 255, 0.09);
  }
}

.grid-field { position: fixed; inset: 0; width: 100vw; height: 100vh; z-index: 0; pointer-events: none; }

[data-grid-board] {
  --cell-raw: clamp(44px, min(calc((100vw - 64px) / 12), calc((100svh - var(--chrome, 150px)) / var(--first-rows, 12))), 80px);
  --cell: var(--cell-raw);
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(12, var(--cell));
  grid-auto-rows: var(--cell);
  justify-content: center;
  align-content: start;
  width: max-content;
  max-width: 100%;
  margin: 0 auto;
}

[data-tile] {
  position: relative;
  min-width: 0;
  box-sizing: border-box;
  border: 1px solid var(--tile-edge);
  background: var(--tile-bg);
  -webkit-backdrop-filter: blur(6px) saturate(1.2);
  backdrop-filter: blur(6px) saturate(1.2);
  padding: calc(var(--cell) * 0.22);
}
[data-tile].bare { background: transparent; border-color: transparent; -webkit-backdrop-filter: none; backdrop-filter: none; padding: 0; }
[data-tile]::after {
  content: "";
  position: absolute;
  inset: -1px;
  pointer-events: none;
  --m: var(--tile-mark);
  background:
    linear-gradient(var(--m), var(--m)) top left / 12px 1.5px no-repeat,
    linear-gradient(var(--m), var(--m)) top left / 1.5px 12px no-repeat,
    linear-gradient(var(--m), var(--m)) top right / 12px 1.5px no-repeat,
    linear-gradient(var(--m), var(--m)) top right / 1.5px 12px no-repeat,
    linear-gradient(var(--m), var(--m)) bottom left / 12px 1.5px no-repeat,
    linear-gradient(var(--m), var(--m)) bottom left / 1.5px 12px no-repeat,
    linear-gradient(var(--m), var(--m)) bottom right / 12px 1.5px no-repeat,
    linear-gradient(var(--m), var(--m)) bottom right / 1.5px 12px no-repeat;
}
[data-tile]::before {
  content: "";
  position: absolute;
  inset: -1px;
  border: 1.5px solid var(--tile-hot);
  pointer-events: none;
  opacity: 0;
  -webkit-mask: conic-gradient(from -90deg, #000 var(--trace), transparent var(--trace));
  mask: conic-gradient(from -90deg, #000 var(--trace), transparent var(--trace));
}
.hud { font: 600 10.5px/1 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.16em; text-transform: uppercase; color: var(--hud); margin: 0 0 8px; }

html.grid-boot [data-tile]:not([data-boot-on-view]) { opacity: 0; transform: translateY(10px) scale(0.985); }
html.grid-boot.grid-landing [data-tile]:not([data-boot-on-view]) { animation: grid-land 0.7s cubic-bezier(0.2, 0.7, 0.2, 1) var(--land, 1s) both; }
html.grid-boot.grid-landing [data-tile]:not([data-boot-on-view])::before {
  animation: grid-trace 0.75s cubic-bezier(0.4, 0, 0.2, 1) var(--land, 1s) both, grid-cool 0.9s ease-out calc(var(--land, 1s) + 0.75s) both;
}
@keyframes grid-land { from { opacity: 0; transform: translateY(10px) scale(0.985); } to { opacity: 1; transform: none; } }
@keyframes grid-trace { from { --trace: 0deg; opacity: 1; } to { --trace: 360deg; opacity: 1; } }
@keyframes grid-cool { from { opacity: 1; } to { opacity: 0; } }
html.grid-boot:not(.grid-landing) [data-tile] { animation: grid-land 0.01s linear 3s both; }
html.grid-landed [data-tile]:not([data-boot-on-view]) { opacity: 1; transform: none; }

html.grid-live [data-boot-on-view]:not(.tile-in) { opacity: 0; transform: translateY(14px) scale(0.985); }
html.grid-live [data-boot-on-view].tile-in { animation: grid-land 0.7s cubic-bezier(0.2, 0.7, 0.2, 1) var(--view-delay, 0s) both; }
html.grid-live [data-boot-on-view].tile-in::before {
  animation: grid-trace 0.75s cubic-bezier(0.4, 0, 0.2, 1) var(--view-delay, 0s) both, grid-cool 0.9s ease-out calc(var(--view-delay, 0s) + 0.75s) both;
}

html.grid-field-off [data-grid-board],
html:not(.grid-js) [data-grid-board] {
  background-image:
    linear-gradient(to right, var(--lattice) 1px, transparent 1px),
    linear-gradient(to bottom, var(--lattice) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  box-shadow: inset -1px -1px 0 var(--lattice);
}
html:not(.grid-snapped) [data-grid-board] { grid-auto-rows: auto; }
html:not(.grid-js) [data-grid-board] { background-image: linear-gradient(to right, var(--lattice) 1px, transparent 1px); }

@media (max-width: 900px) {
  [data-grid-board] { --cell-raw: calc((100vw - 32px) / 6); grid-template-columns: repeat(6, var(--cell)); }
}

/* A PHONE TURNED SIDEWAYS. Up to 900 px wide the board has six columns sized by the WIDTH alone, so a phone on its
   side (844 x 390) got 135-px cells: a 675-px orb tile in a 390-px screen, and nothing else in view (measured). On a
   short, wide screen the cell is held by the height too, as the desktop board's is, so the orb, the greeting and the
   chat bar keep their phone proportions. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 600px) {
  [data-grid-board] { --cell-raw: max(44px, min(calc((100vw - 32px) / 6), calc((100svh - 72px) / 6))); }
}
@supports (width: round(down, 10.5px, 1px)) {
  @media (max-width: 900px) {
    [data-grid-board] { --cell: round(down, var(--cell-raw), 1px); }
  }
}

html.grid-boot .gw { opacity: 0; }
html.grid-boot.grid-landing .gw { animation: grid-write 0.55s ease-out var(--gd, 1.2s) both; }
@keyframes grid-write {
  0% { opacity: 0; color: var(--grid-light); text-shadow: 0 0 14px var(--grid-light); }
  35% { opacity: 1; color: var(--grid-light); text-shadow: 0 0 10px var(--grid-light); }
  100% { opacity: 1; color: inherit; text-shadow: none; }
}

[data-grid-mode="water"] [data-tile] {
  border-color: transparent !important; background-color: transparent !important; box-shadow: none !important;
  -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
}
[data-grid-mode="water"] [data-tile]::after, [data-grid-mode="water"] [data-tile]::before { display: none !important; }

html.grid-clean.grid-field-off [data-grid-board], html.grid-clean:not(.grid-js) [data-grid-board] { background-image: none; box-shadow: none; }
html.grid-paused .aperture::before, html.grid-paused .dl__ribbon::after { animation-play-state: paused !important; }

@media (prefers-reduced-motion: reduce) {
  html.grid-boot [data-tile], html.grid-boot.grid-landing [data-tile] { opacity: 1; transform: none; animation: none; }
  html.grid-boot.grid-landing [data-tile]::before { animation: none; }
  html.grid-live [data-boot-on-view], html.grid-live [data-boot-on-view].tile-in { opacity: 1; transform: none; animation: none; }
  html.grid-live [data-boot-on-view].tile-in::before { animation: none; }
}
