/*
 * The hosted page.
 *
 * Only the chrome around the game is styled here. The board itself is drawn by
 * `modernized-tic.js` with inline styles, which is not a stylistic choice: that file has to survive
 * being dropped onto someone else's page, where a stylesheet of ours would never load and theirs
 * would win every cascade. Here it simply lands in the middle of the screen and this styles what
 * sits around it.
 */

:root {
  color-scheme: light dark;

  --surface: oklch(98.5% 0.005 250);
  --surface-raised: oklch(100% 0 0);
  --surface-sunken: oklch(95% 0.008 250);
  --border: oklch(88% 0.012 250);
  --text: oklch(25% 0.02 260);
  --text-muted: oklch(52% 0.02 260);

  --accent: oklch(55% 0.19 260);
  --accent-text: oklch(99% 0 0);
  --accent-quiet: oklch(94% 0.03 260);

  /* The two players, matching the marks the board draws. */
  --player-o: oklch(55% 0.19 260);
  --player-x: oklch(58% 0.21 15);

  --danger: oklch(52% 0.2 20);

  --radius: 14px;
  --radius-small: 9px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 18px;
  --space-5: 28px;

  --font:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    'Segoe UI', sans-serif;
  --font-mono:
    ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;

  --shadow: 0 18px 50px oklch(25% 0.02 260 / 0.14);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: oklch(19% 0.018 260);
    --surface-raised: oklch(24% 0.02 260);
    --surface-sunken: oklch(16% 0.018 260);
    --border: oklch(34% 0.02 260);
    --text: oklch(95% 0.008 260);
    --text-muted: oklch(72% 0.015 260);

    --accent: oklch(70% 0.16 260);
    --accent-text: oklch(18% 0.02 260);
    --accent-quiet: oklch(30% 0.05 260);

    --player-o: oklch(70% 0.16 260);
    --player-x: oklch(70% 0.18 15);

    --danger: oklch(70% 0.17 20);

    --shadow: 0 18px 50px oklch(0% 0 0 / 0.45);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/*
 * The `hidden` attribute is how this page switches between the lobby and a live game, and the UA
 * rule that implements it is a plain `display: none` that any author `display` beats. Both screens
 * here set their own display, so without this they would stay on screen while claiming to be
 * hidden. Toggling a class instead would work equally well; keeping the attribute means the
 * accessibility tree agrees with what is painted.
 */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-block-size: 100dvb;
  padding-block: var(--space-5);
  padding-inline: var(--space-4);
  color: var(--text);
  background-color: var(--surface);
  font-family: var(--font);
  font-size: clamp(0.95rem, 0.9rem + 0.25vw, 1.05rem);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
  text-underline-offset: 0.2em;
}

.visually-hidden {
  position: absolute;
  clip-path: inset(50%);
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  white-space: nowrap;
}

/* --- lobby ---------------------------------------------------------------------------------- */

.lobby {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  max-inline-size: 34rem;
  margin-inline: auto;
}

.lobby-header {
  text-align: center;

  & h1 {
    margin-block: 0 var(--space-2);
    font-size: clamp(1.6rem, 1.2rem + 2vw, 2.4rem);
    font-weight: 750;
    letter-spacing: -0.025em;
    line-height: 1.15;
  }
}

.tagline {
  margin-block: 0;
  margin-inline: auto;
  max-inline-size: 30rem;
  color: var(--text-muted);
  text-wrap: balance;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: clamp(var(--space-4), 4vw, var(--space-5));
  background-color: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* The banner shown when the page has arrived to accept an invitation. */
.invite {
  padding-block: var(--space-3);
  padding-inline: var(--space-4);
  background-color: var(--accent-quiet);
  border: 1px solid var(--accent);
  border-radius: var(--radius-small);

  & p {
    margin-block: 0;
  }
}

.invite-eyebrow {
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.invite-host {
  margin-block-start: var(--space-1) !important;
  font-size: 1.02rem;
  font-weight: 650;
  text-wrap: balance;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);

  & input {
    padding-block: 11px;
    padding-inline: 13px;
    color: inherit;
    background-color: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-small);
    font: inherit;
    transition:
      border-color 140ms ease,
      box-shadow 140ms ease;

    &:focus-visible {
      outline: none;
      border-color: var(--accent);
      box-shadow: 0 0 0 3px var(--accent-quiet);
    }
  }
}

.field-grow {
  flex: 1 1 auto;
  min-inline-size: 0;
}

.field-label {
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 650;
  letter-spacing: 0.01em;
}

/* "optional", riding along on a field label without competing with it. */
.field-note {
  color: var(--text-muted);
  font-size: 0.76rem;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;

  &::before {
    content: '· ';
  }
}

.code-input {
  font-family: var(--font-mono);
  font-size: 1.05rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
}

.join-row {
  display: flex;
  align-items: end;
  gap: var(--space-2);
}

.button {
  flex: 0 0 auto;
  padding-block: 11px;
  padding-inline: 18px;
  color: var(--text);
  background-color: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  cursor: pointer;
  font: inherit;
  font-weight: 650;
  transition:
    background-color 140ms ease,
    border-color 140ms ease,
    transform 140ms ease;

  &:hover {
    background-color: var(--accent-quiet);
    border-color: var(--accent);
  }

  &:active {
    transform: translateY(1px);
  }

  &:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
}

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

  &:hover {
    /* Only the surface moves on hover: nothing here changes size or weight, so the row cannot
       reflow under the pointer. */
    background-color: oklch(from var(--accent) calc(l - 0.05) c h);
    border-color: oklch(from var(--accent) calc(l - 0.05) c h);
  }
}

.button-small {
  padding-block: 7px;
  padding-inline: 12px;
  font-size: 0.88rem;
}

.button-quiet {
  background-color: transparent;
  border-color: transparent;
  color: var(--text-muted);
}

.divider {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-block: var(--space-1);
  color: var(--text-muted);
  font-size: 0.82rem;

  &::before,
  &::after {
    content: '';
    flex: 1 1 auto;
    block-size: 1px;
    background-color: var(--border);
  }
}

.notice {
  margin-block: 0;
  color: var(--danger);
  font-size: 0.88rem;
  font-weight: 600;
}

.lobby-footer {
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;

  & p {
    margin-block: 0;
  }
}

/* --- the bar under a live game -------------------------------------------------------------- */

/*
 * Three groups: the room, the score and turn, the buttons.
 *
 * A grid rather than a wrapping flex row, because what has to happen on a narrow screen is
 * specific: the middle drops to its own line and the room and the buttons stay together on the
 * first, giving two tidy rows instead of the five a flex wrap produces once the score line arrives.
 * Nothing here is allowed to grow tall — every millimetre it takes is one the board does not get.
 */
.bar {
  position: fixed;
  inset-block-end: 0;
  inset-inline: 0;
  z-index: 2147483646; /* Under the board, which pins itself to the top of the stack. */

  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas:
    'room actions'
    'middle middle';
  align-items: center;
  justify-content: space-between;
  gap: var(--space-1) var(--space-4);
  padding-block: var(--space-2);
  padding-inline: clamp(var(--space-3), 4vw, var(--space-5));

  background-color: color-mix(in oklch, var(--surface-raised) 92%, transparent);
  border-block-start: 1px solid var(--border);
  backdrop-filter: blur(14px);
  padding-block-end: max(var(--space-2), env(safe-area-inset-bottom));
}

/* Wide enough for all three side by side, which is where the middle wants to be. */
@media (min-width: 46rem) {
  .bar {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: 'room middle actions';
    gap: var(--space-2) var(--space-4);
    padding-block: var(--space-3);
  }
}

.bar-room {
  grid-area: room;
}

.bar-middle {
  grid-area: middle;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-inline-size: 0;
}

.bar > .bar-group:last-child {
  grid-area: actions;
  justify-self: end;
}

.bar-group {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.bar-label {
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 650;
}

.bar-status {
  margin-block: 0;
  color: var(--text-muted);
  font-size: 0.92rem;
  font-weight: 650;
  text-align: center;
  text-wrap: balance;

  &.is-yours {
    color: var(--accent);
  }
}

/*
 * The score: two names with the tally between them.
 *
 * Counted by seat on the server, so both players read the same numbers and a rematch swapping who
 * opens does not swap who the numbers belong to.
 */
.bar-score {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: center;
  gap: var(--space-1) var(--space-2);
  margin-block: 0;
  font-size: 0.88rem;
  font-weight: 650;
  line-height: 1.3;
}

.score-tally {
  padding-inline: 7px;
  color: var(--text);
  background-color: var(--surface-sunken);
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.score-draws {
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
}

/*
 * A name the host can change, and only the host gets one — an affordance that refuses when pressed
 * is worse than no affordance, so the other player is shown plain text.
 */
.score-name {
  padding: 0;
  color: inherit;
  background: none;
  border: 0;
  border-block-end: 1px dotted var(--text-muted);
  cursor: pointer;
  font: inherit;
  transition: color 140ms ease, border-color 140ms ease;

  &:hover {
    color: var(--accent);
    border-block-end-color: var(--accent);
  }

  &:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
}

/* On the narrowest screens the chip says "room" by looking like one. */
@media (max-width: 26rem) {
  .bar-label {
    display: none;
  }
}

.code-chip {
  padding-block: 5px;
  padding-inline: 10px;
  color: var(--text);
  background-color: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 650;
  letter-spacing: 0.16em;
  transition:
    background-color 140ms ease,
    border-color 140ms ease;

  &:hover {
    background-color: var(--accent-quiet);
    border-color: var(--accent);
  }

  &:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
}

/* The board is fixed and centred; on a short screen the bar would sit on top of it. */
@media (min-height: 640px) {
  body:has(.bar:not([hidden])) {
    padding-block-end: 5rem;
  }
}

/*
 * While a game is on, this page is the board and nothing else.
 *
 * The lobby is already hidden, but the body keeps its page padding and its scroll, and on a phone
 * that is a board sitting slightly off centre in a document that can be dragged around behind it.
 */
body:has(.bar:not([hidden])) {
  overflow: hidden;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
