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

html {
    height: 100%;
}

body {
    background: #fff;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', sans-serif;
    font-weight: 400;
    letter-spacing: 0.02em;
    padding: 24px 0 16px;
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    --accent: #111;
    --accent-light: #eee;
    --text: #111;
    --button: #111;
}

/* Color Themes */
body.theme-bw {
    --accent: #111;
    --accent-light: #eee;
    --text: #111;
    --button: #111;
    background: #fff;
}

body.theme-ocean {
    --accent: #2B5F7F;
    --accent-light: #D4E5F0;
    --text: #1a1a1a;
    --button: #2B5F7F;
    background: #f0f5f8;
}

body.theme-sunset {
    --accent: #C64545;
    --accent-light: #F0D9D1;
    --text: #1a1a1a;
    --button: #C64545;
    background: #faf6f3;
}

body.theme-sage {
    --accent: #5B8C5A;
    --accent-light: #DFE8DC;
    --text: #1a1a1a;
    --button: #5B8C5A;
    background: #f5f8f4;
}

body.theme-custom {
    background: var(--custom-bg, #faf7f4);
}

.container {
    width: 90%;
    max-width: none;
    margin: 0 auto;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.page-header {
    flex: 0 0 auto;
    margin-bottom: 16px;
}

.page-header h1 {
    margin: 0;
    color: var(--text);
    font-size: clamp(34px, 5vw, 72px);
    font-weight: 400;
    letter-spacing: -0.035em;
    line-height: 1;
    display: flex;
    align-items: center;
    gap: 0.16em;
}

.page-header .logo-mark {
    flex: 0 0 auto;
    width: 1em;
    height: 1em;
    display: block;
}

.page-header h1 strong {
    font-weight: 800;
}

.section {
    margin-bottom: 40px;
}

.section-title {
    flex: 0 0 auto;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    margin-bottom: 12px;
    color: #999;
    font-weight: 400;
}

/* Input Section */
.input-wrapper {
    display: flex;
    gap: 12px;
    margin-bottom: 0;
    align-items: center;
}

input[type="text"] {
    flex: 1;
    padding: 16px 24px;
    border: 1px solid #e0e0e0;
    font-family: inherit;
    font-size: 16px;
    font-weight: 400;
    letter-spacing: 0.02em;
    background: white;
    transition: border-color 0.3s;
}

input[type="text"]:focus {
    outline: none;
    border-color: #333;
}

input[type="text"]::placeholder {
    color: #ccc;
}

.input-button {
    padding: 16px 28px;
    white-space: nowrap;
}

/* Button Group */
.button-group {
    display: flex;
    gap: 12px;
    margin-top: 0;
}

button {
    padding: 14px 28px;
    background: var(--button);
    color: white;
    border: none;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    font-weight: 400;
    cursor: pointer;
    transition: background 0.3s;
}

button:hover {
    opacity: 0.8;
}

button:active {
    opacity: 0.6;
}

button:disabled {
    background: #ddd;
    cursor: not-allowed;
}

/* Names Display */
.names-section {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    margin-bottom: 0;
}

.names-wrapper {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    align-content: center;
    gap: 0.15em 0.19em;
    font-size: 52px;
    line-height: 1.15;
    letter-spacing: 0.007em;
    color: var(--text);
    padding: 8px 0;
    position: relative;
    font-weight: 400;
    flex: 1 1 auto;
    min-height: 0;
    height: auto;
    max-height: none;
    overflow: hidden;
    transition: font-size 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Applied while the auto-fit probes sizes. Transitions must be off during
   measurement or every height read reports the size being animated away from. */
.names-wrapper.is-measuring,
.names-wrapper.is-measuring .name {
    transition: none !important;
}

.name {
    display: inline-flex;
    align-items: center;
    cursor: default;
    padding: 0.03em 0.06em;
    margin: 0;
    border-radius: 0;
    position: relative;
    user-select: none;
    /* Never transition "all" here - it catches the inherited font-size and
       fights the auto-fit. Only transform/box-shadow animate; background and
       text colour switch instantly. A reel step can be as short as ~7ms on a
       long list, well under any transition duration, so animating colour
       here meant it never finished filling in before the next name lit up -
       the highlight read as translucent grey instead of solid. */
    transition: transform 0.14s ease, box-shadow 0.14s ease;
    font-weight: 400;
    max-width: 100%;
    overflow-wrap: anywhere;
}

.name-delete {
    position: absolute;
    top: -0.12em;
    right: -0.18em;
    width: 18px;
    height: 18px;
    background: #e53935;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0;
    font-size: 12px;
    line-height: 0;
    z-index: 1;
    transition: transform 0.15s ease;
}

.name-delete svg {
    width: 9px;
    height: 9px;
    display: block;
    fill: currentColor;
}

.name:hover .name-delete,
.name-delete:focus-visible {
    display: flex;
}

.names-wrapper.is-locked .name-delete {
    display: none !important;
}

.name-delete:hover {
    transform: scale(1.08);
}

.name.spinning {
    background: #1a1a1a;
    color: #fff;
}

/* Decelerating - the reel is closing in. */
.name.spinning.tier-slow {
    transform: scale(1.12);
    box-shadow: 0 0 26px rgba(0, 0, 0, 0.34);
}

/* Final few beats - hold, swell, and glow. */
.name.spinning.tier-final {
    transform: scale(1.24);
    box-shadow: 0 0 44px rgba(0, 0, 0, 0.5);
    animation: spin-final-pulse 0.55s ease-out;
}

.name.winner {
    color: #fff;
    background: var(--accent);
    animation: winner-land 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes spin-final-pulse {
    0% {
        transform: scale(1.12);
    }
    45% {
        transform: scale(1.3);
    }
    100% {
        transform: scale(1.24);
    }
}

@keyframes winner-land {
    0% {
        transform: scale(1.24);
    }
    40% {
        transform: scale(1.38);
    }
    100% {
        transform: scale(1);
    }
}

.empty-state {
    flex: 1 0 100%;
    color: #bbb;
    font-size: 16px;
    line-height: 1.6;
    letter-spacing: 0.02em;
    padding: 0;
    text-align: center;
}

body.fresh-empty .empty-state {
    color: var(--text);
    font-size: clamp(28px, 4.5vw, 52px);
    font-weight: 100;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.82;
}

body.fresh-empty .action-row #nameInput {
    border-color: var(--accent);
    color: var(--text);
    animation: fresh-input-pulse 1.35s ease-in-out 4;
}

body.fresh-empty .action-row #nameInput::placeholder {
    color: var(--text);
    opacity: 0.7;
}

body.fresh-empty .action-row .input-button {
    animation: fresh-button-pulse 1.35s ease-in-out 4;
}

@keyframes fresh-input-pulse {
    0%, 100% {
        box-shadow: 0 0 0 0 transparent;
    }
    50% {
        box-shadow: 0 0 0 6px var(--accent-light);
    }
}

@keyframes fresh-button-pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.04);
    }
}

@media (prefers-reduced-motion: reduce) {
    body.fresh-empty .action-row #nameInput,
    body.fresh-empty .action-row .input-button {
        animation: none;
    }
}

/* Controls */
.controls {
    display: flex;
    flex: 0 0 auto;
    gap: 12px;
    margin: 12px 0 28px;
}

.btn-select {
    padding: 16px 32px;
    font-size: 12px;
}

.action-row {
    display: flex;
    flex: 0 0 auto;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.action-row input[type="text"] {
    flex: 1 1 180px;
    min-width: 140px;
    height: 42px;
    padding: 0 8px;
    font-size: 16px;
    letter-spacing: 0.01em;
}

.action-row button {
    height: 42px;
    padding: 0 16px;
    font-size: 12px;
}

.btn-clear {
    margin-left: 24px;
}

.btn-secondary {
    border: 1px solid var(--button);
    background: transparent;
    color: var(--button);
}

.saved-randoms-section {
    margin-bottom: 40px;
}

.saved-randoms {
    display: grid;
    gap: 10px;
    max-height: 36vh;
    overflow-y: auto;
}

.saved-random {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    border: 1px solid #e0e0e0;
    padding: 14px 16px;
}

.saved-random strong,
.saved-random span {
    display: block;
}

.saved-random strong {
    margin-bottom: 4px;
    color: var(--text);
    font-size: 14px;
}

.saved-random span {
    color: #888;
    font-size: 12px;
}

.saved-randoms-empty {
    color: #888;
    font-size: 12px;
    border: 1px dashed #d7d7d7;
    padding: 20px;
    text-align: center;
}

.saved-random-actions {
    display: flex;
    gap: 8px;
}

.saved-random-actions button {
    padding: 9px 12px;
    font-size: 10px;
}

/* Confetti - spawned at an exact viewport x/y (left/top, set inline per
   particle) and animated outward from there via --tx/--ty/--rot. */
.confetti {
    position: fixed;
    top: 0;
    left: 0;
    width: 8px;
    height: 8px;
    background: var(--accent);
    pointer-events: none;
    /* Above the winner overlay (1000) so the celebration lands on top of it,
       still below the app modal (1100). */
    z-index: 1050;
    animation: confettiBurst 1s cubic-bezier(0.18, 0.82, 0.32, 1) forwards;
}

@keyframes confettiBurst {
    /* translate(-50%, -50%) recenters the particle on its spawn point -
       without it every piece starts offset by its own width/height. */
    0% {
        transform: translate(-50%, -50%) scale(0.4);
        opacity: 1;
    }
    /* A quick overshoot partway through the flight is what sells the "pop"
       of an explosion rather than a smooth glide outward. */
    16% {
        transform: translate(-50%, -50%) translate(calc(var(--tx) * 0.4), calc(var(--ty) * 0.4)) rotate(calc(var(--rot) * 0.35)) scale(1.2);
    }
    70% {
        opacity: 1;
    }
    100% {
        transform: translate(-50%, -50%) translate(var(--tx), var(--ty)) rotate(var(--rot)) scale(0.8);
        opacity: 0;
    }
}

/* Winner Overlay */
.winner-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.98);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    pointer-events: none;
    animation: slideDown 0.5s ease-out forwards;
    cursor: pointer;
}

.winner-overlay.show {
    display: flex;
}

.app-modal {
    position: fixed;
    inset: 0;
    z-index: 1100;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(20, 20, 20, 0.45);
}

.app-modal.show {
    display: flex;
}

.app-modal-card {
    width: min(100%, 460px);
    border-top: 5px solid var(--accent);
    background: white;
    padding: 30px;
    box-shadow: 0 22px 70px rgba(0, 0, 0, 0.22);
}

.app-modal-message {
    margin: 10px 0 22px;
    color: #666;
    font-size: 14px;
    line-height: 1.55;
}

.app-modal-field {
    display: grid;
    gap: 8px;
    margin-bottom: 22px;
    color: #666;
    font-size: 11px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.app-modal-field[hidden] {
    display: none;
}

.app-modal-field input {
    width: 100%;
    border: 1px solid #ccc;
    padding: 13px 14px;
    color: var(--text);
    font-size: 15px;
    letter-spacing: normal;
    text-transform: none;
}

.app-modal-field input:focus {
    border-color: var(--accent);
    outline: 2px solid var(--accent-light);
}

.app-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

.winner-content {
    text-align: center;
}

.winner-label {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    color: #999;
    margin-bottom: 24px;
    font-weight: 400;
}

.winner-name {
    font-size: 72px;
    font-weight: 400;
    letter-spacing: 0.04em;
    color: var(--accent);
    line-height: 1.2;
    max-width: 90vw;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
    }
    to {
        transform: translateY(0);
    }
}

@keyframes slideUp {
    from {
        transform: translateY(0);
    }
    to {
        transform: translateY(-100%);
    }
}

/* Settings */
.settings-button {
    position: fixed;
    bottom: 24px;
    right: 5%;
    width: 55px;
    height: 55px;
    border-radius: 50%;
    /* No background - a plain icon, not a filled circle button. A text
       glyph (the old up-caret) also never sat quite centred inside the flex
       box because glyph metrics vary by font; an SVG sized and centred
       explicitly doesn't have that problem. */
    background: none;
    color: var(--text, #333);
    border: none;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s ease, transform 0.25s ease;
    opacity: 0.7;
    z-index: 500;
}

.settings-button:hover {
    opacity: 1;
}

.settings-button svg {
    display: block;
}

/* A small turn on open, so the cog reads as a toggle rather than a static icon. */
.settings-button[aria-expanded="true"] {
    transform: rotate(35deg);
    opacity: 1;
}

.settings-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    border-top: 1px solid #e0e0e0;
    padding: 22px 5% 28px;
    max-height: 62vh;
    overflow-y: auto;
    z-index: 499;
    transform: translateY(100%);
    transition: transform 0.4s ease;
    box-shadow: 0 -2px 20px rgba(0, 0, 0, 0.1);
}

.settings-panel.open {
    transform: translateY(0);
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
}

.settings-title {
    font-size: 18px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.settings-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #999;
    padding: 0;
}

.settings-split {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 32px;
    min-height: 0;
}

.settings-column {
    min-width: 0;
}

.settings-group {
    margin-bottom: 50px;
}

.settings-group-title {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.15em;
    color: #999;
    margin-bottom: 16px;
    font-weight: 400;
}

input[type="range"] {
    flex: 1;
    height: 2px;
    border: none;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    background: #e0e0e0;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #333;
    cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #333;
    border: none;
    cursor: pointer;
}

.color-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
    gap: 10px;
}

.color-option {
    padding: 10px;
    border: 2px solid #e0e0e0;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
    text-align: center;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 400;
    position: relative;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.color-option .color-preview {
    width: 100%;
    height: 36px;
    border-radius: 2px;
    margin-bottom: 8px;
    display: flex;
    gap: 4px;
}

.color-option .color-swatch {
    flex: 1;
    border-radius: 2px;
}

.color-option:hover {
    border-color: #333;
}

.color-option.active {
    border-color: #333;
    background: #f5f5f5;
    font-weight: 500;
}

.custom-color-control {
    display: grid;
    gap: 8px;
    margin-top: 14px;
    color: #888;
    font-size: 11px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.custom-color-control[hidden] {
    display: none;
}

.custom-color-control input[type="range"] {
    width: 100%;
    height: 10px;
    border-radius: 999px;
    background: linear-gradient(
        to right,
        hsl(0, 75%, 50%),
        hsl(60, 75%, 50%),
        hsl(120, 75%, 50%),
        hsl(180, 75%, 50%),
        hsl(240, 75%, 50%),
        hsl(300, 75%, 50%),
        hsl(360, 75%, 50%)
    );
}

.custom-color-control input[type="range"]::-webkit-slider-thumb {
    background: var(--accent);
    border: 2px solid white;
    box-shadow: 0 0 0 1px #ccc;
}

.custom-preview .color-swatch:first-child {
    background: var(--accent);
}

.custom-preview .color-swatch:last-child {
    background: var(--accent-light);
}

/* Info */
.info {
    flex: 0 0 auto;
    padding: 12px 0 0;
    border-top: 1px solid #e0e0e0;
    font-size: 13px;
    color: #999;
    letter-spacing: 0.02em;
    line-height: 1.6;
}

.info-item {
    margin-bottom: 12px;
}

.info-item strong {
    color: #1a1a1a;
    font-weight: 500;
}

@media (max-width: 700px) {
    body {
        padding: 24px 0;
    }

    .names-wrapper {
        padding: 4px 0;
    }

    .settings-panel {
        max-height: 72vh;
        overflow-y: auto;
    }

    .settings-split {
        grid-template-columns: 1fr;
        gap: 24px;
    }

    .input-wrapper,
    .controls,
    .action-row {
        flex-direction: column;
        align-items: stretch;
    }

    .input-button,
    .btn-select,
    .action-row button,
    .action-row input[type="text"] {
        width: 100%;
    }

    .action-row input[type="text"] {
        flex: 0 0 42px;
        height: 42px;
        min-height: 42px;
        max-height: 42px;
    }

    .btn-clear {
        margin-left: 0;
        margin-top: 16px;
    }

    .save-share {
        padding: 0;
    }

    .saved-random {
        align-items: stretch;
        flex-direction: column;
    }

    .saved-random-actions {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
    }

    .app-modal-card {
        padding: 24px 20px;
    }

    .app-modal-actions {
        flex-direction: column-reverse;
    }

    .app-modal-actions button {
        width: 100%;
    }
}