/* ==========================================================================
   Twinlight: the two lights. Design tokens.
   Direction: the room stays nearly empty; what changes is the light the words are read in.

   One property drives everything:  --tw-balance   0 = solar  ·  0.5 = both (dusk)  ·  1 = lunar
   Set it on <html> (or on any element with class .tw-lit to light that element on its own).

   Every colour below is light falling on a surface, never paint. Three stops are defined for each
   (sun, both, moon) and the live value is mixed in oklab, solar to dusk for 0 to 0.5, dusk to lunar for
   0.5 to 1. Text contrast was checked at every 0.05 of balance: the lowest pair (small meta text on the
   reading leaf) is 5.6 to 1; reading ink is 12.5 to 1 or more.
   ========================================================================== */

@property --tw-balance {
  syntax: "<number>";
  inherits: true;
  initial-value: 0.5;
}

:root {
  --tw-balance: 0.5;                 /* dusk is the default. Never default to full day. */

  /* ---- The stops. Sun (s), both at dusk (b), moon (m). ------------------------------------------
     Sunlight: made light. Warm, coloured, it divides things by hue and draws hard edges.
     Moonlight: returned light. The eye's rods take over, colour drains toward silver-blue-grey,
     edges soften, the whole field is seen at once.                                                    */

  /* Grounds: the dusk around a room (the sky above water, the wall of a room) */
  --s-ground: oklch(0.205 0.022 62);
  --b-ground: oklch(0.195 0.030 305);
  --m-ground: oklch(0.175 0.012 250);

  /* The water and the low ground beneath the sky: where words sit on the approach */
  --s-water: oklch(0.255 0.030 60);
  --b-water: oklch(0.235 0.032 300);
  --m-water: oklch(0.215 0.014 245);

  /* Ink on a ground: the words themselves catch the light */
  --s-ink: oklch(0.935 0.040 86);
  --b-ink: oklch(0.925 0.018 70);
  --m-ink: oklch(0.905 0.008 245);
  --s-ink-soft: oklch(0.770 0.050 80);
  --b-ink-soft: oklch(0.760 0.025 60);
  --m-ink-soft: oklch(0.745 0.010 245);

  /* The light itself, as it lands: gold (sunlight on a surface), silver (moonlight on a surface) */
  --s-light: oklch(0.840 0.115 80);
  --b-light: oklch(0.830 0.060 60);
  --m-light: oklch(0.860 0.022 240);

  /* The reading leaf: a page under the light, the one bright surface in the library */
  --s-leaf: oklch(0.955 0.026 86);
  --b-leaf: oklch(0.935 0.016 65);
  --m-leaf: oklch(0.905 0.008 245);
  --s-leaf-ink: oklch(0.255 0.030 62);
  --b-leaf-ink: oklch(0.245 0.022 40);
  --m-leaf-ink: oklch(0.235 0.014 250);
  --s-leaf-soft: oklch(0.470 0.040 65);
  --b-leaf-soft: oklch(0.460 0.030 40);
  --m-leaf-soft: oklch(0.450 0.016 250);
  --s-leaf-link: oklch(0.450 0.095 62);
  --b-leaf-link: oklch(0.430 0.060 30);
  --m-leaf-link: oklch(0.420 0.040 250);

  /* Shadow colour. In sunlight a shadow is filled by the blue sky, so it is cool and coloured.
     In moonlight the sky's fill is too faint to see, so a shadow is near black.                     */
  --s-shadow: oklch(0.22 0.05 255 / 0.55);
  --b-shadow: oklch(0.12 0.03 290 / 0.60);
  --m-shadow: oklch(0.06 0.00 0 / 0.70);

  /* The Blue Court: the blue hour. The one blue place. It is blue at every balance; only the light
     at its rim turns toward gold or silver.                                                          */
  --tw-court: oklch(0.300 0.118 264);
  --tw-court-deep: oklch(0.215 0.090 266);
  --tw-court-ink: oklch(0.940 0.018 250);
  --tw-court-soft: oklch(0.805 0.042 250);

  /* ---- Type ------------------------------------------------------------------------------------
     One family, an old-style book face, for everything. Iowan Old Style ships with every Mac and
     iPhone and is drawn for long reading on screens; Palatino and Book Antiqua carry the same
     Renaissance pen on Windows; Georgia is the floor. Nothing is downloaded.                         */
  --tw-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", "URW Palladio L", P052, Georgia, serif;

  /* Scale: ratio 1.2 around the reading size, which grows a little with the screen */
  --tw-read: clamp(1.125rem, 1.03rem + 0.38vw, 1.3125rem);   /* 18px on a phone, 21px at a desk */
  --tw-t-small: 0.8333em;    /* meta, captions, glosses */
  --tw-t-base: 1em;
  --tw-t-lead: 1.2em;        /* the standfirst, a line of his over the sky */
  --tw-t-h3: 1.2em;
  --tw-t-h2: 1.44em;
  --tw-t-h1: clamp(1.728em, 1.4em + 1.6vw, 2.488em);
  --tw-t-sky: clamp(1.44em, 1.1em + 1.9vw, 2.0736em);       /* a single line set over the sky */

  --tw-measure: 34em;        /* about 66 characters of Iowan at the reading size */
  --tw-measure-narrow: 26em;
  --tw-leading: 1.62;        /* long reading */
  --tw-leading-tight: 1.25;  /* titles */
  --tw-tracking-caps: 0.07em;

  /* ---- Space: multiples of the reading line ------------------------------------------------- */
  --tw-s-1: 0.25rem;
  --tw-s-2: 0.5rem;
  --tw-s-3: 0.8125rem;
  --tw-s-4: 1.625rem;        /* one line */
  --tw-s-5: 3.25rem;         /* two lines */
  --tw-s-6: 6.5rem;          /* four lines: the breath between rooms of a page */
  --tw-gutter: max(1rem, 4.5vw);

  /* ---- Rules and marks ----------------------------------------------------------------------
     A rule is a streak of light that fades at both ends, not a printed line.                        */
  --tw-hair: 1px;
  --tw-disc: clamp(2.25rem, 4.2vmin, 3.5rem);   /* sun and moon are the same size in the sky */

  /* ---- Focus: a ring of the current light, with a halo, on every focusable thing -------------- */
  --tw-focus-width: 2px;
  --tw-focus-offset: 4px;

  /* ---- Motion: light changes slowly, as dusk does; nothing else moves much --------------------- */
  --tw-ease: cubic-bezier(0.33, 0, 0.15, 1);
  --tw-dur-light: 2400ms;    /* a change of light, such as crossing toward a family */
  --tw-dur-near: 900ms;      /* coming nearer */
  --tw-dur-quick: 180ms;     /* a hover */
}

/* The live tokens are declared on :root and on any .tw-lit element, so a door, a card or a band can
   be lit by a balance of its own while the room keeps its light.                                    */
:root, .tw-lit {
  /* ---- The live mix ---------------------------------------------------------------------------- */
  --tw-w1: clamp(0, var(--tw-balance) * 2, 1);        /* sun toward dusk */
  --tw-w2: clamp(0, var(--tw-balance) * 2 - 1, 1);    /* dusk toward moon */

  /* Strength of each light. Neither ever reaches nothing: no room by the sun alone at full strength,
     none by the moon alone in the dark.                                                              */
  --tw-sun: calc(1 - var(--tw-balance) * 0.82);
  --tw-moon: calc(0.18 + var(--tw-balance) * 0.82);

  /* Edges: sunlight draws them, moonlight softens them. 1 is a hard edge, 0 a soft one. */
  --tw-edge: calc(1 - var(--tw-balance));

  --tw-ground: color-mix(in oklab, var(--m-ground) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-ground) calc(var(--tw-w1) * 100%), var(--s-ground)));
  --tw-water: color-mix(in oklab, var(--m-water) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-water) calc(var(--tw-w1) * 100%), var(--s-water)));
  --tw-ink: color-mix(in oklab, var(--m-ink) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-ink) calc(var(--tw-w1) * 100%), var(--s-ink)));
  --tw-ink-soft: color-mix(in oklab, var(--m-ink-soft) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-ink-soft) calc(var(--tw-w1) * 100%), var(--s-ink-soft)));
  --tw-light: color-mix(in oklab, var(--m-light) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-light) calc(var(--tw-w1) * 100%), var(--s-light)));
  --tw-leaf: color-mix(in oklab, var(--m-leaf) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-leaf) calc(var(--tw-w1) * 100%), var(--s-leaf)));
  --tw-leaf-ink: color-mix(in oklab, var(--m-leaf-ink) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-leaf-ink) calc(var(--tw-w1) * 100%), var(--s-leaf-ink)));
  --tw-leaf-soft: color-mix(in oklab, var(--m-leaf-soft) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-leaf-soft) calc(var(--tw-w1) * 100%), var(--s-leaf-soft)));
  --tw-leaf-link: color-mix(in oklab, var(--m-leaf-link) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-leaf-link) calc(var(--tw-w1) * 100%), var(--s-leaf-link)));
  --tw-shadow-c: color-mix(in oklab, var(--m-shadow) calc(var(--tw-w2) * 100%), color-mix(in oklab, var(--b-shadow) calc(var(--tw-w1) * 100%), var(--s-shadow)));

  /* The gold and the silver as separate lights, for where both fall at once */
  --tw-gold: var(--s-light);
  --tw-silver: var(--m-light);

  /* A surface's cast shadow: long, offset and hard in sunlight; short, wide and soft in moonlight */
  --tw-cast: calc(var(--tw-edge) * 14px) calc(var(--tw-edge) * 20px) calc(2px + var(--tw-balance) * 46px) calc(var(--tw-balance) * 4px) var(--tw-shadow-c);

  /* A rule as a streak of the present light */
  --tw-rule: linear-gradient(90deg, transparent, color-mix(in oklab, var(--tw-light) 55%, transparent) 30%, color-mix(in oklab, var(--tw-light) 55%, transparent) 70%, transparent);
  --tw-focus: var(--tw-light);
}

/* Night reading. A reader who has asked their device for dark gets the leaf unlit: the light falls on
   the words rather than on the paper. The contrast holds as before (ink 12 to 1, meta 6.9 to 1).      */
@media (prefers-color-scheme: dark) {
  :root:not([data-leaf="lit"]) {
    --s-leaf: oklch(0.215 0.020 62);  --b-leaf: oklch(0.205 0.022 300);  --m-leaf: oklch(0.190 0.010 250);
    --s-leaf-ink: oklch(0.880 0.035 86); --b-leaf-ink: oklch(0.870 0.015 70); --m-leaf-ink: oklch(0.860 0.008 245);
    --s-leaf-soft: oklch(0.720 0.040 80); --b-leaf-soft: oklch(0.710 0.020 60); --m-leaf-soft: oklch(0.700 0.010 245);
    --s-leaf-link: oklch(0.820 0.100 80); --b-leaf-link: oklch(0.800 0.050 60); --m-leaf-link: oklch(0.830 0.030 245);
  }
}
:root[data-leaf="unlit"] {
  --s-leaf: oklch(0.215 0.020 62);  --b-leaf: oklch(0.205 0.022 300);  --m-leaf: oklch(0.190 0.010 250);
  --s-leaf-ink: oklch(0.880 0.035 86); --b-leaf-ink: oklch(0.870 0.015 70); --m-leaf-ink: oklch(0.860 0.008 245);
  --s-leaf-soft: oklch(0.720 0.040 80); --b-leaf-soft: oklch(0.710 0.020 60); --m-leaf-soft: oklch(0.700 0.010 245);
  --s-leaf-link: oklch(0.820 0.100 80); --b-leaf-link: oklch(0.800 0.050 60); --m-leaf-link: oklch(0.830 0.030 245);
}

/* Light changes slowly. With reduced motion, it changes at once and nothing drifts. */
:root { transition: --tw-balance var(--tw-dur-light) var(--tw-ease); }
@media (prefers-reduced-motion: reduce) {
  :root {
    --tw-dur-light: 1ms;
    --tw-dur-near: 1ms;
    --tw-dur-quick: 1ms;
  }
}
