/*
 * Page frame: header + three layouts.
 *   >= 1360px     3 columns: list of generated puzzles | board and the replay box (replay controls, tabs
 *                 Result | Step) | puzzle options
 *   768-1359px    2 columns: board, replay box and the list of puzzles | puzzle options
 *   < 768px       1 column: the options become a bottom drawer (#btn-drawer toggles #controls-root.is-open)
 */

:root {
  --sg-layout-max-width: 120rem;
  /* The bar of the Generate buttons fixed to the bottom on a phone (also keeps the toasts above it). */
  --sg-actions-bar-height: calc(var(--sg-control-height) + 2 * var(--sg-space-2));
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  display: inline-block;
  flex: none;
  width: 1.25em;
  height: 1.25em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon--brand {
  width: 1.75rem;
  height: 1.75rem;
  color: var(--sg-color-accent);
}

/* ---- Header ---- */
.app-header {
  background: var(--sg-color-surface);
  border-bottom: 1px solid var(--sg-color-divider);
}

.app-header__inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sg-space-2) var(--sg-space-4);
  max-width: var(--sg-layout-max-width);
  margin-inline: auto;
  padding: var(--sg-space-3) var(--sg-space-4);
}

.app-brand {
  display: flex;
  align-items: center;
  gap: var(--sg-space-3);
  min-width: 0;
}

.app-title {
  margin: 0;
  font-size: var(--sg-font-size-xl);
  letter-spacing: -0.01em;
}

.app-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sg-space-2) var(--sg-space-3);
}

/* ---- Grid of the areas ---- */
.app-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "main"
    "history";
  align-items: start;
  gap: var(--sg-space-4);
  max-width: var(--sg-layout-max-width);
  margin-inline: auto;
  padding: var(--sg-space-4);
}

/* ---- Footer: copyright and the link to the repository ---- */
.app-footer {
  max-width: var(--sg-layout-max-width);
  margin-inline: auto;
  padding: var(--sg-space-2) var(--sg-space-4) var(--sg-space-4);
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
  text-align: center;
}

.app-footer__inner {
  margin: 0;
}

.app-footer__copy::after {
  content: "\00b7";
  display: inline-block;
  margin-inline: var(--sg-space-2);
}

.app-footer__link {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.app-footer__link:hover {
  color: var(--sg-color-accent);
}

/* The puzzle options: a column on the right; below 768px they are the bottom drawer, fixed to the screen. */
.app-side {
  grid-area: side;
  min-width: 0;
}

#history-root {
  grid-area: history;
  min-width: 0;
}

.app-main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-3);
  min-width: 0;
}

/* Heading of the main panel (top left) and the puzzle actions (Play, Export; top right). */
.main-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--sg-space-2) var(--sg-space-3);
}

.main-head__titles {
  flex: 1 1 14rem;
  min-width: 0;
}

.main-head__title {
  margin: 0;
  font-size: var(--sg-font-size-lg);
  overflow-wrap: anywhere;
}

.main-head__subtitle {
  margin: var(--sg-space-1) 0 0;
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
}

.main-actions {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--sg-space-2);
  margin-inline-start: auto;
}

/* The result and the description of the current step share the box of the replay controls, as tabs. */
.player__tabs {
  display: flex;
  gap: var(--sg-space-1);
  border-bottom: 1px solid var(--sg-color-divider);
}

.tab {
  margin-bottom: -1px;
  padding: var(--sg-space-2) var(--sg-space-3);
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--sg-color-text-muted);
  font-weight: 650;
}

.tab:hover {
  color: var(--sg-color-text);
}

.tab[aria-selected="true"] {
  border-bottom-color: var(--sg-color-accent);
  color: var(--sg-color-text);
}

.player__panel {
  min-width: 0;
}

.player__panel[hidden] {
  display: none;
}

/* The result card loses its own frame inside the panel. */
.player__panel .card {
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.result-area {
  min-width: 0;
}

.result-area:empty {
  display: none;
}

/* ---- Panels ---- */
.panel {
  padding: var(--sg-space-4);
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
  box-shadow: var(--sg-shadow-1);
}

.panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sg-space-2);
  margin-bottom: var(--sg-space-3);
}

.panel__title {
  margin: 0 0 var(--sg-space-3);
  font-size: var(--sg-font-size-lg);
}

.panel__header > .panel__title {
  margin: 0;
}

.panel__header--start {
  justify-content: flex-start;
}

.panel__subtitle {
  margin: 0 0 var(--sg-space-2);
  font-size: 1rem;
}

.controls-form {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-4);
}

.controls-actions {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-2);
  margin-bottom: var(--sg-space-4);
  padding-bottom: var(--sg-space-4);
  border-bottom: 1px solid var(--sg-color-divider);
}

/* ---- Board area ---- */
.board-frame {
  display: flex;
  justify-content: center;
  padding: var(--sg-space-3);
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
  box-shadow: var(--sg-shadow-1);
}

.board-svg {
  width: 100%;
  height: auto;
  max-height: 72vh;
}

/* ---- Player ---- */
.player {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-3);
  padding: var(--sg-space-3) var(--sg-space-4);
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
}

/* Replay controls: first / previous / next / last and the slider, at the top of the Step tab. */
.player__controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--sg-space-3) var(--sg-space-4);
  margin-bottom: var(--sg-space-3);
}

.player__transport {
  display: flex;
  flex: 0 0 auto;
  gap: var(--sg-space-2);
}

.player__timeline {
  display: flex;
  flex: 1 1 14rem;
  align-items: center;
  gap: var(--sg-space-3);
  min-width: 0;
}

.player__track {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  padding-bottom: 0.5rem;
}

.player__timeline .slider {
  display: block;
  min-width: 0;
}

/* Anchor points: one notch under the slider for every step, at the centre of the thumb for that value. */
.player__ticks {
  position: absolute;
  top: calc(0.875rem + 0.7rem);
  inset-inline: 0.625rem;
  height: 0.4rem;
  pointer-events: none;
}

.player__tick {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(var(--i, 0) / var(--n, 1) * 100%);
  width: 2px;
  margin-left: -1px;
  border-radius: 1px;
  background: var(--sg-color-border);
}

.player__time {
  flex: none;
  width: 15ch;
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
  font-variant-numeric: tabular-nums;
  text-align: end;
  white-space: nowrap;
}

/* ---- Note under the replay controls (js/ui/cache.js) and the symbol legend ---- */
.cache-note {
  margin: 0;
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
}

.board-legend:not(:empty) {
  margin-top: var(--sg-space-3);
  padding: var(--sg-space-2) var(--sg-space-4);
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
}

/* ---- Run states (js/ui/theme.js sets #app-layout[data-run]): idle, busy, ready, error, cancelled ---- */
[data-run="error"] .board-frame {
  border-color: var(--sg-color-danger);
}

/* Hints of the panels that have nothing to show yet; the texts are translated spans, one per state. */
.empty-hint {
  margin: 0;
  padding: var(--sg-space-3) var(--sg-space-4);
  border: 1px dashed var(--sg-color-border);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
}

.empty-hint [data-state] {
  display: none;
}

#result-hint {
  display: none;
  min-width: 0;
}

#result-root:empty + #result-hint {
  display: block;
}

[data-run="ready"] #result-hint {
  display: none;
}

[data-run="error"] #result-hint {
  border-style: solid;
  border-color: var(--sg-color-danger);
  background: var(--sg-color-danger-bg);
  color: var(--sg-color-text);
}

[data-run="idle"] #result-hint [data-state="idle"],
[data-run="busy"] #result-hint [data-state="busy"],
[data-run="error"] #result-hint [data-state="error"],
[data-run="cancelled"] #result-hint [data-state="cancelled"],
[data-run="idle"] #steps-hint [data-state="idle"] {
  display: inline;
}

#steps-hint {
  display: none;
  margin-bottom: var(--sg-space-3);
}

[data-run="idle"] #steps-hint {
  display: block;
}

/* ---- Drawer parts are inert above the mobile breakpoint ---- */
.drawer-toggle,
.drawer-backdrop {
  display: none;
}

/* ---- Responsive layout ----
   Breakpoints (the same in Sudoku Player): >= 768px two columns, >= 1360px three columns, < 768px the mobile
   layout, < 600px a smaller title. The desktop rules come first; the mobile block is last so that it wins
   over the base rules of components.css. */

/* ---- >= 768px: board, replay and result | puzzle options; the list of puzzles under the result ---- */
@media (min-width: 768px) {
  .app-layout {
    grid-template-columns: minmax(0, 1fr) clamp(16rem, 30vw, 21.25rem);
    grid-template-areas:
      "main side"
      "history side";
  }

  .app-side {
    position: sticky;
    top: var(--sg-space-4);
    max-height: calc(100vh - 2 * var(--sg-space-4));
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 2px;
  }
}

/* ---- >= 1360px: the list of puzzles gets its own column on the left ---- */
@media (min-width: 1360px) {
  .app-layout {
    grid-template-columns: clamp(19rem, 21vw, 23rem) minmax(0, 1fr) clamp(16rem, 24vw, 21.25rem);
    grid-template-areas: "history main side";
  }

  #history-root {
    position: sticky;
    top: var(--sg-space-4);
    max-height: calc(100vh - 2 * var(--sg-space-4));
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

/* ---- < 600px: a smaller title ---- */
@media (max-width: 599.98px) {
  .app-title {
    font-size: var(--sg-font-size-lg);
  }
}

/* ---- < 768px: the mobile layout ---- */
@media (max-width: 767.98px) {
  /* Header: title, Settings and Help share one row; language and theme live in the Settings dialog. */
  .app-header__inner {
    flex-wrap: nowrap;
    gap: var(--sg-space-2);
    padding: var(--sg-space-2) var(--sg-space-3);
  }

  .app-brand {
    flex: 1 1 auto;
  }

  .app-tools {
    flex: none;
    flex-wrap: nowrap;
    gap: var(--sg-space-1);
  }

  #lang-switch,
  #theme-switch {
    display: none;
  }

  /* Icon-only buttons: the labels stay for screen readers (the same as .visually-hidden). */
  .app-tools .btn__label,
  .main-actions #btn-export .btn__label,
  .main-actions #btn-export-code .btn__label,
  .drawer-toggle > span {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .app-tools .btn,
  .main-actions #btn-export,
  .main-actions #btn-export-code {
    padding-inline: var(--sg-space-2);
  }

  /* Play, Print and Export sit above the puzzle heading. */
  .main-actions {
    order: -1;
    flex: 1 1 100%;
    gap: var(--sg-space-1);
    margin-inline-start: 0;
  }

  .main-actions .btn {
    min-height: 2rem;
    gap: var(--sg-space-1);
    font-size: var(--sg-font-size-sm);
  }

  .main-actions .btn .icon {
    width: 1.1em;
    height: 1.1em;
  }

  /* Page: room for the bar of the Generate buttons fixed to the bottom. */
  body {
    padding-bottom: calc(var(--sg-actions-bar-height) + env(safe-area-inset-bottom, 0px));
  }

  .app-layout {
    padding: var(--sg-space-3);
    gap: var(--sg-space-3);
  }

  /* The icon-only Options button sits at the end of the row of the Generate buttons (always visible). */
  .drawer-toggle {
    position: fixed;
    right: var(--sg-space-3);
    bottom: calc(var(--sg-space-2) + env(safe-area-inset-bottom, 0px));
    z-index: var(--sg-z-drawer-bar);
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--sg-control-height);
    height: var(--sg-control-height);
    padding: 0;
    border: 1px solid var(--sg-color-border);
    border-radius: var(--sg-radius-2);
    background: var(--sg-color-surface-2);
    color: var(--sg-color-text);
  }

  .drawer-toggle__chevron {
    display: none;
  }

  .drawer-toggle:hover {
    background: var(--sg-color-hover);
  }

  .drawer-toggle[aria-expanded="true"] {
    border-color: var(--sg-color-accent);
    background: var(--sg-color-accent);
    color: var(--sg-color-accent-text);
  }

  /* The panel is always on screen: [options form, only when open] [Generate buttons]. */
  #controls-root {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: var(--sg-z-drawer);
    display: flex;
    flex-direction: column;
    max-height: 75vh;
    max-height: min(75vh, 100dvh - 8rem);
    padding: 0;
    overflow: hidden;
    border-width: 1px 0 0;
    border-radius: var(--sg-radius-3) var(--sg-radius-3) 0 0;
    box-shadow: var(--sg-shadow-2);
  }

  #controls-root .panel__title,
  #controls-root .controls-form {
    display: none;
  }

  #controls-root.is-open .panel__title {
    display: block;
    flex: none;
    margin: 0;
    padding: var(--sg-space-3) var(--sg-space-4) var(--sg-space-2);
  }

  #controls-root.is-open .controls-form {
    display: flex;
    order: 2;
    flex: 1 1 auto;
    min-height: 0;
    padding: 0 var(--sg-space-4) var(--sg-space-3);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  #controls-root .controls-actions {
    order: 3;
    flex: none;
    flex-direction: row;
    box-sizing: border-box;
    height: calc(var(--sg-actions-bar-height) + env(safe-area-inset-bottom, 0px));
    margin: 0;
    padding: var(--sg-space-2) calc(var(--sg-space-3) + var(--sg-control-height) + var(--sg-space-2))
      calc(var(--sg-space-2) + env(safe-area-inset-bottom, 0px)) var(--sg-space-3);
    border-top: 1px solid var(--sg-color-border);
    border-bottom: 0;
  }

  .controls-actions .btn {
    flex: 1 1 0;
    gap: var(--sg-space-1);
    width: auto;
    min-width: 0;
    padding-inline: var(--sg-space-2);
    font-size: var(--sg-font-size-sm);
    white-space: nowrap;
  }

  .controls-actions .btn .icon {
    width: 1.1em;
    height: 1.1em;
  }

  .controls-actions .btn__label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* While a run is busy, Cancel takes the place of Generate random. */
  .controls-actions:has(#btn-cancel:not([hidden])) #btn-regenerate {
    display: none;
  }

  .drawer-backdrop {
    position: fixed;
    inset: 0;
    z-index: var(--sg-z-drawer-backdrop);
    display: block;
    background: var(--sg-color-backdrop);
    opacity: 0;
    visibility: hidden;
    transition:
      opacity var(--sg-motion-base) ease,
      visibility 0s linear var(--sg-motion-base);
  }

  #controls-root.is-open + .drawer-backdrop {
    opacity: 1;
    visibility: visible;
    transition-delay: 0s;
  }

  .app-side {
    display: contents;
  }

  .board-frame {
    padding: var(--sg-space-2);
  }

  .player {
    padding: var(--sg-space-3);
  }
}
