:root {
    --bg: #0b0d12;
    --bg-elev: #12151c;
    --bg-elev-2: #191d26;
    --border: #262c38;
    --border-strong: #343b4a;
    --text: #e8ecf5;
    --text-dim: #9aa3b4;
    --text-mute: #6a7283;
    --accent: #ff2e4d;
    --accent-hover: #ff4e6a;
    --success: #22c55e;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    --shadow-sm: 0 4px 14px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: radial-gradient(1200px 800px at 10% -10%, #161a24 0%, var(--bg) 55%) no-repeat fixed;
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
    font-size: 15px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1, h2 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }

.uv {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* ─── Header ──────────────────────────────────────────────────────────── */

.uv__header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 14px 22px;
    background: linear-gradient(180deg, rgba(18, 21, 28, 0.95) 0%, rgba(11, 13, 18, 0.95) 100%);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
}

.uv__brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.uv__logo {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--accent) 0%, #ff6a88 100%);
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 18px;
    box-shadow: 0 4px 14px rgba(255, 46, 77, 0.35);
    flex-shrink: 0;
}

.uv__brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.uv__brand-text h1 {
    font-size: 18px;
    color: var(--text);
}
.uv__tag {
    font-size: 12px;
    color: var(--text-mute);
    margin-top: 2px;
}

.uv__form {
    display: flex;
    flex: 1;
    gap: 10px;
    max-width: 900px;
    margin-left: auto;
}

.uv__input {
    flex: 1;
    min-width: 0;
    padding: 11px 15px;
    background: var(--bg-elev);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.uv__input::placeholder { color: var(--text-mute); }
.uv__input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(255, 46, 77, 0.18);
    background: var(--bg-elev-2);
}

.uv__submit {
    padding: 11px 20px;
    background: var(--accent);
    color: #fff;
    border: 0;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background 0.15s, transform 0.05s;
    white-space: nowrap;
}
.uv__submit:hover { background: var(--accent-hover); }
.uv__submit:active { transform: translateY(1px); }

.uv__mode {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: var(--bg-elev);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.uv__mode:hover {
    background: var(--bg-elev-2);
    border-color: var(--border-strong);
}
.uv__mode.is-on {
    background: rgba(34, 197, 94, 0.12);
    border-color: var(--success);
    color: #7ee9a6;
}
.uv__mode-icon { font-size: 14px; line-height: 1; }

/* ─── Error ───────────────────────────────────────────────────────────── */

.uv__error {
    margin: 14px 22px 0;
    padding: 10px 14px;
    background: rgba(255, 46, 77, 0.1);
    color: #ff8fa3;
    border: 1px solid rgba(255, 46, 77, 0.3);
    border-radius: var(--radius-sm);
    font-size: 14px;
}

/* ─── Empty state ─────────────────────────────────────────────────────── */

.uv__empty {
    margin: auto;
    max-width: 620px;
    padding: 48px 24px;
    text-align: center;
    color: var(--text-dim);
}
.uv__empty-icon {
    font-size: 48px;
    margin-bottom: 14px;
    opacity: 0.6;
}
.uv__empty h2 {
    color: var(--text);
    font-size: 20px;
    margin-bottom: 8px;
}
.uv__empty p { margin: 0 0 18px; }
.uv__empty-hints {
    list-style: none;
    padding: 0;
    margin: 20px 0 0;
    text-align: left;
    font-size: 13px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 18px;
}
.uv__empty-hints li {
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
}
.uv__empty-hints li:last-child { border-bottom: 0; }
.uv__empty-hints code {
    background: var(--bg-elev-2);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 12px;
    color: var(--text);
}

/* ─── Stage ───────────────────────────────────────────────────────────── */

.uv__stage {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: row;
    gap: 16px;
    padding: 18px 22px 22px;
    overflow: hidden;
}

.uv__main {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: grid;
    place-items: center;
    container-type: size;
}
.uv__main:empty { display: none; }

.uv__grid {
    flex: 0 1 auto;
    align-self: stretch;
    height: 100%;
    max-width: 60%;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}
.uv__grid:empty { display: none; }
.uv__grid::-webkit-scrollbar { height: 8px; }
.uv__grid::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

/* ─── Tile ────────────────────────────────────────────────────────────── */

.tile {
    position: relative;
    background: #000;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    aspect-ratio: 16 / 9;
    box-shadow: var(--shadow-sm);
    transition: border-color 0.15s, transform 0.15s, box-shadow 0.15s;
}
.tile:hover {
    border-color: var(--border-strong);
}

.tile__player,
.tile__player > iframe {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
}

.tile__overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    pointer-events: none;
    background: linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.55) 0%,
        rgba(0, 0, 0, 0) 22%,
        rgba(0, 0, 0, 0) 70%,
        rgba(0, 0, 0, 0.75) 100%
    );
    opacity: 0;
    transition: opacity 0.15s;
}
.tile:hover .tile__overlay,
.tile:focus-within .tile__overlay {
    opacity: 1;
}

.tile__titlebar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    pointer-events: none;
    min-width: 0;
}

.tile__badge {
    display: none;
    padding: 2px 7px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: #fff;
    background: var(--accent);
    border-radius: 4px;
    text-transform: uppercase;
    flex-shrink: 0;
    box-shadow: 0 2px 6px rgba(255, 46, 77, 0.35);
}
.tile--main .tile__badge { display: inline-block; }

.tile__title {
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.tile__actions {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
    padding: 10px 12px;
    pointer-events: auto;
}

.tile__btn {
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 8px;
    background: rgba(10, 12, 16, 0.7);
    color: #fff;
    cursor: pointer;
    font-size: 15px;
    line-height: 1;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
    display: grid;
    place-items: center;
}
.tile__btn:hover {
    background: rgba(30, 34, 42, 0.95);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
}
.tile__btn:active { transform: translateY(0); }

.tile__btn--audio.is-on {
    background: var(--success);
    color: #062c14;
    box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.25), 0 4px 12px rgba(34, 197, 94, 0.35);
}
.tile__btn--audio.is-on:hover { background: #2dd96b; }

.tile__btn--remove:hover { background: var(--accent); }

.tile__btn--drag {
    cursor: grab;
    touch-action: none;
    letter-spacing: -1px;
    font-size: 16px;
}
.tile__btn--drag:active { cursor: grabbing; }

/* ─── Drag & drop ─────────────────────────────────────────────────────── */
.uv--dragging,
.uv--dragging * { user-select: none !important; }
.uv--dragging .tile__player,
.uv--dragging iframe { pointer-events: none !important; }
.uv--dragging { cursor: grabbing; }

.tile--dragging { opacity: 0.35; }
.tile--dragging .tile__overlay { opacity: 0 !important; }

.tile--drop-before {
    box-shadow: inset 4px 0 0 var(--accent), 0 0 0 1px var(--accent), var(--shadow-sm);
}
.tile--drop-after {
    box-shadow: inset -4px 0 0 var(--accent), 0 0 0 1px var(--accent), var(--shadow-sm);
}

.uv__main--drop-target {
    outline: 3px dashed var(--accent);
    outline-offset: -10px;
    border-radius: var(--radius);
}

/* ─── Share dialog ────────────────────────────────────────────────────── */

.uv__dialog {
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--text);
    max-width: 560px;
    width: calc(100% - 32px);
    border-radius: var(--radius);
}
.uv__dialog::backdrop {
    background: rgba(5, 7, 11, 0.72);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}
.uv__dialog-inner {
    position: relative;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
    padding: 24px 24px 22px;
}
.uv__dialog-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 34px;
    height: 34px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 16px;
    transition: background 0.15s, color 0.15s;
}
.uv__dialog-close:hover {
    background: var(--bg-elev-2);
    color: var(--text);
}

.uv__dialog-section { padding: 4px 0 2px; }
.uv__dialog-section h3 {
    font-size: 15px;
    font-weight: 700;
    margin: 0 0 6px;
    color: var(--text);
}
.uv__dialog-section p {
    margin: 0 0 14px;
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.5;
}

.uv__dialog-row {
    display: flex;
    gap: 8px;
    align-items: stretch;
}
.uv__dialog-row .uv__submit { white-space: nowrap; }

.uv__dialog-divider {
    height: 1px;
    background: var(--border);
    margin: 18px -4px;
}

.uv__share-result {
    margin-top: 14px;
    padding: 14px;
    background: var(--bg-elev-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    text-align: center;
}
.uv__share-label {
    font-size: 11px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-mute);
    margin-bottom: 8px;
}
.uv__share-row {
    display: flex;
    align-items: center;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
.uv__share-code {
    font-family: "SF Mono", Menlo, Consolas, "Courier New", monospace;
    font-size: 34px;
    font-weight: 700;
    letter-spacing: 0.18em;
    color: var(--text);
    padding: 6px 16px;
    background: var(--bg);
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-sm);
    user-select: all;
}
.uv__copy-btn {
    padding: 8px 14px;
    font-size: 13px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border-strong);
}
.uv__copy-btn:hover { background: var(--accent); border-color: var(--accent); }
.uv__share-hint {
    margin-top: 10px;
    font-size: 12px;
    color: var(--text-mute);
}

.uv__share-error {
    margin-top: 12px;
    padding: 8px 12px;
    background: rgba(255, 46, 77, 0.1);
    color: #ff8fa3;
    border: 1px solid rgba(255, 46, 77, 0.3);
    border-radius: var(--radius-sm);
    font-size: 13px;
}

.uv__code-input {
    font-family: "SF Mono", Menlo, Consolas, "Courier New", monospace;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    font-size: 16px;
    text-align: center;
}

.uv__ghost {
    position: fixed;
    z-index: 10000;
    pointer-events: none;
    background: linear-gradient(135deg, rgba(255, 46, 77, 0.85) 0%, rgba(255, 110, 138, 0.85) 100%);
    color: #fff;
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: var(--radius);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 14px;
    font-size: 14px;
    font-weight: 700;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    transform: rotate(-2deg) scale(0.96);
    opacity: 0.95;
    transition: transform 0.1s;
}

/* Grid tiles — fixed column width so flex-wrap can flow into extra columns */
.uv__grid > .tile {
    width: 280px;
    flex: 0 0 auto;
}

/* Main tile — fit inside its container preserving 16:9 without cropping */
.tile--main {
    width: min(100cqw, 100cqh * 16 / 9);
    max-width: 100%;
    max-height: 100%;
    border: 1px solid var(--border-strong);
    box-shadow: var(--shadow);
}
.tile--main .tile__overlay {
    background: linear-gradient(
        180deg,
        rgba(0, 0, 0, 0.55) 0%,
        rgba(0, 0, 0, 0) 18%,
        rgba(0, 0, 0, 0) 78%,
        rgba(0, 0, 0, 0.55) 100%
    );
}
.tile--main .tile__btn--promote { display: none; }

/* Audio-on outline for clearer feedback */
.tile:has(.tile__btn--audio.is-on) {
    border-color: var(--success);
    box-shadow: 0 0 0 1px var(--success), var(--shadow-sm);
}

/* ─── Grid-only mode ──────────────────────────────────────────────────── */
.uv--grid-only .uv__main { display: none !important; }
.uv--grid-only .uv__grid {
    flex: 1 1 auto;
    max-width: none;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(var(--cols, 1), var(--tile-w, 1fr));
    grid-auto-rows: var(--tile-h, 1fr);
    justify-content: center;
    align-content: center;
    gap: 12px;
    overflow: hidden;
}
.uv--grid-only .uv__grid > .tile {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
}
.uv--grid-only .tile__btn--promote { display: none; }
.uv--grid-only .tile__badge { display: none !important; }

/* Error state */
.tile--error {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), var(--shadow-sm);
}

/* ─── Responsive ──────────────────────────────────────────────────────── */

@media (max-width: 820px) {
    .uv:not(.uv--grid-only) { height: auto; min-height: 100vh; overflow: visible; }
    .uv__header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
        padding: 12px 14px;
    }
    .uv__form { margin-left: 0; max-width: none; }
    .uv:not(.uv--grid-only) .uv__stage {
        flex-direction: column;
        padding: 14px;
        overflow: visible;
    }
    .uv:not(.uv--grid-only) .uv__main { container-type: normal; }
    .uv:not(.uv--grid-only) .tile--main {
        width: 100%;
        aspect-ratio: 16 / 9;
        max-height: 60vh;
    }
    .uv:not(.uv--grid-only) .uv__grid {
        flex-direction: row;
        flex-wrap: wrap;
        align-content: flex-start;
        height: auto;
        max-width: none;
        overflow: visible;
    }
    .uv:not(.uv--grid-only) .uv__grid > .tile { width: calc(50% - 5px); }
}

@media (max-width: 480px) {
    .uv:not(.uv--grid-only) .uv__grid > .tile { width: 100%; }
    .tile__btn { width: 32px; height: 32px; font-size: 14px; }
}
