/* ===================================================================
   DocuSense Design System — "The Reading Room"

   An academic journal aesthetic: warm paper, hairline rules, serif
   display type, and one accent used the way a reader's pen would be.
   Deliberately not a dark neon SaaS product.

   Foundations: Swiss editorial grid · Crimson Pro / Atkinson Hyperlegible
   · scholarly navy + citation gold on book stock.
   =================================================================== */

/* ---------- Tokens: light (primary) ---------- */
:root {
  /* Surfaces — warm paper rather than blue-grey screen */
  --paper:          #faf7f2;
  --paper-raised:   #fffdfa;
  --paper-sunk:     #f2ede4;
  --paper-wash:     #ece5d9;

  /* Ink */
  --ink:            #14212e;
  --ink-soft:       #4a5b6d;
  /* Measured: #7d8b99 gave only 2.99:1 on the sunk paper, failing AA for
     the small labels that use it. This clears 4.5:1 on every surface. */
  --ink-faint:      #5a6b7c;
  --ink-inverse:    #faf7f2;

  /* Structure */
  --navy:           #1e3a5f;
  --navy-deep:      #16293f;

  /* The reader's pen — used sparingly, like a citation mark */
  --gold:           #a94e08;
  /* Hover darkens rather than lightens. #d97706 left the accent button's
     near-white label at 3.14:1. */
  --gold-bright:    #8f4205;
  --gold-wash:      rgba(169, 78, 8, 0.10);
  --gold-edge:      rgba(169, 78, 8, 0.32);
  /* Whatever sits on a gold fill. Gold inverts between themes and this has to
     invert with it -- see .btn-accent. */
  --on-gold:        #fffdfa;

  /* Rules and hairlines — warm, never pure grey */
  --rule:           #ded6c9;
  --rule-strong:    #c9bfae;
  --rule-hair:      rgba(20, 33, 46, 0.09);

  /* Status */
  --ok:             #2f6f4f;
  --warn:           #a16207;
  --err:            #a52a2a;
  --err-wash:       rgba(165, 42, 42, 0.08);

  /* Type */
  --serif:  'Crimson Pro', 'Iowan Old Style', Georgia, 'Times New Roman', serif;
  --sans:   'Atkinson Hyperlegible', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --mono:   'SFMono-Regular', ui-monospace, 'JetBrains Mono', Menlo, Consolas, monospace;

  /* Modular scale (1.25) */
  --t-xs:   0.75rem;
  --t-sm:   0.875rem;
  --t-base: 1rem;
  --t-md:   1.125rem;
  --t-lg:   1.375rem;
  --t-xl:   1.75rem;
  --t-2xl:  2.25rem;
  --t-3xl:  3rem;
  --t-4xl:  4rem;
  --t-5xl:  5.5rem;

  --lead-tight:   1.1;
  --lead-snug:    1.3;
  --lead-normal:  1.55;
  --lead-relaxed: 1.75;

  /* Spacing — 8px grid */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 2.5rem;
  --space-8: 3rem;
  --space-10: 4rem;
  --space-12: 6rem;
  --space-16: 8rem;

  /* Geometry — paper has corners, not pills */
  --r-sm: 2px;
  --r-md: 3px;
  --r-lg: 4px;

  /* Elevation reads as paper lifting, so shadows stay warm and tight */
  --lift-1: 0 1px 2px rgba(20, 33, 46, 0.05);
  --lift-2: 0 2px 8px rgba(20, 33, 46, 0.07), 0 1px 2px rgba(20, 33, 46, 0.05);
  --lift-3: 0 8px 28px rgba(20, 33, 46, 0.10), 0 2px 6px rgba(20, 33, 46, 0.06);

  /* Motion — entry decelerates, exit is quicker */
  --ease-out:  cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in:   cubic-bezier(0.4, 0, 1, 1);
  --ease-soft: cubic-bezier(0.4, 0, 0.2, 1);
  --dur-fast:  160ms;
  --dur-base:  260ms;
  --dur-slow:  480ms;
  --dur-page:  700ms;

  --measure: 68ch;   /* comfortable reading line length */
  --gutter:  clamp(1.25rem, 5vw, 4.5rem);
  --shell:   1240px;
}

/* ---------- Tokens: dark (a dim reading lamp, not a terminal) ---------- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --paper:        #15120e;
    --paper-raised: #1d1914;
    --paper-sunk:   #100e0b;
    --paper-wash:   #262019;

    --ink:          #f2ece1;
    --ink-soft:     #b9ad9c;
    --ink-faint:    #8f8475;
    --ink-inverse:  #15120e;

    --navy:         #9db8d6;
    --navy-deep:    #c3d4e8;

    --gold:         #e0973f;
    --gold-bright:  #f0ad55;
    --gold-wash:    rgba(224, 151, 63, 0.14);
    --gold-edge:    rgba(224, 151, 63, 0.38);
    /* Dark theme's gold is a light amber, so a near-white label on it reads
       2.39:1. The text on a gold fill goes dark here. */
    --on-gold:      #15120e;

    --rule:         #342c22;
    --rule-strong:  #4a3f31;
    --rule-hair:    rgba(242, 236, 225, 0.10);

    --ok:           #6fbf8f;
    --warn:         #d9a441;
    --err:          #e2857f;
    --err-wash:     rgba(226, 133, 127, 0.12);

    --lift-1: 0 1px 2px rgba(0, 0, 0, 0.4);
    --lift-2: 0 2px 10px rgba(0, 0, 0, 0.45);
    --lift-3: 0 10px 34px rgba(0, 0, 0, 0.55);
  }
}

[data-theme='dark'] {
  --paper:        #15120e;
  --paper-raised: #1d1914;
  --paper-sunk:   #100e0b;
  --paper-wash:   #262019;

  --ink:          #f2ece1;
  --ink-soft:     #b9ad9c;
  --ink-faint:    #8f8475;
  --ink-inverse:  #15120e;

  --navy:         #9db8d6;
  --navy-deep:    #c3d4e8;

  --gold:         #e0973f;
  --gold-bright:  #f0ad55;
  --gold-wash:    rgba(224, 151, 63, 0.14);
  --gold-edge:    rgba(224, 151, 63, 0.38);
  /* Dark theme's gold is a light amber, so a near-white label on it reads
     2.39:1. The text on a gold fill goes dark here. */
  --on-gold:      #15120e;

  --rule:         #342c22;
  --rule-strong:  #4a3f31;
  --rule-hair:    rgba(242, 236, 225, 0.10);

  --ok:           #6fbf8f;
  --warn:         #d9a441;
  --err:          #e2857f;
  --err-wash:     rgba(226, 133, 127, 0.12);

  --lift-1: 0 1px 2px rgba(0, 0, 0, 0.4);
  --lift-2: 0 2px 10px rgba(0, 0, 0, 0.45);
  --lift-3: 0 10px 34px rgba(0, 0, 0, 0.55);
}

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

* { margin: 0; padding: 0; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: var(--t-base);
  line-height: var(--lead-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  min-height: 100vh;

  /* Faint paper grain: two offset gradients, cheap and non-repeating enough
     to read as stock rather than a visible tile. */
  background-image:
    radial-gradient(circle at 18% 12%, var(--gold-wash) 0%, transparent 42%),
    radial-gradient(circle at 86% 78%, rgba(30, 58, 95, 0.045) 0%, transparent 46%);
  background-attachment: fixed;
}

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

/* A component setting `display` would otherwise override the UA rule for
   [hidden] and leave the element visible. */
[hidden] { display: none !important; }

button, input, textarea, select { font: inherit; color: inherit; }

button { background: none; border: none; cursor: pointer; }

a { color: inherit; text-decoration: none; }

ul, ol { list-style: none; }

/* ---------- Typography ---------- */
h1, h2, h3, h4 {
  font-family: var(--serif);
  font-weight: 600;
  line-height: var(--lead-tight);
  letter-spacing: -0.015em;
  color: var(--ink);
  text-wrap: balance;   /* stops orphaned words on headline last lines */
}

h1 { font-size: clamp(2.5rem, 6vw, var(--t-5xl)); font-weight: 500; }
h2 { font-size: clamp(1.875rem, 4vw, var(--t-3xl)); }
h3 { font-size: var(--t-xl); }
h4 { font-size: var(--t-lg); }

p { text-wrap: pretty; }

/* Small-caps section label with a rule — the journal's running head */
.eyebrow {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--sans);
  font-size: var(--t-xs);
  font-weight: 700;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--gold);
}

.eyebrow::before {
  content: '';
  width: 2rem;
  height: 1px;
  background: var(--gold);
  opacity: 0.55;
}

/* Section number set like a footnote marker */
.folio {
  font-family: var(--mono);
  font-size: var(--t-xs);
  color: var(--ink-faint);
  letter-spacing: 0.08em;
}

.lede {
  font-family: var(--serif);
  font-size: var(--t-lg);
  line-height: var(--lead-relaxed);
  color: var(--ink-soft);
  max-width: var(--measure);
}

/* The pen stroke: a highlight that draws itself across a phrase */
.mark {
  position: relative;
  display: inline;
  color: var(--ink);
  background-image: linear-gradient(var(--gold-wash), var(--gold-wash));
  background-repeat: no-repeat;
  background-size: 0% 100%;
  background-position: 0 0;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  padding: 0 0.12em;
  margin: 0 -0.12em;
  transition: background-size var(--dur-slow) var(--ease-out);
}

.mark.is-drawn { background-size: 100% 100%; }

/* Underline that draws in on hover, like ruling a line under a reference */
.rule-link {
  position: relative;
  color: var(--ink);
  padding-bottom: 1px;
}

.rule-link::after {
  content: '';
  position: absolute;
  left: 0; bottom: 0;
  width: 100%; height: 1px;
  background: var(--gold);
  transform: scaleX(0);
  transform-origin: right;
  transition: transform var(--dur-base) var(--ease-out);
}

.rule-link:hover::after,
.rule-link:focus-visible::after {
  transform: scaleX(1);
  transform-origin: left;
}

/* ---------- Wordmark ---------- */
/* Shared by the landing masthead, the auth panel, and the app sidebar. */
.wordmark {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
  color: var(--ink);
}

.wordmark__mark { display: flex; flex-shrink: 0; color: var(--navy); }

.wordmark__text {
  font-family: var(--serif);
  font-size: var(--t-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

/* ---------- Layout ---------- */
.shell {
  width: 100%;
  max-width: var(--shell);
  margin-inline: auto;
  padding-inline: var(--gutter);
}

.measure { max-width: var(--measure); }

.hairline {
  border: 0;
  border-top: 1px solid var(--rule);
}

/* ---------- Buttons ---------- */
.btn {
  --btn-bg: transparent;
  --btn-fg: var(--ink);
  --btn-edge: var(--rule-strong);

  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 44px;                 /* touch target */
  padding: var(--space-3) var(--space-5);
  background: var(--btn-bg);
  color: var(--btn-fg);
  border: 1px solid var(--btn-edge);
  border-radius: var(--r-md);
  font-family: var(--sans);
  font-size: var(--t-sm);
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition:
    background-color var(--dur-fast) var(--ease-soft),
    color var(--dur-fast) var(--ease-soft),
    border-color var(--dur-fast) var(--ease-soft),
    transform var(--dur-fast) var(--ease-soft);
}

.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }

.btn-primary {
  --btn-bg: var(--navy);
  --btn-fg: var(--ink-inverse);
  --btn-edge: var(--navy);
}

/* In dark mode --navy becomes a light blue, so the label must go dark to
   stay readable. Scoped inside the media query: unscoped, the :not() also
   matched a light-OS visitor who had made no explicit choice, putting dark
   text on the dark navy button. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .btn-primary { --btn-fg: #15120e; }
}

[data-theme='dark'] .btn-primary { --btn-fg: #15120e; }

.btn-primary:hover { --btn-bg: var(--navy-deep); --btn-edge: var(--navy-deep); }

.btn-accent {
  --btn-bg: var(--gold);
  /* Not a literal. --gold is a dark amber in light mode and a light one in
     dark mode, so a hardcoded near-white label measured 2.39:1 in dark and
     3.14:1 on hover in light. Same trap as .btn-primary below. */
  --btn-fg: var(--on-gold);
  --btn-edge: var(--gold);
}

.btn-accent:hover { --btn-bg: var(--gold-bright); --btn-edge: var(--gold-bright); }

.btn-quiet {
  --btn-bg: transparent;
  --btn-edge: var(--rule);
}

.btn-quiet:hover { --btn-bg: var(--paper-sunk); --btn-edge: var(--rule-strong); }

.btn-lg { min-height: 52px; padding: var(--space-4) var(--space-6); font-size: var(--t-base); }
.btn-sm { min-height: 38px; padding: var(--space-2) var(--space-4); font-size: var(--t-xs); }

.btn-icon {
  min-height: 44px;
  min-width: 44px;
  padding: var(--space-2);
  border-color: transparent;
}

.btn-icon:hover { background: var(--paper-sunk); border-color: var(--rule); }

.btn[disabled] { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ---------- Forms ---------- */
.field { position: relative; margin-bottom: var(--space-5); }

.field label {
  display: block;
  font-size: var(--t-xs);
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: var(--space-2);
}

.field input {
  width: 100%;
  min-height: 48px;
  padding: var(--space-3) var(--space-4);
  background: var(--paper-raised);
  border: 1px solid var(--rule-strong);
  border-radius: var(--r-md);
  font-size: var(--t-base);
  color: var(--ink);
  transition:
    border-color var(--dur-fast) var(--ease-soft),
    box-shadow var(--dur-fast) var(--ease-soft);
}

.field input::placeholder { color: var(--ink-faint); }

.field input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-wash);
}

/* ---------- Focus ---------- */
:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
  border-radius: var(--r-sm);
}

/* ---------- Utilities ---------- */
.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;
}

.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-4);
  z-index: 200;
  padding: var(--space-3) var(--space-4);
  background: var(--navy);
  color: var(--ink-inverse);
  border-radius: var(--r-md);
}

.skip-link:focus { top: var(--space-4); }

/* ---------- Scroll reveal ---------- */
/* Scoped to .js-reveal, which an inline head script sets. If scripting is
   unavailable, blocked, or errors before it runs, the class is never added
   and every element simply renders — content must never depend on JS to be
   visible. */
.js-reveal [data-reveal] {
  opacity: 0;
  transform: translateY(18px);
  transition:
    opacity var(--dur-slow) var(--ease-out),
    transform var(--dur-slow) var(--ease-out);
  transition-delay: var(--reveal-delay, 0ms);
}

.js-reveal [data-reveal].is-in { opacity: 1; transform: none; }

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }

  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  /* Render the finished state rather than the start of the animation. */
  .js-reveal [data-reveal] { opacity: 1; transform: none; }
  .mark { background-size: 100% 100%; }
}

/* ── Theme toggle ─────────────────────────────────────────────────────
   On every page (js/theme.js). The icon shows the mode you would switch
   *to*: a moon in light mode, a sun in dark. */
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
[data-theme='dark'] .theme-toggle .icon-sun { display: block; }
[data-theme='dark'] .theme-toggle .icon-moon { display: none; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .theme-toggle .icon-sun { display: block; }
  :root:not([data-theme='light']) .theme-toggle .icon-moon { display: none; }
}

/* ── Signed-in state ──────────────────────────────────────────────────
   Pages that can be seen either way mark their alternatives; <html> gets
   .is-signed-in before paint when a session token is stored. */
html:not(.is-signed-in) [data-signed-in],
html.is-signed-in [data-signed-out] { display: none !important; }
