/* ===== RESET & BASE ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-dark: #0f0f1a;
    --bg-card: #1a1a2e;
    --bg-card-hover: #222240;
    --accent: #e94560;
    --accent-glow: rgba(233, 69, 96, 0.4);
    --green: #00d68f;
    --green-glow: rgba(0, 214, 143, 0.4);
    --yellow: #ffc107;
    --text-primary: #ffffff;
    --text-secondary: #a0a0b8;
    --text-dim: #6c6c80;
    --border: #2a2a45;
    --gauge-flat: #e94560;
    --gauge-mid: #ffc107;
    --gauge-tune: #00d68f;
    --chart-bg: rgba(255, 255, 255, 0.03);
    --chart-line: rgba(255, 255, 255, 0.15);
    --chart-trace: #e94560;
    --chart-zone: rgba(0, 214, 143, 0.12);
}

[data-theme="light"] {
    --bg-dark: #f0f0f5;
    --bg-card: #ffffff;
    --bg-card-hover: #e8e8f0;
    --accent: #d6304a;
    --accent-glow: rgba(214, 48, 74, 0.3);
    --green: #00b377;
    --green-glow: rgba(0, 179, 119, 0.3);
    --yellow: #e5a800;
    --text-primary: #1a1a2e;
    --text-secondary: #555570;
    --text-dim: #8888a0;
    --border: #d0d0e0;
    --gauge-flat: #d6304a;
    --gauge-mid: #e5a800;
    --gauge-tune: #00b377;
    --chart-bg: rgba(0, 0, 0, 0.03);
    --chart-line: rgba(0, 0, 0, 0.12);
    --chart-trace: #d6304a;
    --chart-zone: rgba(0, 179, 119, 0.1);
}

/* ===== SR-ONLY ===== */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

html, body {
    height: 100%;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-dark);
    color: var(--text-primary);
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 480px;
    margin: 0 auto;
    padding: 12px 16px;
}

/* ===== FOCUS STYLES ===== */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

select:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ===== HEADER ===== */
header {
    text-align: center;
    padding: 8px 0;
    flex-shrink: 0;
}

.header-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
}

header h1 {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: -0.5px;
}

#theme-toggle {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 1.1rem;
    width: 34px;
    height: 34px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, border-color 0.2s;
}

#theme-toggle:hover {
    background: var(--bg-card-hover);
}

#tuning-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* ===== TUNING BAR ===== */
#tuning-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 8px 0;
    flex-shrink: 0;
    flex-wrap: wrap;
}

#tuning-selector {
    flex: 1;
    min-width: 140px;
    max-width: 200px;
}

#tuning-select {
    width: 100%;
    padding: 6px 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.8rem;
    cursor: pointer;
    appearance: auto;
}

#calibration-bar {
    display: flex;
    align-items: center;
    gap: 6px;
}

#calibration-bar button {
    width: 28px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    line-height: 1;
}

#calibration-bar button:hover {
    background: var(--bg-card-hover);
}

#a4-display {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
    min-width: 90px;
    text-align: center;
}

/* ===== MAIN CONTENT (responsive wrapper) ===== */
#main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
}

/* ===== TUNER DISPLAY ===== */
#tuner-display {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 0;
}

/* Note Display */
#note-container {
    display: flex;
    align-items: baseline;
    gap: 4px;
}

#detected-note {
    font-size: 5rem;
    font-weight: 700;
    line-height: 1;
    transition: color 0.2s;
}

#detected-octave {
    font-size: 1.8rem;
    font-weight: 300;
    color: var(--text-secondary);
}

#frequency-display {
    font-size: 0.9rem;
    color: var(--text-dim);
    font-weight: 300;
    font-variant-numeric: tabular-nums;
}

/* ===== CONFIDENCE INDICATOR ===== */
#confidence-container {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    max-width: 200px;
}

#confidence-bar {
    flex: 1;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    overflow: hidden;
}

#confidence-fill {
    height: 100%;
    width: 0%;
    border-radius: 2px;
    transition: width 0.15s, background-color 0.15s;
    background: var(--text-dim);
}

#confidence-label {
    font-size: 0.65rem;
    color: var(--text-dim);
    font-variant-numeric: tabular-nums;
    min-width: 28px;
}

/* Gauge */
#gauge-container {
    width: 100%;
    max-width: 360px;
    padding: 12px 0;
}

#gauge-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.65rem;
    color: var(--text-dim);
    padding: 0 8px;
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

#gauge {
    padding: 0 8px;
}

#gauge-track {
    position: relative;
    height: 28px;
    background: linear-gradient(90deg,
        var(--gauge-flat) 0%,
        var(--gauge-mid) 25%,
        var(--gauge-tune) 42%,
        var(--gauge-tune) 58%,
        var(--gauge-mid) 75%,
        var(--gauge-flat) 100%
    );
    border-radius: 14px;
    overflow: visible;
}

#gauge-center-zone {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
    width: 12px;
    height: 100%;
    background: rgba(255, 255, 255, 0.15);
    border-left: 2px solid rgba(255, 255, 255, 0.6);
    border-right: 2px solid rgba(255, 255, 255, 0.6);
}

#gauge-needle {
    position: absolute;
    top: -4px;
    left: 50%;
    transform: translateX(-50%);
    width: 6px;
    height: 36px;
    background: white;
    border-radius: 3px;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.6);
    transition: left 0.08s ease-out;
    z-index: 2;
}

#cents-display {
    text-align: center;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 6px;
    font-variant-numeric: tabular-nums;
    font-weight: 400;
}

/* ===== PITCH HISTORY CANVAS ===== */
#pitch-history {
    width: 100%;
    max-width: 360px;
    height: 48px;
    border-radius: 8px;
    background: var(--chart-bg);
}

/* Status */
#status-message {
    font-size: 0.9rem;
    font-weight: 600;
    padding: 6px 18px;
    border-radius: 20px;
    transition: all 0.3s;
    min-height: 32px;
    display: flex;
    align-items: center;
}

#status-message.flat {
    color: var(--accent);
    background: rgba(233, 69, 96, 0.1);
}

#status-message.sharp {
    color: var(--accent);
    background: rgba(233, 69, 96, 0.1);
}

#status-message.close {
    color: var(--yellow);
    background: rgba(255, 193, 7, 0.1);
}

#status-message.in-tune {
    color: var(--green);
    background: rgba(0, 214, 143, 0.1);
    animation: pulse-green 1.5s infinite;
}

@keyframes pulse-green {
    0%, 100% { box-shadow: 0 0 0 0 var(--green-glow); }
    50% { box-shadow: 0 0 0 8px transparent; }
}

/* ===== SIDEBAR ===== */
#sidebar {
    flex-shrink: 0;
}

/* ===== STRING SELECTOR ===== */
#string-selector {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px 0;
}

.string-row {
    display: flex;
    gap: 8px;
}

.string-btn {
    flex: 1;
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: 12px;
    color: var(--text-primary);
    padding: 10px 6px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    font-family: inherit;
}

.string-btn:active {
    transform: scale(0.96);
}

.string-btn.active {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.08);
    box-shadow: 0 0 12px var(--accent-glow);
}

.string-btn.locked {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.15);
}

.string-btn.in-tune {
    border-color: var(--green) !important;
    background: rgba(0, 214, 143, 0.1) !important;
    box-shadow: 0 0 12px var(--green-glow) !important;
}

.string-number {
    font-size: 0.65rem;
    color: var(--text-dim);
    font-weight: 300;
}

.string-note {
    font-size: 1.1rem;
    font-weight: 700;
}

.string-hz {
    font-size: 0.6rem;
    color: var(--text-dim);
    font-weight: 300;
    font-variant-numeric: tabular-nums;
}

/* Tone trigger inside string button */
.tone-trigger {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 20px;
    height: 20px;
    border: none;
    border-radius: 50%;
    background: var(--border);
    color: var(--text-dim);
    font-size: 0.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s;
    padding: 0;
    font-family: inherit;
    line-height: 1;
}

.tone-trigger:hover {
    background: var(--accent);
    color: white;
}

.tone-trigger.playing {
    background: var(--accent);
    color: white;
    animation: tone-pulse 0.8s infinite;
}

@keyframes tone-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ===== CONTROLS ===== */
#controls {
    flex-shrink: 0;
    padding: 8px 0;
}

#start-btn {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 14px;
    background: var(--accent);
    color: white;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

#start-btn:active {
    transform: scale(0.98);
}

#start-btn.listening {
    background: #333355;
    box-shadow: 0 0 0 2px var(--accent);
}

[data-theme="light"] #start-btn.listening {
    background: #d0d0e0;
    color: var(--text-primary);
}

/* ===== FOOTER ===== */
footer {
    flex-shrink: 0;
    text-align: center;
    padding: 6px 0;
    font-size: 0.7rem;
    color: var(--text-dim);
}

/* ===== RESPONSIVE ===== */

/* Most phones in portrait */
@media (max-height: 700px) {
    #app { padding: 6px 12px; }
    header { padding: 2px 0; }
    header h1 { font-size: 1.2rem; }
    #tuning-bar { padding: 4px 0; }
    #detected-note { font-size: 3.5rem; }
    #tuner-display { gap: 4px; }
    #gauge-container { padding: 4px 0; }
    #gauge-labels { margin-bottom: 3px; }
    #cents-display { margin-top: 3px; }
    #pitch-history { height: 32px; }
    #status-message { padding: 4px 14px; min-height: 24px; font-size: 0.8rem; }
    #string-selector { padding: 6px 0; gap: 4px; }
    .string-row { gap: 6px; }
    .string-btn { padding: 6px 4px; border-radius: 10px; }
    .string-note { font-size: 1rem; }
    .string-hz { font-size: 0.55rem; }
    #controls { padding: 4px 0; }
    #start-btn { padding: 10px; border-radius: 10px; font-size: 0.9rem; }
    footer { display: none; }
}

/* Very short phones */
@media (max-height: 570px) {
    #detected-note { font-size: 2.8rem; }
    #pitch-history { display: none; }
    .string-btn { padding: 4px 3px; }
    .string-number { font-size: 0.55rem; }
    .string-note { font-size: 0.9rem; }
    #start-btn { padding: 8px; }
}

/* Tall phones */
@media (min-height: 800px) {
    #tuner-display { gap: 14px; }
    #gauge-container { padding: 20px 0; }
}

/* Tablet portrait */
@media (min-width: 600px) {
    #app {
        max-width: 600px;
        padding: 16px 24px;
    }

    #detected-note { font-size: 6rem; }

    .string-btn {
        padding: 14px 10px;
    }

    #gauge-container {
        max-width: 480px;
    }

    #pitch-history {
        max-width: 480px;
        height: 56px;
    }
}

/* Wide / desktop: two-column layout */
@media (min-width: 900px) {
    #app {
        max-width: 900px;
    }

    #main-content {
        flex-direction: row;
        gap: 24px;
    }

    #tuner-display {
        flex: 2;
    }

    #sidebar {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        min-width: 240px;
        max-width: 300px;
    }

    #string-selector {
        padding: 8px 0;
    }
}

/* Phone landscape */
@media (orientation: landscape) and (max-height: 500px) {
    #app {
        max-width: 100%;
        padding: 6px 16px;
        flex-direction: column;
    }

    #main-content {
        flex-direction: row;
        gap: 16px;
    }

    #tuner-display {
        flex: 2;
        gap: 4px;
    }

    #sidebar {
        flex: 1;
        min-width: 180px;
    }

    header { padding: 2px 0; }
    header h1 { font-size: 1.1rem; }
    #tuning-label { display: none; }
    #detected-note { font-size: 3rem; }
    #gauge-container { padding: 4px 0; }
    #pitch-history { height: 28px; }
    .string-btn { padding: 6px 4px; }
    #string-selector { padding: 4px 0; gap: 4px; }
    .string-row { gap: 4px; }
    #controls { padding: 4px 0; }
    #start-btn { padding: 10px; }
    footer { display: none; }
    #tuning-bar { padding: 4px 0; }
}
