:root {
    color-scheme: light;
    --bg: #f1f0eb;
    --surface: #fff;
    --surface-muted: #f7f6f2;
    --text: #182738;
    --text-soft: #5c6875;
    --border: #e2e6e9;
    --gold: #aa7827;
    --gold-soft: #f4ead4;
    --midnight: #20364e;
    --coral: #ad4939;
    --green: #28744d;
    --focus: #2464a4;
    --shadow: 0 18px 50px rgba(31, 48, 64, 0.1);
    --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
}

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

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    min-width: 280px;
    min-height: 100vh;
    min-height: 100svh;
    margin: 0;
    padding: max(10px, env(safe-area-inset-top)) 14px max(10px, env(safe-area-inset-bottom));
    display: flex;
    justify-content: center;
    align-items: stretch;
    color: var(--text);
    background:
        radial-gradient(ellipse at 50% 0%, rgba(255, 255, 255, 0.92), transparent 62%),
        var(--bg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, "Helvetica Neue", sans-serif;
    font-size: 16px;
    font-weight: 400;
    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;
}

main {
    width: min(100%, 560px);
    margin: auto;
    padding: clamp(20px, 4.5vw, 34px);
    background: var(--surface);
    border: 1px solid rgba(32, 54, 78, 0.08);
    border-radius: 26px;
    box-shadow: var(--shadow);
}

.app-header {
    margin: 0 0 16px;
    text-align: center;
}

.app-header h1 {
    margin: 0;
    color: var(--midnight);
    font-size: clamp(21px, 5vw, 25px);
    font-weight: 650;
    letter-spacing: -0.035em;
    line-height: 1.15;
}

.app-header .subtitle {
    margin: 5px 0 0;
    color: var(--text-soft);
    font-size: 13px;
    line-height: 1.4;
}

/* The five choices stay large enough to tap and fit narrow phone screens. */
#string-selector {
    width: min(100%, 390px);
    margin: 13px auto 14px;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 7px;
}

#string-selector button {
    min-width: 0;
    min-height: 42px;
    padding: 7px 4px;
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--midnight);
    background: var(--surface-muted);
    font-size: 13px;
    font-weight: 600;
    line-height: 1.1;
    cursor: pointer;
    touch-action: manipulation;
    transition: background-color 160ms var(--ease), border-color 160ms var(--ease), color 160ms var(--ease), transform 160ms var(--ease);
}

#string-selector button:is([aria-pressed="true"], [data-selected="true"], .active, .selected) {
    border-color: var(--midnight);
    color: #fff;
    background: var(--midnight);
}

#string-selector button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

#session-status {
    width: fit-content;
    max-width: 100%;
    min-height: 0;
    margin: 0 auto 10px;
    padding: 4px 10px;
    border: 1px solid transparent;
    border-radius: 999px;
    color: var(--text-soft);
    background: var(--surface-muted);
    font-size: 12px;
    line-height: 1.35;
    text-align: center;
    overflow-wrap: anywhere;
}

#session-status:empty {
    display: none;
}

#toggle-btn.idle + #waveform {
    display: none;
}

#session-status:is(.error, .warning, [data-state="error"]) {
    border-color: #e7c8c1;
    color: #87392d;
    background: #fff5f2;
}

#session-status:is(.success, [data-state="success"]) {
    border-color: #c7dfd0;
    color: #235e40;
    background: #f0f8f3;
}

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

.view.hidden {
    display: none;
}

.live-display {
    width: 100%;
    margin: 0 0 12px;
    text-align: center;
}

.note-display {
    min-height: 0.95em;
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 5px;
    color: var(--midnight);
    font-size: clamp(66px, 19vw, 84px);
    font-weight: 550;
    letter-spacing: -0.065em;
    line-height: 0.98;
    font-variant-numeric: lining-nums;
}

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

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

#meter {
    width: min(100%, 360px);
    margin: 14px auto 0;
}

.meter-track {
    position: relative;
    height: 44px;
    overflow: hidden;
    border: 1px solid #dfe4e8;
    border-radius: 13px;
    background: linear-gradient(90deg, #f8e9e3 0%, #fbf7ec 30%, #f4f6f1 50%, #fbf7ec 70%, #f8e9e3 100%);
}

.meter-tick {
    position: absolute;
    top: 50%;
    z-index: 1;
    color: #596572;
    font-size: 11px;
    font-weight: 550;
    font-variant-numeric: tabular-nums;
    transform: translateY(-50%);
}

.meter-tick-left { left: 11px; }
.meter-tick-right { right: 11px; }

.meter-tick-center {
    left: 50%;
    width: 1px;
    height: 17px;
    color: transparent;
    background: #aab3bb;
    transform: translate(-50%, -50%);
}

#meter-needle {
    position: absolute;
    top: 5px;
    bottom: 5px;
    left: 50%;
    z-index: 2;
    width: 4px;
    border-radius: 999px;
    background: var(--gold);
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.75), 0 1px 5px rgba(32, 54, 78, 0.18);
    transform: translateX(-50%);
    transition: left 80ms linear, background-color 140ms linear;
}

.toggle-btn {
    width: min(100%, 300px);
    min-height: 60px;
    margin: 4px auto 0;
    padding: 12px 22px;
    border: 0;
    border-radius: 15px;
    color: #fff;
    background: var(--midnight);
    box-shadow: 0 5px 14px rgba(32, 54, 78, 0.18);
    font-size: 15px;
    font-weight: 700;
    letter-spacing: 0.09em;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 150ms var(--ease), background-color 150ms var(--ease), box-shadow 150ms var(--ease);
}

.toggle-btn.active {
    background: var(--coral);
    box-shadow: 0 5px 14px rgba(173, 73, 57, 0.2);
}

#waveform {
    display: block;
    width: 100%;
    max-width: 640px;
    height: 64px;
    margin: 13px auto 0;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface-muted);
}

/* Session summary */
.dashboard {
    width: 100%;
    padding: clamp(18px, 4vw, 28px);
    border: 1px solid var(--border);
    border-radius: 20px;
    background: var(--surface);
}

.dashboard h2 {
    margin: 0 0 17px;
    color: var(--midnight);
    font-size: 20px;
    font-weight: 650;
    letter-spacing: -0.025em;
    line-height: 1.2;
    text-align: center;
}

.score-card {
    position: relative;
    margin-bottom: 20px;
    padding: 0 0 19px;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

.score-label,
.highlight .label {
    color: var(--text-soft);
    font-size: 11px;
    font-weight: 650;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.score-value {
    margin-top: 2px;
    color: var(--midnight);
    font-size: clamp(64px, 17vw, 82px);
    font-weight: 600;
    letter-spacing: -0.07em;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}

.score-out-of {
    color: var(--text-soft);
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}

.detected-key {
    width: 100%;
    margin: 0 0 16px;
    padding: 12px 16px;
    border: 1px solid #ead9b7;
    border-radius: 13px;
    background: #fcf8ef;
    text-align: center;
}

.detected-key:empty {
    display: none;
}

.detected-key .label {
    color: var(--text-soft);
    font-size: 10px;
    font-weight: 650;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.detected-key .value {
    margin-top: 2px;
    color: var(--midnight);
    font-size: 20px;
    font-weight: 650;
}

.detected-key .detail {
    margin-top: 2px;
    color: var(--text-soft);
    font-size: 12px;
}

.highlight-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 18px;
}

.highlight {
    min-width: 0;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 13px;
    background: var(--surface-muted);
}

.highlight .label {
    display: block;
    line-height: 1.3;
}

.highlight .value {
    margin-top: 5px;
    color: var(--midnight);
    font-size: 21px;
    font-weight: 650;
    font-variant-numeric: tabular-nums;
}

.highlight .detail {
    margin-top: 2px;
    color: var(--text-soft);
    font-size: 12px;
}

.highlight.best {
    border-color: #bdd8c7;
    background: #f2f8f4;
}

.highlight.worst {
    border-color: #e7c6bf;
    background: #fff5f2;
}

.table-wrap {
    max-width: 100%;
    margin-bottom: 18px;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 12px;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
}

#per-note-table {
    width: 100%;
    min-width: 470px;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}

#per-note-table th {
    padding: 11px 12px;
    color: var(--text-soft);
    background: var(--surface-muted);
    border-bottom: 1px solid var(--border);
    font-size: 10px;
    font-weight: 650;
    letter-spacing: 0.07em;
    text-align: left;
    text-transform: uppercase;
    white-space: nowrap;
}

#per-note-table td {
    padding: 11px 12px;
    border-bottom: 1px solid var(--border);
}

#per-note-table tbody tr:last-child td {
    border-bottom: 0;
}

#per-note-table tbody tr.row-best {
    background: #f0f7f2;
}

#per-note-table tbody tr.row-worst {
    background: #fff3f0;
}

.precision-bar {
    display: inline-block;
    width: 76px;
    height: 7px;
    margin-right: 7px;
    overflow: hidden;
    border-radius: 999px;
    background: #e5e9eb;
    vertical-align: middle;
}

.precision-bar > span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--green);
}

.secondary-btn {
    display: block;
    min-width: 120px;
    min-height: 46px;
    margin: 0 auto;
    padding: 10px 22px;
    border: 1px solid var(--midnight);
    border-radius: 12px;
    color: var(--midnight);
    background: transparent;
    font-size: 14px;
    font-weight: 650;
    cursor: pointer;
    touch-action: manipulation;
    transition: color 150ms var(--ease), background-color 150ms var(--ease);
}

@media (hover: hover) and (pointer: fine) {
    #string-selector button:hover:not(:disabled) {
        border-color: #9aa8b4;
        background: #eeefeb;
    }

    #string-selector button:is([aria-pressed="true"], [data-selected="true"], .active, .selected):hover {
        background: #2b4662;
    }

    .toggle-btn:hover {
        background: #2b4662;
        box-shadow: 0 8px 20px rgba(32, 54, 78, 0.22);
        transform: translateY(-1px);
    }

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

    .secondary-btn:hover {
        color: #fff;
        background: var(--midnight);
    }
}

.toggle-btn:active,
.secondary-btn:active,
#string-selector button:active {
    transform: scale(0.98);
}

@media (max-width: 520px) {
    #per-note-table {
        min-width: 0;
        table-layout: fixed;
        font-size: 12px;
    }

    #per-note-table th,
    #per-note-table td {
        padding: 9px 5px;
    }

    #per-note-table th {
        font-size: 9px;
        letter-spacing: 0;
    }

    #per-note-table th:nth-child(1) { width: 29%; }
    #per-note-table th:nth-child(2) { width: 16%; }
    #per-note-table th:nth-child(3) { width: 28%; }
    #per-note-table th:nth-child(4) { width: 27%; }

    .precision-bar { display: none; }
    .string-tag { display: block; }

    body {
        padding-right: 10px;
        padding-left: 10px;
    }

    main {
        padding: 20px;
        border-radius: 22px;
    }

    .app-header {
        margin-bottom: 13px;
    }

    #string-selector {
        gap: 6px;
        margin-top: 11px;
        margin-bottom: 12px;
    }

    #string-selector button {
        min-height: 42px;
        border-radius: 11px;
        font-size: 12px;
    }

    .note-display {
        font-size: clamp(62px, 19vw, 74px);
    }

    .live-display {
        margin-bottom: 11px;
    }

    #meter {
        margin-top: 12px;
    }

    .toggle-btn {
        width: 100%;
        min-height: 56px;
        margin-top: 2px;
    }

    #waveform {
        height: 54px;
        margin-top: 11px;
    }

    .dashboard {
        padding: 18px;
    }

    .score-card {
        margin-bottom: 16px;
        padding-bottom: 15px;
    }

    .highlight-row {
        gap: 8px;
    }

    .highlight {
        padding: 12px;
    }
}

@media (max-width: 360px) {
    body {
        padding-right: 8px;
        padding-left: 8px;
    }

    main {
        padding: 17px;
    }

    #string-selector {
        gap: 5px;
    }

    #string-selector button {
        padding-right: 2px;
        padding-left: 2px;
        font-size: 11px;
    }

    .highlight .value {
        font-size: 19px;
    }
}

@media (max-height: 700px) and (max-width: 600px) {
    body {
        padding-top: max(8px, env(safe-area-inset-top));
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    main {
        padding-top: 17px;
        padding-bottom: 17px;
    }

    .app-header {
        margin-bottom: 10px;
    }

    #string-selector {
        margin-top: 9px;
        margin-bottom: 9px;
    }

    #session-status {
        margin-bottom: 7px;
    }

    .note-display {
        font-size: clamp(60px, 18vw, 70px);
    }

    #meter {
        margin-top: 10px;
    }

    #waveform {
        height: 46px;
    }
}

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