/* ---------- Tokens ---------- */
:root {
  --paper: #ece3cf;        /* aged laid paper */
  --ink: #2b1d17;          /* brown-black printing ink */
  --ink-soft: #6a5646;
  --oxblood: #5b1621;      /* spine, cover cloth, button */
  --oxblood-deep: #3a0c14;
  --gilt: #b8914a;         /* gold tooling */
  --gilt-light: #dcc083;
  --leaf: #17100e;         /* dark inner page the form sits on */
  --leaf-text: #f1e8d6;
  --leaf-muted: #b5a38d;
  --error: #f2a090;
  --success: #b4d8a8;

  --font-display: "Libre Caslon Display", "Libre Caslon Text", Georgia, serif;
  --font-text: "Libre Caslon Text", Georgia, "Times New Roman", serif;
  --font-ui: "Instrument Sans", "Segoe UI", system-ui, -apple-system, sans-serif; /* form controls */

  --field: #261a17;        /* input boxes, a step lighter than the page */
  --field-line: rgba(241, 232, 214, 0.24);

  --gutter: 16px;
  --board: 10px;           /* how far the cover boards stand out past the page */
}

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

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--font-text);
  font-size: 1rem;
  line-height: 1.6;
  background-color: var(--paper);
  /* Laid paper: fine laid lines, wider chain lines, and uneven light */
  background-image:
    repeating-linear-gradient(90deg, rgba(43, 29, 23, 0.04) 0 1px, transparent 1px 28px),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.22) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse 70% 60% at 25% 20%, rgba(255, 255, 255, 0.45), transparent 70%),
    radial-gradient(ellipse 60% 50% at 85% 95%, rgba(91, 22, 33, 0.07), transparent 70%);
}

[hidden] { display: none !important; }

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

a { color: inherit; text-underline-offset: 3px; }

:focus-visible { outline: 2px solid var(--gilt); outline-offset: 3px; }

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

/* ---------- Layout ---------- */
.desk {
  width: 100%;
  max-width: 600px; /* stacked layout: one comfortable column, even on tablets */
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(28px, env(safe-area-inset-top)) max(var(--gutter), env(safe-area-inset-right)) max(32px, env(safe-area-inset-bottom)) max(calc(var(--gutter) + 14px), env(safe-area-inset-left));
  display: grid;
  gap: 40px;
  align-content: start;
}

/* ---------- Title page (left) ---------- */
.title-page {
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.headline {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(2.75rem, 1.6rem + 4.6vw, 5.75rem);
  line-height: 0.98;
  letter-spacing: -0.015em;
  color: var(--oxblood-deep);
  text-wrap: balance;
}

.blurb {
  margin: 0;
  max-width: 36ch;
  font-size: 1.0625rem;
  line-height: 1.7;
  color: var(--ink-soft);
}

/* The publisher's mark sits at the foot of the title page, as in a printed book */
/* Logo and credits side by side, so the foot of the page stays short */
.imprint {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

/* Each credit stays on one line; the logo gives up width on narrow screens instead */
.imprint img { flex: 0 1 200px; min-width: 112px; width: 200px; border-radius: 4px; }
.imprint p { flex: none; white-space: nowrap; }
.imprint p { margin: 0; }

/* ---------- The book (right) ---------- */
/* On phones the book comes first so the form is reachable without scrolling */
.book {
  order: -1;
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

/* Cover board: the front face is the cloth cover, the back face is the inside with the logo */
.cover { position: relative; }

.cover-front { display: none; }

.cover-back {
  height: 100%;
  padding: var(--board);
  background: var(--oxblood);
  border-radius: 6px 6px 0 0;
}

.pastedown {
  height: 100%;
  min-height: 176px;
  display: grid;
  place-items: center;
  padding: 12px;
  background: #000;
  border-radius: 2px;
}

/* The logo's own background is pure black, so it sits on the black panel without a visible edge */
.pastedown img { width: min(210px, 62vw); }

/* Spine with raised gold bands. On phones it runs down the left edge. */
.spine {
  position: absolute;
  z-index: 3;
  top: 0;
  bottom: 0;
  left: -14px;
  width: 16px;
  border-radius: 6px 0 0 6px;
  background: linear-gradient(90deg, var(--oxblood-deep), var(--oxblood) 45%, #47101a);
  box-shadow: inset -1px 0 0 rgba(220, 192, 131, 0.35);
}

/* Four raised bands: each is a rounded ridge in the leather, lit from above,
   with a fine gold line tooled along either side of it */
.spine::before {
  --band: linear-gradient(180deg,
    var(--gilt) 0 1px,
    transparent 1px 2px,
    #8d2c3a 2px 3.5px,
    #6c1a27 3.5px 6px,
    #3a0c14 6px 8.5px,
    transparent 8.5px 9.5px,
    var(--gilt) 9.5px 10.5px);
  content: "";
  position: absolute;
  inset: 9% 0;
  background: var(--band), var(--band), var(--band), var(--band);
  background-size: 100% 10.5px;
  background-repeat: no-repeat;
  background-position: 0 0%, 0 33.333%, 0 66.667%, 0 100%;
  filter: drop-shadow(0 1px 0.5px rgba(0, 0, 0, 0.45));
}

/* The page block: board edge, gilded fore-edge, then the dark page itself */
.leaf {
  position: relative;
  padding: 0 var(--board) var(--board);
  background: var(--oxblood);
  border-radius: 0 0 6px 6px;
  box-shadow: 0 28px 50px -26px rgba(43, 29, 23, 0.6);
}

.page {
  position: relative;
  padding: 30px 22px 30px;
  background: var(--leaf);
  color: var(--leaf-text);
  border-radius: 0 0 2px 2px;
  display: grid;
  gap: 26px;
  align-content: center;
}

/* Gilded page edges, seen along the open side of the page block: the stacked
   pages show as fine, uneven lines under a gold sheen that catches the light */
.page::after {
  content: "";
  position: absolute;
  top: 5px;
  right: -5px;
  bottom: 5px;
  width: 5px;
  background:
    linear-gradient(180deg, rgba(255, 246, 214, 0.35), transparent 22% 78%, rgba(40, 20, 5, 0.35)),
    repeating-linear-gradient(180deg, rgba(70, 45, 12, 0.5) 0 0.5px, transparent 0.5px 2.1px),
    repeating-linear-gradient(180deg, rgba(255, 240, 200, 0.3) 0 0.5px, transparent 0.5px 3.3px),
    linear-gradient(90deg, #8f6a2a, #e8cf8f 45%, #c29d55 70%, #8f6a2a);
  border-radius: 0 2px 2px 0;
  box-shadow: 1px 0 1px rgba(0, 0, 0, 0.35);
}

.page-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(1.9rem, 1.5rem + 1.4vw, 2.4rem);
  line-height: 1.1;
  color: var(--leaf-text);
}

.page-lede {
  margin: 10px 0 0;
  color: var(--leaf-muted);
  font-size: 0.9375rem;
  line-height: 1.55;
  max-width: 40ch;
}

/* ---------- Form ---------- */
/* Everything on the page except its heading uses the sans, so it reads as a form, not a book */
.page { font-family: var(--font-ui); }

.auth-form { display: grid; gap: 20px; }

.field { display: grid; gap: 8px; }

.field-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

.field label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--leaf-text);
}

.field-row a {
  font-size: 0.875rem;
  color: var(--gilt-light);
}

.field-row a:hover { color: var(--leaf-text); }

.control { position: relative; }

.control input {
  width: 100%;
  height: 52px;
  padding: 0 16px;
  border: 1px solid var(--field-line);
  border-radius: 8px;
  background: var(--field);
  color: var(--leaf-text);
  font: inherit;
  font-size: 16px; /* 16px stops iOS zooming into the field */
  transition: border-color 0.15s, box-shadow 0.15s;
}

.control input::placeholder { color: rgba(181, 163, 141, 0.75); }

.control input:hover { border-color: rgba(241, 232, 214, 0.4); }

.control input:focus {
  outline: none;
  border-color: var(--gilt-light);
  box-shadow: 0 0 0 3px rgba(220, 192, 131, 0.22);
}

.control input[aria-invalid="true"] { border-color: var(--error); background: #2c1916; }
.control input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px rgba(242, 160, 144, 0.22); }

/* Keep browser autofill from painting a pale box over the field */
.control input:-webkit-autofill {
  -webkit-text-fill-color: var(--leaf-text);
  -webkit-box-shadow: 0 0 0 40px var(--field) inset;
  caret-color: var(--leaf-text);
}

input[type="password"]::-ms-reveal { display: none; }

.control--password input { padding-right: 52px; }

.eye {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--leaf-muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.eye:hover, .eye[aria-pressed="true"] { color: var(--gilt-light); }

.eye svg { width: 21px; height: 21px; }
.eye .icon-hide { display: none; }
.eye[aria-pressed="true"] .icon-show { display: none; }
.eye[aria-pressed="true"] .icon-hide { display: block; }

.error {
  margin: 0;
  color: var(--error);
  font-size: 0.875rem;
  line-height: 1.45;
}

.error:empty { display: none; }

.button {
  width: 100%;
  min-height: 52px;
  margin-top: 6px;
  padding: 0 20px;
  border: 1px solid var(--gilt);
  border-radius: 8px;
  background: #7a1f2c;
  color: #fbefd6;
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s;
}

.button:hover { background-color: #8c2534; }
.button:active { transform: translateY(1px); }
.button[aria-busy="true"] { cursor: progress; opacity: 0.85; }

.form-status {
  margin: 0;
  color: var(--success);
  font-size: 0.875rem;
  text-align: center;
}

.form-status:empty { display: none; }

/* ---------- Forgot password extras ---------- */
.sent { display: grid; gap: 14px; }
.sent .page-title:focus { outline: none; }
.sent p { margin: 0; color: var(--leaf-muted); }
.sent strong { color: var(--leaf-text); font-weight: 600; overflow-wrap: anywhere; }

.back-link {
  justify-self: start;
  font-size: 0.9375rem;
  color: var(--gilt-light);
}

.back-link:hover { color: var(--leaf-text); }

/* ---------- The turning page: login on the front, "back to log in" on the reverse ---------- */
.leaf { grid-column: 1; grid-row: 2; }

/* On phones the sheet lies exactly over the reset page, board edge and all */
.sheet {
  grid-column: 1;
  grid-row: 2;
  position: relative;
  z-index: 2;
  padding: 0 var(--board) var(--board);
  background: var(--oxblood);
  border-radius: 0 0 6px 6px;
}

.sheet-face, .sheet-front .page { height: 100%; }

.sheet-back { display: none; }

.page--verso { justify-items: start; }

/* "Lighten" lets the page show through the logo's black square, so there's no
   visible edge and nothing has to be cropped */
.verso-logo {
  width: min(250px, 66%);
  margin-left: -6px; /* the artwork has a little black margin of its own */
  mix-blend-mode: lighten;
}

.button--quiet {
  width: auto;
  padding: 0 28px;
  background: transparent;
  color: var(--gilt-light);
}

.button--quiet:hover { background-color: rgba(220, 192, 131, 0.1); }

/* ---------- Phones and tablets: the logo on black, the form on a paper page ---------- */
@media (max-width: 1199px) {
  /* Edge to edge: the black logo area runs to the top of the screen */
  .desk {
    max-width: none;
    padding: 0;
    gap: 0;
    grid-template-rows: auto 1fr; /* on tall screens the paper runs to the bottom */
    grid-template-columns: minmax(0, 1fr); /* never wider than the screen, whatever is inside */
  }

  /* Fine print scales down on narrow phones so each credit still fits on one line */
  .imprint { gap: 12px; font-size: clamp(0.6875rem, 3.3vw, 0.8125rem); }
  .imprint img { min-width: 84px; }

  .spine, .page::after { display: none; }

  .cover-back { padding: 0; border-radius: 0; background: #000; }

  .pastedown {
    min-height: 0;
    padding: max(28px, env(safe-area-inset-top)) 16px 58px; /* the paper page overlaps the bottom */
  }

  .pastedown img { width: min(250px, 66vw); }

  /* The paper page: both the login and the reset page lie in this one place */
  .leaf, .sheet {
    margin-top: -30px;
    padding: 0;
    border-radius: 22px 22px 0 0;
    background-color: var(--paper);
    background-image:
      repeating-linear-gradient(90deg, rgba(43, 29, 23, 0.04) 0 1px, transparent 1px 28px),
      repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.22) 0 1px, transparent 1px 3px);
    /* Shadow falls only upward onto the black, never down onto the paper below */
    box-shadow: 0 -14px 24px -10px rgba(0, 0, 0, 0.55);

    /* Ink on paper: the form's colours flip from the dark book page */
    --leaf-text: var(--ink);
    --leaf-muted: var(--ink-soft);
    --field: #fbf8f1;
    --field-line: rgba(43, 29, 23, 0.26);
    --gilt-light: var(--oxblood);
    --error: #a3261c;
    --success: #2f6b3a;
  }

  .leaf { position: relative; z-index: 2; }

  /* The ribbon bookmark hangs over the top edge of the page */
  .leaf::before, .sheet::before {
    content: "";
    position: absolute;
    z-index: 3;
    top: -8px;
    right: 28px;
    width: 22px;
    height: 92px;
    background: linear-gradient(90deg, #4a0f19, #6e1a26 50%, #4a0f19);
    clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 86%, 0 100%);
    filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.3));
  }

  .leaf .page, .sheet .page {
    max-width: 560px;
    margin: 0 auto;
    padding: 34px 24px 30px;
    background: transparent;
  }

  .page-title { color: var(--oxblood-deep); }

  .control input::placeholder { color: #9a8c7c; }
  .control input:hover { border-color: rgba(43, 29, 23, 0.45); }
  .control input:focus { box-shadow: 0 0 0 3px rgba(91, 22, 33, 0.16); }
  .control input[aria-invalid="true"] { background: #fbf1ee; }
  .control input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px rgba(163, 38, 28, 0.16); }

  /* Black behind everything, so the paper always rises over black */
  body { background: #000; }

  /* The rest of the title page carries on down the same paper, full width */
  .title-page {
    padding: 12px max(24px, calc((100% - 512px) / 2)) max(36px, env(safe-area-inset-bottom));
    gap: 20px;
    background-color: var(--paper);
    background-image:
      repeating-linear-gradient(90deg, rgba(43, 29, 23, 0.04) 0 1px, transparent 1px 28px),
      repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.22) 0 1px, transparent 1px 3px);
    animation: page-rise 0.9s cubic-bezier(0.2, 0.75, 0.2, 1) 0.2s both;
  }

  .headline { font-size: clamp(2.25rem, 1.6rem + 3vw, 3.25rem); }

  /* Taken out of the flow, not just hidden, so the reset page sets the height */
  .book.is-forgot .sheet { display: none; }

  /* The field label already says what to type */
  .sheet-front .page-lede { display: none; }

  /* A comfortable tap area for a short link */
  .field-row a { padding: 10px 0; margin: -10px 0; }

  /* On load, the paper page rises from the bottom. Once a page has been turned it doesn't again. */
  .book:not(.has-turned) .leaf,
  .book:not(.has-turned) .sheet {
    animation: page-rise 0.9s cubic-bezier(0.2, 0.75, 0.2, 1) 0.2s both;
  }

  /* No room for a two-page spread: the new page swings in from the spine instead */
  .book.has-turned.is-forgot .leaf .page,
  .book.has-turned:not(.is-forgot) .sheet .page {
    transform-origin: left center;
    animation: page-in 0.6s cubic-bezier(0.2, 0.7, 0.2, 1) both;
  }
}

@keyframes page-rise {
  from { transform: translateY(55vh); }
  to   { transform: none; }
}

@keyframes page-in {
  from { transform: perspective(1400px) rotateY(-75deg); opacity: 0; }
  to   { transform: none; opacity: 1; }
}

/* ---------- Desktop: title page beside an open book ---------- */
/* Below 1200px a two-page spread leaves the fields too narrow, so it stays stacked */
@media (min-width: 1200px) {
  .desk {
    max-width: 1480px;
    grid-template-columns: minmax(0, 1fr) minmax(0, 2.1fr);
    align-items: center;
    align-content: center;
    gap: clamp(36px, 4vw, 72px);
    padding: 48px clamp(32px, 4vw, 64px);
  }

  .title-page {
    align-self: stretch;
    justify-content: center;
    padding: 24px 0;
    container-type: inline-size;
  }

  /* Sized to its column so "worth more." always holds one line beside the bigger book */
  .headline { font-size: clamp(2.75rem, 21cqi, 6rem); }

  .title-page .imprint { margin-top: 40px; }

  .book {
    order: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    min-height: clamp(600px, 80vh, 780px);
    perspective: 2400px;
  }

  /* The cover is hinged on the spine. It sits over the page and swings open to the left. */
  .cover {
    grid-column: 2;
    grid-row: 1;
    z-index: 2;
    transform-origin: left center;
    transform-style: preserve-3d;
    transform: rotateY(-180deg);
    animation: open-cover 1.6s cubic-bezier(0.55, 0.05, 0.2, 1) 0.45s both;
    pointer-events: none;
  }

  .cover-front, .cover-back {
    position: absolute;
    inset: 0;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }

  .cover-front {
    display: grid;
    place-items: center;
    padding: 48px 40px;
    border-radius: 2px 8px 8px 2px;
    background:
      repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.07) 0 1px, transparent 1px 3px),
      repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.03) 0 1px, transparent 1px 3px),
      var(--oxblood);
    box-shadow: 0 30px 50px -26px rgba(43, 29, 23, 0.7), inset 0 0 40px rgba(0, 0, 0, 0.25);
  }

  /* Blind-tooled double frame stamped into the cloth */
  .cover-front::before, .cover-front::after {
    content: "";
    position: absolute;
    border: 1px solid var(--gilt);
    pointer-events: none;
  }
  .cover-front::before { inset: 20px; }
  .cover-front::after { inset: 26px; opacity: 0.55; }

  /* Title label set into the cover */
  .cover-label {
    width: 72%;
    padding: 10px;
    background: #000;
    border: 1px solid var(--gilt);
    box-shadow: inset 0 0 0 3px #000, inset 0 0 0 4px rgba(184, 145, 74, 0.5), 0 2px 6px rgba(0, 0, 0, 0.5);
  }

  .cover-back {
    transform: rotateY(180deg);
    padding: var(--board) 0 var(--board) var(--board);
    border-radius: 8px 0 0 8px;
    box-shadow: -18px 30px 50px -26px rgba(43, 29, 23, 0.6);
  }

  .pastedown { padding: 36px; border-radius: 2px 0 0 2px; }
  .pastedown img { width: min(100%, 460px); }

  .spine {
    left: 50%;
    top: -6px;
    bottom: -6px;
    width: 22px;
    transform: translateX(-50%);
    border-radius: 4px;
    box-shadow: inset 1px 0 0 rgba(220, 192, 131, 0.3), inset -1px 0 0 rgba(220, 192, 131, 0.3), 0 0 12px rgba(0, 0, 0, 0.35);
  }

  .spine::before { inset: 8% 0; }

  .leaf {
    grid-column: 2;
    grid-row: 1;
    padding: var(--board) var(--board) var(--board) 0;
    border-radius: 0 8px 8px 0;
    box-shadow: 18px 30px 50px -26px rgba(43, 29, 23, 0.6);
  }

  .page {
    height: 100%;
    padding: 48px 30px 44px 36px;
    border-radius: 0 2px 2px 0;
  }

  /* Until the cover has opened it lies on top of the pages; after that, turned pages go over it */
  .book:not(.book--open) .cover { z-index: 5; }
  .spine { z-index: 6; }

  /* The sheet is hinged on the spine like the cover. Turned, it lies on the left-hand side. */
  .sheet {
    grid-column: 2;
    grid-row: 1;
    z-index: 3;
    margin: var(--board) var(--board) var(--board) 0;
    padding: 0;
    background: none;
    border-radius: 0;
    transform-origin: left center;
    transform-style: preserve-3d;
    transition: transform 1.15s cubic-bezier(0.65, 0.05, 0.25, 1);
  }

  .book.is-forgot .sheet { transform: rotateY(-180deg); }
  .book.no-turn .sheet { transition: none; }

  .sheet-face {
    position: absolute;
    inset: 0;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }

  .sheet-back { display: block; transform: rotateY(180deg); }

  /* The reverse is a left-hand page: margins and gilded edge are mirrored */
  .sheet-back .page {
    padding: 48px 36px 44px 30px;
    border-radius: 2px 0 0 2px;
  }

  .sheet-back .page::after { right: auto; left: -4px; border-radius: 2px 0 0 2px; }

  /* A shadow falls across the page while it is in the air */
  .sheet-face::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 1;
    background: linear-gradient(90deg, rgba(0, 0, 0, 0.45), transparent 70%);
    opacity: 0;
    transition: opacity 0.55s ease;
    pointer-events: none;
  }

  .sheet.is-turning .sheet-face::before { opacity: 1; }

  .back-link--inline { display: none; }
}

@keyframes open-cover {
  from { transform: rotateY(0deg); }
  to   { transform: rotateY(-180deg); }
}

/* Open without animating: always on the forgot-password page, and on the login page
   once the cover has opened, so resizing the window doesn't replay it */
.book--open .cover { animation: none; }

/* ---------- Motion preference ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
