/* Shared styles for the dispatch card, theme and controls, used by both
   index.html (random dispatches) and book.html (reading in order).
   Page-specific additions live in each page's own <style>. */

:root {
  --bg-deep: #2C2416;
  --bg-page: #F5F0E8;
  --fg: #2C2416;
  --accent: #876612;
  --border: #C4B28E;
  --muted: #A89070;
  /* text/icons drawn directly on --bg-deep (buttons outside the card) */
  --on-deep: #F5F0E8;
}

[data-theme="dark"] {
  --bg-deep: #1A1510;
  --bg-page: #2C2416;
  --fg: #D4C9B5;
  --accent: #C4A135;
  --border: #6B5A3E;
  --muted: #8F7E63;
  --on-deep: #D4C9B5;
}

/* ── Seasonal accents ──
   --accent must reach 4.5:1 against --bg-page and --muted 4.5:1 against
   --bg-deep (WCAG 1.4.3); re-check both when tweaking these. */
[data-season="spring"] { --accent: #4D7735; --border: #A3C48E; --muted: #7A9E68; }
[data-season="summer"] { --accent: #985E0E; --border: #D4A84E; --muted: #B89050; }
[data-season="autumn"] { --accent: #8B4513; --border: #C49A6C; --muted: #B18551; }
[data-season="winter"] { --accent: #4A6D8C; --border: #8AAEC4; --muted: #6F92A7; }

[data-season="spring"][data-theme="dark"] { --accent: #7AB85A; --border: #4A6B38; --muted: #618D4D; }
[data-season="summer"][data-theme="dark"] { --accent: #E89830; --border: #7A5A2E; --muted: #9E7D41; }
[data-season="autumn"][data-theme="dark"] { --accent: #CC783D; --border: #6A4A2E; --muted: #AB7745; }
[data-season="winter"][data-theme="dark"] { --accent: #6A9DC4; --border: #3A5A70; --muted: #6384A0; }

* { margin: 0; padding: 0; box-sizing: border-box; }

body, main {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

body {
  min-height: 100vh;
  justify-content: center;
  background: var(--bg-deep);
  font-family: 'EB Garamond', 'Georgia', serif;
  padding: 2rem 1rem;
  /* subtle noise texture via SVG */
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
}

/* ── The card ── */
.dispatch-card {
  width: 100%;
  max-width: 620px;
  /* Square by default, but free to grow for long dispatches. Not
     aspect-ratio: as a flex item of <main> its automatic minimum height is
     capped at the ratio height, so long text would spill out. Above 720px
     the card is always exactly max-width (620px) wide. */
  min-height: 620px;
  background: var(--bg-page);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* No justify-content:center — the auto margins on .content and
     .footnote-area centre things when there's room, and when there isn't,
     centring would push the content up past the top padding. */
  /* --star-clear keeps content below the favourite star (top + height +
     a small gap); set per breakpoint alongside .fav-btn's position. */
  --star-clear: 4.25rem;
  padding: 3.5rem;
  padding-top: var(--star-clear);
  /* paper texture */
  background-image:
    url("data:image/svg+xml,%3Csvg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)' opacity='0.03'/%3E%3C/svg%3E");
  box-shadow:
    0 2px 40px rgba(0,0,0,0.4),
    inset 0 0 80px rgba(139,105,20,0.05);
  transition: opacity 0.4s ease;
}

.dispatch-card.fading { opacity: 0; }

/* ── Entrance animation ── */
@keyframes card-entrance {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.dispatch-card.entering {
  animation: card-entrance 0.8s ease-out forwards;
  transition: none;
}

/* ── Double border ── */
.dispatch-card::before,
.dispatch-card::after {
  content: '';
  position: absolute;
  border: 1px solid var(--border);
  pointer-events: none;
}
.dispatch-card::before {
  inset: 14px;
}
.dispatch-card::after {
  inset: 20px;
}

/* ── Corner ornaments ── */
.corner {
  position: absolute;
  width: 28px;
  height: 28px;
  border-color: var(--accent);
  border-style: solid;
  border-width: 0;
}
.corner-tl { top: 28px; left: 28px; border-top-width: 2px; border-left-width: 2px; }
.corner-tr { top: 28px; right: 28px; border-top-width: 2px; border-right-width: 2px; }
.corner-bl { bottom: 28px; left: 28px; border-bottom-width: 2px; border-left-width: 2px; }
.corner-br { bottom: 28px; right: 28px; border-bottom-width: 2px; border-right-width: 2px; }

/* ── Wax seal ── */
.wax-seal {
  position: absolute;
  bottom: 18px;
  right: 18px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 35%, #C0392B 0%, #922B21 55%, #7B241C 100%);
  box-shadow:
    inset 0 2px 4px rgba(255,255,255,0.2),
    inset 0 -2px 6px rgba(0,0,0,0.3),
    0 2px 8px rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 1;
}
.wax-seal::after {
  content: 'D';
  font-family: 'Playfair Display', serif;
  font-weight: 500;
  font-size: 1.1rem;
  color: rgba(0,0,0,0.25);
  text-shadow: 0 1px 0 rgba(255,255,255,0.15);
}

/* ── Content area ── */
.content {
  text-align: center;
  max-width: 85%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  /* With the footnote's margin-top:auto, centres the content in the space
     above the footnote */
  margin-top: auto;
}

.classification {
  font-family: 'Cormorant Garamond', serif;
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.25rem;
}

.numeral {
  font-family: 'Cormorant Garamond', serif;
  font-weight: 300;
  font-size: 0.85rem;
  color: var(--accent);
  letter-spacing: 0.15em;
  margin-bottom: 0.75rem;
}

/* ── Ornamental rules ── */
.rule {
  display: flex;
  align-items: center;
  gap: 0;
  width: 70%;
  margin: 0.6rem 0;
}
.rule-sm { width: 40%; }

.rule-line {
  flex: 1;
  height: 1px;
  background: var(--accent);
}
.rule-diamond {
  width: 8px;
  height: 8px;
  background: var(--accent);
  transform: rotate(45deg);
  flex-shrink: 0;
  margin: 0 2px;
}
.rule-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.title {
  font-family: 'Playfair Display', serif;
  font-weight: 500;
  font-size: clamp(1.6rem, 4.5vw, 2.4rem);
  color: var(--fg);
  line-height: 1.2;
  margin: 0.3rem 0;
}

.body-text {
  font-family: 'EB Garamond', serif;
  font-style: italic;
  font-weight: 400;
  font-size: clamp(1rem, 2.8vw, 1.25rem);
  line-height: 1.55;
  color: var(--fg);
  margin-top: 0.5rem;
}

/* ── Footnote pushed to the bottom ──
   Kept in normal flow (not absolutely positioned) so long bodies push it
   down instead of overlapping it; the card's min-height lets it grow. */
.footnote-area {
  margin-top: auto;
  padding-top: 1rem;
  /* Narrower than the card so long footnotes wrap before reaching the wax
     seal in the bottom-right corner; centred so wrapped lines stay balanced */
  max-width: 85%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.footnote {
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-weight: 400;
  font-size: 0.9rem;
  color: var(--accent);
  letter-spacing: 0.05em;
}

/* ── Favourite star ── */
.fav-btn {
  position: absolute;
  top: 28px;
  left: 50%;
  transform: translateX(-50%);
  background: none;
  border: none;
  padding: 0.3rem;
  cursor: pointer;
  font-size: 1.2rem;
  color: var(--accent);
  transition: color 0.3s ease, transform 0.2s ease;
  z-index: 2;
  letter-spacing: 0;
  text-transform: none;
}
.fav-btn:hover, .fav-btn:focus-visible {
  color: var(--accent);
  background: none;
  border: none;
  transform: translateX(-50%) scale(1.15);
}
/* Colour alone doesn't mark focus here (idle is already --accent), so draw
   an outline for keyboard users (WCAG 2.4.7) */
.fav-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.fav-btn.active {
  color: var(--accent);
}

/* ── Controls ── */
.controls {
  margin-top: 2rem;
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 620px;
}

button {
  font-family: 'Cormorant Garamond', serif;
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--on-deep);
  background: transparent;
  border: 1px solid var(--border);
  padding: 1rem 2rem;
  cursor: pointer;
  transition: all 0.3s ease;
}
button:hover,
button:focus-visible {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg-page);
  outline: none;
}
button:disabled {
  opacity: 0.4;
  cursor: default;
}

.counter {
  font-family: 'Cormorant Garamond', serif;
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.1em;
  margin-top: 1rem;
}

.page-title {
  font-family: 'Playfair Display', serif;
  font-weight: 400;
  font-size: 0.9rem;
  color: var(--muted);
  letter-spacing: 0.15em;
  margin-bottom: 1.5rem;
  text-transform: uppercase;
}

/* ── Theme toggle ── */
.theme-toggle {
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-size: 0.8rem;
  color: var(--muted);
  background: none;
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
  letter-spacing: 0.1em;
  text-transform: none;
  transition: color 0.3s ease;
  position: fixed;
  top: 1rem;
  right: 1rem;
}
/* --on-deep, not --accent: the seasonal accents are tuned for the light
   card and drop to ~2-3:1 against the dark page background */
.theme-toggle:hover,
.theme-toggle:focus-visible {
  color: var(--on-deep);
  background: none;
  border: none;
}
.theme-toggle:focus-visible {
  outline: 2px solid var(--muted);
  outline-offset: 2px;
}

/* ── Letter-shortcut toggle (inline in the keyboard hint) ── */
.shortcut-toggle {
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
  text-transform: none;
  text-decoration: underline;
  text-underline-offset: 0.2em;
  background: none;
  border: none;
  padding: 0.25rem;
}
.shortcut-toggle:hover,
.shortcut-toggle:focus-visible {
  color: var(--on-deep);
  background: none;
  border: none;
}
.shortcut-toggle:focus-visible {
  outline: 2px solid var(--muted);
  outline-offset: 2px;
}

/* ── Service record ── */
.service-record {
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.08em;
  margin-top: 0.5rem;
}

/* ── Link between the random dispatches and the book ── */
.book-link {
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-underline-offset: 0.2em;
  margin-top: 1rem;
  padding: 0.25rem;
}
.book-link:hover,
.book-link:focus-visible { color: var(--on-deep); }
.book-link:focus-visible {
  outline: 2px solid var(--muted);
  outline-offset: 2px;
}

/* Hide keyboard hint on touch-only devices */
@media (hover: none) {
  .hint { display: none; }
}

/* ── Responsive ── */
@media (max-width: 720px) {
  .dispatch-card {
    --star-clear: 3.5rem;
    padding: 2.5rem;
    padding-top: var(--star-clear);
    min-height: 420px;
  }
  .content { max-width: 92%; }
  .corner {
    width: 20px;
    height: 20px;
  }
  .corner-tl { top: 20px; left: 20px; }
  .corner-tr { top: 20px; right: 20px; }
  .corner-bl { bottom: 20px; left: 20px; }
  .corner-br { bottom: 20px; right: 20px; }
  .wax-seal { width: 34px; height: 34px; bottom: 12px; right: 12px; }
  .wax-seal::after { font-size: 0.9rem; }
  .fav-btn { top: 20px; font-size: 1rem; }
  button { padding: 0.7rem 1.2rem; font-size: 0.8rem; letter-spacing: 0.12em; }
  .controls { gap: 0.4rem; }
  .theme-toggle { font-size: 0.7rem; top: 0.5rem; right: 0.5rem; }
  .page-title { font-size: 0.75rem; padding-right: 5rem; }
}

/* ── Landscape / short viewports ── */
@media (max-height: 600px) {
  body { padding: 0.75rem 1rem; }
  .dispatch-card {
    padding: 1.5rem;
    padding-top: var(--star-clear);
  }
  .controls { margin-top: 1rem; }
  .page-title { margin-bottom: 0.75rem; }
  button { padding: 0.5rem 1rem; }
  .service-record { margin-top: 0.25rem; }
}

/* Only where the card isn't square (see max-width: 720px above); on wider
   screens it would override the card's 620px minimum and break the square. */
@media (max-height: 600px) and (max-width: 720px) {
  .dispatch-card { min-height: 300px; }
}

/* ── Very small screens ── */
@media (max-width: 400px) {
  button { padding: 0.6rem 0.8rem; font-size: 0.75rem; letter-spacing: 0.08em; }
  .dispatch-card { --star-clear: 3.25rem; padding: 2rem; padding-top: var(--star-clear); }
  .corner { width: 16px; height: 16px; }
  .corner-tl { top: 16px; left: 16px; }
  .corner-tr { top: 16px; right: 16px; }
  .corner-bl { bottom: 16px; left: 16px; }
  .corner-br { bottom: 16px; right: 16px; }
  .wax-seal { width: 28px; height: 28px; bottom: 10px; right: 10px; }
  .wax-seal::after { font-size: 0.75rem; }
  .fav-btn { top: 16px; }
}

/* ── Reduced motion: drop the slide-in and scale effects. The opacity
   fade is kept (short, non-directional) so the JS timing still works. ── */
@media (prefers-reduced-motion: reduce) {
  /* animationend never fires with animation:none, so .entering stays on;
     restore the opacity transition it would otherwise disable. */
  .dispatch-card.entering { animation: none; transition: opacity 0.4s ease; }
  .fav-btn:hover, .fav-btn:focus-visible { transform: translateX(-50%); }
  button, .fav-btn, .theme-toggle { transition: none; }
}

/* ── Print / screenshot friendly ── */
@media print {
  body { background: white; padding: 0; }
  .controls, .counter, .page-title, .hint { display: none; }
  .dispatch-card {
    box-shadow: none;
    max-width: 100%;
    min-height: 0;
  }
}
