/*
 * Reusable components. Class vocabulary shared with every UI module:
 *   .btn (+ --primary --secondary --ghost --icon --sm, state: disabled)
 *   .segmented > .segmented__btn[aria-pressed="true"]
 *   .field > .field__label + .textarea + .field__hint + .field__error
 *   .field-group (fieldset) > .field-group__legend, .checkbox, .switch, .disclosure
 *   .badge--accent, .toast (+ --success --warning --danger) inside #toast-root, .modal inside #modal-root
 *   Player parts at the end: .history-list, the Open dialog
 */

/* ---- Buttons ---- */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sg-space-2);
  min-height: var(--sg-control-height);
  padding: 0 var(--sg-space-4);
  border: 1px solid transparent;
  border-radius: var(--sg-radius-2);
  background: transparent;
  color: var(--sg-color-text);
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  user-select: none;
  transition:
    background-color var(--sg-motion-fast) ease,
    border-color var(--sg-motion-fast) ease,
    color var(--sg-motion-fast) ease,
    transform var(--sg-motion-fast) ease;
}

.btn--primary {
  border-color: var(--sg-color-accent);
  background: var(--sg-color-accent);
  color: var(--sg-color-accent-text);
}

.btn--secondary {
  border-color: var(--sg-color-border);
  background: var(--sg-color-surface-2);
}

.btn--ghost {
  background: transparent;
}

.btn--icon {
  width: var(--sg-control-height);
  padding: 0;
}

.btn--sm {
  min-height: 2rem;
  padding: 0 var(--sg-space-3);
  font-size: var(--sg-font-size-sm);
}

.btn--sm.btn--icon {
  width: 2rem;
  padding: 0;
}

.btn--primary:where(:hover, :active):not(:disabled) {
  border-color: var(--sg-color-accent-hover);
  background: var(--sg-color-accent-hover);
}

.btn--secondary:where(:hover, :active):not(:disabled),
.btn--ghost:where(:hover, :active):not(:disabled) {
  background: var(--sg-color-hover);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  border-color: var(--sg-color-divider);
  border-style: dashed;
  background: var(--sg-color-surface-2);
  color: var(--sg-color-disabled-text);
  cursor: not-allowed;
}

.btn--ghost:disabled {
  border-color: transparent;
  background: transparent;
}

/* ---- Segmented control (language, theme) ---- */
.segmented {
  display: inline-flex;
  max-width: 100%;
}

.segmented__btn {
  position: relative;
  display: inline-flex;
  flex: 1 1 auto;
  align-items: center;
  justify-content: center;
  min-height: 2.25rem;
  margin-inline-start: -1px;
  padding: 0 var(--sg-space-3);
  border: 1px solid var(--sg-color-border);
  background: var(--sg-color-surface);
  color: var(--sg-color-text);
  font-size: var(--sg-font-size-sm);
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  transition:
    background-color var(--sg-motion-fast) ease,
    color var(--sg-motion-fast) ease;
}

.segmented__btn:first-child {
  margin-inline-start: 0;
  border-radius: var(--sg-radius-2) 0 0 var(--sg-radius-2);
}

.segmented__btn:last-child {
  border-radius: 0 var(--sg-radius-2) var(--sg-radius-2) 0;
}

.segmented__btn--icon {
  flex: none;
  width: 2.25rem;
  padding: 0;
}

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

.segmented__btn[aria-pressed="true"] {
  z-index: 1;
  border-color: var(--sg-color-accent);
  background: var(--sg-color-accent);
  color: var(--sg-color-accent-text);
}

.segmented__btn:focus-visible {
  z-index: 2;
}

/* ---- Form fields ---- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-1);
  min-width: 0;
}

.field__label {
  font-size: var(--sg-font-size-sm);
  font-weight: 650;
}

.field__hint {
  color: var(--sg-color-text-muted);
  font-size: 0.8125rem;
}

.field__error {
  color: var(--sg-color-danger);
  font-size: 0.8125rem;
  font-weight: 600;
}

textarea[aria-invalid="true"] {
  border-color: var(--sg-color-danger);
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-3);
  padding: var(--sg-space-3);
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-2);
}

.field-group__legend {
  padding: 0 var(--sg-space-1);
  font-size: var(--sg-font-size-sm);
  font-weight: 650;
}

textarea {
  width: 100%;
  min-height: var(--sg-control-height);
  padding: var(--sg-space-2) var(--sg-space-3);
  border: 1px solid var(--sg-color-border);
  border-radius: var(--sg-radius-2);
  background-color: var(--sg-color-surface);
  color: var(--sg-color-text);
}

textarea:hover {
  border-color: var(--sg-color-text-muted);
}

/* The browser default placeholder grey fails 4.5:1 on dark surfaces. */
textarea::placeholder {
  color: var(--sg-color-text-muted);
  opacity: 1;
}

/* ---- Checkbox / radio ---- */
.checkbox {
  display: flex;
  align-items: flex-start;
  gap: var(--sg-space-2);
  cursor: pointer;
}

.checkbox > input {
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  margin: 0.125rem 0 0;
}

/* ---- Switch: <label class="switch"><input class="switch__input" type="checkbox" role="switch">
        <span class="switch__track" aria-hidden="true"></span><span class="switch__text">…</span></label>
   ---- */
.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--sg-space-3);
  cursor: pointer;
}

.switch__input {
  position: absolute;
  width: 2.5rem;
  height: 1.5rem;
  margin: 0;
  opacity: 0;
  cursor: inherit;
}

.switch__track {
  position: relative;
  flex: none;
  width: 2.5rem;
  height: 1.5rem;
  border: 2px solid var(--sg-color-border);
  border-radius: var(--sg-radius-pill);
  background: var(--sg-color-surface-2);
  transition:
    background-color var(--sg-motion-fast) ease,
    border-color var(--sg-motion-fast) ease;
}

.switch__track::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 0.125rem;
  width: 0.875rem;
  height: 0.875rem;
  border-radius: 50%;
  background: var(--sg-color-text-muted);
  transform: translateY(-50%);
  transition:
    transform var(--sg-motion-fast) ease,
    background-color var(--sg-motion-fast) ease;
}

.switch__input:checked + .switch__track {
  border-color: var(--sg-color-accent);
  background: var(--sg-color-accent);
}

.switch__input:checked + .switch__track::after {
  background: var(--sg-color-accent-text);
  transform: translate(1rem, -50%);
}

.switch__input:focus-visible + .switch__track {
  outline: var(--sg-focus-width) solid var(--sg-color-focus);
  outline-offset: var(--sg-focus-offset);
}

.switch__input:disabled + .switch__track {
  opacity: 0.6;
}

/* ---- Disclosure (<details class="disclosure"><summary>…</summary><div class="disclosure__body">) ---- */
.disclosure {
  border: 1px solid var(--sg-color-divider);
  border-radius: var(--sg-radius-2);
}

.disclosure > summary {
  display: flex;
  align-items: center;
  gap: var(--sg-space-2);
  min-height: var(--sg-control-height);
  padding: var(--sg-space-2) var(--sg-space-3);
  border-radius: var(--sg-radius-2);
  font-weight: 650;
  list-style: none;
}

.disclosure > summary::-webkit-details-marker {
  display: none;
}

.disclosure > summary::before {
  content: "";
  flex: none;
  width: 0.45rem;
  height: 0.45rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform var(--sg-motion-fast) ease;
}

.disclosure[open] > summary::before {
  transform: rotate(45deg);
}

.disclosure__body {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-3);
  padding: var(--sg-space-1) var(--sg-space-3) var(--sg-space-3);
}

/* ---- Badge ---- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sg-space-1);
  padding: 0.125rem 0.625rem;
  border: 1px solid var(--sg-color-border);
  border-radius: var(--sg-radius-pill);
  background: var(--sg-color-surface);
  color: var(--sg-color-text);
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

.badge--accent {
  border-color: var(--sg-color-accent);
  background: var(--sg-color-accent-soft);
  color: var(--sg-color-accent);
}

/* ---- Toasts (announced by #toast-root, role="status") ---- */
.toast-root {
  position: fixed;
  right: var(--sg-space-4);
  bottom: var(--sg-space-4);
  z-index: var(--sg-z-toast);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--sg-space-2);
  width: min(26rem, calc(100vw - 2rem));
  pointer-events: none;
}

/* While a dialog is open the toasts sit under its panel (a11y.js moves them in), not over its buttons. */
.modal > .toast-root {
  position: static;
  align-items: stretch;
  width: min(100%, 30rem);
  margin-top: var(--sg-space-3);
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--sg-space-3);
  width: 100%;
  padding: var(--sg-space-3) var(--sg-space-4);
  border: 1px solid var(--sg-color-border);
  border-inline-start: 4px solid var(--sg-color-accent);
  border-radius: var(--sg-radius-2);
  background: var(--sg-color-surface);
  color: var(--sg-color-text);
  box-shadow: var(--sg-shadow-2);
  pointer-events: auto;
  animation: sg-toast-in var(--sg-motion-base) ease-out;
}

.toast--success {
  border-inline-start-color: var(--sg-color-success);
}

.toast--warning {
  border-inline-start-color: var(--sg-color-warning);
}

.toast--danger {
  border-inline-start-color: var(--sg-color-danger);
}

.toast__message {
  flex: 1 1 auto;
  min-width: 0;
  overflow-wrap: anywhere;
}

.toast__close {
  flex: none;
}

@media (max-width: 767.98px) {
  .toast-root {
    right: var(--sg-space-3);
    bottom: calc(var(--play-keys-height, 0px) + var(--sg-space-3) + env(safe-area-inset-bottom, 0px));
    left: var(--sg-space-3);
    width: auto;
  }
}

/* ---- Modal: <dialog class="modal"> (preferred, showModal()) or <div class="modal" role="dialog"> ---- */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--sg-z-modal);
  display: grid;
  place-items: center;
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: var(--sg-space-4);
  overflow: auto;
  border: 0;
  background: var(--sg-color-backdrop);
  color: var(--sg-color-text);
  animation: sg-fade-in var(--sg-motion-base) ease-out;
}

dialog.modal:not([open]) {
  display: none;
}

.modal::backdrop {
  background: transparent;
}

.modal__panel {
  display: flex;
  flex-direction: column;
  width: min(100%, 34rem);
  max-height: calc(100vh - 2 * var(--sg-space-4));
  max-height: calc(100dvh - 2 * var(--sg-space-4));
  overflow: hidden;
  border: 1px solid var(--sg-color-border);
  border-radius: var(--sg-radius-3);
  background: var(--sg-color-surface);
  box-shadow: var(--sg-shadow-2);
}

.modal__panel--wide {
  width: min(100%, 52rem);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sg-space-3);
  padding: var(--sg-space-3) var(--sg-space-4);
  border-bottom: 1px solid var(--sg-color-divider);
}

.modal__title {
  font-size: var(--sg-font-size-lg);
}

.modal__body {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: var(--sg-space-3);
  padding: var(--sg-space-4);
  overflow: auto;
}

.modal__footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--sg-space-2);
  padding: var(--sg-space-3) var(--sg-space-4);
  border-top: 1px solid var(--sg-color-divider);
  background: var(--sg-color-surface-2);
}

/* ---- Keyframes (neutralised by the reduced-motion rules in base.css) ---- */
@keyframes sg-toast-in {
  from {
    opacity: 0;
    transform: translateY(0.75rem);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes sg-fade-in {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@media (forced-colors: active) {
  .switch__track,
  .badge {
    border: 2px solid currentColor;
  }
}

/* ---- Settings dialog (js/ui/a11y.js): radio choices as buttons, switch rows ---- */
.choice-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sg-space-2);
}

.choice {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--sg-control-height);
  min-height: var(--sg-control-height);
  padding: 0 var(--sg-space-3);
  border: 1px solid var(--sg-color-border);
  border-radius: var(--sg-radius-2);
  background: var(--sg-color-surface);
  color: var(--sg-color-text);
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition:
    background-color var(--sg-motion-fast) ease,
    color var(--sg-motion-fast) ease;
}

.choice > input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: inherit;
}

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

.choice:has(> input:checked) {
  border-color: var(--sg-color-accent);
  background: var(--sg-color-accent);
  color: var(--sg-color-accent-text);
}

.choice:has(> input:focus-visible) {
  outline: var(--sg-focus-width) solid var(--sg-color-focus);
  outline-offset: var(--sg-focus-offset);
}

.choice__text--small {
  font-size: var(--sg-font-size-sm);
}

.choice__text--large {
  font-size: var(--sg-font-size-xl);
}

.setting {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-1);
}

@media (forced-colors: active) {
  .choice:has(> input:checked),
  .segmented__btn[aria-pressed="true"] {
    border-width: 3px;
    text-decoration: underline;
  }
}

/* ---- Player: recent list, open dialog, errors ---- */
.modal__lead {
  max-width: 62ch;
  margin: 0 0 var(--sg-space-3);
}

/* List of puzzles (left panel and welcome screen): the rules of the generator's list of puzzles, plus the
   status icon. */
.history-list {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-1);
}

.history-item {
  position: relative;
  min-width: 0;
  border-radius: 0;
}

.history-item:hover {
  background: var(--sg-color-hover);
}

.history-item__open {
  display: flex;
  width: 100%;
  flex-direction: column;
  gap: 0;
  min-width: 0;
  padding: 0.5rem 5rem 0.5rem var(--sg-space-3);
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  line-height: 1.35;
  text-align: start;
}

.history-item__open[aria-current="true"] {
  box-shadow: inset 3px 0 0 var(--sg-color-accent);
  background: var(--sg-color-accent-soft);
}

.history-item__title {
  overflow: hidden;
  font-size: var(--sg-font-size-base);
  font-weight: 650;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* A large, faded icon between the name and the remove button: Solved or In progress. */
.history-item__status {
  position: absolute;
  top: 50%;
  inset-inline-end: 2.75rem;
  display: inline-flex;
  transform: translateY(-50%);
  opacity: 0.25;
  pointer-events: none;
  color: var(--sg-color-text-muted);
}

.history-item__status[data-status="solved"] {
  color: var(--sg-color-success);
}

.history-item__status[data-status="progress"] {
  color: var(--sg-color-accent);
}

.history-item__status .icon {
  width: 1.75rem;
  height: 1.75rem;
  stroke-width: 2;
}

.history-item__meta {
  color: var(--sg-color-text-muted);
  font-size: var(--sg-font-size-sm);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.history-item__level {
  font-weight: 650;
}

.history-item .history-item__remove.btn--sm.btn--icon {
  position: absolute;
  top: 50%;
  inset-inline-end: 0.5rem;
  transform: translateY(-50%);
  width: 1.5rem;
  height: 1.5rem;
  min-height: 0;
  padding: 0;
}

.history-item__remove .icon {
  width: 0.875rem;
  height: 0.875rem;
}

.welcome__recent-title {
  margin: 0 0 var(--sg-space-2);
  font-size: var(--sg-font-size-lg);
}

.open-text {
  min-height: 7rem;
  font-size: var(--sg-font-size-sm);
}

.open-errors {
  margin-top: var(--sg-space-2);
}

.error-list {
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-2);
  padding-inline-start: var(--sg-space-5);
  list-style: disc;
}

@media (forced-colors: active) {
  .history-item__open[aria-current="true"] {
    outline: 2px solid currentColor;
  }
}
