/* Tides: two fields of colour and very little text.
   The scene's colours are computed in visualSystem.ts (OKLab, see docs/turrell/PALETTE.md) and
   written as one gradient on #visual-container; this file only lays things out.
   Modern browsers only. */

:root {
  /* The visible screen height, measured in main.ts (fitToScreen). 100vh until then. */
  --app-height: 100vh;

  /* Water line, from the top of the scene */
  --horizon-position: 50%;
  --horizon-shimmer: 0; /* 0 = calm, 1 = windy */

  /* Text colours: each a tint of the colour behind it (visualSystem.ts updateInk).
     Quiet fallbacks until the first state arrives. */
  --ink-state: rgba(255, 255, 255, 0.5);
  --ink-high: rgba(255, 255, 255, 0.4);
  --ink-weather: rgba(255, 255, 255, 0.4);
  --ink-staff-high: rgba(255, 255, 255, 0.35);
  --ink-staff-low: rgba(255, 255, 255, 0.35);
  --ink-depth: rgba(255, 255, 255, 0.35);
  --ink-sound-on: rgba(255, 255, 255, 0.8);
  --ink-sound-off: rgba(255, 255, 255, 0.3);
  /* Text colour glides; long enough that a flip from dark to light tint at dusk is a fade */
  --ink-glide: 6s;

  /* Timing (set from config.ts at startup) */
  --tick: 2000ms;       /* data update interval; the water line glides over the same time */
  --reveal: 2.5s;       /* fade from loading veil */
  --drift-px: 10px;
  --drift-period: 13min;

  /* Spacing and type */
  --pad: clamp(1.25rem, 3.2vw, 2.75rem);
  --pad-top: max(var(--pad), env(safe-area-inset-top));
  --pad-right: max(var(--pad), env(safe-area-inset-right));
  --pad-bottom: max(var(--pad), env(safe-area-inset-bottom));
  --pad-left: max(var(--pad), env(safe-area-inset-left));
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', 'Segoe UI', sans-serif;
}

/* Reset and base styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* The page behind the scene carries the scene's own bottom colour (set from JS each update),
   so no edge of any viewport can show a different colour. This is the loading dusk. */
html, body {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  overflow: hidden;
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  background-color: #16223a;
}

/* The scene is pinned to the top-left and sized to the real visible height (see fitToScreen
   in main.ts), not to height: 100%, which can come up 20 pt short in an iOS home-screen app
   with the black-translucent status bar */
#app {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: var(--app-height);
  overflow: hidden;
}

#visual-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  background-color: #16223a;
  transition: opacity var(--reveal) ease;
}

/* Fine random grain at ±1-2 levels (an image made once in visualSystem.ts addGrain) */
#grain {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-repeat: repeat;
  background-size: 128px 128px;
  pointer-events: none;
}

/* Water line layer - full height, moved by transform (compositor only, no layout per frame) */
#horizon-layer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform: translate3d(0, var(--horizon-position), 0);
  pointer-events: none;
  will-change: transform;
  transition: transform var(--tick) linear, opacity var(--reveal) ease;
}

/* The edge between sky and sea is drawn by the colour itself; this is only a faint glint on
   it, which the wind sets shimmering. Round 2: a soft 8 px lens rather than a 1 px line, so it
   reads as light on the water, not a drawn edge */
#horizon-line {
  position: absolute;
  left: 0;
  right: 0;
  top: -4px;
  height: 8px;
  /* a soft lens of light: fades up and down here, and out to the sides through the mask */
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0));
  -webkit-mask-image: linear-gradient(to right, transparent, #000 50%, transparent);
  mask-image: linear-gradient(to right, transparent, #000 50%, transparent);
  filter: blur(calc(var(--horizon-shimmer) * 1.5px));
  animation: horizon-shimmer 7s ease-in-out infinite;
}

/* The shimmer scales with the wind (--horizon-shimmer, 0 = calm: no movement at all) */
@keyframes horizon-shimmer {
  0%, 100% { opacity: calc(1 - var(--horizon-shimmer) * 0.15); transform: translateY(calc(var(--horizon-shimmer) * -1px)); }
  33% { opacity: 1; transform: translateY(calc(var(--horizon-shimmer) * 1px)); }
  66% { opacity: calc(1 - var(--horizon-shimmer) * 0.1); transform: translateY(calc(var(--horizon-shimmer) * -0.5px)); }
}

/* Shared style for the small numbers on the tide staff */
#current-depth,
.depth-label {
  font-size: 0.75rem;
  font-weight: 400;
  letter-spacing: 0.06em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  transition: color var(--ink-glide) linear, opacity 3s ease;
}

/* Current height, just under the water line, lined up with the staff labels */
#current-depth {
  position: absolute;
  right: calc(var(--pad-right) + 1rem);
  top: 0.5rem;
  color: var(--ink-depth);
}

/* The tide staff: today's high and low at their true heights on the scale (scale.ts) */
#depth-scale {
  position: absolute;
  right: var(--pad-right);
  top: 0;
  bottom: 0;
  width: 1px;
  pointer-events: none;
  transition: opacity 3s ease;
  animation: drift-x calc(var(--drift-period) * 1.3) ease-in-out infinite alternate;
}

.depth-marker {
  position: absolute;
  right: -0.375rem;
  width: 0.75rem;
  height: 1px;
  background: currentColor;
  transition: top var(--tick) linear;
}

/* The ticks sit exactly at their height on the scale; near a screen edge only the label is
   nudged inward (the --*-label-shift values) */
#high-tide-marker { top: var(--high-marker, 10%); color: var(--ink-staff-high); }
#low-tide-marker { top: var(--low-marker, 90%); color: var(--ink-staff-low); }
#high-tide-marker .depth-label { top: var(--high-label-shift, 0px); }
#low-tide-marker .depth-label { top: var(--low-label-shift, 0px); }

.depth-label {
  position: absolute;
  right: 1.375rem;
  top: 0;
  transform: translateY(-50%);
  color: inherit;
}

.depth-key {
  text-transform: uppercase;
  font-size: 0.625rem;
  letter-spacing: 0.18em;
  margin-right: 0.35em;
}

/* The text: tide state, next high, weather. Each line has its own tint (updateInk). */
#info-overlay {
  position: absolute;
  left: var(--pad-left);
  /* Leaves the right-hand column to the tide staff and its labels */
  max-width: min(24rem, calc(100vw - var(--pad-left) - var(--pad-right) - 7rem));
  z-index: 100;
  pointer-events: none;
  top: var(--pad-top);
  /* --info-shift moves the block below the water line when there's no room above it (JS) */
  translate: 0 var(--info-shift, 0px);
  transition: opacity 3s ease, translate var(--tick) linear;
  animation: drift var(--drift-period) ease-in-out infinite alternate;
}

.info-state,
.info-line,
.info-weather {
  transition: color var(--ink-glide) linear;
}

.info-state {
  color: var(--ink-state);
  font-size: clamp(1.75rem, 3.6vw, 2.75rem);
  font-weight: 300;
  letter-spacing: -0.01em;
  line-height: 1.1;
  margin: 0 0 0.6rem;
}

.info-line {
  color: var(--ink-high);
  font-size: clamp(0.875rem, 1.35vw, 1rem);
  font-weight: 400;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
}

.info-weather {
  color: var(--ink-weather);
  margin-top: 0.35rem;
  font-size: 0.8125rem;
  font-weight: 400;
  letter-spacing: 0.02em;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
}

.info-line:empty,
.info-weather:empty {
  display: none;
}

/* Switching sides of the water line: fade out, move, fade back in */
#info-overlay.is-moving {
  opacity: 0 !important;
  transition: opacity 0.8s ease !important;
}

#info-overlay.no-glide {
  transition: none !important;
}

/* Loading: a softly breathing line over the dusk, then the scene fades in */
.is-loading .info-state {
  animation: breathe 3.2s ease-in-out infinite;
}

.is-loading #visual-container,
.is-loading #horizon-layer,
.is-loading #depth-scale {
  opacity: 0;
}

@keyframes breathe {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 0.9; }
}

/* Unknown tide: the staff and height reading step back, since their numbers would be invented */
.tide-unknown #depth-scale,
.tide-unknown #current-depth {
  opacity: 0;
}

.depth-crowded #current-depth {
  opacity: 0;
}

/* Sound button: the icon alone. Lit when the sound is on, dim when it's off. Nothing else. */
#audio-toggle {
  position: absolute;
  left: var(--pad-left);
  bottom: var(--pad-bottom);
  width: 2.75rem;
  height: 2.75rem;
  margin-left: -0.75rem; /* the icon, not the tap area, lines up with the text */
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-appearance: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-sound-off);
  cursor: pointer;
  z-index: 100;
  transition: color 1.2s ease;
  animation: drift calc(var(--drift-period) * 0.8) ease-in-out infinite alternate-reverse;
}

#audio-toggle[data-state='on'] {
  color: var(--ink-sound-on);
}

#audio-toggle:focus {
  outline: none;
}

#audio-toggle:focus-visible {
  outline: 1px solid currentColor;
  outline-offset: 2px;
}

.audio-icon {
  width: 1.25rem;
  height: 1.25rem;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Slow drift so nothing sits on the same pixels of an always-on screen for hours */
@keyframes drift {
  0% { transform: translate3d(calc(var(--drift-px) * -1), calc(var(--drift-px) * -0.6), 0); }
  50% { transform: translate3d(calc(var(--drift-px) * 0.4), calc(var(--drift-px) * 0.8), 0); }
  100% { transform: translate3d(var(--drift-px), calc(var(--drift-px) * -0.2), 0); }
}

@keyframes drift-x {
  from { transform: translate3d(calc(var(--drift-px) * -0.8), 0, 0); }
  to { transform: translate3d(calc(var(--drift-px) * 0.8), 0, 0); }
}

/* Nightly refresh (kiosk.ts): fade to dark before reloading */
.is-leaving #app {
  opacity: 0;
  transition: opacity 3s ease;
}

/* Responsive adjustments */
@media (max-width: 600px) {
  .info-weather {
    font-size: 0.75rem;
  }

  #current-depth,
  .depth-label {
    font-size: 0.6875rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  #horizon-line,
  .is-loading .info-state {
    animation: none;
  }
}
