/*
  The stylesheet of the game page and the Help page. It follows
  dancroak.com/web/css: six cascade layers in one order, the shared
  token names, and width breakpoints written as ranges.

  Each page wears a class on its <html> element, because the two pages
  differ from the root down: .game-page (index.html) and .help-page
  (help.html). The Help page links /style.css, which the server sends
  for every build, and carries no style element.

  main.js writes three custom properties and reads one:
  --viewport--inset-top, -right, -bottom, and -left on the root,
  --timer--progress on .timer-display, and --color--sand for the beach
  that the canvas draws.
*/

@layer reset, tokens, base, components, utilities, print;

@layer reset {
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
}

@layer tokens {
  :root {
    --color--text: #f4f7ff;
    --color--bg: #050d22;
    --color--board: #6e809b;
    --color--sand: #c4a35a;
    --color--hud: var(--color--bg);
    --color--timer: var(--color--bg);
    --color--timer-track: rgba(5, 13, 34, 0.24);
    --color--button: #0079ff;
    --color--button-hover: #5c99ff;
    --color--button-text: white;
    --color--overlay-backdrop: rgba(7, 13, 30, 0.24);
    --color--overlay-bg: var(--color--bg);
    --color--overlay-text: #e9f0ff;
    --color--overlay-button: #2f70dd;
    --color--overlay-button-hover: #275fc0;

    --font-family--base:
      -apple-system,
      BlinkMacSystemFont,
      "Segoe UI",
      Roboto,
      Helvetica,
      Arial,
      sans-serif;
    --font-size--x-small: 0.7rem;
    --font-size--small: 14px;
    --font-size--base: 16px;
    --font-size--large: 18px;

    --spacing--small: 8px;
    --spacing--medium-small: 12px;
    --spacing--medium: 16px;

    /* main.js sets each inset from window.visualViewport. */
    --viewport--inset-top: 0px;
    --viewport--inset-right: 0px;
    --viewport--inset-bottom: 0px;
    --viewport--inset-left: 0px;
    --safe-area--inset-top: max(
      env(safe-area-inset-top),
      var(--viewport--inset-top)
    );
    --safe-area--inset-right: max(
      env(safe-area-inset-right),
      var(--viewport--inset-right)
    );
    --safe-area--inset-bottom: max(
      env(safe-area-inset-bottom),
      var(--viewport--inset-bottom)
    );
    --safe-area--inset-left: max(
      env(safe-area-inset-left),
      var(--viewport--inset-left)
    );

    /* The board takes the height that the page chrome leaves. Off the
       spacing scale: 10px, 18px, and 28px fit the title and the footer
       around the board. */
    --layout--app-padding-top: calc(
      var(--safe-area--inset-top) + var(--spacing--small)
    );
    --layout--app-padding-bottom: calc(
      var(--safe-area--inset-bottom) + var(--spacing--small)
    );
    --layout--app-padding-inline: var(--spacing--medium-small);
    --layout--shell-gap: 10px;
    --layout--footer-reserve: 28px;
    --layout--board-inline-gutter: 10px;
    --layout--board-top-gutter: var(--spacing--small);
    --layout--board-bottom-gutter: 18px;
    --layout--board-ui-reserve: calc(
      var(--layout--app-padding-top) + var(--layout--app-padding-bottom) +
        var(--layout--shell-gap) +
        var(--layout--footer-reserve) +
        var(--layout--board-top-gutter) +
        var(--layout--board-bottom-gutter)
    );
  }
}

@layer base {
  button {
    border: none;
    border-radius: 8px;
    background: var(--color--button);
    color: var(--color--button-text);
    font-weight: 800;
    font-size: var(--font-size--base);
    padding: var(--spacing--medium-small) 32px;
  }

  button:hover {
    background: var(--color--button-hover);
  }

  button:active {
    transform: none;
  }

  button:focus-visible {
    outline: 2px solid rgba(187, 210, 255, 0.95);
    outline-offset: 2px;
  }
}

@layer components {
  /* The sheet goes to the browser as written, with no build step to
     flatten nesting, so each rule names its whole selector. */
  .game-page {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 100%;
    overflow-x: hidden;
    background: var(--color--bg);
    -webkit-tap-highlight-color: transparent;
  }

  .game-page body {
    position: relative;
    width: 100%;
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    font-family: var(--font-family--base);
    color: var(--color--text);
    background: var(--color--bg);
  }

  .game-page header {
    text-align: center;
    margin-bottom: 0.5rem;
  }

  /* 0.85rem with wide letter spacing: the title, off the type scale. */
  .game-page header h1 {
    font-size: 0.85rem;
    font-weight: 600;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--color--text);
  }

  .game-page header h1::after {
    content: "";
    display: block;
    width: 24px;
    height: 2px;
    margin: 6px auto 0;
    background: var(--color--sand);
  }

  .game-page footer {
    text-align: center;
    margin-top: 0.25rem;
  }

  .game-page footer p {
    font-size: var(--font-size--x-small);
    font-weight: 300;
    color: var(--color--sand);
  }

  .game-page footer a {
    color: var(--color--sand);
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-color: var(--color--sand);
    text-underline-offset: 2px;
    transition: color 0.2s, text-decoration-color 0.2s;
  }

  .game-page footer a:hover {
    color: var(--color--sand);
    text-decoration-style: solid;
    text-decoration-color: var(--color--sand);
  }

  .app {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 820px;
    margin: 0 auto;
    min-height: 100%;
    padding: var(--layout--app-padding-top)
      calc(var(--layout--app-padding-inline) + var(--safe-area--inset-right))
      var(--layout--app-padding-bottom)
      calc(var(--layout--app-padding-inline) + var(--safe-area--inset-left));
    display: grid;
    grid-template-rows: minmax(0, 1fr);
  }

  .game-container {
    position: relative;
    display: grid;
    place-items: start center;
    min-height: 0;
    padding: var(--layout--board-top-gutter) 0
      var(--layout--board-bottom-gutter);
  }

  .game-shell {
    display: grid;
    width: 100%;
    grid-template-rows: auto auto;
    gap: var(--layout--shell-gap);
    align-content: start;
    justify-items: center;
    opacity: 1;
    transition: opacity 140ms ease;
  }

  .canvas-wrap {
    position: relative;
    height: min(700px, calc(100vh - var(--layout--board-ui-reserve)));
    height: min(700px, calc(100dvh - var(--layout--board-ui-reserve)));
    height: min(700px, calc(100svh - var(--layout--board-ui-reserve)));
    aspect-ratio: 39 / 70;
    width: auto;
    max-width: calc(100% - var(--layout--board-inline-gutter) * 2);
    margin-inline: auto;
    overflow: hidden;
    border: none;
    background: var(--color--board);
    touch-action: none;
  }

  .game-canvas {
    display: block;
    width: 100%;
    height: 100%;
  }

  /* 30px, and 24px on a phone: the HUD fits the beach strip of the
     board, off the type scale. */
  .score-display {
    position: absolute;
    top: auto;
    bottom: calc((58px - 1em) / 2 - 4px);
    right: 14px;
    font-size: 30px;
    line-height: 1;
    font-weight: 600;
    color: var(--color--hud);
    text-shadow: none;
    transition: color 260ms ease;
    pointer-events: none;
  }

  .timer-display {
    --timer--progress: 0;
    --timer--size: 28px;
    --timer--ring: 4px;
    position: absolute;
    top: auto;
    bottom: calc((58px - var(--timer--size)) / 2 - 4px);
    left: 14px;
    width: var(--timer--size);
    height: var(--timer--size);
    border-radius: 50%;
    overflow: hidden;
    background: conic-gradient(
      from -90deg,
      var(--color--timer) calc(var(--timer--progress) * 1turn),
      var(--color--timer-track) 0
    );
    border: none;
    -webkit-mask: radial-gradient(
      farthest-side,
      transparent calc(100% - var(--timer--ring)),
      #000 calc(100% - var(--timer--ring))
    );
    mask: radial-gradient(
      farthest-side,
      transparent calc(100% - var(--timer--ring)),
      #000 calc(100% - var(--timer--ring))
    );
    opacity: 0.86;
    transition: background 120ms linear, opacity 180ms ease;
    pointer-events: none;
  }

  @media (width <= 540px) {
    .score-display {
      font-size: 24px;
    }

    .timer-display {
      --timer--size: 24px;
    }
  }

  .overlay {
    position: absolute;
    inset: 0;
    background: var(--color--overlay-backdrop);
    display: grid;
    place-items: center;
    padding: var(--spacing--medium) var(--spacing--medium-small);
  }

  .overlay-card {
    width: min(370px, 100%);
    background: var(--color--overlay-bg);
    color: var(--color--overlay-text);
    padding: 18px 14px 16px;
    text-align: center;
    border: none;
    border-radius: 0;
    box-shadow: none;
    display: grid;
    gap: var(--spacing--small);
  }

  .overlay-card h2 {
    margin: 0 0 5px 0;
    font-size: var(--font-size--large);
    line-height: 1.2;
    font-weight: 620;
  }

  .overlay-card h2:empty {
    display: none;
  }

  .overlay-card .overlay-description {
    margin: 0 2px 6px;
    font-size: var(--font-size--small);
    color: rgba(255, 255, 255, 0.9);
    line-height: 1.35;
  }

  .overlay-card .overlay-score {
    margin: 0;
    font-size: var(--font-size--base);
    font-weight: 550;
    color: rgba(233, 241, 255, 0.97);
  }

  .overlay-card button {
    margin-top: 6px;
    justify-self: center;
    background: var(--color--overlay-button);
  }

  .overlay-card button:hover {
    background: var(--color--overlay-button-hover);
  }

  .help-page {
    --color--text: #2c3e50;
    --color--text-light: #6c757d;
    --color--bg: #f8f9fa;
    --color--link: #1a4a6e;
    --font-family--base:
      -apple-system,
      BlinkMacSystemFont,
      "Segoe UI",
      Roboto,
      sans-serif;
    min-height: 100%;
    background: var(--color--bg);
  }

  .help-page body {
    font-family: var(--font-family--base);
    line-height: 1.6;
    color: var(--color--text);
    background: var(--color--bg);
    padding: 20px;
    max-width: 800px;
    margin: 0 auto;
  }

  .help-page header {
    text-align: center;
    margin: 2rem 0 3rem;
  }

  .help-page h1 {
    font-size: 2rem;
    margin-bottom: 0.75rem;
  }

  .help-page h2 {
    font-size: 1.5rem;
    margin: 2rem 0 1rem 0;
    padding-bottom: 0.5rem;
  }

  .help-page p,
  .help-page ul {
    margin-bottom: 1rem;
  }

  .help-page ul {
    margin-left: 2rem;
  }

  .help-page li {
    margin-bottom: 0.5rem;
  }

  .help-page a {
    color: var(--color--link);
    text-decoration: none;
  }

  .help-page a:hover {
    text-decoration: underline;
  }

  .help-page footer {
    margin: 3rem 0 2rem;
    text-align: center;
    color: var(--color--text-light);
    font-size: 0.9rem;
  }

  @media (width <= 600px) {
    .help-page body {
      padding: 15px;
    }

    .help-page h1 {
      font-size: 1.5rem;
    }

    .help-page h2 {
      font-size: 1.3rem;
    }
  }
}

@layer utilities {
  .hidden {
    display: none !important;
  }
}
