* {
    box-sizing: border-box;
}

:root {
    --bg: #0d0d12;
    --card: #17171f;
    --accent: #7c5cff;
    --accent-2: #ff5c8a;
    --text: #f2f2f7;
    --muted: #8b8b9a;
}

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
    overscroll-behavior-x: none;
    overflow-x: hidden;
    touch-action: pan-y;
    user-select: none;
}

input[type="range"] {
    touch-action: pan-x;
}

.app {
    height: 100dvh;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.hidden {
    display: none !important;
}

.screen {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: env(safe-area-inset-top) 20px env(safe-area-inset-bottom);
}

/* Onboarding */
#onboarding-screen {
    justify-content: center;
    align-items: center;
}

.onboarding-card {
    width: 100%;
    max-width: 340px;
    background: var(--card);
    border-radius: 20px;
    padding: 28px 24px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.logo {
    margin: 0 0 4px;
    text-align: center;
    font-size: 32px;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.subtitle {
    margin: 0 0 20px;
    text-align: center;
    color: var(--muted);
    font-size: 14px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 16px;
    font-size: 13px;
    color: var(--muted);
}

.field input {
    background: #0d0d12;
    border: 1px solid #2a2a35;
    border-radius: 12px;
    padding: 12px 14px;
    color: var(--text);
    font-size: 16px;
}

.field input:focus {
    outline: none;
    border-color: var(--accent);
}

.btn {
    border: none;
    border-radius: 14px;
    padding: 14px 20px;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
}

.btn-primary {
    width: 100%;
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    color: white;
    margin-top: 4px;
}

/* Home screen */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
}

.greeting {
    font-size: 18px;
    font-weight: 600;
}

.icon-btn {
    background: none;
    border: none;
    font-size: 22px;
    cursor: pointer;
    color: var(--text);
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.stage {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: safe center;
    gap: 28px;
    padding: 12px 0;
    position: relative;
}

.hint {
    color: var(--muted);
    font-size: 14px;
    text-align: center;
    margin: 0;
    padding: 0 10px;
}

.hint-rotate {
    display: none;
}

@media (orientation: portrait) {
    .hint-rotate {
        display: block;
    }
}

.line-frame {
    position: relative;
    width: 100%;
    max-width: 320px;
    background: var(--card);
    border-radius: 20px;
    padding: 40px 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

@media (orientation: landscape) {
    #home-screen .hint {
        display: none;
    }

    .topbar {
        padding: 6px 0;
    }

    .stage {
        gap: 8px;
        padding: 6px 0;
    }

    .line-frame {
        max-width: min(90vw, 700px);
        padding: 12px 16px;
    }

    .line-rect {
        height: 44px;
    }

    .direction-hint {
        top: 2px;
        font-size: 14px;
    }

    .sandman {
        width: 30px;
    }

    .length-control {
        max-width: 260px;
        font-size: 12px;
    }

    .btn-go {
        width: 64px;
        height: 64px;
        font-size: 13px;
    }

    .countdown {
        font-size: 48px;
    }

    .status {
        min-height: 14px;
        font-size: 12px;
    }

    .score-line {
        font-size: 11px;
    }

    .tabbar {
        padding: 4px 0 calc(4px + env(safe-area-inset-bottom));
    }

    .tab {
        font-size: 16px;
        flex-direction: row;
        gap: 4px;
    }

    .tab span {
        font-size: 10px;
    }
}

.line-rect {
    position: relative;
    height: 70px;
    border: 2px dashed #3a3a48;
    border-radius: 10px;
    overflow: visible;
}

.line-track {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    height: 14px;
    width: calc(var(--line-cm, 7) * var(--px-per-cm, 37.8) * 1px);
    max-width: calc(100% - 32px);
    border-radius: 7px;
    background: linear-gradient(90deg, #ffffff, #d8d8ff);
    box-shadow: 0 0 12px rgba(255, 255, 255, 0.5);
    clip-path: inset(0 0 0 0%);
}

.line-rect.sweeping .line-track {
    transition: clip-path 3s linear;
    clip-path: inset(0 0 0 100%);
}

.line-rect.swept .line-track {
    clip-path: inset(0 0 0 100%);
}

.sandman {
    position: absolute;
    left: 0;
    top: 50%;
    width: 44px;
    height: auto;
    transform: translate(-100%, -50%);
    opacity: 0;
    pointer-events: none;
}

.sandman.sweeping {
    opacity: 1;
    transition: left 3s linear;
    left: calc(16px + var(--line-cm, 7) * var(--px-per-cm, 37.8) * 1px);
}

.direction-hint {
    position: absolute;
    top: 12px;
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    gap: 4px;
    color: var(--accent);
    font-size: 20px;
    font-weight: 700;
}

.direction-hint span {
    animation: hint-slide 1s ease-in-out infinite;
    opacity: 0.3;
}

.direction-hint span:nth-child(2) {
    animation-delay: 0.15s;
}

.direction-hint span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes hint-slide {
    0%, 100% {
        opacity: 0.3;
        transform: translateX(0);
    }
    50% {
        opacity: 1;
        transform: translateX(4px);
    }
}

.countdown {
    position: absolute;
    font-size: 96px;
    font-weight: 800;
    color: var(--accent);
    text-shadow: 0 0 30px rgba(124, 92, 255, 0.6);
}

.btn-go {
    width: 140px;
    height: 140px;
    border-radius: 25%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0 10px;
    line-height: 1.15;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: white;
    font-size: 22px;
    letter-spacing: 1px;
    box-shadow: 0 10px 30px rgba(124, 92, 255, 0.45);
}

.btn-go:disabled {
    opacity: 0.5;
}

.status {
    min-height: 20px;
    color: var(--muted);
    font-size: 14px;
    text-align: center;
    margin: 0;
}

.btn-secondary {
    width: 100%;
    background: none;
    border: 1px solid #2a2a35;
    color: var(--text);
}

.calibrate-frame {
    width: calc(100% + 40px);
    margin: 0 -20px;
    background: var(--card);
    padding: 30px 0;
    padding-left: max(12px, env(safe-area-inset-left));
    overflow-x: auto;
}

.calibrate-bar {
    height: 34px;
    width: calc(8.56 * var(--px-per-cm, 37.8) * 1px);
    min-width: 60px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
}

.calibrate-actions {
    width: 100%;
    max-width: 320px;
    display: flex;
    gap: 10px;
}

.calibrate-actions .btn {
    flex: 1;
}

.length-control {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
}

.length-control input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

.score-line {
    color: var(--muted);
    font-size: 13px;
    margin: 0;
}

/* Tab bar */
.tabbar {
    display: flex;
    justify-content: space-around;
    padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid #23232e;
}

.legal-footer {
    text-align: center;
    padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
}

.legal-footer a {
    color: var(--muted);
    text-decoration: none;
    font-size: 13px;
}

.legal-footer a:hover,
.legal-footer a:focus-visible {
    color: var(--text);
    text-decoration: underline;
}

.legal-page {
    max-width: 760px;
    margin: 0 auto;
    padding: 28px 20px;
}

.legal-page h1 {
    margin-top: 0;
}

.legal-page h2 {
    margin-bottom: 8px;
}

.legal-page section {
    margin-bottom: 20px;
}

.tab {
    background: none;
    border: none;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    font-size: 20px;
    cursor: pointer;
}

.tab span {
    font-size: 11px;
    font-weight: 600;
}

.tab.active {
    color: var(--text);
}

/* Friends & leaderboard screens */
.list-stage {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding-top: 10px;
}

.add-friend-form {
    display: flex;
    gap: 3px;
}

.add-friend-form input {
    flex: 1;
    background: var(--card);
    border: 1px solid #2a2a35;
    border-radius: 12px;
    padding: 12px 14px;
    color: var(--text);
    font-size: 16px;
}

.add-friend-form input:focus {
    outline: none;
    border-color: var(--accent);
}

.btn-compact {
    width: auto;
    padding: 12px 18px;
}

.segmented {
    display: flex;
    background: var(--card);
    border-radius: 12px;
    padding: 4px;
    gap: 4px;
}

.segment {
    flex: 1;
    border: none;
    background: none;
    color: var(--muted);
    padding: 10px;
    border-radius: 9px;
    font-weight: 700;
    cursor: pointer;
}

.segment.active {
    background: linear-gradient(90deg, var(--accent), var(--accent-2));
    color: white;
}

.entry-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.entry-list li {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--card);
    border-radius: 14px;
    padding: 12px 14px;
}

.entry-list li.you {
    border: 1px solid var(--accent);
}

.entry-rank {
    width: 22px;
    text-align: center;
    color: var(--muted);
    font-weight: 700;
    font-size: 13px;
}

.entry-name {
    flex: 1;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.entry-score {
    color: var(--accent);
    font-weight: 700;
    font-size: 14px;
}

.remove-friend-btn {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 16px;
    cursor: pointer;
    padding: 0 0 0 4px;
}

.empty-hint {
    color: var(--muted);
    font-size: 14px;
    text-align: center;
    margin-top: 20px;
}
