/*
 * Cairn base styles: reset, typography, element defaults and utility classes.
 * Everything here uses the tokens from tokens.css. Element defaults are wrapped
 * in :where() so they carry no specificity and any class can override them.
 */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-md);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

:where(h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd, pre) {
  margin: 0;
}

:where(ul, ol)[class] {
  margin: 0;
  padding: 0;
  list-style: none;
}

:where(img, svg, video, canvas) {
  display: block;
  max-width: 100%;
}

:where(button, input, select, textarea) {
  margin: 0;
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
}

:where(button) {
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}

:where(fieldset) {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

:where(legend) {
  padding: 0;
}

:where(table) {
  border-collapse: collapse;
  border-spacing: 0;
}

:where(th) {
  text-align: left;
  font-weight: 600;
}

:where(summary) {
  cursor: pointer;
}

:where(:disabled, [aria-disabled="true"]) {
  cursor: not-allowed;
}

[hidden] {
  display: none !important;
}

/* ---------------------------------------------------------------------------
 * Typography
 * ------------------------------------------------------------------------- */
:where(h1, h2, h3, h4, h5, h6) {
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.01em;
}

:where(h1) {
  font-size: var(--text-2xl);
}

:where(h2) {
  font-size: var(--text-xl);
}

:where(h3) {
  font-size: var(--text-lg);
}

:where(h4, h5, h6) {
  font-size: var(--text-md);
}

:where(strong, b) {
  font-weight: 600;
}

:where(small) {
  font-size: var(--text-xs);
}

:where(code, kbd, samp, pre) {
  font-family: var(--font-mono);
  font-size: 0.92em;
}

:where(code, kbd) {
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

:where(pre) {
  padding: var(--space-3);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  line-height: 1.5;
}

:where(pre) :where(code) {
  padding: 0;
  border: 0;
  background: none;
  font-size: inherit;
}

:where(hr) {
  height: 0;
  margin: var(--space-4) 0;
  border: 0;
  border-top: 1px solid var(--border);
}

::selection {
  background: var(--selection);
  color: var(--text);
}

/* Long-form text (legal pages, help text): restores the spacing the reset removed. */
.prose {
  max-width: 68ch;
  line-height: 1.6;
}

.prose > * + * {
  margin-top: var(--space-4);
}

.prose :where(h2, h3) {
  margin-top: var(--space-7);
}

.prose :where(ul, ol) {
  padding-left: var(--space-5);
}

.prose :where(li) + :where(li) {
  margin-top: var(--space-1);
}

/* ---------------------------------------------------------------------------
 * Links
 * ------------------------------------------------------------------------- */
:where(a) {
  color: var(--accent);
  text-decoration: none;
  text-underline-offset: 2px;
}

:where(a:hover) {
  text-decoration: underline;
}

/* Links inside running text are underlined so they do not rely on color alone. */
:where(p, li, dd, .prose) > :where(a:not([class])) {
  text-decoration: underline;
}

.link {
  color: var(--accent);
  font-weight: 500;
  text-decoration: none;
  text-underline-offset: 2px;
  cursor: pointer;
}

.link:hover {
  color: var(--accent-hover);
  text-decoration: underline;
}

/* ---------------------------------------------------------------------------
 * Focus
 * ------------------------------------------------------------------------- */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------------------------------------------------------------------------
 * Form element defaults (text-like inputs, selects, textareas)
 * ------------------------------------------------------------------------- */
:where(
    input:not(
        [type="checkbox"],
        [type="radio"],
        [type="range"],
        [type="file"],
        [type="color"],
        [type="submit"],
        [type="button"],
        [type="reset"],
        [type="image"],
        [type="hidden"]
      ),
    select,
    textarea
  ) {
  max-width: 100%;
  min-height: var(--control-height);
  padding: 5px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-md);
  line-height: 20px;
  transition:
    border-color var(--duration-fast) ease,
    box-shadow var(--duration-fast) ease;
}

:where(
    input:not(
        [type="checkbox"],
        [type="radio"],
        [type="range"],
        [type="file"],
        [type="color"],
        [type="submit"],
        [type="button"],
        [type="reset"],
        [type="image"],
        [type="hidden"]
      ),
    select,
    textarea
  ):hover {
  border-color: var(--text-faint);
}

:where(
    input:not(
        [type="checkbox"],
        [type="radio"],
        [type="range"],
        [type="file"],
        [type="color"],
        [type="submit"],
        [type="button"],
        [type="reset"],
        [type="image"],
        [type="hidden"]
      ),
    select,
    textarea
  ):focus {
  border-color: var(--focus);
  outline: 2px solid var(--focus);
  outline-offset: -1px;
}

:where(input, select, textarea):disabled {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text-faint);
}

:where(input, select, textarea)[aria-invalid="true"] {
  border-color: var(--danger);
}

:where(input, select, textarea)[aria-invalid="true"]:focus {
  outline-color: var(--danger);
}

:where(textarea) {
  display: block;
  min-height: 72px;
  resize: vertical;
}

:where(input[type="checkbox"], input[type="radio"], input[type="range"]) {
  accent-color: var(--accent);
}

:where(input[type="search"])::-webkit-search-cancel-button,
:where(input[type="search"])::-webkit-search-decoration {
  -webkit-appearance: none;
  appearance: none;
}

::placeholder {
  color: var(--text-faint);
  opacity: 1;
}

:where(label) {
  cursor: default;
}

/* ---------------------------------------------------------------------------
 * Scrollbars
 * ------------------------------------------------------------------------- */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: var(--radius-full);
  background: var(--border-strong);
  background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
  background-color: var(--text-faint);
}

/*
 * Set by the overlay manager while a modal or drawer is open. Only the root is
 * clipped: clipping <body> as well would make it the scroll container and
 * un-stick the sticky sidebar and topbar of a scrolled page.
 */
html.scroll-locked {
  overflow: hidden;
}

/* ---------------------------------------------------------------------------
 * Utility classes
 * ------------------------------------------------------------------------- */
.row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.col {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-width: 0;
}

.wrap {
  flex-wrap: wrap;
}

.grow {
  flex: 1 1 0;
  min-width: 0;
}

.between {
  justify-content: space-between;
}

.center {
  align-items: center;
  justify-content: center;
  text-align: center;
}

.grid-2,
.grid-3,
.grid-4 {
  display: grid;
  gap: var(--space-4);
  min-width: 0;
}

.grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.grid-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

@media (max-width: 1100px) {
  .grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 900px) {
  .grid-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .grid-2,
  .grid-3,
  .grid-4 {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* After .row, .stack and the grids, so a gap utility overrides their default gap. */
.gap-1 {
  gap: var(--space-1);
}

.gap-2 {
  gap: var(--space-2);
}

.gap-3 {
  gap: var(--space-3);
}

.gap-4 {
  gap: var(--space-4);
}

.gap-5 {
  gap: var(--space-5);
}

.gap-6 {
  gap: var(--space-6);
}

.muted {
  color: var(--text-muted);
}

.faint {
  color: var(--text-faint);
}

.small {
  font-size: var(--text-xs);
}

.strong {
  font-weight: 600;
}

.mono {
  font-family: var(--font-mono);
  font-size: 0.92em;
}

.truncate {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nowrap {
  white-space: nowrap;
}

/* Right-aligns text, and pushes the element to the far end of a flex row. */
.right {
  margin-left: auto;
  text-align: right;
}

.hidden {
  display: none !important;
}

.divider {
  align-self: stretch;
  height: 0;
  margin: var(--space-4) 0;
  border: 0;
  border-top: 1px solid var(--border);
}

/* Visually hidden, still announced by screen readers. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  border: 0;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* ---------------------------------------------------------------------------
 * App boot and router error states
 * ------------------------------------------------------------------------- */
.boot {
  display: grid;
  min-height: 100vh;
  place-items: center;
  color: var(--text-muted);
  font-size: var(--text-md);
}

.page-error {
  max-width: 520px;
  margin: var(--space-8) auto;
  padding: var(--space-6);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  text-align: center;
}

.page-error h2 {
  margin-bottom: var(--space-2);
  font-size: var(--text-lg);
}

.page-error p {
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

/* ---------------------------------------------------------------------------
 * Motion preferences
 * ------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
