/* ============================================
   TOAST NOTIFICATIONS
   ============================================

   Every toast opens out of one place: a small capsule pinned to the top centre
   of the window, carrying the ArcaneMetriX mark, the way a phone's notifications
   grow out of its camera cutout. It is pinned to the window rather than to the
   header (the header scrolls away on phones) and does not depend on who is
   signed in. The mark appears first, spins, and slides to the bar's left edge as
   the bar opens around it; it stays there, and the two fade out together when the
   toast goes (`.toast-leaving`, `TOAST_EXIT_MS`).

   Two kinds share the motion:
   - a plain toast is a bar with its icon, message, action and close;
   - a moment (`ToastMoment`: an achievement, PRO started) opens further, into a
     card on the theme's panel with an accent glow: a pill naming the moment, one
     large line, a label, and a burst of shards in the five mana colors.

   Only the look lives here. The behaviour is `ToastContainer`'s and unchanged:
   the countdown and its hover pause, click to the log, the action and close
   buttons, a swipe to dismiss, at most four on screen. */

.toast-container {
    position: fixed;
    top: max(var(--space-2-5), env(safe-area-inset-top, 0px));
    left: 50%;
    transform: translateX(-50%);
    z-index: var(--z-toast);
    display: flex;
    /* The newest toast nearest the capsule, the older ones below it. */
    flex-direction: column-reverse;
    align-items: center;
    gap: var(--space-2);
    pointer-events: none;
    width: min(420px, calc(100vw - var(--space-6)));
}

/* --- A plain toast: the mark slides aside and the bar opens -------------- */

.toast {
    position: relative;
    isolation: isolate;
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    width: 100%;
    /* Room at the left for the mark, which rests there once the toast is open. */
    padding: var(--space-3) var(--space-3-5) var(--space-3) calc(var(--space-12) + var(--space-1));
    /* Round as the capsule it opens out of; a second line keeps the same corners. */
    border-radius: var(--radius-4xl);
    background: var(--popup-bg);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--popup-border);
    /* The accent is a glow around the bar now, not a stripe down its side. */
    box-shadow:
        0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 30%, transparent),
        0 6px 22px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 18%, transparent),
        0 10px 28px var(--popup-shadow);
    color: var(--text-color);
    font-size: 0.875rem;
    line-height: 1.45;
    pointer-events: auto;
    overflow: hidden;
    touch-action: pan-y;
    /* The mark comes first, alone, for 0.2s; then the bar opens around it as it slides
       to its place at the left (`toastMarkSlide`, the same timing). */
    animation: toastReveal 0.56s cubic-bezier(0.22, 1, 0.36, 1) 0.2s both;
}

/* Opening: from a 36px round capsule at the centre, just big enough for the mark,
   out to the full bar. The left edge of the opening travels with the mark, which
   stays inside it the whole way. The last frame clips well outside the box, so the
   glow is never cut off. */
@keyframes toastReveal {
    0% {
        clip-path: inset(calc(50% - 18px) calc(50% - 18px) round 18px);
    }
    86% {
        clip-path: inset(0 0 0 0 round 20px);
    }
    100% {
        clip-path: inset(-60px round 20px);
    }
}

/* Leaving: the bar and its mark fade out together. */
.toast.toast-leaving {
    animation: toastFadeAway 0.28s ease both;
    pointer-events: none;
}

@keyframes toastFadeAway {
    from { opacity: 1; }
    to { opacity: 0; transform: translateY(-4px) scale(0.98); }
}

/* The mark: the ArcaneMetriX icon in a ring of the toast's color (green, red, amber or
   blue, as the toast's type is). It appears alone at the centre, spins as the header's
   logo does, and slides to the bar's left edge as the bar opens around it, where it stays
   for as long as the toast is shown. */
.toast:not(.toast-moment) .toast-mark {
    position: absolute;
    top: 50%;
    left: var(--space-3);
    width: 28px;
    height: 28px;
    margin-top: -14px;
    border-radius: var(--radius-round);
    background:
        url("/public/assets/brand/arcane-metrix-icon-64.png") center / 20px 20px no-repeat,
        color-mix(in srgb, var(--toast-accent, var(--accent-color)) 16%, var(--popup-bg));
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 60%, transparent);
    pointer-events: none;
    animation:
        toastMarkAppear 0.2s ease both,
        toastMarkSpin 0.76s cubic-bezier(0.34, 1.56, 0.64, 1) both,
        toastMarkSlide 0.56s cubic-bezier(0.22, 1, 0.36, 1) 0.2s both;
}

@keyframes toastMarkAppear {
    from { opacity: 0; scale: 0.6; }
    to { opacity: 1; scale: 1; }
}

/* One turn, with the header logo's overshoot (`spin-once` in styles/logo.css). */
@keyframes toastMarkSpin {
    from { rotate: 0deg; }
    to { rotate: 360deg; }
}

/* From the centre of the bar to its resting place at the left. */
@keyframes toastMarkSlide {
    from { left: calc(50% - 14px); }
    to { left: var(--space-3); }
}

/* A moment's capsule: a small pill carrying the mark, on show while the card opens out
   of it and closes back in, gone while the card is open. It draws its own edge, since
   the card's border and glow are clipped away while it is this small. */
.toast-moment .toast-mark {
    position: absolute;
    top: 0;
    left: 50%;
    width: 40px;
    height: 36px;
    margin-left: -20px;
    border-radius: var(--radius-pill);
    background:
        url("/public/assets/brand/arcane-metrix-icon-64.png") center / 22px 22px no-repeat,
        color-mix(in srgb, var(--toast-accent, var(--accent-color)) 16%, var(--popup-bg));
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 55%, transparent);
    opacity: 0;
    pointer-events: none;
    animation: toastMarkOpen 0.52s ease both;
}

@keyframes toastMarkOpen {
    0%, 30% { opacity: 1; transform: scale(1); }
    55%, 100% { opacity: 0; transform: scale(0.6); }
}

.toast-moment.toast-leaving .toast-mark {
    animation: toastMarkClose 0.28s ease both;
}

@keyframes toastMarkClose {
    0%, 35% { opacity: 0; transform: scale(0.6); }
    70%, 100% { opacity: 1; transform: scale(1); }
}

/* The words arrive quickly once the bar has begun to open. */
.toast > :not(.toast-mark):not(.toast-shards) {
    animation: toastContentIn 0.22s ease 0.36s both;
}

/* A moment's words leave before its card closes back into the capsule. */
.toast-moment.toast-leaving > :not(.toast-mark):not(.toast-shards) {
    animation: toastContentOut 0.12s ease both;
}

@keyframes toastContentIn {
    from { opacity: 0; transform: translateY(-3px); }
    to { opacity: 1; transform: none; }
}

@keyframes toastContentOut {
    to { opacity: 0; }
}

/* Thin auto-dismiss progress bar. `--toast-duration` is set inline by the widget from
   the toast's actual `duration_ms`, and hovering pauses the animation to mirror the
   countdown pause in ToastContainer — bar and timer stay in step. */
.toast::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    height: 2px;
    width: 100%;
    background: var(--toast-accent, var(--accent-color));
    opacity: 0.35;
    animation: toastProgress var(--toast-duration, 6500ms) linear forwards;
    transform-origin: left;
}

.toast:hover::after {
    animation-play-state: paused;
}

.toast:focus-within::after {
    animation-play-state: paused;
}

@keyframes toastProgress {
    from { width: 100%; }
    to   { width: 0%; }
}

.toast-icon {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    margin-top: var(--space-px);
    color: var(--toast-accent, var(--accent-color));
}

.toast-message {
    flex: 1;
    min-width: 0;
    word-break: break-word;
    color: var(--text-secondary);
}

.toast-close {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    cursor: pointer;
    color: var(--text-muted);
    border: none;
    background: none;
    padding: 0;
    margin-top: var(--space-px);
    opacity: 0.55;
    transition: opacity 0.15s;
    line-height: 1;
}

.toast-action {
    flex-shrink: 0;
    border: 0;
    background: none;
    color: var(--toast-accent, var(--accent-color));
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    padding: 0 var(--space-1);
}

.toast-action:hover,
.toast-action:focus-visible {
    text-decoration: underline;
}

.toast-close:hover {
    opacity: 1;
}

/* The message is a real control; action and dismiss stay sibling buttons. */
.toast .toast-open-log {
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.toast .toast-open-log:hover,
.toast .toast-open-log:focus-visible {
    color: var(--text-color);
    text-decoration: underline;
}

.toast .toast-open-log.toast-moment-log {
    margin-top: var(--space-2);
    font-size: 0.8rem;
}

/* Type accent colors — drives the icon, the glow and the progress bar */
.toast-success { --toast-accent: var(--success-color); }
.toast-error   { --toast-accent: var(--danger-color);  }
.toast-warning { --toast-accent: var(--warning-color); }
.toast-info    { --toast-accent: var(--accent-color);  }

/* --- A moment: the capsule opened into a card, with a burst ------------- */

.toast.toast-moment {
    flex-direction: column;
    align-items: center;
    gap: var(--space-1);
    width: min(280px, 100%);
    padding: var(--space-4) var(--space-5);
    /* A squircle's softness: well past the bar's corners. */
    border-radius: calc(var(--radius-4xl) * 1.7);
    text-align: center;
    /* The burst flies out past the card's edge. */
    overflow: visible;
    box-shadow:
        0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 45%, transparent),
        0 0 42px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 34%, transparent),
        0 18px 40px var(--popup-shadow);
    animation: toastMomentOpen 0.72s cubic-bezier(0.22, 1.25, 0.36, 1) both;
}

/* The same opening, further: the capsule grows wide, then tall, into the card,
   with a small overshoot as it lands. Clipped to the card's own round corners
   until it has landed, then well outside them for the glow and the burst. */
@keyframes toastMomentOpen {
    0% {
        opacity: 0;
        clip-path: inset(0 calc(50% - 20px) calc(100% - 36px) calc(50% - 20px) round 18px);
        transform: translateY(-6px) scale(0.94);
    }
    14% {
        opacity: 1;
        clip-path: inset(0 calc(50% - 20px) calc(100% - 36px) calc(50% - 20px) round 18px);
        transform: none;
    }
    45% {
        clip-path: inset(0 0 calc(100% - 44px) 0 round 22px);
    }
    78% {
        clip-path: inset(0 0 0 0 round 34px);
        transform: scale(1.03);
    }
    100% {
        opacity: 1;
        clip-path: inset(-140px round 34px);
        transform: none;
    }
}

.toast-moment.toast-leaving {
    animation: toastMomentClose 0.28s cubic-bezier(0.55, 0, 0.75, 0.2) both;
}

@keyframes toastMomentClose {
    0% {
        clip-path: inset(0 0 0 0 round 34px);
        opacity: 1;
    }
    55% {
        clip-path: inset(0 0 calc(100% - 44px) 0 round 22px);
        opacity: 1;
    }
    100% {
        clip-path: inset(0 calc(50% - 20px) calc(100% - 36px) calc(50% - 20px) round 18px);
        opacity: 0;
        transform: translateY(-6px) scale(0.94);
    }
}

.toast-moment .toast-mark { animation-duration: 0.72s; }

.toast-moment > :not(.toast-mark):not(.toast-shards) {
    animation-duration: 0.3s;
    animation-delay: 0.36s;
}

/* The pill naming the moment, as the reference's app badge. */
.toast-moment-pill {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-1) var(--space-3) var(--space-1) var(--space-1-5);
    border-radius: var(--radius-pill);
    background: color-mix(in srgb, var(--toast-accent, var(--accent-color)) 22%, var(--popup-bg));
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 55%, transparent);
    color: var(--text-color);
    font-size: 0.7rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.toast-moment-mark {
    width: 18px;
    height: 18px;
    background: url("/public/assets/brand/arcane-metrix-icon-64.png") center / contain no-repeat;
}

.toast-moment-headline {
    margin-top: var(--space-2);
    color: var(--text-color);
    font-size: 1.55rem;
    font-weight: 800;
    line-height: 1.12;
    letter-spacing: -0.01em;
    overflow-wrap: anywhere;
}

.toast-moment-label {
    color: var(--toast-accent, var(--accent-color));
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

/* The message is the toast's words for the log and screen readers; the card says
   it with the pill, the headline and the label. */
.toast-moment-message {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

.toast-moment .toast-action {
    margin-top: var(--space-2);
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-pill);
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--toast-accent, var(--accent-color)) 55%, transparent);
}

.toast-moment .toast-close {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3-5);
    margin: 0;
}

/* The card's panel is a layer of its own, between the burst and the words, so the
   shards are only ever seen outside the card and never cross its text. */
.toast-moment {
    background: transparent;
}

.toast-moment::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: inherit;
    background: var(--popup-bg);
}

/* The progress bar stays inside the card's round corners. */
.toast-moment::after {
    left: 24px;
    right: 24px;
    bottom: 8px;
    width: auto;
    border-radius: var(--radius-pill);
    animation-name: toastMomentProgress;
}

@keyframes toastMomentProgress {
    from { right: 24px; }
    to { right: calc(100% - 24px); }
}

/* The burst: twelve shards thrown out from behind the card as it lands, in the
   five mana colors and the moment's accent. Each shard's direction and spin are
   its own (`--dx`, `--dy`, `--spin`). */
.toast-shards {
    position: absolute;
    inset: 0;
    z-index: -2;
    pointer-events: none;
}

.toast-shards i {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 10px;
    height: 18px;
    margin: -9px 0 0 -5px;
    border-radius: var(--radius-sm);
    background: var(--shard, var(--accent-color));
    box-shadow: 0 0 10px color-mix(in srgb, var(--shard, var(--accent-color)) 60%, transparent);
    opacity: 0;
    animation: toastShard 1.4s cubic-bezier(0.16, 1, 0.3, 1) 0.4s both;
}

@keyframes toastShard {
    0% {
        opacity: 0;
        transform: translate(calc(var(--dx) * 0.35), calc(var(--dy) * 0.35)) rotate(0deg) scale(0.4);
    }
    12% { opacity: 1; }
    65% { opacity: 1; }
    100% {
        opacity: 0;
        transform: translate(var(--dx), var(--dy)) rotate(var(--spin)) scale(1);
    }
}

.toast-shards i:nth-child(6n + 1) { --shard: var(--mtg-white-mana); }
.toast-shards i:nth-child(6n + 2) { --shard: var(--mtg-blue-mana); }
.toast-shards i:nth-child(6n + 3) { --shard: var(--mtg-generic-mana); }
.toast-shards i:nth-child(6n + 4) { --shard: var(--mtg-red-mana); }
.toast-shards i:nth-child(6n + 5) { --shard: var(--mtg-green-mana); }
.toast-shards i:nth-child(6n + 6) { --shard: var(--toast-accent, var(--accent-color)); }

.toast-shards i:nth-child(1)  { --dx: -170px; --dy: -40px;  --spin: -160deg; }
.toast-shards i:nth-child(2)  { --dx: 165px;  --dy: -55px;  --spin: 140deg; }
.toast-shards i:nth-child(3)  { --dx: -120px; --dy: 95px;   --spin: 210deg; }
.toast-shards i:nth-child(4)  { --dx: 130px;  --dy: 100px;  --spin: -200deg; }
.toast-shards i:nth-child(5)  { --dx: -40px;  --dy: 150px;  --spin: 120deg; }
.toast-shards i:nth-child(6)  { --dx: 55px;   --dy: 145px;  --spin: -130deg; }
.toast-shards i:nth-child(7)  { --dx: -185px; --dy: 55px;   --spin: 180deg; }
.toast-shards i:nth-child(8)  { --dx: 190px;  --dy: 30px;   --spin: -170deg; }
.toast-shards i:nth-child(9)  { --dx: -95px;  --dy: -20px;  --spin: 95deg; }
.toast-shards i:nth-child(10) { --dx: 100px;  --dy: -10px;  --spin: -110deg; }
.toast-shards i:nth-child(11) { --dx: -150px; --dy: 140px;  --spin: 240deg; }
.toast-shards i:nth-child(12) { --dx: 150px;  --dy: 150px;  --spin: -230deg; }

/* --- Less motion ---------------------------------------------------------- */

/* A reader who asked for less motion gets a fade in and out: no opening out of the
   capsule, no burst. */
@media (prefers-reduced-motion: reduce) {
    .toast,
    .toast.toast-moment {
        animation: toastFadeIn 0.2s ease both;
    }

    .toast.toast-leaving,
    .toast-moment.toast-leaving {
        animation: toastFadeOut 0.2s ease both;
    }

    .toast > :not(.toast-mark):not(.toast-shards),
    .toast-leaving > :not(.toast-mark):not(.toast-shards) {
        animation: none;
    }

    .toast:not(.toast-moment) .toast-mark {
        animation: none;
    }

    .toast-moment .toast-mark,
    .toast-shards {
        display: none;
    }

    @keyframes toastFadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }

    @keyframes toastFadeOut {
        to { opacity: 0; }
    }
}
