:root {
    --paper: #f3e6cc;
    --paper-light: #fbf5e8;
    --paper-deep: #ead0b6;
    --ink: #3a2414;
    --ink-soft: #5a3b26;
    --fur-dark: #7a4526;
    --accent: #b8442a;
    --accent-ink: #fbf2e2;
    --accent-text: #9a3a22;
    --river: #1d4f4a;
    --ui-font: "Cabinet Grotesk", "Trebuchet MS", system-ui, sans-serif;
    --ease-out: cubic-bezier(0.2, 0, 0, 1);
}

.rank-open, .rank, .rank button, .rank input { font-family: var(--ui-font); }

.rank-open {
    position: fixed;
    z-index: 10;
    top: max(18px, env(safe-area-inset-top));
    right: max(16px, env(safe-area-inset-right));
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 18px 0 14px;
    border: 0;
    border-radius: 999px;
    background: var(--paper);
    color: var(--ink);
    font-size: 17px;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 3px 0 rgba(58, 36, 20, 0.28), 0 6px 14px rgba(30, 15, 5, 0.25);
    transition: transform 160ms var(--ease-out), background-color 160ms var(--ease-out);
}
.rank-open[hidden] { display: none; }
.rank-open svg, .rank svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.rank-open svg { color: var(--accent-text); }
.rank-open:hover { background: var(--paper-light); transform: translateY(-1px); }
.rank-open:active { transform: scale(0.97); }
.rank-open:focus-visible { outline: 3px solid var(--accent-text); outline-offset: 3px; box-shadow: 0 0 0 6px var(--paper); }

.rank {
    width: min(92vw, 420px);
    max-height: min(88vh, 720px);
    padding: 0;
    border: 0;
    background: transparent;
    overflow: visible;
    touch-action: auto;
    filter: drop-shadow(0 4px 0 rgba(58, 36, 20, 0.3)) drop-shadow(0 14px 22px rgba(30, 15, 5, 0.38));
}
.rank::backdrop { background: rgba(24, 14, 6, 0.55); }
.rank[open] { animation: rank-in 200ms var(--ease-out); }

.rank-sheet {
    position: relative;
    display: flex;
    flex-direction: column;
    max-height: min(88vh, 720px);
    padding: 24px 22px 22px;
    background: var(--paper);
    color: var(--ink);
    border-radius: 10px;
    clip-path: polygon(0.6% 1.2%, 18% 0, 41% 0.9%, 63% 0.2%, 86% 1%, 99.4% 0, 100% 22%, 99.2% 47%, 100% 71%, 99.3% 98.6%, 78% 100%, 55% 99.1%, 31% 100%, 9% 99.2%, 0 100%, 0.8% 74%, 0 49%, 0.7% 24%);
}
.rank-sheet[hidden], .rank-form[hidden], .rank-board[hidden] { display: none; }

.rank h2 { margin: 0 44px 8px 0; font-size: 26px; font-weight: 800; line-height: 1.15; color: var(--ink); }
.rank-lead { margin: 0 0 16px; font-size: 17px; line-height: 1.45; color: var(--ink-soft); }

.rank-x {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--ink-soft);
    cursor: pointer;
}
.rank-x:hover { background: rgba(184, 68, 42, 0.12); color: var(--ink); }

.rank-form { display: flex; flex-direction: column; }
.rank-form label { font-size: 16px; font-weight: 800; margin-bottom: 8px; color: var(--ink); }
.rank-form input {
    min-height: 48px;
    padding: 0 14px;
    border: 2px solid var(--fur-dark);
    border-radius: 8px;
    background: var(--paper-light);
    color: var(--ink);
    font-size: 19px;
    font-weight: 700;
}
.rank-form input[aria-invalid="true"] { border-color: var(--accent-text); }
.rank-hint { margin: 8px 0 0; font-size: 15px; color: var(--ink-soft); }
.rank-error { margin: 6px 0 0; min-height: 1.4em; font-size: 15px; font-weight: 800; color: var(--accent-text); }

.rank-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.rank-primary, .rank-soft, .rank-ghost {
    min-height: 48px;
    padding: 0 22px;
    border: 0;
    border-radius: 999px;
    font-size: 17px;
    font-weight: 800;
    cursor: pointer;
    transition: transform 160ms var(--ease-out), background-color 160ms var(--ease-out);
}
.rank-primary { min-width: 140px; background: var(--accent); color: var(--accent-ink); }
.rank-primary:hover { background: #a63c24; }
.rank-soft { background: rgba(184, 68, 42, 0.14); color: var(--ink); }
.rank-soft:hover { background: rgba(184, 68, 42, 0.24); }
.rank-ghost { background: transparent; color: var(--ink-soft); }
.rank-ghost:hover { background: rgba(184, 68, 42, 0.12); color: var(--ink); }
.rank-primary:active, .rank-soft:active, .rank-ghost:active { transform: scale(0.97); }
.rank-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.rank-primary[aria-busy="true"] { background: var(--accent-text); cursor: progress; pointer-events: none; }
.rank-board .rank-actions { justify-content: center; }
.rank button:focus-visible, .rank input:focus-visible { outline: 3px solid var(--accent-text); outline-offset: 2px; }

.rank-board { display: flex; flex-direction: column; min-height: 0; }
.rank-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    touch-action: pan-y;
    overscroll-behavior: contain;
    min-height: 0;
}
.rank-list li {
    display: grid;
    grid-template-columns: 36px minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    min-height: 40px;
    padding: 0 10px;
    border-radius: 8px;
    font-size: 17px;
}
.rank-list li + li { border-top: 1px dashed rgba(122, 69, 38, 0.3); }
.rank-list li.is-you { background: var(--paper-deep); border-top-color: transparent; }
.rank-list li.is-you + li { border-top-color: transparent; }
.rank-pos { font-weight: 800; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.rank-list li:nth-child(-n+3) .rank-pos { color: var(--accent-text); font-size: 19px; }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.rank-you { margin-left: 8px; padding: 1px 8px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 15px; font-weight: 800; }
.rank-score { font-weight: 800; font-variant-numeric: tabular-nums; }
.rank-skeleton { height: 40px; background: linear-gradient(90deg, rgba(122, 69, 38, 0.1), rgba(122, 69, 38, 0.18), rgba(122, 69, 38, 0.1)); background-size: 200% 100%; animation: rank-shimmer 1.2s linear infinite; }
.rank-skeleton + .rank-skeleton { margin-top: 6px; border-top: 0 !important; }

.rank-state { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; color: var(--ink-soft); font-size: 17px; }
.rank-state:empty { display: none; }
.rank-state p { margin: 0; }
.rank-state.is-quiet { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.rank-state-icon svg { width: 44px; height: 44px; color: var(--accent-text); }

@keyframes rank-in { from { opacity: 0; transform: translateY(12px) rotate(-1deg); } }
@keyframes rank-shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
    .rank[open], .rank-skeleton { animation: none; }
    .rank-open, .rank-primary, .rank-soft, .rank-ghost { transition: none; }
    .rank-open:hover, .rank-open:active, .rank-primary:active, .rank-soft:active, .rank-ghost:active { transform: none; }
}
