body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: #e6ebf5;
    background: radial-gradient(circle at top, #111b2d 0%, #060a12 55%, #04070c 100%);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

header {
    padding: 16px 28px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(5, 10, 18, 0.78);
    border-bottom: 1px solid rgba(80, 110, 150, 0.3);
    box-shadow: 0 12px 20px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(12px);
    position: sticky;
    top: 0;
    z-index: 20;
}

header h1 {
    margin: 0;
    font-weight: 300;
    letter-spacing: 1.4px;
    font-size: 22px;
}

main {
    flex: 1;
    display: grid;
    grid-template-columns: 360px 1fr;
    gap: 20px;
    padding: 20px 24px;
    box-sizing: border-box;
}

@media (max-width: 1280px) {
    main {
        grid-template-columns: minmax(0, 1fr);
    }
}

#control-panel {
    background: rgba(12, 18, 30, 0.75);
    border: 1px solid rgba(80, 110, 150, 0.35);
    border-radius: 14px;
    padding: 18px 20px 28px;
    display: flex;
    flex-direction: column;
    gap: 18px;
    box-shadow: 0 24px 40px rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(14px);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 13px;
    letter-spacing: 0.35px;
    color: rgba(199, 214, 238, 0.88);
}

select,
input[type="range"] {
    width: 100%;
    background: rgba(22, 30, 46, 0.92);
    border: 1px solid rgba(90, 118, 158, 0.55);
    color: #edf3ff;
    border-radius: 8px;
    padding: 8px 10px;
    box-sizing: border-box;
    font-size: 13px;
}

.range-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.range-value {
    min-width: 86px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-size: 15px;
    font-weight: 600;
    color: #8fe1ff;
}

.toggle-row {
    display: flex;
    gap: 10px;
}

.toggle-row button {
    flex: 1;
    padding: 10px 12px;
    font-size: 13px;
    letter-spacing: 0.4px;
    text-transform: uppercase;
    border-radius: 10px;
    border: 1px solid rgba(96, 128, 184, 0.6);
    background: linear-gradient(140deg, rgba(35, 52, 82, 0.88), rgba(15, 24, 40, 0.94));
    color: #dce7fb;
    cursor: pointer;
    transition: all 0.25s ease;
}

.toggle-row button.active {
    border-color: rgba(133, 216, 255, 0.9);
    box-shadow: 0 0 14px rgba(120, 210, 255, 0.35), inset 0 0 12px rgba(120, 210, 255, 0.22);
    color: #ffffff;
}

#device-info {
    display: grid;
    gap: 10px;
    background: rgba(17, 26, 41, 0.6);
    border-radius: 12px;
    padding: 12px 16px;
    border: 1px solid rgba(96, 128, 182, 0.38);
}

.info-line {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: rgba(204, 218, 239, 0.9);
}

.info-line span.value {
    font-family: "Roboto Mono", "Courier New", monospace;
    font-size: 13px;
    font-weight: 600;
    color: #9ae5ff;
}

#piston-status-row {
    transition: color 0.3s ease;
}

#piston-status-row .value {
    transition: color 0.3s ease;
}

#piston-status-row[data-state="running"] .value {
    color: #ffd580;
}

#piston-status-row[data-state="waiting"] .value {
    color: #ffe8a6;
}

#piston-status-row[data-state="success"] .value {
    color: #7affc8;
}

#piston-status-row[data-state="error"] .value {
    color: #ff9da3;
}

#sensor-list {
    display: grid;
    gap: 10px;
}

.sensor-card {
    padding: 10px 12px;
    border-radius: 10px;
    background: rgba(24, 32, 48, 0.78);
    border: 1px solid rgba(98, 138, 194, 0.45);
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
}

.sensor-card .sensor-label {
    font-size: 11px;
    letter-spacing: 0.3px;
    text-transform: uppercase;
    color: rgba(205, 220, 243, 0.85);
}

.sensor-card .value {
    font-family: "Roboto Mono", monospace;
    color: #ffd580;
    font-weight: 600;
}

.sensor-card .sensor-time {
    font-family: "Roboto Mono", monospace;
    font-size: 11px;
    color: rgba(197, 214, 239, 0.8);
}

#proofrun-controls {
    gap: 10px;
}

.proofrun-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

#proofrun-button {
    padding: 10px 16px;
    border-radius: 10px;
    border: 1px solid rgba(120, 176, 255, 0.65);
    background: linear-gradient(140deg, rgba(52, 86, 138, 0.92), rgba(18, 30, 52, 0.94));
    color: #e9f3ff;
    text-transform: uppercase;
    letter-spacing: 0.45px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.25s ease;
}

#proofrun-button:hover:not(:disabled) {
    background: linear-gradient(140deg, rgba(72, 120, 188, 0.94), rgba(22, 36, 62, 0.96));
    box-shadow: 0 0 14px rgba(118, 190, 255, 0.25);
}

#proofrun-button.running {
    border-color: rgba(255, 213, 128, 0.8);
    background: linear-gradient(140deg, rgba(120, 82, 26, 0.88), rgba(45, 28, 8, 0.96));
    color: #fff0d0;
    box-shadow: 0 0 18px rgba(255, 207, 102, 0.28);
}

#proofrun-button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    box-shadow: none;
}

.proofrun-hint {
    font-size: 12px;
    color: rgba(192, 210, 238, 0.72);
    letter-spacing: 0.2px;
}

#scene-container {
    position: relative;
    border-radius: 16px;
    overflow: hidden;
    border: 1px solid rgba(80, 110, 150, 0.35);
    box-shadow: 0 26px 50px rgba(3, 7, 13, 0.55);
    background: rgba(10, 15, 24, 0.85);
}

canvas {
    width: 100%;
    height: 100%;
    display: block;
}

#legend {
    position: absolute;
    right: 24px;
    bottom: 24px;
    background: rgba(10, 16, 27, 0.85);
    border-radius: 12px;
    padding: 12px 14px;
    border: 1px solid rgba(107, 140, 194, 0.4);
    color: #dbe6fb;
    font-size: 11px;
    letter-spacing: 0.35px;
    box-shadow: 0 16px 28px rgba(0, 0, 0, 0.45);
}

.legend-row {
    display: flex;
    gap: 10px;
    align-items: center;
    margin-bottom: 6px;
}

.legend-swatch {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid rgba(255, 255, 255, 0.3);
}

#fps-counter {
    position: absolute;
    left: 18px;
    bottom: 18px;
    font-size: 11px;
    font-family: "Roboto Mono", monospace;
    color: rgba(160, 210, 255, 0.8);
    background: rgba(7, 11, 18, 0.65);
    padding: 6px 8px;
    border-radius: 6px;
    border: 1px solid rgba(120, 160, 210, 0.35);
}

/* iPad and tablet responsive design */
@media (min-width: 768px) and (max-width: 1366px) {
    main {
        grid-template-columns: 380px 1fr;
        gap: 15px;
        padding: 15px 20px;
    }
    
    #control-panel {
        padding: 20px;
    }
    
    /* Touch-friendly controls for iPad */
    input[type="range"] {
        height: 8px;
        -webkit-appearance: none;
        appearance: none;
    }
    
    input[type="range"]::-webkit-slider-thumb {
        height: 22px;
        width: 22px;
        -webkit-appearance: none;
        appearance: none;
    }
    
    button {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 16px;
        border-radius: 8px;
    }
    
    select {
        min-height: 44px;
        font-size: 16px;
        padding: 10px 14px;
    }
    
    .control-group {
        margin-bottom: 18px;
    }
}

/* iPad portrait mode */
@media (max-width: 1024px) and (orientation: portrait) {
    main {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
        gap: 10px;
        padding: 10px 15px;
    }
    
    #control-panel {
        max-height: 45vh;
        overflow-y: auto;
        margin-bottom: 10px;
    }
    
    #simulation-container {
        height: 55vh;
        min-height: 400px;
    }
    
    /* Compact layout for portrait */
    .control-group {
        margin-bottom: 12px;
    }
    
    .control-row {
        display: flex;
        gap: 8px;
        align-items: center;
        flex-wrap: wrap;
    }
}

/* Touch device optimizations */
@media (pointer: coarse) {
    button, select, input[type="range"] {
        min-height: 44px;
        min-width: 44px;
    }
    
    .header-link {
        padding: 14px 18px;
        font-size: 15px;
    }
    
    /* Larger touch targets for sensor controls */
    .sensor-control {
        padding: 12px;
        margin: 8px 0;
    }
    
    /* Better spacing for touch */
    .control-group {
        margin-bottom: 20px;
    }
}

/* iPad landscape optimizations */
@media (min-width: 1024px) and (max-width: 1366px) and (orientation: landscape) {
    main {
        grid-template-columns: 420px 1fr;
    }
    
    #control-panel {
        max-height: calc(100vh - 120px);
        overflow-y: auto;
    }
}
