/* ===========================================================================
   Northwest Falls: base + shared components
   Loaded by every page after tokens.css. Contains the reset, typography,
   and the component vocabulary (buttons, fields, cards, modals, toasts,
   skeletons) that home / dashboard / login / northlink all reuse.
   =========================================================================== */

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  /* Reserve the scrollbar so opening a modal doesn't shift the layout. */
  scrollbar-gutter: stable;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-body);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}

/* The theme script sets data-theme before paint; this class is added once the
   stylesheet has applied so we can fade the first paint in rather than flash. */
html:not(.theme-ready) body { opacity: 0; }
html.theme-ready body { opacity: 1; transition: opacity var(--dur-med) var(--ease-out); }

h1, h2, h3, h4, h5, h6 {
  margin: 0;
  font-weight: 600;
  line-height: var(--leading-tight);
  letter-spacing: -0.02em;
  color: var(--text-primary);
}
h1 { font-size: var(--text-4xl); letter-spacing: -0.035em; }
h2 { font-size: var(--text-3xl); letter-spacing: -0.03em; }
h3 { font-size: var(--text-xl); }
h4 { font-size: var(--text-lg); }

p { margin: 0; }

a {
  color: inherit;
  text-decoration: none;
  transition: color var(--dur-fast) var(--ease-out);
}

img, svg, video { max-width: 100%; display: block; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; padding: 0; }

hr {
  border: none;
  border-top: 1px solid var(--border-subtle);
  margin: var(--space-8) 0;
}

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

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}
:focus:not(:focus-visible) { outline: none; }

/* Scrollbars: thin and theme-aware, Firefox + WebKit. */
* { scrollbar-width: thin; scrollbar-color: var(--surface-3) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--surface-3);
  border-radius: var(--radius-pill);
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--border-strong); background-clip: content-box; }

/* ---------------------------------------------------------------------------
   Layout helpers
   --------------------------------------------------------------------------- */
.nw-container {
  width: 100%;
  max-width: var(--page-max);
  margin-inline: auto;
  padding-inline: var(--space-6);
}
.nw-container--narrow { max-width: var(--page-narrow); }

.nw-stack   { display: flex; flex-direction: column; gap: var(--space-4); }
.nw-row     { display: flex; align-items: center; gap: var(--space-3); }
.nw-row--between { justify-content: space-between; }
.nw-spacer  { flex: 1 1 auto; }

.nw-muted   { color: var(--text-secondary); }
.nw-dim     { color: var(--text-tertiary); }
.nw-mono    { font-family: var(--font-mono); font-size: 0.92em; letter-spacing: -0.01em; }
.nw-truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.nw-sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

[hidden] { display: none !important; }

/* ---------------------------------------------------------------------------
   Buttons
   --------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0 var(--space-5);
  height: 38px;
  border-radius: var(--radius-pill);
  border: 1px solid transparent;
  font-size: var(--text-sm);
  font-weight: 500;
  letter-spacing: -0.005em;
  white-space: nowrap;
  user-select: none;
  transition: background var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              color var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out),
              opacity var(--dur-fast) var(--ease-out);
}
.btn:active:not(:disabled) { transform: translateY(0.5px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }

/* The primary action inverts the page rather than taking a brand colour.
   White on black, black on white. It is the only pure-contrast element on a
   screen, so it needs no colour to be the obvious thing to press, and it stays
   correct in both themes without a second set of values. */
.btn--primary {
  background: var(--invert-bg);
  color: var(--invert-fg);
  font-weight: 600;
}
.btn--primary:hover:not(:disabled) { background: var(--invert-hover); }
.btn--primary:active:not(:disabled) { background: var(--invert-hover); }

/* Reserved for the few places that need the brand colour to carry meaning,
   such as a confirmed destructive-opposite (restore, approve). */
.btn--accent { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn--accent:hover:not(:disabled) { background: var(--accent-hover); }

.btn--secondary {
  background: transparent;
  border-color: var(--border);
  color: var(--text-primary);
}
.btn--secondary:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border-strong); }

.btn--ghost { background: transparent; color: var(--text-secondary); }
.btn--ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text-primary); }

.btn--danger { background: transparent; border-color: var(--border); color: var(--danger); }
.btn--danger:hover:not(:disabled) { background: color-mix(in srgb, var(--danger) 12%, transparent); border-color: var(--danger); }

.btn--lg { height: 48px; padding-inline: var(--space-8); font-size: var(--text-base); }
.btn--sm { height: 31px; padding-inline: var(--space-4); font-size: var(--text-xs); }
.btn--icon { width: 38px; padding: 0; }
.btn--icon.btn--sm { width: 31px; }
.btn--block { width: 100%; }

/* Inline spinner state. `.is-loading` hides the label and shows the ring so
   the button never changes width mid-request. */
.btn.is-loading { color: transparent !important; pointer-events: none; position: relative; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 15px; height: 15px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: nw-spin 640ms linear infinite;
  color: var(--accent-fg);
}
.btn--secondary.is-loading::after,
.btn--ghost.is-loading::after { color: var(--text-secondary); }

@keyframes nw-spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------------------
   Form fields
   --------------------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: var(--space-2); }

.field__label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-secondary);
}
.field__hint { font-size: var(--text-xs); color: var(--text-tertiary); line-height: 1.45; }
.field__error { font-size: var(--text-xs); color: var(--danger); line-height: 1.45; }

.input, .textarea, .select {
  width: 100%;
  padding: 0 var(--space-3);
  height: 40px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: var(--text-base);
  transition: border-color var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out);
}
.textarea { height: auto; min-height: 96px; padding: var(--space-3); resize: vertical; line-height: 1.55; }
.select {
  appearance: none;
  padding-right: var(--space-8);
  /* Chevron drawn with a gradient pair so it needs no external asset and
     inherits currentColor is not possible here. Kept neutral grey. */
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-tertiary) 50%),
    linear-gradient(135deg, var(--text-tertiary) 50%, transparent 50%);
  background-position: calc(100% - 17px) 17px, calc(100% - 12px) 17px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.input::placeholder, .textarea::placeholder { color: var(--text-quaternary); }
.input:hover, .textarea:hover, .select:hover { border-color: var(--border-strong); }
.input:focus, .textarea:focus, .select:focus {
  outline: none;
  border-color: var(--accent-border);
  background: var(--surface-2);
  box-shadow: 0 0 0 3px var(--accent-tint);
}
.input[aria-invalid="true"], .textarea[aria-invalid="true"] {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 12%, transparent);
}
.input:disabled, .textarea:disabled, .select:disabled { opacity: 0.5; cursor: not-allowed; }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: var(--space-3); cursor: pointer; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch__track {
  width: 38px; height: 22px; flex: none;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  position: relative;
  transition: background var(--dur-med) var(--ease-out), border-color var(--dur-med) var(--ease-out);
}
.switch__track::after {
  content: "";
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform var(--dur-med) var(--ease-spring), background var(--dur-med) var(--ease-out);
}
.switch input:checked + .switch__track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch__track::after { transform: translateX(16px); background: var(--accent-fg); }
.switch input:focus-visible + .switch__track { box-shadow: 0 0 0 3px var(--accent-tint); }

/* ---------------------------------------------------------------------------
   Surfaces
   --------------------------------------------------------------------------- */
.card {
  background: var(--surface-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
}
.card--raised { background: var(--surface-panel); box-shadow: var(--shadow-md); }
.card--interactive { transition: border-color var(--dur-med) var(--ease-out), background var(--dur-med) var(--ease-out), transform var(--dur-med) var(--ease-out); }
.card--interactive:hover { border-color: var(--border-strong); background: var(--surface-2); }

.card__title { font-size: var(--text-md); font-weight: 600; }
.card__sub   { font-size: var(--text-sm); color: var(--text-tertiary); margin-top: var(--space-1); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  height: 21px;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  border: 1px solid var(--border-subtle);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}
.badge--accent  { background: var(--accent-soft); border-color: var(--accent-border); color: var(--accent); }
.badge--info    { background: var(--accent-2-soft); border-color: transparent; color: var(--accent-2); }
.badge--warning { background: color-mix(in srgb, var(--warning) 14%, transparent); color: var(--warning); border-color: transparent; }
.badge--danger  { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); border-color: transparent; }

/* Empty state: used by every list before it has content. */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-3);
  padding: var(--space-16) var(--space-6);
}
.empty__icon {
  width: 52px; height: 52px;
  display: grid; place-items: center;
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  color: var(--text-tertiary);
  font-size: 20px;
}
.empty__title { font-size: var(--text-md); font-weight: 600; }
.empty__body  { font-size: var(--text-sm); color: var(--text-tertiary); max-width: 38ch; }

/* ---------------------------------------------------------------------------
   Skeletons: the "ambient" loading state.
   Two layers: a soft base tint plus a slow highlight sweep. The sweep is
   deliberately unhurried (1.6s) so a screen full of them feels like breathing
   rather than strobing.
   --------------------------------------------------------------------------- */
.skel {
  position: relative;
  overflow: hidden;
  background: var(--skeleton-base);
  border-radius: var(--radius-sm);
}
.skel::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, var(--skeleton-shine), transparent);
  animation: nw-shimmer 1.6s var(--ease-in-out) infinite;
}
@keyframes nw-shimmer { 100% { transform: translateX(100%); } }

.skel--text  { height: 11px; border-radius: var(--radius-pill); }
.skel--title { height: 17px; border-radius: var(--radius-pill); }
.skel--art   { aspect-ratio: 1; border-radius: var(--radius-md); }
.skel--line-70 { width: 70%; }
.skel--line-50 { width: 50%; }
.skel--line-35 { width: 35%; }

/* ---------------------------------------------------------------------------
   Entrance animations
   `.reveal` elements start invisible and are unhidden by an IntersectionObserver
   (core/reveal.js) adding `.is-revealed`. Without JS they must still be
   readable, so `.no-js` short-circuits the whole thing.
   --------------------------------------------------------------------------- */
.reveal {
  opacity: 0;
  transform: translateY(14px);
  transition: opacity var(--dur-lazy) var(--ease-out), transform var(--dur-lazy) var(--ease-out);
  transition-delay: var(--reveal-delay, 0ms);
}
.reveal.is-revealed { opacity: 1; transform: none; }
html.no-js .reveal { opacity: 1; transform: none; }

@keyframes nw-fade-up {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}
@keyframes nw-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes nw-pop {
  from { opacity: 0; transform: translateY(8px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

/* ---------------------------------------------------------------------------
   Modals
   Markup contract (see core/ui-utils.js openModal):
     <div class="modal" id="x" hidden>
       <div class="modal__scrim" data-close></div>
       <div class="modal__panel" role="dialog" aria-modal="true"> … </div>
     </div>
   --------------------------------------------------------------------------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: grid;
  place-items: center;
  padding: var(--space-6);
}
.modal__scrim {
  position: absolute;
  inset: 0;
  background: var(--scrim);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: nw-fade-in var(--dur-med) var(--ease-out);
}
.modal__panel {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: min(86vh, 780px);
  display: flex;
  flex-direction: column;
  background: var(--surface-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  animation: nw-pop var(--dur-slow) var(--ease-out);
  overflow: hidden;
}
.modal__panel--wide { max-width: 720px; }
.modal__panel--full { max-width: 940px; }

.modal.is-closing .modal__scrim,
.modal.is-closing .modal__panel { animation: nw-fade-in var(--dur-fast) var(--ease-out) reverse forwards; }

.modal__head {
  display: flex;
  align-items: flex-start;
  gap: var(--space-4);
  padding: var(--space-6) var(--space-6) var(--space-4);
  flex: none;
}
.modal__title { font-size: var(--text-lg); font-weight: 600; }
.modal__sub   { font-size: var(--text-sm); color: var(--text-tertiary); margin-top: var(--space-1); }
.modal__close {
  margin-left: auto;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-tertiary);
  flex: none;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.modal__close:hover { background: var(--surface-2); color: var(--text-primary); }

.modal__body {
  padding: 0 var(--space-6) var(--space-6);
  overflow-y: auto;
  flex: 1 1 auto;
}
.modal__foot {
  display: flex;
  gap: var(--space-3);
  justify-content: flex-end;
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border-subtle);
  background: var(--surface-1);
  flex: none;
}

body.modal-open { overflow: hidden; }

/* ---------------------------------------------------------------------------
   Toasts
   --------------------------------------------------------------------------- */
.toast-host {
  position: fixed;
  bottom: var(--space-6);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column-reverse;
  gap: var(--space-2);
  pointer-events: none;
  width: min(420px, calc(100vw - var(--space-8)));
}
.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  pointer-events: auto;
  animation: nw-pop var(--dur-slow) var(--ease-spring);
}
.toast.is-leaving { animation: nw-fade-in var(--dur-med) var(--ease-out) reverse forwards; }
.toast__icon { flex: none; width: 16px; text-align: center; }
.toast--success .toast__icon { color: var(--success); }
.toast--error   .toast__icon { color: var(--danger); }
.toast--info    .toast__icon { color: var(--info); }
.toast__msg { flex: 1 1 auto; }

/* ---------------------------------------------------------------------------
   Dropdown menu (avatar menu, row actions)
   --------------------------------------------------------------------------- */
.menu {
  position: absolute;
  min-width: 216px;
  padding: var(--space-2);
  background: var(--surface-overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-dropdown);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  animation: nw-pop var(--dur-med) var(--ease-out);
}
.menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-xs);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  text-align: left;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.menu__item:hover { background: var(--surface-2); color: var(--text-primary); }
.menu__item i { width: 15px; font-size: 13px; text-align: center; color: var(--text-tertiary); }
.menu__item:hover i { color: inherit; }
.menu__item--danger { color: var(--danger); }
.menu__item--danger i { color: var(--danger); }
.menu__sep { height: 1px; margin: var(--space-2) 0; background: var(--border-subtle); }
.menu__label {
  padding: var(--space-2) var(--space-3) var(--space-1);
  font-size: var(--text-xs);
  color: var(--text-quaternary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* ---------------------------------------------------------------------------
   Global loader: shown while the auth gate resolves, then faded out.
   --------------------------------------------------------------------------- */
.global-loader {
  position: fixed;
  inset: 0;
  z-index: var(--z-loader);
  display: grid;
  place-items: center;
  background: var(--bg);
  transition: opacity var(--dur-slow) var(--ease-out), visibility var(--dur-slow);
}
.global-loader.is-done { opacity: 0; visibility: hidden; }
.global-loader__inner { display: flex; flex-direction: column; align-items: center; gap: var(--space-4); }
.global-loader__mark {
  width: 44px; height: 44px;
  animation: nw-breathe 2.6s var(--ease-in-out) infinite;
}
.global-loader__status { font-size: var(--text-sm); color: var(--text-tertiary); }

@keyframes nw-breathe {
  0%, 100% { opacity: 0.5; transform: scale(0.96); }
  50%      { opacity: 1;   transform: scale(1); }
}

/* Thin top progress bar for route/section transitions. */
.route-bar {
  position: fixed; top: 0; left: 0;
  height: 2px; width: 0;
  background: var(--accent);
  z-index: var(--z-loader);
  transition: width var(--dur-slow) var(--ease-out), opacity var(--dur-med) var(--ease-out);
  box-shadow: 0 0 10px var(--accent-glow);
}

/* ---------------------------------------------------------------------------
   Responsive
   --------------------------------------------------------------------------- */
@media (max-width: 720px) {
  h1 { font-size: var(--text-3xl); }
  h2 { font-size: var(--text-2xl); }
  .nw-container { padding-inline: var(--space-4); }
  .modal { padding: 0; align-items: flex-end; }
  .modal__panel {
    max-width: none;
    max-height: 92vh;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    animation: nw-sheet-up var(--dur-slow) var(--ease-out);
  }
  .modal__foot { flex-direction: column-reverse; }
  .modal__foot .btn { width: 100%; }
  .toast-host { bottom: var(--space-4); }
}

@keyframes nw-sheet-up {
  from { transform: translateY(18px); opacity: 0; }
  to   { transform: none; opacity: 1; }
}
