:root {
    --bg-color: #121212;
    --card-bg: #1e1e1e;
    --accent-color: #ff9800;
    --text-color: #e0e0e0;
    --text-dim: #999;
    --success: #4caf50;
    --error: #f44336;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    margin: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    overflow-x: hidden;
}

#app {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 96%;
    padding: 2rem 1rem;
}

.screen {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    animation: fadeIn 0.3s ease-out;
}

.hidden {
    display: none !important;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

h1 {
    font-size: 2rem;
    text-align: center;
    color: var(--accent-color);
}

@media (max-width: 600px) {
    h1 {
        font-size: 1.5rem;
    }
}

h3 {
    margin-bottom: 0.5rem;
    color: var(--text-dim);
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
}

.subtitle {
    color: var(--text-dim);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.checkbox-group {
    flex-direction: row;
    align-items: center;
    gap: 0.8rem;
    cursor: pointer;
}

.checkbox-group input {
    width: 20px;
    height: 20px;
    accent-color: var(--accent-color);
}

.small-select {
    padding: 0.4rem;
    background: #222;
    color: white;
    border: 1px solid #444;
    border-radius: 4px;
    font-size: 0.8rem;
    margin-left: auto;
}

select {
    padding: 0.8rem;
    background: var(--card-bg);
    color: white;
    border: 1px solid #333;
    border-radius: 4px;
    font-size: 1rem;
}

button {
    padding: 1rem;
    background-color: var(--accent-color);
    color: black;
    border: none;
    border-radius: 4px;
    font-weight: bold;
    font-size: 1.1rem;
    cursor: pointer;
    transition: transform 0.1s, background-color 0.2s;
}

button:hover {
    background-color: #ffa726;
}

button:active {
    transform: scale(0.98);
}

button:disabled {
    background-color: #333;
    color: #666;
    cursor: not-allowed;
}

/* Subdivision Selector */
.subdivision-selector {
    display: flex;
    gap: 10px;
    width: 100%;
}

.sub-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #252525;
    border: 1px solid #333;
    padding: 0.6rem 0.3rem;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s;
    color: var(--text-dim);
    font-weight: normal;
}

.sub-btn:hover {
    background-color: #333;
    border-color: #555;
    color: var(--text-color);
    transform: none; /* Override generic button transform */
}

.sub-btn:active {
    transform: scale(0.98);
}

.sub-btn.active {
    background-color: rgba(255, 152, 0, 0.15);
    border-color: var(--accent-color);
    color: var(--accent-color);
    font-weight: bold;
}

.icon-note {
    width: 28px;
    height: 38px;
    margin-bottom: 0.3rem;
    fill: currentColor;
    display: block;
}

.sub-btn span {
    font-size: 0.9rem;
    line-height: 1;
}

/* Override default button styles for sub-btn */
.sub-btn {
    font-size: inherit; /* Reset */
}

.exercise-info {
    background: var(--card-bg);
    padding: 1.2rem;
    border-radius: 8px;
}

.control-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 1rem;
}

.full-width {
    grid-column: span 2;
}

.input-with-button {
    display: flex;
    gap: 0.5rem;
    width: 100%;
}

.input-with-button select, 
.input-with-button input {
    flex: 1;
    min-width: 0;
}

.input-with-button button {
    flex-shrink: 0;
    padding: 0 0.8rem;
    font-size: 0.9rem;
    white-space: nowrap;
}

@media (max-width: 600px) {
    .control-grid {
        grid-template-columns: 1fr;
    }
    
    .full-width {
        grid-column: span 1;
    }

    .exercise-info {
        padding: 1rem;
    }

    .input-with-button {
        flex-wrap: wrap;
    }
    
    .input-with-button button {
        flex: 1;
        padding: 0.6rem !important;
    }
}

.small-number-input {
    width: 50px !important;
    height: 32px;
    background: transparent;
    color: var(--accent-color);
    border: none;
    font-size: 0.95rem;
    font-weight: 700;
    text-align: center;
    outline: none;
    font-family: inherit;
    padding: 0;
    margin: 0;
}

.note-stepper {
    display: flex;
    align-items: center;
    background: #252525;
    border: 1px solid #444;
    border-radius: 6px;
    width: fit-content;
    overflow: hidden;
    transition: all 0.2s;
    margin-top: 4px;
}

.note-stepper:focus-within {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(255, 152, 0, 0.2);
}

.step-btn {
    background: transparent;
    color: var(--text-dim);
    border: none;
    width: 28px;
    height: 32px;
    font-size: 0.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    padding: 0 !important;
}

.step-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: white;
}

#note-down {
    border-right: 1px solid #333;
}

#note-up {
    border-left: 1px solid #333;
}

.step-btn:active {
    background: rgba(255, 255, 255, 0.1);
}

.small-number-input:hover {
    border-color: #666;
    background: #2a2a2a;
}

.small-number-input:focus {
    border-color: var(--accent-color);
    background: #333;
    box-shadow: 0 0 0 2px rgba(255, 152, 0, 0.2);
}

.small-btn {
    padding: 0 0.8rem !important;
    font-size: 0.8rem !important;
}

.note-name {
    color: var(--accent-color);
    font-weight: 800;
    margin-left: 0.3rem;
    font-size: 0.95rem;
    text-transform: uppercase;
}

.hint-text {
    font-size: 0.65rem;
    color: var(--text-dim);
    font-weight: normal;
    font-style: italic;
    margin-left: 6px;
    opacity: 0.7;
}

#note-group {
    background: rgba(255, 255, 255, 0.03);
    padding: 0.7rem;
    border-radius: 8px;
    border: 1px solid #2a2a2a;
    transition: all 0.2s;
}

#note-group label {
    margin-bottom: 4px;
    display: flex;
    align-items: baseline;
}

#note-group:hover {
    border-color: #333;
}

@keyframes highlight-pulse {
    0% { border-color: var(--accent-color); background: rgba(255, 152, 0, 0.2); transform: scale(1.02); }
    100% { border-color: #2a2a2a; background: rgba(255, 255, 255, 0.03); transform: scale(1); }
}

.detected-pulse {
    animation: highlight-pulse 0.4s ease-out;
}

@keyframes pulse {
    0% { opacity: 1; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent-color);
    margin: 0.5rem 0;
}

#duration-value {
    color: var(--accent-color);
    font-weight: bold;
}

/* Layout for Setup Screen (Side-by-side on Desktop) */
#screen-setup {
    width: 100%;
}

@media (min-width: 900px) {
    #screen-setup {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto auto 1fr;
        column-gap: 2rem;
        row-gap: 1.5rem;
        align-items: start;
    }

    #screen-setup .main-header {
        grid-column: span 2;
        text-align: center;
    }

    /* Configuration block - now on the RIGHT */
    #screen-setup .exercise-info:first-of-type {
        grid-column: 2;
        grid-row: 2;
    }

    /* Start button - now on the RIGHT */
    #start-btn {
        grid-column: 2;
        grid-row: 3;
        align-self: start;
    }

    /* History container - now on the LEFT */
    #history-container {
        grid-column: 1;
        grid-row: 2 / span 2;
        margin-top: 0 !important;
    }
}

/* Play Screen & Results */
#screen-play, #screen-results {
    max-width: 700px;
    width: 100%;
    margin: auto;
    position: relative;
}

.status-bar {
    text-align: center;
}

#timer {
    font-size: 4rem;
    font-weight: bold;
    font-variant-numeric: tabular-nums;
    margin-bottom: 1rem;
}

@media (max-width: 600px) {
    #timer {
        font-size: 3rem;
    }
}

#countdown-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(18, 18, 18, 0.95);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

#countdown-number {
    font-size: 8rem;
    font-weight: 900;
    color: var(--accent-color);
    text-transform: uppercase;
}

@media (max-width: 600px) {
    #countdown-number {
        font-size: 5rem;
    }
}

.countdown-label {
    font-size: 1.2rem;
    letter-spacing: 4px;
    color: var(--text-dim);
    margin-bottom: 1rem;
}

#progress-container {
    height: 8px;
    background: #333;
    border-radius: 4px;
    overflow: hidden;
}

#progress-bar {
    height: 100%;
    background: var(--accent-color);
    width: 0%;
    transition: width 0.1s linear;
}

.hit-indicator-container {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: 100%;
}

.hit-indicator-label {
    font-size: 0.7rem;
    font-weight: bold;
    color: var(--text-dim);
    letter-spacing: 1px;
    width: 50px;
}

.hit-indicator-label.early { text-align: right; }
.hit-indicator-label.late { text-align: left; }

.hit-indicator {
    height: 80px;
    background: #000;
    position: relative;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 1px solid #333;
    flex-grow: 1;
}

.center-line {
    position: absolute;
    height: 100%;
    width: 2px;
    background: #4caf50;
    box-shadow: 0 0 15px #4caf50;
    z-index: 1;
}

#hit-cloud-canvas {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
}

/* Removed .hit-marker styles as we now use canvas */

.instructions {
    text-align: center;
    color: var(--text-dim);
    font-style: italic;
}

/* Results Screen */
.results-header {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.results-header h1 {
    font-size: 1.2rem;
    color: white;
}

.metrics-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

@media (max-width: 500px) {
    .metrics-grid {
        grid-template-columns: 1fr;
    }
    
    .metric-card:not(:last-child)::after {
        display: none;
    }
}

.metric-card {
    text-align: left;
    position: relative;
}

.metric-card:not(:last-child)::after {
    content: "";
    position: absolute;
    right: -0.5rem;
    top: 0;
    bottom: 0;
    width: 1px;
    background: #333;
}

.metric-card label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: white;
    font-size: 0.9rem;
    font-weight: bold;
    margin-bottom: 1rem;
    position: relative; /* Make label the positioning context */
}

.info-icon {
    font-size: 1rem;
    cursor: help;
    opacity: 0.6;
    transition: opacity 0.2s;
    position: relative;
    display: inline-block;
}

.info-icon:hover {
    opacity: 1;
    color: var(--accent-color);
}

.custom-tooltip {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 10px;
    background: #333;
    color: #e0e0e0;
    padding: 0.8rem;
    border-radius: 4px;
    font-size: 0.85rem;
    width: 200px;
    z-index: 20;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    border: 1px solid #555;
    line-height: 1.5;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
    text-align: left;
    font-weight: normal;
    cursor: auto;
}

.custom-tooltip.visible {
    opacity: 1;
    pointer-events: auto;
}

.custom-tooltip::after {
    content: "";
    position: absolute;
    top: -6px;
    left: 50%;
    transform: translateX(-50%);
    border-width: 0 6px 6px;
    border-style: solid;
    border-color: transparent transparent #333 transparent;
}

.metric-card .value {
    font-size: 2.5rem;
    font-weight: 800;
}

.metric-card .sub-label {
    font-size: 0.9rem;
    margin-top: 0.2rem;
}

/* Quick Calibration Button */
.text-btn {
    background: none;
    border: 1px solid #444;
    color: var(--text-dim);
    font-size: 0.65rem;
    padding: 3px 8px;
    margin-top: 8px;
    cursor: pointer;
    border-radius: 4px;
    transition: all 0.2s;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.text-btn:hover {
    color: var(--accent-color);
    border-color: var(--accent-color);
    background: rgba(255, 152, 0, 0.1);
}

.chart-container {
    background: #000;
    padding: 1rem;
    border-radius: 4px;
    position: relative;
    border: 1px solid #333;
}

.chart-labels {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    pointer-events: none;
    font-size: 0.8rem;
    font-weight: 600;
    color: #aaa;
    padding: 10px;
}

.chart-labels span:nth-child(1) { position: absolute; top: 10px; left: 10px; } /* -40ms */
.chart-labels span:nth-child(6) { position: absolute; bottom: 10px; left: 10px; } /* +40ms */
.chart-labels span:nth-child(2) { position: absolute; bottom: -25px; left: 0; } /* 0:00 */
.chart-labels span:nth-child(3) { position: absolute; bottom: -25px; left: 25%; } /* 0:30 */
.chart-labels span:nth-child(4) { position: absolute; bottom: -25px; left: 50%; } /* 1:00 */
.chart-labels span:nth-child(5) { position: absolute; bottom: -25px; right: 0; } /* 2:00 */

#error-chart {
    width: 100%;
    height: 200px;
}

.feedback-box {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid #333;
    padding: 1rem;
    border-radius: 4px;
    text-align: center;
    margin-top: 2.5rem;
}

#res-feedback {
    font-size: 1.1rem;
}

/* High Score Congratulation */
.high-score-badge {
    display: inline-block;
    background: linear-gradient(135deg, #FFD700 0%, #FFA000 100%);
    color: black;
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    font-weight: 900;
    font-size: 0.85rem;
    text-transform: uppercase;
    margin-bottom: 0.8rem;
    box-shadow: 0 4px 15px rgba(255, 160, 0, 0.4);
    animation: bounceIn 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

@keyframes bounceIn {
    0% { transform: scale(0.3); opacity: 0; }
    50% { transform: scale(1.1); }
    70% { transform: scale(0.9); }
    100% { transform: scale(1); opacity: 1; }
}

.actions {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1rem;
}

@media (max-width: 500px) {
    .actions {
        grid-template-columns: 1fr;
    }
}

button.secondary {
    background: #333;
    color: white;
}

#save-btn {
    background: linear-gradient(180deg, #ff9800 0%, #e65100 100%);
    color: white;
}

/* History List */
#history-container {
    margin-top: 1rem;
    animation: fadeIn 0.3s ease-out;
}

.history-header {
    display: none;
}

#history-graph-view {
    margin-bottom: 1.5rem;
    position: relative;
}

.chart-tooltip {
    position: absolute;
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid var(--accent-color);
    padding: 0.6rem 0.8rem;
    border-radius: 4px;
    font-size: 0.8rem;
    pointer-events: none;
    z-index: 10;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    white-space: nowrap;
    line-height: 1.4;
}

.chart-tooltip b {
    color: var(--accent-color);
}

.chart-tooltip .date {
    font-size: 0.7rem;
    color: #888;
    display: block;
    margin-top: 0.2rem;
}

.inline-number-input {
    background: transparent;
    border: none;
    color: var(--accent-color);
    font-weight: bold;
    font-size: inherit;
    font-family: inherit;
    width: 50px;
    padding: 0;
    margin: 0;
    cursor: text;
    border-bottom: 1px dashed var(--accent-color);
}

/* Remove arrows from number input */
.inline-number-input::-webkit-outer-spin-button,
.inline-number-input::-webkit-inner-spin-button,
.small-number-input::-webkit-outer-spin-button,
.small-number-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    appearance: none;
    margin: 0;
}
.inline-number-input[type=number],
.small-number-input[type=number] {
    -moz-appearance: textfield;
    appearance: textfield;
}

.inline-number-input:focus {
    outline: none;
    border-bottom: 1px solid var(--accent-color);
}

#bpm-accuracy-chart {
    width: 100%;
    height: 300px;
    background: #000;
    border-radius: 4px;
    margin-bottom: 0.5rem;
}

.chart-legend {
    font-size: 0.7rem;
    color: var(--text-dim);
    text-align: center;
    font-style: italic;
}

/* History Table */
#history-table-container {
    margin-top: 1rem;
    overflow-x: auto;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
    color: var(--text-color);
}

.history-table th {
    text-align: left;
    padding: 0.8rem 0.5rem;
    color: var(--text-dim);
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 1px;
    border-bottom: 1px solid #333;
    user-select: none;
    transition: color 0.2s, background-color 0.2s;
}

.history-table th:hover:not(:last-child) {
    color: white;
    background: rgba(255, 255, 255, 0.05);
}

.history-table td {
    padding: 0.8rem 0.5rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.history-table tr:hover {
    background: rgba(255, 255, 255, 0.02);
}

.col-score {
    font-weight: 800;
    font-size: 1rem;
}

.col-date {
    color: #888;
    white-space: nowrap;
}

.col-trend {
    font-weight: 600;
}

.delete-btn-small {
    background: transparent;
    color: #444;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    transition: color 0.2s;
    border-radius: 4px;
    height: 24px;
    width: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.delete-btn-small:hover {
    color: var(--error);
    background: rgba(244, 67, 54, 0.1);
}

/* Mini Comparison Table for Results Screen */
.history-table-mini {
    border-top: 1px solid #333;
}

.history-table-mini h3 {
    font-size: 0.8rem;
    margin-bottom: 1.2rem;
    text-align: center;
    color: var(--text-dim);
    letter-spacing: 2px;
}

.history-table-mini .history-table {
    font-size: 0.8rem;
}

.history-table-mini .history-table td {
    padding: 0.6rem 0.5rem;
}

.history-table-mini tr.current-session {
    background: rgba(255, 152, 0, 0.15) !important;
}

.history-table-mini tr.current-session td {
    color: var(--accent-color);
}

.history-table-mini tr.current-session td.col-date {
    color: var(--accent-color);
    font-weight: 800;
}

.warning-message {
    position: relative;
    font-size: 0.8rem;
    color: #ffb74d;
    margin-top: 8px;
    padding: 8px 30px 8px 8px;
    background: rgba(255, 183, 77, 0.1);
    border-radius: 4px;
    border-left: 3px solid #ffb74d;
    line-height: 1.4;
}

.warning-message .close-btn {
    position: absolute;
    top: 50%;
    right: 8px;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: #ffb74d;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.warning-message .close-btn:hover {
    opacity: 1;
}

.warning-message.hidden {
    display: none;
}

.footer-feedback {
    margin-top: auto;
    text-align: center;
    font-size: 0.85rem;
    color: var(--text-dim);
}

.footer-feedback a {
    color: var(--text-dim);
    text-decoration: none;
    transition: color 0.2s;
    border-bottom: 1px solid transparent;
}

.footer-feedback a:hover {
    color: var(--accent-color);
    border-bottom-color: var(--accent-color);
}

.feedback-hint {
    margin-top: 0.8rem;
    font-size: 0.75rem;
    opacity: 0.6;
    line-height: 1.4;
}

.feedback-hint strong {
    color: var(--text-color);
    display: block;
    margin-top: 2px;
}
