/* ============================================================
   Glass theme — the palette + background a user picks in
   Settings → Preferences → Theme (ThemePreference.php).

   Any element carrying data-palette="<key>" gets the palette's
   colours as --theme* tokens and has the app's brand tokens
   (--color-primary*, --color-brand-orchid*) remapped inside it, so
   existing rules follow the palette without edits. --color-accent
   is deliberately NOT remapped: aquamarine means "live".

   An element carrying data-bg="aurora|black" shows the matching
   layer of a .theme-backdrop inside it.

   Colours come from the --palette-* tokens in variables.css.
   ============================================================ */

[data-palette] {
    --theme: var(--palette-lavender);
    --theme-rgb: var(--palette-lavender-rgb);
    --theme-ink: var(--palette-lavender-ink);
    --theme-glass-rgb: var(--palette-lavender-glass-rgb);
    --theme-link: var(--palette-lavender-link);
}
[data-palette="warm"]       { --theme: var(--palette-warm);       --theme-rgb: var(--palette-warm-rgb);       --theme-ink: var(--palette-warm-ink);       --theme-glass-rgb: var(--palette-warm-glass-rgb); --theme-link: var(--palette-warm-link); }
[data-palette="ocean"]      { --theme: var(--palette-ocean);      --theme-rgb: var(--palette-ocean-rgb);      --theme-ink: var(--palette-ocean-ink);      --theme-glass-rgb: var(--palette-ocean-glass-rgb); --theme-link: var(--palette-ocean-link); }
[data-palette="orchid"]     { --theme: var(--palette-orchid);     --theme-rgb: var(--palette-orchid-rgb);     --theme-ink: var(--palette-orchid-ink);     --theme-glass-rgb: var(--palette-orchid-glass-rgb); --theme-link: var(--palette-orchid-link); }
[data-palette="terracotta"] { --theme: var(--palette-terracotta); --theme-rgb: var(--palette-terracotta-rgb); --theme-ink: var(--palette-terracotta-ink); --theme-glass-rgb: var(--palette-terracotta-glass-rgb); --theme-link: var(--palette-terracotta-link); }
[data-palette="mint"]       { --theme: var(--palette-mint);       --theme-rgb: var(--palette-mint-rgb);       --theme-ink: var(--palette-mint-ink);       --theme-glass-rgb: var(--palette-mint-glass-rgb); --theme-link: var(--palette-mint-link); }
[data-palette="silver"]     { --theme: var(--palette-silver);     --theme-rgb: var(--palette-silver-rgb);     --theme-ink: var(--palette-silver-ink);     --theme-glass-rgb: var(--palette-silver-glass-rgb); --theme-link: var(--palette-silver-link); }

/* Brand tokens and the glass surface, derived from the palette. */
[data-palette] {
    --color-primary: var(--theme);
    --color-primary-rgb: var(--theme-rgb);
    --color-primary-hover: color-mix(in srgb, var(--theme) 80%, white);
    --color-primary-light: color-mix(in srgb, var(--theme) 60%, white);
    --color-primary-dark: color-mix(in srgb, var(--theme) 75%, black);
    --color-link: var(--theme-link);
    --color-link-hover: color-mix(in srgb, var(--theme-link) 70%, white);
    --color-brand-orchid: var(--theme);
    --color-brand-orchid-rgb: var(--theme-rgb);
    --color-brand-orchid-dark: color-mix(in srgb, var(--theme) 75%, black);
    /* variables.css aliases these on :root, where they resolve to lavender;
       declared again here so they follow the palette. */
    --primary: var(--color-primary);
    --primary-color: var(--color-primary);
    --primary-dark: var(--color-primary-dark);
    --primary-light: var(--color-primary-light);
    --input-border-focus: var(--color-primary);

    /* Muted text sits on glass over bright backdrops, where the stock grey
       falls to about 2.7:1. --text-muted resolved on :root, so restate both. */
    --color-text-muted: #c8c8d0;
    --text-muted: var(--color-text-muted);

    /* Frosted glass: see-through fill, a heavy blur that keeps the
       backdrop's colour, a bright top rim, a sheen that catches light at
       the top-left and again faintly at the bottom-right, a deep soft
       shadow. Nested surfaces use the lighter --glass-inner-*. */
    --glass-bg: rgba(var(--theme-glass-rgb), 0.26);
    --glass-bg-strong: rgba(var(--theme-glass-rgb), 0.55);
    --glass-edge: rgba(var(--color-white-rgb), 0.12);
    --glass-hi: rgba(var(--color-white-rgb), 0.30);
    --glass-sheen: linear-gradient(160deg,
        rgba(var(--color-white-rgb), 0.13) 0%,
        rgba(var(--color-white-rgb), 0.04) 32%,
        rgba(var(--color-white-rgb), 0.01) 62%,
        rgba(var(--color-white-rgb), 0.05) 100%);
    --glass-blur: 36px;
    --glass-filter: blur(var(--glass-blur)) saturate(180%);
    --glass-shadow:
        inset 0 1px 0 var(--glass-hi),
        inset 0 0 0 1px rgba(var(--color-white-rgb), 0.03),
        0 30px 60px -24px rgba(var(--color-black-rgb), 0.6),
        0 10px 24px -12px rgba(var(--color-black-rgb), 0.35);
    --glass-radius: var(--radius-2xl);
    --glass-inner-bg: linear-gradient(160deg, rgba(var(--color-white-rgb), 0.08), rgba(var(--color-white-rgb), 0.03));
    --glass-inner-edge: rgba(var(--color-white-rgb), 0.09);
    --glass-inner-hi: rgba(var(--color-white-rgb), 0.14);

    /* The aurora's light field, one layer per token. */
    --aurora-1: radial-gradient(60% 55% at 12% 8%, rgba(var(--theme-rgb), 0.55), transparent 70%);
    --aurora-2: radial-gradient(45% 45% at 88% 12%, rgba(var(--palette-ocean-rgb), 0.28), transparent 70%);
    --aurora-3: radial-gradient(55% 50% at 70% 92%, rgba(var(--palette-mint-rgb), 0.20), transparent 70%);
    --aurora-4: radial-gradient(50% 60% at 25% 85%, rgba(var(--palette-orchid-rgb), 0.35), transparent 70%);
}
/* Black: the panels are black glass too. The fill matches the page,
   so each one reads by its rim, inset highlight and sheen alone. */
[data-bg="black"] {
    --glass-bg: rgba(var(--color-black-rgb), 0.6);
}
/* High contrast: the backdrop stops competing with the text. */
@media (prefers-contrast: high) {
    [data-palette],
    [data-bg] {
        --glass-bg: rgba(var(--theme-glass-rgb), 0.9);
    }
    [data-bg="black"] {
        --glass-bg: rgba(var(--color-black-rgb), 0.9);
    }
}

/* ── Backdrop ─────────────────────────────────────────────
   Fixed to the viewport, so it shows through the gutters around the
   floating main panel. z-index 0, not -1: at -1 the opaque
   .main-content / .content backgrounds (dark-mode.css) paint over
   it. Page content above it needs z-index >= 1; the fixed sidebar
   (z-index 100) already is. */
.theme-backdrop {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}
/* Sign-in pages: the Ghost Fibers background (partials/auth_backdrop.php).
   Same box as the backdrop; once it mounts, ghost-fibers.js puts
   .ghost-fibers-on on <html> and the theme backdrop fades out while the
   fibers fade in (their intro), so there is no aurora → black → fibers cut. */
.auth-fibers {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}
.ghost-fibers-on .theme-backdrop {
    opacity: 0;
    visibility: hidden;
    transition: opacity 1.6s ease, visibility 0s 1.6s;
}
.theme-backdrop > .tb-layer {
    position: absolute;
    inset: 0;
    opacity: 0;
    /* opacity:0 is still painted; hide unpicked layers once they've faded. */
    visibility: hidden;
    transition: opacity 0.6s ease, visibility 0s 0.6s;
}

.tb-aurora {
    background: var(--aurora-1), var(--aurora-2), var(--aurora-3), var(--aurora-4), var(--color-bg-primary);
}
.tb-aurora .tb-orb { position: absolute; border-radius: 50%; filter: blur(60px); opacity: 0.7; }
.tb-aurora .tb-o1 {
    width: 520px; height: 520px; left: 30%; top: 18%;
    background: rgba(var(--theme-rgb), 0.35);
    animation: tbDrift 28s ease-in-out infinite alternate;
}
.tb-aurora .tb-o2 {
    width: 380px; height: 380px; right: 6%; top: 46%;
    background: rgba(var(--palette-mint-rgb), 0.16);
    animation: tbDrift 34s ease-in-out infinite alternate-reverse;
}

/* Veil: the aurora, quieter. One hue and a faint echo, wide and soft. */
.tb-veil {
    background:
        radial-gradient(70% 60% at 15% 0%, rgba(var(--theme-rgb), 0.32), transparent 72%),
        radial-gradient(60% 55% at 95% 100%, rgba(var(--theme-rgb), 0.16), transparent 72%),
        radial-gradient(40% 40% at 80% 10%, rgba(var(--palette-ocean-rgb), 0.10), transparent 70%),
        var(--color-bg-primary);
}
.tb-veil::before {
    width: 60%; aspect-ratio: 1; left: 25%; top: 10%; border-radius: 50%;
    background: rgba(var(--theme-rgb), 0.14); filter: blur(90px);
    animation: tbDrift 36s ease-in-out infinite alternate;
}

/* Ribbon: a northern-lights curtain across the top third. */
.tb-ribbon {
    background:
        radial-gradient(80% 40% at 50% 0%, rgba(var(--theme-rgb), 0.18), transparent 70%),
        var(--color-bg-primary);
}
.tb-ribbon::before {
    /* Kept dim: links that cross the streak must stay above 4.5:1. */
    left: -20%; right: -20%; top: 6%; height: 34%;
    background: linear-gradient(90deg,
        transparent 0%,
        rgba(var(--palette-mint-rgb), 0.21) 18%,
        rgba(var(--theme-rgb), 0.33) 42%,
        rgba(var(--palette-ocean-rgb), 0.24) 66%,
        rgba(var(--palette-orchid-rgb), 0.18) 84%,
        transparent 100%);
    filter: blur(50px);
    transform: rotate(-8deg) skewX(-18deg);
    border-radius: 50%;
    animation: tbSway 30s ease-in-out infinite alternate;
}
.tb-ribbon::after {
    left: -20%; right: -20%; top: 0; height: 55%;
    background: repeating-linear-gradient(90deg, rgba(var(--color-white-rgb), 0.035) 0 2px, transparent 2px 14px);
    -webkit-mask-image: linear-gradient(180deg, transparent, var(--color-black) 30%, transparent 90%);
            mask-image: linear-gradient(180deg, transparent, var(--color-black) 30%, transparent 90%);
    transform: skewX(-18deg);
    filter: blur(1px);
}

/* Horizon: a dark sky, light rising from below the bottom edge. */
.tb-horizon {
    background:
        radial-gradient(90% 55% at 50% 115%, rgba(var(--theme-rgb), 0.55), transparent 70%),
        radial-gradient(60% 35% at 15% 105%, rgba(var(--palette-orchid-rgb), 0.30), transparent 70%),
        radial-gradient(60% 35% at 85% 105%, rgba(var(--palette-ocean-rgb), 0.25), transparent 70%),
        linear-gradient(180deg, var(--color-black), var(--color-bg-primary));
}
.tb-horizon::before {
    left: 10%; right: 10%; bottom: 14%; height: 1px;
    background: linear-gradient(90deg, transparent, rgba(var(--theme-rgb), 0.7), transparent);
    box-shadow: 0 0 40px 8px rgba(var(--theme-rgb), 0.25);
}

/* Mono: only the chosen colour, so the palette reads pure. */
.tb-mono {
    background:
        radial-gradient(55% 55% at 10% 5%, rgba(var(--theme-rgb), 0.45), transparent 70%),
        radial-gradient(50% 50% at 90% 95%, rgba(var(--theme-rgb), 0.28), transparent 70%),
        radial-gradient(35% 35% at 70% 20%, rgba(var(--theme-rgb), 0.12), transparent 70%),
        var(--color-bg-primary);
}
.tb-mono::before {
    width: 50%; aspect-ratio: 1; left: 35%; top: 25%; border-radius: 50%;
    background: rgba(var(--theme-rgb), 0.16); filter: blur(80px);
    animation: tbDrift 30s ease-in-out infinite alternate;
}

/* Mesh: a richer four-corner mesh, all from the chosen colour. */
.tb-mesh {
    background:
        radial-gradient(60% 60% at 0% 0%, rgba(var(--theme-rgb), 0.32), transparent 65%),
        radial-gradient(60% 60% at 100% 0%, color-mix(in srgb, rgba(var(--theme-rgb), 0.28), var(--palette-ocean) 20%), transparent 65%),
        radial-gradient(60% 60% at 100% 100%, color-mix(in srgb, rgba(var(--theme-rgb), 0.22), var(--color-black) 30%), transparent 65%),
        radial-gradient(60% 60% at 0% 100%, color-mix(in srgb, rgba(var(--theme-rgb), 0.26), var(--palette-orchid) 40%), transparent 65%),
        color-mix(in srgb, var(--color-bg-primary) 85%, var(--theme));
}
.tb-mesh::before {
    inset: 0;
    background: radial-gradient(40% 40% at 50% 50%, rgba(var(--color-black-rgb), 0.55), transparent 80%);
}

/* Signal: faint rings spreading from the corner, like a call going out. */
.tb-signal {
    background:
        radial-gradient(45% 45% at 100% 100%, rgba(var(--theme-rgb), 0.35), transparent 70%),
        radial-gradient(40% 40% at 0% 0%, rgba(var(--theme-rgb), 0.12), transparent 70%),
        var(--color-bg-primary);
}
.tb-signal::before {
    inset: 0;
    background: repeating-radial-gradient(circle at 100% 100%,
        rgba(var(--theme-rgb), 0.16) 0 1px, transparent 1px 46px);
    -webkit-mask-image: radial-gradient(circle at 100% 100%, var(--color-black) 0%, transparent 75%);
            mask-image: radial-gradient(circle at 100% 100%, var(--color-black) 0%, transparent 75%);
}

/* Midnight: deep blue-black, a faint glow of the chosen colour, a few stars. */
.tb-midnight {
    background:
        radial-gradient(50% 45% at 60% 45%, rgba(var(--theme-rgb), 0.14), transparent 75%),
        linear-gradient(180deg,
            color-mix(in srgb, var(--palette-ocean) 9%, var(--color-black)) 0%,
            color-mix(in srgb, var(--palette-ocean) 4%, var(--color-black)) 55%,
            var(--color-black) 100%);
}
.tb-midnight::before {
    inset: 0;
    background:
        radial-gradient(1px 1px at 12% 18%, rgba(var(--color-white-rgb), 0.5), transparent),
        radial-gradient(1px 1px at 38% 8%, rgba(var(--color-white-rgb), 0.35), transparent),
        radial-gradient(1px 1px at 72% 14%, rgba(var(--color-white-rgb), 0.45), transparent),
        radial-gradient(1px 1px at 88% 30%, rgba(var(--color-white-rgb), 0.3), transparent),
        radial-gradient(1px 1px at 5% 60%, rgba(var(--color-white-rgb), 0.3), transparent);
}

/* Graphite: neutral charcoal, a soft spotlight from above, fine grain. */
.tb-graphite {
    background:
        radial-gradient(70% 50% at 50% -10%, rgba(var(--color-white-rgb), 0.09), transparent 70%),
        radial-gradient(40% 30% at 85% 0%, rgba(var(--theme-rgb), 0.10), transparent 70%),
        linear-gradient(180deg, color-mix(in srgb, var(--color-bg-primary) 80%, var(--color-white) 4%), var(--color-black));
}
.tb-graphite::before {
    inset: 0; opacity: 0.35; mix-blend-mode: overlay;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 1  0 0 0 0 1  0 0 0 .5 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

/* Black: a flat black page; panels read by their rim and sheen. */
.tb-black { background: var(--color-black); }

.tb-veil::before, .tb-ribbon::before, .tb-ribbon::after, .tb-horizon::before,
.tb-mono::before, .tb-mesh::before, .tb-signal::before, .tb-midnight::before,
.tb-graphite::before { content: ""; position: absolute; pointer-events: none; }

/* A .theme-preview tile and each .theme-bg-chip (Settings) hold their
   backdrop inside themselves instead of pinning it to the viewport. */
.theme-preview,
.theme-bg-chip { position: relative; isolation: isolate; overflow: hidden; }
.theme-preview > .theme-backdrop,
.theme-bg-chip > .theme-backdrop { position: absolute; }
.theme-preview .tb-aurora .tb-orb,
.theme-bg-chip .tb-aurora .tb-orb { width: 55%; height: auto; aspect-ratio: 1; }
/* Blurs sized for the viewport wash a small tile out; scale them down. */
.theme-preview .tb-aurora .tb-orb,
.theme-preview .tb-layer::before { filter: blur(28px); }
.theme-bg-chip .tb-aurora .tb-orb,
.theme-bg-chip .tb-layer::before { filter: blur(10px); }
.theme-bg-chip .tb-ribbon::before { height: 40%; }
/* A 76px chip has to read at a glance: lift the subtle styles a little. */
.theme-bg-chip > .theme-backdrop { filter: brightness(1.8) saturate(1.3); }
.theme-bg-chip .tb-graphite::before,
.theme-preview .tb-graphite::before { filter: none; }

[data-bg="aurora"] .tb-aurora,
[data-bg="veil"] .tb-veil,
[data-bg="ribbon"] .tb-ribbon,
[data-bg="horizon"] .tb-horizon,
[data-bg="mono"] .tb-mono,
[data-bg="mesh"] .tb-mesh,
[data-bg="signal"] .tb-signal,
[data-bg="midnight"] .tb-midnight,
[data-bg="graphite"] .tb-graphite,
[data-bg="black"] .tb-black { opacity: 1; visibility: visible; transition-delay: 0s; }

@keyframes tbDrift {
    from { transform: translate(0, 0) scale(1); }
    to   { transform: translate(60px, 40px) scale(1.1); }
}
@keyframes tbSway {
    from { transform: rotate(-8deg) skewX(-18deg) translateX(-4%); }
    to   { transform: rotate(-5deg) skewX(-12deg) translateX(4%); }
}

@media (prefers-reduced-motion: reduce) {
    .tb-aurora .tb-orb,
    .tb-veil::before, .tb-ribbon::before, .tb-mono::before { animation: none; }
    .theme-backdrop > .tb-layer { transition: none; }
}
