/*
 * Copyright © 2026 Imhsan. All rights reserved.
 * Ordslingan is proprietary software.
 * See https://ordslingan.heliopolis.se/license.txt for details.
 */

@font-face {
  font-family: "Instrument Sans";
  src: url("../fonts/instrument-sans-latin.woff2") format("woff2");
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
}

:root {
  color-scheme: only dark;

  /* Kanagawa Wave */
  --page: #181820; /* Also used by the theme-color meta tag. */
  --surface: #1f1f28;
  --text: #dcd7ba;
  --muted: #c8c093;
  --fuji-gray: #727169;
  --line: #717c7c;
  --accent: #ffa066;
  --accent-text: #16161d;
  --used-letter: var(--line);
  --path: var(--accent);

  /* Typography */
  --font: "Instrument Sans", system-ui, sans-serif;
  --body-font-size: 1rem;
  --button-font-size: 1.125rem;
  --history-font-size: 1.5rem;
  --brand-font-size: 1.5rem;
  --letter-font-size: 1.875rem;
  --word-font-size: clamp(2rem, 7vw, 2.25rem);
  --board-mark-font-size: clamp(7.5rem, 34vw, 11rem);
  --line-height: 1.5;
  --compact-line-height: 1;
  --heading-line-height: 1.25;
  --button-weight: 650;
  --bold-weight: 700;
  --letter-spacing: 0.05em;
  --heading-letter-spacing: calc(0.12em + var(--letter-spacing));
  --word-letter-spacing: calc(0.22em + var(--letter-spacing));
  --mark-letter-spacing: calc(-0.075em + var(--letter-spacing));

  /* Layout and controls */
  --space: 0.75rem;
  --word-separator-space: calc(var(--space) * 2);
  --dot-size: 0.375rem;
  --dot-spacing: calc(var(--space) * 1.5);
  --game-min-width: 20rem;
  --game-width: 30rem;
  --help-dialog-scale: 0.8;
  --help-backdrop-blur: 0.375rem;
  --control-size: 2.75rem;
  --letter-size: clamp(3rem, 17.5vw, 3.75rem);
  --square-ratio: 1;
  --board-tracks: 16% 20% 28% 20% 16%;
  --board-inset: 8%;
  --board-message-clearance: calc(var(--board-inset) + var(--letter-size) / 2 + var(--space) / 2);
  --board-message-inset: max(18%, var(--board-message-clearance));
  --board-message-max-height: 55%;
  --board-path-z-index: 1;
  --letter-label-z-index: 2;
  --border-width: 1px;
  --thin-divider-width: 1px;
  --path-width: 3px;
  --outline-offset: 3px;
  --radius-control: 0.375rem;
  --radius-board: 1rem;
  --radius-letter: 50%;
  --opacity-mark: 0.05;
  --opacity-disabled: 0.55;
}

@media (width >= 48rem) {
  :root {
    --game-width: 27rem;
  }
}

* {
  box-sizing: border-box;
  scrollbar-width: none;
}

[hidden] {
  display: none !important;
}

body {
  display: flex;
  flex-direction: column;
  min-block-size: 100dvh;
  min-inline-size: var(--game-min-width);
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: var(--body-font-size) / var(--line-height) var(--font);
  letter-spacing: var(--letter-spacing);
  user-select: none;
}

.game,
.game-header {
  inline-size: 100%;
  max-inline-size: var(--game-width);
  margin-inline: auto;
}

.game {
  display: flex;
  flex: 1 0 auto;
  flex-direction: column;
  gap: var(--space);
}

.game-divider {
  flex: none;
  inline-size: 100%;
  max-inline-size: var(--game-width);
  block-size: var(--thin-divider-width);
  margin: var(--space) auto;
  border: 0;
  background: var(--line) content-box;
}

.game-header + .game-divider {
  margin-block-start: 0;
}

.play > .game-divider,
.help-dialog > .game-divider:last-of-type {
  margin-block-end: 0;
}

.help-dialog > .game-divider {
  padding-inline: var(--space);
}

.play {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space);
}

.game-header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: var(--space);
  padding: var(--space);
  font: var(--bold-weight) var(--brand-font-size) / var(--compact-line-height) var(--font);
  letter-spacing: var(--letter-spacing);

  > :is(h1, h2) {
    display: flex;
    align-items: center;
    gap: var(--space);
    margin: 0;
    font: inherit;
    line-height: var(--heading-line-height);
    overflow-wrap: anywhere;
  }

  > h1 {
    letter-spacing: var(--heading-letter-spacing);
    text-transform: uppercase;
  }

  > h2 {
    font-size: var(--button-font-size);
    letter-spacing: var(--letter-spacing);
  }
}

.brand-badge {
  display: grid;
  flex: none;
  place-items: center;
  inline-size: 1lh;
  block-size: 1lh;
  border-radius: var(--radius-control);
  background: var(--accent);
  color: var(--accent-text);
  letter-spacing: normal;
}

.status,
.current-word,
.actions {
  margin-inline: var(--space);
}

.board-shell {
  padding-block: calc(var(--space) / 2);
  padding-inline: var(--space);
}

.board {
  position: relative;
  display: grid;
  grid-template: var(--board-tracks) / var(--board-tracks);
  grid-template-areas:
    ".      top-1    top-2    top-3    ."
    "left-3 .        .        .        right-1"
    "left-2 .        .        .        right-2"
    "left-1 .        .        .        right-3"
    ".      bottom-3 bottom-2 bottom-1 .";
  inline-size: 100%;
  aspect-ratio: var(--square-ratio);
  touch-action: none;

  &::before {
    position: absolute;
    inset: var(--board-inset);
    border: var(--border-width) solid var(--line);
    border-radius: var(--radius-board);
    background: var(--surface);
    content: "";
    pointer-events: none;
  }
}

.board-mark,
.board-solved {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;

  > span {
    text-box: trim-both cap alphabetic;
  }
}

.board-mark {
  color: var(--fuji-gray);
  font: var(--bold-weight) var(--board-mark-font-size) / var(--compact-line-height) var(--font);
  letter-spacing: var(--mark-letter-spacing);
  opacity: var(--opacity-mark);

  > span {
    translate: calc(-1 * var(--mark-letter-spacing)) 0;
  }
}

.board-solved {
  color: var(--text);
  font: var(--button-weight) var(--letter-font-size) / var(--compact-line-height) var(--font);
  letter-spacing: normal;
}

.board-path {
  position: absolute;
  z-index: var(--board-path-z-index);
  inset: 0;
  inline-size: 100%;
  block-size: 100%;
  pointer-events: none;
}

.board-message {
  position: absolute;
  inset-inline: var(--board-message-inset);
  inset-block-end: var(--board-message-clearance);
  max-block-size: var(--board-message-max-height);
  margin: 0;
  padding: var(--space);
  color: var(--fuji-gray);
  text-align: center;
  overflow: hidden;
  overflow-wrap: anywhere;
  pointer-events: none;
}

.board-message--top {
  inset-block: var(--board-message-clearance) auto;
}

button {
  --button-background: var(--surface);
  border: var(--border-width) solid var(--line);
  border-radius: var(--radius-control);
  background: var(--button-background);
  color: var(--button-text, var(--text));
  font: var(--button-weight) var(--button-font-size) / var(--compact-line-height) var(--font);
  letter-spacing: var(--letter-spacing);
  min-block-size: var(--control-size);
  cursor: pointer;
  touch-action: manipulation;

  &:focus-visible {
    outline: var(--border-width) solid var(--path);
    outline-offset: var(--outline-offset);
  }

  &:disabled {
    opacity: var(--opacity-disabled);
    cursor: default;
  }
}

.letter {
  position: relative;
  display: grid;
  place-items: center;
  place-self: center;
  inline-size: var(--letter-size);
  min-block-size: 0;
  aspect-ratio: var(--square-ratio);
  padding: 0;
  border-radius: var(--radius-letter);
  font-size: var(--letter-font-size);
  letter-spacing: normal;
  -webkit-tap-highlight-color: transparent;

  &.used {
    --button-background: var(--used-letter);
    --button-text: var(--accent-text);
  }

  &.active {
    --button-background: var(--path);
    --button-text: var(--accent-text);
    border-color: var(--path);
  }

  &:disabled {
    opacity: 1;
  }
}

.letter-label {
  position: relative;
  z-index: var(--letter-label-z-index);
  text-box: trim-both cap alphabetic;
}

.button-primary {
  --button-background: var(--accent);
  --button-text: var(--accent-text);
}

.current-word {
  display: flex;
  align-items: center;
  justify-content: safe center;
  block-size: calc(var(--line-height) * 1em);
  font-size: var(--word-font-size);
  font-weight: var(--bold-weight);
  letter-spacing: var(--word-letter-spacing);
  text-indent: var(--word-letter-spacing);
  overflow: auto hidden;
  white-space: nowrap;
  text-align: center;

  &:empty {
    display: grid;
    place-items: center;
  }

  &:empty::before {
    inline-size: var(--dot-size);
    aspect-ratio: var(--square-ratio);
    border-radius: var(--radius-letter);
    background: currentColor;
    box-shadow:
      calc(-1 * var(--dot-spacing)) 0 currentColor,
      var(--dot-spacing) 0 currentColor;
    content: "";
  }
}

.actions {
  display: flex;
  gap: var(--space);

  > button {
    flex: 1 1 0;
    padding: var(--space);
    border: 0;
  }
}

.word-summary {
  display: flex;
  flex: 1 0 auto;
  flex-direction: column;
  gap: calc(var(--space) / 2);
  padding-inline: var(--space);

  > h2 {
    display: flex;
    justify-content: space-between;
    gap: var(--space);
    margin: 0;
    font-size: inherit;
    font-weight: var(--bold-weight);
    text-transform: uppercase;
  }

  > h2 > span {
    text-box: trim-end cap alphabetic;
  }
}

.status {
  margin-block: 0;
  color: var(--muted);
  text-align: center;
}

.history-list {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  align-content: start;
  justify-content: center;
  column-gap: var(--word-separator-space);
  min-block-size: 1lh;
  margin-block: 0;
  padding: 0;
  list-style: none;
  font-size: var(--history-font-size);

  > li {
    position: relative;
    max-inline-size: 100%;
  }

  > li + li:not([data-row-start])::before {
    position: absolute;
    inset-inline-end: 100%;
    inline-size: var(--word-separator-space);
    block-size: 1lh;
    background: currentColor;
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4 12h15m-6-6 6 6-6 6' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    mask-position: center;
    mask-size: var(--history-font-size);
    mask-repeat: no-repeat;
    content: "";
  }

  a {
    color: inherit;
    font-weight: var(--bold-weight);
    overflow-wrap: anywhere;
    text-decoration: none;
  }

  a:focus-visible {
    outline: var(--border-width) solid var(--path);
  }
}

.game-footer {
  flex: none;
  inline-size: calc(min(100%, var(--game-width)) - 2 * var(--space));
  margin-block-start: auto;
  margin-inline: auto;
  padding-block: var(--space);
  color: var(--fuji-gray);
  text-align: center;

  > a {
    color: inherit;
    text-decoration: none;
  }
}

.help-control {
  all: unset;
  font: var(--button-weight) var(--brand-font-size) / var(--heading-line-height) var(--font);
  cursor: pointer;

  @media (pointer: coarse) {
    &[command="close"]:focus-visible {
      outline: none;
    }
  }
}

body:has(.help-dialog:modal) {
  overflow: hidden;
}

.help-dialog {
  position: fixed;
  inset: 0;
  inline-size: calc(min(100%, var(--game-width)) * var(--help-dialog-scale));
  block-size: calc(100dvh * var(--help-dialog-scale));
  max-inline-size: none;
  max-block-size: none;
  margin: auto;
  padding: 0;
  border: var(--thin-divider-width) solid var(--line);
  border-radius: var(--radius-control);
  background: var(--page);
  color: inherit;
  overflow: hidden auto;
  overscroll-behavior: contain;
  user-select: none;

  &:modal {
    display: flex;
    flex-direction: column;
  }

  &::backdrop {
    background: transparent;
    backdrop-filter: blur(var(--help-backdrop-blur));
  }
}

.help-content {
  flex: 1 0 auto;
  padding: var(--space);
}

.help-rules {
  display: grid;
  gap: var(--space);
  margin: 0;
  padding-inline-start: calc(var(--space) * 2);
}
