:root {
    color-scheme: light;
    --bg: #f1f0eb;
    --surface: #fff;
    --surface-muted: #f5f4f0;
    --text: #182738;
    --text-soft: #6a7480;
    --gold: #aa7827;
    --midnight: #20364e;
    --night: #13223a;
    --coral: #ad4939;
    --green: #28744d;
    --focus: #2464a4;
    --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
    --spring: cubic-bezier(0.2, 1.4, 0.4, 1);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, "Helvetica Neue", sans-serif;
}

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

html {
    background: var(--bg);
}

body {
    min-width: 280px;
    margin: 0;
    color: var(--text);
    background: var(--bg);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    -webkit-tap-highlight-color: transparent;
}

button {
    font: inherit;
}

button:focus-visible,
[tabindex]:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 3px;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

main {
    width: min(100%, 560px);
    min-height: 100vh;
    min-height: 100svh;
    margin: 0 auto;
    padding:
        max(14px, env(safe-area-inset-top))
        max(14px, env(safe-area-inset-right))
        max(14px, env(safe-area-inset-bottom))
        max(14px, env(safe-area-inset-left));
    display: flex;
    flex-direction: column;
}

body[data-view="tuner"] main {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
}

.view {
    width: 100%;
    display: flex;
    flex-direction: column;
}

.view.hidden {
    display: none;
}

/* ---------- Afinador ---------- */
#tuner-view {
    flex: 1;
    min-height: 0;
    gap: 14px;
}

.live-display {
    flex: none;
    padding-top: 6px;
    text-align: center;
}

.note-display {
    min-height: 0.95em;
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 4px;
    color: var(--midnight);
    font-size: clamp(72px, 24vw, 112px);
    font-weight: 600;
    letter-spacing: -0.065em;
    line-height: 0.98;
}

#octave {
    color: var(--text-soft);
    font-size: 0.36em;
    font-weight: 500;
    letter-spacing: -0.02em;
}

#cents-label {
    min-height: 1.4em;
    margin-top: 6px;
    color: var(--text-soft);
    font-size: 15px;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
    transition: color 140ms linear;
}

#cents-label.error {
    color: var(--coral);
}

.pitch-graph-wrap {
    position: relative;
    flex: 1;
    min-height: 200px;
    overflow: hidden;
    border-radius: 24px;
    background: var(--night);
}

#pitch-graph {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
}

.toggle-btn,
.primary-btn {
    flex: none;
    width: 100%;
    min-height: 58px;
    padding: 12px 22px;
    border: 0;
    border-radius: 18px;
    color: #fff;
    background: var(--midnight);
    font-size: 15px;
    font-weight: 700;
    letter-spacing: 0.08em;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 150ms var(--ease), background-color 150ms var(--ease);
}

.toggle-btn.active {
    background: var(--coral);
}

.toggle-btn:disabled {
    opacity: 0.7;
}

.primary-btn {
    letter-spacing: 0.02em;
}

/* ---------- Resumen ---------- */
#results-view {
    gap: 18px;
}

.results-head {
    display: flex;
    align-items: center;
    gap: 16px;
    padding-top: 4px;
}

.score-ring {
    position: relative;
    flex: none;
    width: 76px;
    height: 76px;
}

.score-ring svg {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
}

.ring-track,
.ring-arc {
    fill: none;
    stroke-width: 8;
}

.ring-track {
    stroke: #e2e0d8;
}

.ring-arc {
    stroke: var(--green);
    stroke-linecap: round;
}

.score-value {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--midnight);
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.04em;
    font-variant-numeric: tabular-nums;
}

.results-head h2 {
    margin: 0;
    color: var(--midnight);
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.03em;
    line-height: 1.15;
}

.session-meta {
    margin: 2px 0 0;
    color: var(--text-soft);
    font-size: 14px;
}

.neck {
    display: flex;
    justify-content: center;
}

.neck-svg {
    display: block;
    max-width: 100%;
    height: auto;
    overflow: visible;
}

.neck-svg text {
    font-family: var(--font);
    text-anchor: middle;
    dominant-baseline: central;
    pointer-events: none;
}

.string-letter {
    fill: rgba(255, 255, 255, 0.42);
    font-size: 10px;
    font-weight: 700;
}

.neck-dot {
    cursor: pointer;
    outline: none;
}

.neck-dot-inner {
    transform-box: fill-box;
    transform-origin: center;
    animation: dot-in 520ms var(--spring) both;
    transition: transform 180ms var(--ease);
}

.neck-dot:focus-visible .neck-dot-inner {
    transform: scale(1.12);
}

.neck-dot:active .neck-dot-inner {
    transform: scale(0.94);
}

.dot-note {
    font-weight: 800;
    letter-spacing: -0.02em;
}

.dot-hz {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.neck-empty {
    fill: rgba(255, 255, 255, 0.6);
    font-size: 15px;
    font-weight: 650;
}

@keyframes dot-in {
    from { opacity: 0; transform: scale(0.2); }
    to { opacity: 1; transform: scale(1); }
}

.legend {
    width: min(100%, 300px);
    margin: -6px auto 0;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-soft);
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
}

.legend-bar {
    flex: 1;
    height: 6px;
    border-radius: 999px;
}

.results-actions {
    position: sticky;
    bottom: 0;
    z-index: 5;
    margin-top: auto;
    padding: 16px 0 max(2px, env(safe-area-inset-bottom));
    background: linear-gradient(180deg, rgba(241, 240, 235, 0), var(--bg) 45%);
}

/* ---------- Ficha de nota ---------- */
.note-sheet {
    position: fixed;
    inset: 0;
    z-index: 20;
}

.note-sheet[hidden] {
    display: none;
}

.sheet-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(13, 23, 38, 0.32);
    opacity: 0;
    transition: opacity 240ms var(--ease);
}

.sheet-card {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    width: min(100%, 560px);
    margin: 0 auto;
    padding: 20px 18px max(20px, env(safe-area-inset-bottom));
    border-radius: 24px 24px 0 0;
    background: var(--surface);
    box-shadow: 0 -12px 40px rgba(13, 23, 38, 0.18);
    transform: translateY(105%);
    transition: transform 300ms var(--spring);
}

.note-sheet.open .sheet-backdrop { opacity: 1; }
.note-sheet.open .sheet-card { transform: translateY(0); }

.sheet-head {
    display: flex;
    align-items: center;
    gap: 14px;
}

.sheet-dot {
    flex: none;
    width: 52px;
    height: 52px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    font-size: 17px;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.sheet-heading {
    flex: 1;
    min-width: 0;
}

.sheet-heading h3 {
    margin: 0;
    color: var(--midnight);
    font-size: 17px;
    font-weight: 700;
    line-height: 1.25;
}

.sheet-heading p {
    margin: 2px 0 0;
    color: var(--text-soft);
    font-size: 13px;
}

.sheet-close {
    flex: none;
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 50%;
    color: var(--text-soft);
    background: var(--surface-muted);
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
}

.sheet-stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin: 18px 0 0;
    text-align: center;
}

.sheet-stats dt {
    color: var(--text-soft);
    font-size: 11px;
    font-weight: 600;
}

.sheet-stats dd {
    margin: 0;
    color: var(--midnight);
    font-size: 19px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.sheet-tip {
    margin: 16px 0 0;
    color: var(--text-soft);
    font-size: 13px;
    text-align: center;
}

.sheet-tip[hidden] {
    display: none;
}

@media (hover: hover) and (pointer: fine) {
    .toggle-btn:hover,
    .primary-btn:hover {
        background: #2b4662;
    }

    .toggle-btn.active:hover {
        background: #963d30;
    }

    .neck-dot:hover .neck-dot-inner {
        transform: scale(1.08);
    }
}

.toggle-btn:active,
.primary-btn:active {
    transform: scale(0.98);
}

@media (max-height: 640px) {
    .note-display { font-size: clamp(56px, 17vw, 80px); }
    #tuner-view { gap: 10px; }
    .toggle-btn { min-height: 52px; }
}

@media (min-width: 640px) and (min-height: 720px) {
    main {
        padding-top: 32px;
        padding-bottom: 24px;
    }

    body[data-view="tuner"] main {
        height: min(100vh, 960px);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
