:root {
    --bg-color: #f0f2f5;
    --text-color: #333333;
    --panel-bg: rgba(255, 255, 255, 0.7);
    --accent-color: #007bff;
    --accent-hover: #0056b3;
    --danger-color: #dc3545;
    --success-color: #28a745;
    --border-color: rgba(0, 0, 0, 0.1);
    --shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
    --glass-border: 1px solid rgba(255, 255, 255, 0.18);
    --font-mono: 'SF Mono', 'Roboto Mono', 'Courier New', monospace;
}

body.dark-theme {
    --bg-color: #121212;
    --text-color: #e0e0e0;
    --panel-bg: rgba(30, 30, 30, 0.7);
    --accent-color: #4dabf7;
    --accent-hover: #339af0;
    --danger-color: #ff6b6b;
    --success-color: #51cf66;
    --border-color: rgba(255, 255, 255, 0.1);
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; margin: 0; padding: 0; outline: none; -webkit-tap-highlight-color: transparent; }

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

/* Navigation */
.nav-bar {
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    width: 100%;
    background: var(--bg-color); /* Ensure nav is readable */
}

.nav-link {
    text-decoration: none;
    color: var(--text-color);
    font-weight: 600;
    opacity: 0.7;
    transition: opacity 0.2s;
}
.nav-link:hover { opacity: 1; }

.mode-switch-container {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
}

.mode-switch {
    display: inline-flex;
    background: var(--panel-bg);
    padding: 4px;
    border-radius: 20px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    align-items: center;
}

.mode-btn {
    padding: 6px 12px;
    border-radius: 16px;
    border: none;
    background: transparent;
    color: var(--text-color);
    font-size: 2rem;
    cursor: pointer;
    opacity: 0.6;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.mode-btn.active {
    background: var(--accent-color);
    color: white;
    opacity: 1;
    font-weight: 600;
}

/* Main Grid Layout */
.main-container {
    flex: 1;
    display: grid;
    grid-template-columns: 0.84fr 1.16fr; /* Timer takes less space */
    gap: 40px;
    padding: 20px 40px;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    align-items: start;
}

/* Timer Section (Left Column) */
.timer-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

/* Laps Section (Right Column) */
.laps-section {
    width: 100%;
    position: sticky;
    top: 20px;
}

/* Event Title */
.event-section {
    margin-bottom: 20px;
    text-align: center;
    width: 100%;
    max-width: 400px;
}

.event-input {
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-color);
    font-size: 1.5rem;
    text-align: center;
    width: 100%;
    padding: 5px;
    transition: border-color 0.3s;
}

.event-input:focus {
    border-bottom-color: var(--accent-color);
}

.event-input::placeholder {
    opacity: 0.4;
}

/* Timer Display */
.timer-wrapper {
    position: relative;
    margin: 20px 0 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    aspect-ratio: 1 / 1;
    max-width: 300px;
    max-height: 300px;
}

.display {
    font-family: var(--font-mono);
    font-size: 5rem;
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.03em;
    text-shadow: 0 2px 10px rgba(0,0,0,0.1);
    z-index: 2;
    text-align: center;
}

.milliseconds {
    font-size: 0.4em;
    opacity: 0.7;
    display: inline-block;
    width: 2ch;
}

/* Progress Ring */
.progress-ring {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
    z-index: 1;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.5s;
}

.progress-ring circle {
    fill: transparent;
    stroke-width: 8;
    stroke-linecap: round;
    transform-origin: center;
}

.progress-ring-bg { stroke: var(--border-color); }
.progress-ring-fill {
    stroke: var(--accent-color);
    transition: stroke-dashoffset 0.1s linear;
}

/* Controls */
.controls {
    display: grid;
    grid-template-columns: repeat(3, auto);
    gap: 20px;
    margin-bottom: 30px;
    align-items: center;
    justify-content: center;
}

.btn-circle {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.2s, background 0.2s;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-circle:active { transform: scale(0.95); }
.btn-circle:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-main {
    width: 80px;
    height: 80px;
    font-size: 1.1rem;
}

.btn-start { background: var(--success-color); color: white; }
.btn-stop { background: var(--danger-color); color: white; }
.btn-reset { background: var(--panel-bg); color: var(--text-color); border: 1px solid var(--border-color); }
.btn-lap { background: var(--panel-bg); color: var(--text-color); border: 1px solid var(--border-color); }

/* Focus Presets */
.focus-presets {
    display: none;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    justify-content: center;
}

.preset-btn {
    padding: 8px 16px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    background: var(--panel-bg);
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s;
}

.preset-btn:hover { background: var(--accent-color); color: white; border-color: var(--accent-color); }

/* Lap Table */
.lap-container {
    width: 100%;
    background: var(--panel-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 16px;
    border: var(--glass-border);
    box-shadow: var(--shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 600px; /* Fixed height on desktop */
    transition: opacity 0.3s;
}

.lap-header {
    display: grid;
    grid-template-columns: 0.8fr 1.5fr 1.5fr;
    padding: 15px 20px;
    background: rgba(0,0,0,0.03);
    font-weight: 600;
    font-size: 2rem;
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 5;
}

.lap-list {
    overflow-y: auto;
    flex: 1;
    scrollbar-width: thin;
    scrollbar-color: var(--accent-color) transparent;
}

.lap-row {
    display: grid;
    grid-template-columns: 0.8fr 1.5fr 1.5fr;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    font-family: var(--font-mono);
    font-size: 0.95rem;
    animation: slideIn 0.2s ease-out;
    align-items: center;
}

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

.lap-row.fastest { color: var(--success-color); background: rgba(40, 167, 69, 0.05); }
.lap-row.slowest { color: var(--danger-color); background: rgba(220, 53, 69, 0.05); }

/* Utility Bar */
.utility-bar {
    margin-top: 20px;
    display: flex;
    gap: 20px;
    justify-content: center;
    width: 100%;
}

.util-btn {
    background: transparent;
    border: none;
    color: var(--text-color);
    opacity: 0.6;
    cursor: pointer;
    font-size: 2rem;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: opacity 0.2s;
    padding: 8px;
}

.util-btn:hover { opacity: 1; }

/* Toast */
.toast {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #333;
    color: white;
    padding: 10px 20px;
    border-radius: 30px;
    font-size: 2rem;
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
    z-index: 100;
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* Mobile Responsive Grid */
@media (max-width: 850px) {
    .main-container {
        grid-template-columns: 1fr;
        gap: 30px;
        padding: 20px;
    }

    .timer-section {
        order: 1;
    }

    .laps-section {
        order: 2;
        position: static;
    }

    .lap-container {
        height: auto;
        max-height: 400px;
    }

    .display {
        font-size: 15vw;
    }

    .timer-wrapper {
        margin: 10px 0 30px;
    }
}

/* Fullscreen Mode */
body.fullscreen .nav-bar,
body.fullscreen .laps-section,
body.fullscreen .event-section,
body.fullscreen .utility-bar,
body.fullscreen .focus-presets,
body.fullscreen .info-container {
    display: none !important;
}

body.fullscreen .main-container {
    grid-template-columns: 1fr;
    height: 100vh;
    padding: 0;
    max-width: none;
    align-items: center;
    justify-items: center;
    margin: 0;
}

body.fullscreen .timer-section {
    height: 100%;
    justify-content: center;
}

body.fullscreen .timer-wrapper {
    max-width: none;
    max-height: none;
    width: auto;
    height: auto;
    aspect-ratio: auto;
}

body.fullscreen .display {
    font-size: 18vw;
}

body.fullscreen .controls {
    position: fixed;
    bottom: 50px;
    transition: opacity 0.5s;
}

body.fullscreen.inactive .controls {
    opacity: 0;
}

/* Focus Mode Specifics */
body.focus-mode .laps-section {
    display: none;
}

body.focus-mode .main-container {
    grid-template-columns: 1fr;
    justify-items: center;
}

body.focus-mode .progress-ring {
    opacity: 1;
}

body.focus-mode .focus-presets {
    display: flex;
}

/* Info Section */
.info-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 40px;
    padding: 40px;
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
}

.shortcuts-list {
    list-style: none;
    padding: 0;
}

.shortcuts-list li {
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

kbd {
    background-color: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    box-shadow: 0 1px 1px rgba(0,0,0,0.2);
    color: var(--text-color);
    display: inline-block;
    font-size: 0.85em;
    font-weight: 700;
    line-height: 1;
    padding: 2px 4px;
    white-space: nowrap;
}

@media (max-width: 768px) {
    .info-container {
        grid-template-columns: 1fr;
    }
}
