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

[hidden] {
    display: none !important;
}

/* ───────────── Tokens ─────────────
   The room takes the traditional color of the mystery being prayed.
   --rubric is the missal convention: instructions printed in red. */
body {
    --ground: #2a2320;
    --ink: #eadfd0;
    --rubric: #e58c7a;

    --bead: color-mix(in srgb, var(--ink) 80%, var(--ground));
    --bead-hi: color-mix(in srgb, var(--bead) 55%, white);
    --bead-lo: color-mix(in srgb, var(--bead) 60%, black);
    --bead-soft: color-mix(in srgb, var(--ink) 55%, var(--ground));
    --rubric-lo: color-mix(in srgb, var(--rubric) 60%, black);
    --thread: color-mix(in srgb, var(--ink) 45%, transparent);
    --faint: color-mix(in srgb, var(--ink) 22%, transparent);

    --font: 'Literata', Georgia, 'Times New Roman', serif;
    --step--1: 0.875rem;
    --step-0: 1rem;
    --step-1: 1.35rem;
    --step-2: 2rem;
    --step-3: clamp(2rem, 4.2vw, 3.25rem);
}

body[data-mystery="joyful"]    { --ground: #dce5ea; --ink: #17262f; --rubric: #b8322a; }
body[data-mystery="sorrowful"] { --ground: #55191f; --ink: #f4ded7; --rubric: #f2a090; }
body[data-mystery="glorious"]  { --ground: #c8982c; --ink: #241a05; --rubric: #65140d; }
body[data-mystery="luminous"]  { --ground: #1e3a86; --ink: #e6ecff; --rubric: #ffd9a0; }

/* Several languages side by side: give the text more of the screen */
body[data-langs="2"] { --content-flex: 1.7; }
body[data-langs="3"] { --content-flex: 2.7; }

/* ───────────── Base ───────────── */
body {
    font-family: var(--font);
    font-size: var(--step-0);
    background: var(--ground);
    color: var(--ink);
    height: 100dvh;
    overflow: hidden;
    transition: background-color 0.6s ease, color 0.6s ease;
}

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

.rosary-container {
    height: 100%;
    display: flex;
    gap: clamp(24px, 4vw, 64px);
    padding: clamp(24px, 4vw, 56px);
    transition: transform 0.1s ease;
}

.rosary-container.swipe-left  { transform: translateX(-5px); }
.rosary-container.swipe-right { transform: translateX(5px); }

.content-column {
    flex: var(--content-flex, 1);
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* ───────────── Prayer ───────────── */
.mystery-title {
    font-size: var(--step-0);
    opacity: 0.75;
    min-height: 1.5em;
    flex-shrink: 0;
}

.prayer-section {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding-top: clamp(12px, 3vh, 40px);
}

@keyframes prayerFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.prayer-section.fade-in {
    animation: prayerFadeIn 0.3s ease forwards;
}

.prayer-type {
    font-size: var(--step-3);
    font-weight: 300;
    line-height: 1.1;
    letter-spacing: -0.01em;
    flex-shrink: 0;
}

.position-indicator {
    font-size: var(--step-0);
    font-style: italic;
    color: var(--rubric);
    min-height: 1.5em;
    margin: 0.5rem 0 1rem;
    flex-shrink: 0;
}

.prayer-cols {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
    align-content: start;
    column-gap: clamp(20px, 3vw, 48px);
    padding-right: 12px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: var(--thread) transparent;
}

.prayer-cols[data-n="1"] { --cols: 1; }
.prayer-cols[data-n="2"] { --cols: 2; }
.prayer-cols[data-n="3"] { --cols: 3; }

.prayer-cols[data-n="2"] .prayer-type { font-size: clamp(1.6rem, 2.8vw, 2.4rem); }
.prayer-cols[data-n="3"] .prayer-type { font-size: clamp(1.35rem, 2.1vw, 1.85rem); }

.prayer-col {
    min-width: 0;
    display: grid;
    grid-row: 1 / span 3;
    grid-template-rows: subgrid;
}

.prayer-text {
    white-space: pre-line;
    max-width: 40ch;
    font-size: var(--step-1);
    line-height: 1.7;
}

.prayer-cols[data-n="2"] .prayer-text { font-size: 1.15rem; line-height: 1.65; }
.prayer-cols[data-n="3"] .prayer-text { font-size: 1rem; line-height: 1.6; }

/* ───────────── Language picker (selection screen) ───────────── */
.language-picker {
    display: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

.picker-label {
    font-style: italic;
    opacity: 0.75;
    margin-right: 0.25rem;
}

.lang-option {
    font: inherit;
    min-height: 44px;
    padding: 0.5rem 1.1rem;
    color: var(--ink);
    background: transparent;
    border: 1.5px solid var(--thread);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.lang-option:hover {
    border-color: var(--ink);
}

.lang-option[aria-pressed="true"] {
    background: var(--ink);
    color: var(--ground);
    border-color: var(--ink);
    font-weight: 600;
}

/* ───────────── Mystery picker (selection screen) ───────────── */
.mystery-picker {
    display: none;
    flex-direction: column;
    gap: 2px;
    max-width: 24rem;
    margin-top: 1rem;
}

.mystery-option {
    display: grid;
    grid-template-columns: 1.25rem 1fr auto;
    grid-template-areas:
        "bead name today"
        "bead days today";
    column-gap: 0.9rem;
    align-items: center;
    text-align: left;
    font: inherit;
    color: inherit;
    background: none;
    border: 0;
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    min-height: 52px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.mystery-option:hover {
    background: color-mix(in srgb, var(--ink) 9%, transparent);
}

.option-bead {
    grid-area: bead;
    width: 0.9rem;
    height: 0.9rem;
    border-radius: 50%;
    border: 2px solid var(--thread);
    transition: background-color 0.2s ease, border-color 0.2s ease;
}

.mystery-option.is-selected .option-bead {
    background: var(--rubric);
    border-color: var(--rubric);
}

.option-name {
    grid-area: name;
    font-size: var(--step-1);
    line-height: 1.2;
}

.mystery-option.is-selected .option-name {
    font-weight: 600;
}

.option-days {
    grid-area: days;
    font-size: var(--step--1);
    opacity: 0.75;
}

.option-today {
    grid-area: today;
    font-size: var(--step--1);
    font-style: italic;
    color: var(--rubric);
}

#begin-button {
    align-self: flex-start;
    margin-top: 1.25rem;
}

/* ───────────── Footer: progress, decades, controls ───────────── */
.column-foot {
    flex-shrink: 0;
    padding-top: 1rem;
}

.progress-bar {
    height: 2px;
    background: var(--faint);
    margin-bottom: 0.9rem;
}

.progress-fill {
    height: 100%;
    width: 0;
    background: var(--ink);
    transition: width 0.3s ease;
}

.decade-counter {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 1.5rem;
    font-size: var(--step--1);
}

.decade-ticks {
    display: inline-flex;
    gap: 6px;
}

.decade-ticks:empty {
    display: none;
}

.tick {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    border: 1.5px solid var(--thread);
}

.tick.done { background: var(--ink); border-color: var(--ink); }
.tick.now  { background: var(--rubric); border-color: var(--rubric); }

.controls {
    font-size: var(--step--1);
    font-style: italic;
    color: var(--rubric);
    margin-top: 0.4rem;
}

.nav {
    display: flex;
    gap: 0.75rem;
    margin-top: 1rem;
}

body[data-screen="select"] .column-foot {
    display: none;
}

body[data-screen="pray"] .mystery-picker {
    display: none;
}

body[data-screen="select"] .mystery-picker {
    display: flex;
}

body[data-screen="select"] .language-picker {
    display: flex;
}

body[data-screen="select"] .prayer-cols {
    flex: none;
    overflow: visible;
}

/* The selection screen is taller than short windows: let it scroll */
body[data-screen="select"] .prayer-section {
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--thread) transparent;
}

body[data-screen="pray"] .language-picker {
    display: none;
}

/* ───────────── Buttons ───────────── */
.button {
    font: inherit;
    font-weight: 600;
    min-height: 48px;
    min-width: 7rem;
    padding: 0.7rem 1.5rem;
    color: var(--ink);
    background: transparent;
    border: 1.5px solid var(--ink);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease, opacity 0.2s ease;
}

.button:hover {
    background: color-mix(in srgb, var(--ink) 12%, transparent);
}

.button:active {
    transform: translateY(1px);
}

.button-primary {
    background: var(--ink);
    color: var(--ground);
}

.button-primary:hover {
    background: var(--ink);
    opacity: 0.88;
}

/* ───────────── Rosary drawing ───────────── */
.rosary-visual-container {
    flex: 1;
    display: none;
    min-width: 0;
    min-height: 0;
}

.rosary-visual-container.show {
    display: flex;
    align-items: center;
    justify-content: center;
}

.visual-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.visual-wrapper svg {
    width: 100%;
    height: 100%;
    overflow: visible;
}

/* Chain and cross are matte, taken from the ink color */
.visual-wrapper svg line,
.visual-wrapper svg ellipse {
    stroke: var(--thread);
}

.visual-wrapper svg rect[fill="#7a3a08"] {
    fill: var(--rubric-lo);
    stroke: var(--rubric);
}

.visual-wrapper svg text {
    fill: var(--bead-hi);
}

.visual-wrapper svg circle.svg-bead {
    stroke: color-mix(in srgb, var(--ink) 30%, transparent);
}

.svg-bead.completed {
    opacity: 0.4;
}

.svg-bead.current {
    transform-box: fill-box;
    transform-origin: center;
    animation:
        bead-arrive 0.38s cubic-bezier(0.34, 1.56, 0.64, 1) forwards,
        bead-pulse  1.25s ease-in-out 0.38s infinite alternate;
}

@keyframes bead-arrive {
    from {
        transform: scale(0.85);
        filter: drop-shadow(0 0 2px var(--rubric));
    }
    to {
        transform: scale(1.3);
        filter: drop-shadow(0 0 4px var(--rubric)) drop-shadow(0 0 8px var(--rubric));
    }
}

@keyframes bead-pulse {
    from {
        transform: scale(1.3);
        filter: drop-shadow(0 0 4px var(--rubric)) drop-shadow(0 0 8px var(--rubric));
    }
    to {
        transform: scale(1.62);
        filter: drop-shadow(0 0 10px var(--rubric)) drop-shadow(0 0 22px var(--rubric));
    }
}

/* ───────────── Final slide ───────────── */
.final-slide {
    position: fixed;
    inset: 0;
    background: var(--ground);
    color: var(--ink);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 24px;
    opacity: 0;
    overflow-y: auto;
    transition: opacity 0.6s ease;
}

.final-slide.visible {
    opacity: 1;
}

.final-content {
    width: 100%;
    max-width: 32rem;
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 1.25rem;
}

.final-content h1 {
    font-size: var(--step-3);
    font-weight: 300;
    line-height: 1.1;
    letter-spacing: -0.01em;
}

.final-content p {
    font-size: var(--step-1);
}

.gif-container {
    width: 200px;
    height: 200px;
    border-radius: 6px;
    overflow: hidden;
    flex-shrink: 0;
}

.gif-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* BitToByte author link */
.bittobyte-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 1rem;
    padding: 9px 14px;
    border: 1px solid var(--faint);
    border-radius: 6px;
    text-decoration: none;
    color: inherit;
    transition: border-color 0.25s ease;
}

.bittobyte-btn:hover {
    border-color: var(--ink);
}

.bittobyte-logo {
    font-family: 'Courier New', monospace;
    font-weight: bold;
    font-size: 0.78em;
    background: var(--ink);
    color: var(--ground);
    padding: 4px 8px;
    border-radius: 4px;
    letter-spacing: 0.08em;
    flex-shrink: 0;
}

.bittobyte-info {
    display: flex;
    flex-direction: column;
    flex: 1;
    line-height: 1.2;
}

.bittobyte-made {
    font-size: var(--step--1);
    opacity: 0.75;
}

.bittobyte-name {
    font-size: var(--step--1);
    font-weight: 600;
}

.bittobyte-arrow {
    flex-shrink: 0;
    transition: transform 0.25s ease;
}

.bittobyte-btn:hover .bittobyte-arrow {
    transform: translate(2px, -2px);
}

.hidden {
    display: none !important;
}

/* ───────────── Small screens ───────────── */
@media (max-width: 900px) {
    body {
        height: auto;
        min-height: 100dvh;
        overflow-y: auto;
    }

    .rosary-container {
        height: auto;
        min-height: 100dvh;
        flex-direction: column;
        gap: 16px;
        padding: 16px 16px 0;
        -webkit-user-select: none;
        user-select: none;
    }

    .content-column {
        flex: 1;
    }

    .prayer-section {
        flex: 1;
        padding-top: 0;
    }

    .prayer-cols {
        flex: none;
        overflow: visible;
        grid-template-columns: 1fr;
        row-gap: 1.75rem;
        padding-right: 0;
    }

    .prayer-col {
        display: block;
        grid-row: auto;
    }

    .prayer-col + .prayer-col {
        border-top: 1px solid var(--faint);
        padding-top: 1.5rem;
    }

    .prayer-cols[data-n] .prayer-type { font-size: var(--step-2); }
    .prayer-cols[data-n] .prayer-text { font-size: var(--step-1); max-width: none; }

    body[data-langs] { --content-flex: 1; }

    /* More text on screen: shrink the drawing to leave room */
    body[data-langs="2"] .rosary-visual-container,
    body[data-langs="3"] .rosary-visual-container {
        height: min(28vh, 260px);
    }

    .rosary-visual-container {
        order: -1;
        flex: none;
        height: min(44vh, 400px);
    }

    .column-foot {
        position: sticky;
        bottom: 0;
        background: var(--ground);
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
        transition: background-color 0.6s ease;
    }

    .nav .button {
        flex: 1;
    }

    .final-content {
        gap: 1rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
    }

    .svg-bead.current {
        transform: scale(1.5);
        filter: drop-shadow(0 0 6px var(--rubric));
    }
}
