/* || Imports */
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap');

/* || Variables */
:root {
    /* Palette */
    --color-bg: #ffffff;
    --color-bg-panel: #ffffff;
    --color-bg-track: #f7f9fb;
    
    --color-text-main: #1a1a1a;
    --color-text-muted: #555555;
    
    /* Blue Shades */
    --color-accent: #1484e6;        /* Main Blue */
    --color-accent-dark: #0f6cb8;   /* Darker Blue */
    --color-accent-light: #5faaf0;  /* Lighter Blue */
    --color-accent-bg: #e6f2fc;     /* Very Light Blue */
    
    --color-record: #0f6cb8; 
    
    --color-border: #e0e0e0;
    --shadow-soft: 0 4px 12px rgba(0,0,0,0.05);
    --shadow-medium: 0 4px 20px rgba(0,0,0,0.08);
    
    --font-ui: 'Space Mono', monospace;
    --font-mono: 'Space Mono', monospace;
    
    --radius-md: 12px;
    --radius-full: 9999px;
}

::selection {
    color: var(--color-bg);
    background-color: var(--color-accent);
}

/* Material Symbols */
.material-symbols-outlined {
    font-family: 'Material Symbols Outlined';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    display: inline-block;
    line-height: 1;
    text-transform: none;
    letter-spacing: normal;
    word-wrap: normal;
    white-space: nowrap;
    direction: ltr;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    -moz-osx-font-smoothing: grayscale;
    font-feature-settings: 'liga';
}

.material-symbols-outlined.icon-large {
    font-size: 40px;
}

/* || Reset & Base */
* {
    box-sizing: border-box;
    touch-action: none; 
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}


html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: var(--color-bg);
    color: var(--color-text-main);
    font-family: var(--font-ui);
    overflow: hidden;
}

.hidden {
    display: none !important;
}

/* || Rotate Hint */
#rotate-hint {
    display: none; /* Default hidden */
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(255,255,255,0.95);
    backdrop-filter: blur(10px);
    z-index: 10000;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 40px;
}
#rotate-hint .material-icons {
    font-size: 48px;
    color: var(--color-accent);
    margin-bottom: 20px;
}

/* Show hint ONLY on small screens in portrait AND if not dismissed */
@media screen and (max-width: 600px) and (orientation: portrait) {
    #rotate-hint.active {
        display: flex;
    }
}

.text-button {
    background: transparent;
    border: 1px solid var(--color-border);
    padding: 8px 16px;
    border-radius: var(--radius-md);
    margin-top: 20px;
    color: var(--color-text-muted);
    font-weight: 500;
}
.text-button:active {
    background-color: var(--color-bg-track);
}


/* || Layout */
.app-container {
    display: grid;
    width: 100%;
    height: 100%;
    /* Default / Portrait Layout - new order: plot, controls, track, panel */
    grid-template-areas: 
        "plot"
        "controls"
        "track"
        "panel";
    grid-template-rows: 1fr auto auto auto;
    grid-template-columns: 100%;
}

@media screen and (max-width: 600px) and (orientation: portrait) {
    .app-container {
        grid-template-rows: 1fr auto auto auto;
    }
}

@media screen and (orientation: landscape) {
    .app-container {
        grid-template-areas:
            "plot panel"
            "track track";
        grid-template-rows: 1fr 110px; 
        grid-template-columns: 1fr 340px; 
    }
}

/* || Track Section */
.section-track {
    grid-area: track;
    position: relative;
    background-color: var(--color-bg-track);
    border-bottom: 1px solid var(--color-border);
    box-shadow: inset 0 0 20px rgba(0,0,0,0.02);
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

@media screen and (orientation: landscape) {
    .section-track {
        border-bottom: none;
        border-top: 1px solid var(--color-border);
        flex-direction: row;
        align-items: center;
        padding: 0 24px 0 24px;
        gap: 12px;
    }
}

@media screen and (orientation: portrait) {
    .section-track {
        border-top: 1px solid var(--color-border);
        border-bottom: none;
        padding: 8px 6px;
    }
}

#space-canvas {
    width: 100%;
    height: 100%;
    cursor: grab;
    display: block;
}

@media screen and (orientation: landscape) {
    #space-canvas {
        flex: 1;
        min-width: 0;
        min-height: 0;
    }
}

@media screen and (orientation: portrait) {
    #space-canvas {
        height: 80px;
    }
}

#space-canvas:active { cursor: grabbing; }

/* Track controls container - for portrait mode */
#track-controls {
    grid-area: controls;
    display: none;
}

@media screen and (orientation: portrait) {
    #track-controls {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 10px 16px;
        min-height: 76px; /* Ensure panel contains the 56px record button + padding */
        background: var(--color-bg-panel);
        border-top: 1px solid var(--color-border);
        border-bottom: 1px solid var(--color-border);
        position: relative;
    }
    
    /* Record button on the left side */
    #track-controls .fab-button {
        position: absolute;
        left: 16px;
    }
    
    /* Left-handed mode: record on right */
    body.left-handed #track-controls .fab-button {
        left: auto;
        right: 16px;
    }
}

/* || Plot Section */
.section-plot {
    grid-area: plot;
    position: relative;
    background-color: var(--color-bg);
    display: flex;
    min-width: 0;
    min-height: 0;
}

@media screen and (orientation: landscape) {
    .section-plot {
        border-right: 1px solid var(--color-border);
    }
}

#plot-canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: crosshair;
}

/* || Playback Controls */
.playback-controls {
    position: absolute;
    top: 12px;
    right: 12px;
    background: rgba(255, 255, 255, 0.95);
    padding: 4px 10px;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-soft);
    display: flex;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--color-border);
    z-index: 15;
    transition: opacity 0.2s, transform 0.2s;
}

.playback-controls.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
}

.playback-info {
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: bold;
    color: var(--color-accent);
    min-width: 40px;
    text-align: right;
}

.icon-button-large {
    background: var(--color-accent);
    color: white;
    border: none;
    border-radius: 50%;
    width: 28px; height: 28px;
    display: flex;
    align-items: center; justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(20, 132, 230, 0.3);
}
.icon-button-large:hover {
    background: var(--color-accent-dark);
}
.icon-button-large:active {
    transform: scale(0.95);
}
.icon-button-large .material-symbols-outlined {
    font-size: 16px;
}

/* Playback in track controls (vertical mode) - keep same look, just static */
.playback-controls.in-track {
    position: static;
    top: auto;
    right: auto;
    transform: none;
}
.playback-controls.in-track.hidden {
    display: none;
}

/* || Panel Section */
.section-panel {
    grid-area: panel;
    background-color: var(--color-bg-panel);
    padding: 16px 20px;
    overflow-y: auto;
    /* Allow vertical scrolling on mobile for container AND children */
    touch-action: pan-y; 
    user-select: text; /* Allow selection */
    display: flex;
    flex-direction: column;
    z-index: 5;
    max-height: 35vh;
    min-width: 0;
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) transparent;
}

/* Ensure children don't block scroll */
.section-panel * {
    touch-action: pan-y;
    user-select: text;
}

.section-panel::-webkit-scrollbar {
    width: 4px;
}
.section-panel::-webkit-scrollbar-track {
    background: transparent;
}
.section-panel::-webkit-scrollbar-thumb {
    background-color: var(--color-border);
    border-radius: 4px;
}

@media screen and (orientation: landscape) {
    .section-panel {
        max-height: none;
        border-left: 1px solid var(--color-border);
        padding: 20px;
    }
}

.panel-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

@media screen and (orientation: landscape) {
    .panel-content {
        gap: 20px;
    }
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.header h2 {
    margin: 0;
    font-size: 20px;
    font-weight: 700;
    font-family: var(--font-mono);
    color: var(--color-text-main);
    letter-spacing: -0.5px;
}

/* || Controls & Inputs */
.control-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.control-group span {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-muted);
}

.centered-buttons {
    justify-content: stretch;
    gap: 12px;
}

/* || Icon Toggles (Quick Actions) */
.quick-actions-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.icon-toggle {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 48px;
    background: transparent;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: all 0.2s ease;
    padding: 0;
}

.icon-toggle:active {
    transform: scale(0.96);
}

.icon-toggle:hover {
    background-color: var(--color-bg-track);
    color: var(--color-text-main);
}

.icon-toggle.active {
    background-color: var(--color-accent);
    border-color: var(--color-accent);
    color: white;
    box-shadow: 0 4px 10px rgba(20, 132, 230, 0.25);
}

.icon-toggle .material-symbols-outlined {
    font-size: 24px;
}



/* Toggle Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
    position: absolute; cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #e0e0e0;
    transition: .3s;
    border-radius: 24px;
}
.slider:before {
    position: absolute; content: "";
    height: 18px; width: 18px;
    left: 3px; bottom: 3px;
    background-color: white;
    transition: .3s;
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
input:checked + .slider { background-color: var(--color-accent); }
input:checked + .slider:before { transform: translateX(20px); }

/* Range Slider */
input[type=range] {
    -webkit-appearance: none; 
    width: 100%; 
    background: transparent; 
    height: 24px;
    padding: 0;
    margin: 0;
    cursor: pointer;
}

#message-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

#message-overlay.hidden {
    display: none;
}

.message-panel {
    background: #fff;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 90%;
    width: 320px;
    animation: fadeInScale 0.2s ease-out;
}

.message-icon {
    font-size: 48px;
    color: #666;
    margin-bottom: 15px;
    display: block;
}

.message-panel h3 {
    margin: 0 0 10px 0;
    font-size: 20px;
    color: #333;
}

.message-panel p {
    margin: 0 0 25px 0;
    color: #666;
    line-height: 1.5;
}

.message-panel .action-button {
    width: 100%;
}
input[type=range]:focus { outline: none; }

/* Track */
input[type=range]::-webkit-slider-runnable-track {
    width: 100%;
    height: 4px;
    background: #e0e0e0;
    border-radius: 2px;
    border: none;
}
input[type=range]::-moz-range-track {
    width: 100%;
    height: 4px;
    background: #e0e0e0;
    border-radius: 2px;
    border: none;
}

/* Thumb */
input[type=range]::-webkit-slider-thumb {
    -webkit-appearance: none;
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: var(--color-accent);
    margin-top: -6px; /* align center with 4px track: (4 - 16) / 2 */
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    transition: transform 0.1s;
}
input[type=range]::-moz-range-thumb {
    height: 16px;
    width: 16px;
    border-radius: 50%;
    background: var(--color-accent);
    border: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    transition: transform 0.1s;
}

input[type=range]:active::-webkit-slider-thumb {
    transform: scale(1.2);
    background: var(--color-accent-dark);
}
input[type=range]:active::-moz-range-thumb {
    transform: scale(1.2);
    background: var(--color-accent-dark);
}

/* Buttons */
button {
    cursor: pointer;
    border: none;
    font-family: var(--font-mono);
    transition: all 0.2s;
    outline: none;
}

.icon-button {
    background: transparent;
    color: var(--color-text-main);
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-button:hover { background-color: rgba(0,0,0,0.05); }

.action-button {
    flex: 1;
    background-color: var(--color-bg);
    border: 1px solid var(--color-border);
    color: var(--color-text-main);
    padding: 12px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 700;
}
.action-button:hover {
    background-color: var(--color-bg-secondary);
    border-color: var(--color-accent);
    color: var(--color-accent);
}

#clear-button:hover {
    border-color: var(--color-record);
    color: var(--color-record);
}

/* Floating Record Button */
.fab-button {
    position: absolute;
    width: 56px; height: 56px; 
    border-radius: var(--radius-full);
    background-color: var(--color-bg);
    color: var(--color-record);
    border: 5px solid rgba(20, 132, 230, 0.15); 
    box-shadow: var(--shadow-medium);
    z-index: 20;
    display: grid;
    place-items: center;
    flex-shrink: 0;
}

.fab-button .material-symbols-outlined {
    font-size: 28px;
}

.fab-button:hover {
    transform: scale(1.05);
    border-color: rgba(20, 132, 230, 0.25);
}
.fab-button:active {
    transform: scale(0.95);
}

.fab-button.recording {
    background-color: var(--color-record);
    color: white;
    border-color: rgba(20, 132, 230, 0.3);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(20, 132, 230, 0.4); }
    70% { box-shadow: 0 0 0 15px rgba(20, 132, 230, 0); }
    100% { box-shadow: 0 0 0 0 rgba(20, 132, 230, 0); }
}

/* Landscape mode: record button on left side of track, centered vertically */
@media screen and (orientation: landscape) {
    .fab-button {
        position: static;
    }
    
    /* Left-handed mode: button on right */
    body.left-handed .section-track {
        flex-direction: row-reverse;
    }
}

/* Portrait mode: button in track controls, not absolute */
@media screen and (orientation: portrait) {
    .fab-button.in-track {
        position: static;
    }
}

.instructions p {
    font-family: var(--font-ui);
    font-size: 13px;
    color: var(--color-text-muted);
    line-height: 1.5;
    margin: 0;
    text-align: center;
}

/* || Sync Overlay */
#sync-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.sync-panel {
    background: var(--color-bg-panel);
    border: 1px solid var(--color-border);
    border-radius: 24px;
    padding: 24px;
    width: 90%;
    max-width: 400px;
    box-shadow: var(--shadow-medium);
}

.sync-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}
.sync-header h2 { margin: 0; font-size: 20px; }

.sync-content {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.sync-section h3 {
    margin: 0 0 8px 0;
    font-size: 12px;
    color: var(--color-accent);
    letter-spacing: 1px;
}

.desc {
    margin: 0 0 12px 0;
    font-size: 13px;
    color: var(--color-text-muted);
}

.code-display {
    background: var(--color-bg-track);
    padding: 16px;
    border-radius: var(--radius-md);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-family: var(--font-mono);
    font-size: 24px;
    font-weight: bold;
    letter-spacing: 4px;
    color: var(--color-text-main);
}

.input-row {
    display: flex;
    gap: 8px;
}

#join-id-input {
    flex: 1;
    padding: 12px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-family: var(--font-mono);
    font-size: 16px;
    text-transform: uppercase;
    text-align: center;
}
#join-id-input:focus {
    border-color: var(--color-accent);
    outline: none;
}

.divider {
    text-align: center;
    font-size: 12px;
    color: var(--color-border);
    font-weight: bold;
    position: relative;
}
.divider::before, .divider::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40%;
    height: 1px;
    background: var(--color-border);
}
.divider::before { left: 0; }
.divider::after { right: 0; }

#connection-status {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    background: var(--color-accent-bg);
    border-radius: var(--radius-md);
    color: var(--color-accent-dark);
    font-weight: 500;
}
.status-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: var(--color-accent);
    animation: blink 1s infinite;
}
@keyframes blink { 50% { opacity: 0.4; } }

/* Client Indicator */
#client-indicator {
    position: fixed;
    bottom: 20px; left: 50%;
    transform: translateX(-50%);
    background: var(--color-accent);
    color: white;
    padding: 8px 16px;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: var(--shadow-medium);
    z-index: 100;
    font-weight: 500;
}
#client-indicator .icon-button-small {
    background: rgba(255,255,255,0.2);
    border: none;
    border-radius: 50%;
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    color: white;
}
#client-indicator .material-symbols-outlined { font-size: 20px; }
