:root {
    --bg-color: #020617;
    --panel-bg: rgba(15, 23, 42, 0.7);
    --primary: #38bdf8;
    --primary-light: #7dd3fc;
    --secondary: #475569;
    --accent-red: #f43f5e;
    --accent-green: #10b981;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --border: rgba(56, 189, 248, 0.2);
    --shaft-bg: #0f172a;
    --elevator-bg: #334155;
    --shadow: 0 0 20px rgba(56, 189, 248, 0.1);
    --glow-blue: 0 0 15px rgba(56, 189, 248, 0.4);
    --glow-red: 0 0 15px rgba(244, 63, 94, 0.4);
    --glow-gold: 0 0 15px rgba(251, 191, 36, 0.4);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Outfit', sans-serif;
}

body {
    background-color: var(--bg-color);
    background-image: 
        radial-gradient(circle at 50% 50%, #1e293b 0%, #020617 100%),
        linear-gradient(rgba(56, 189, 248, 0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(56, 189, 248, 0.03) 1px, transparent 1px);
    background-size: 100% 100%, 30px 30px, 30px 30px;
    color: var(--text-main);
    overflow-x: hidden;
    overflow-y: auto;
    min-height: 100vh;
}

.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    padding: 1rem;
    gap: 1rem;
}

/* Header */
header {
    background: var(--panel-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    padding: 1rem 2rem;
    border-radius: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow);
    border-left: 4px solid var(--primary);
}

.badge {
    display: inline-block;
    background: rgba(56, 189, 248, 0.1);
    color: var(--primary);
    font-size: 0.7rem;
    font-weight: 700;
    padding: 0.3rem 1rem;
    border-radius: 2rem;
    margin-bottom: 0.5rem;
    letter-spacing: 0.1em;
    border: 1px solid var(--border);
    text-shadow: var(--glow-blue);
}

h1 {
    font-size: 1.8rem;
    font-weight: 700;
    background: linear-gradient(135deg, #fff 0%, var(--primary) 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.02em;
}

.subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.status-bar {
    display: flex;
    gap: 1rem;
}

.status-item {
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 0.6rem 1.2rem;
    background: rgba(15, 23, 42, 0.5);
    border-radius: 0.75rem;
    border: 1px solid var(--border);
    box-shadow: inset 0 0 10px rgba(0,0,0,0.2);
}

#system-status.danger {
    color: var(--accent-red);
}

#system-status.success {
    color: var(--accent-green);
    text-shadow: 0 0 10px rgba(16, 185, 129, 0.4);
}

.editable-mass {
    display: flex;
    align-items: center;
    gap: 5px;
    border: 1px solid var(--primary) !important;
    background: rgba(56, 189, 248, 0.1) !important;
}

#input-mass {
    background: transparent;
    border: none;
    border-bottom: 1px solid var(--primary);
    color: var(--primary);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    width: 60px;
    text-align: center;
    outline: none;
}

/* Remove arrows from number input */
#input-mass::-webkit-outer-spin-button,
#input-mass::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Main Layout */
main {
    flex: 1;
    display: grid;
    grid-template-columns: 320px 1fr 340px;
    gap: 1rem;
    overflow: hidden;
}

.panel {
    background: var(--panel-bg);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    border-radius: 1rem;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    box-shadow: var(--shadow);
    overflow-y: auto;
}

.panel-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 1rem;
    position: relative;
}

.panel-header::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 0;
    width: 60px;
    height: 3px;
    background: var(--primary);
    box-shadow: var(--glow-blue);
}

.panel-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

/* Component Tags */
.component-group h3 {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.75rem;
}

.tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    list-style: none;
}

.tag {
    background: rgba(56, 189, 248, 0.05);
    color: var(--primary-light);
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.8rem;
    font-weight: 600;
    border: 1px solid var(--border);
    cursor: help;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
}

.tag:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%);
    background: #1e293b;
    color: white;
    padding: 0.5rem 0.75rem;
    border-radius: 0.4rem;
    font-size: 0.75rem;
    width: 150px;
    text-align: center;
    z-index: 100;
    pointer-events: none;
    box-shadow: var(--shadow);
}

.tag:hover {
    background: rgba(56, 189, 248, 0.15);
    border-color: var(--primary);
    box-shadow: var(--glow-blue);
    transform: translateY(-3px) scale(1.02);
}

.tag.factor {
    background: rgba(244, 63, 94, 0.05);
    color: var(--accent-red);
    border-color: rgba(244, 63, 94, 0.2);
}

.tag.factor:hover {
    background: rgba(244, 63, 94, 0.15);
    border-color: var(--accent-red);
    box-shadow: var(--glow-red);
}

.info-box {
    background: #fffbeb;
    border: 1px solid #fef3c7;
    padding: 1rem;
    border-radius: 0.5rem;
    font-size: 0.85rem;
    color: #92400e;
}

/* Simulation Viewport */
.simulation-container {
    background: #1e293b;
    border-radius: 1rem;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.simulation-view {
    flex: 1;
    position: relative;
    padding: 2rem;
}

.shaft {
    position: absolute;
    top: 50px;
    bottom: 50px;
    left: 100px;
    right: 100px;
    background: rgba(15, 23, 42, 0.5);
    border: 2px solid #334155;
    border-radius: 4px;
}

.guide-rail {
    position: absolute;
    width: 4px;
    height: 100%;
    background: #475569;
}

.guide-rail.left { left: 39%; margin-left: -5px; }
.guide-rail.right { left: 41%; margin-left: 1px; }

.pulley-system {
    position: absolute;
    top: -25px;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
}

.pulley {
    width: 60px;
    height: 60px;
    border: 6px solid #64748b;
    border-radius: 50%;
    background: #94a3b8;
    position: relative;
}

.pulley::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 12px;
    height: 12px;
    background: #1e293b;
    border-radius: 50%;
    transform: translate(-50%, -50%);
}

.cables-svg {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

.elevator-car {
    position: absolute;
    width: 80px;
    height: 100px;
    left: 40%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #475569 0%, #1e293b 100%);
    border: 1px solid var(--primary);
    border-radius: 4px;
    transition: top 0.1s linear;
    box-shadow: var(--glow-blue);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.car-body {
    width: 100%;
    height: 100%;
    position: relative;
}

.car-window {
    width: 50px;
    height: 40px;
    background: rgba(56, 189, 248, 0.1);
    border: 1px solid var(--primary);
    position: relative;
    overflow: hidden;
}

.car-window::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(56, 189, 248, 0.2), transparent);
    animation: sweep 3s infinite;
}

@keyframes sweep {
    0% { left: -100%; }
    100% { left: 100%; }
}

.car-label {
    position: absolute;
    bottom: -25px;
    left: 0;
    width: 100%;
    text-align: center;
    color: #94a3b8;
    font-size: 0.7rem;
    font-family: 'JetBrains Mono';
}

.counterweight {
    position: absolute;
    width: 40px;
    height: 60px;
    left: 60%;
    transform: translateX(-50%);
    background: #475569;
    border: 2px solid #334155;
    border-radius: 2px;
}

.buffer {
    position: absolute;
    bottom: 0;
    width: 60px;
    height: 20px;
    background: #ef4444;
    border-radius: 4px 4px 0 0;
}

.car-buffer { left: 40%; transform: translateX(-50%); }
.cw-buffer { left: 60%; transform: translateX(-50%); }

/* Controls */
.controls {
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(4px);
    padding: 1.5rem;
    display: flex;
    justify-content: center;
    gap: 1rem;
    border-top: 1px solid #334155;
}

.control-btn {
    padding: 0.75rem 2rem;
    border-radius: 0.5rem;
    border: none;
    font-weight: 700;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--primary);
    color: white;
}

.control-btn:hover { background: var(--primary-light); transform: scale(1.05); }
.control-btn.secondary { background: #334155; }
.control-btn:active { transform: scale(0.95); }

/* Charts/Telemetry */
.telemetry-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.metric {
    background: #f8fafc;
    border: 1px solid var(--border);
    padding: 1rem;
    border-radius: 0.75rem;
}

.metric label {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.metric .value {
    font-family: 'JetBrains Mono', monospace;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
}

.energy-bar {
    height: 12px;
    background: rgba(15, 23, 42, 0.8);
    border-radius: 6px;
    overflow: hidden;
    display: flex;
    margin-top: 0.5rem;
    border: 1px solid var(--border);
}

.ke-bar { 
    background: var(--accent-red); 
    box-shadow: var(--glow-red);
    transition: width 0.1s; 
}

.pe-bar { 
    background: var(--primary); 
    box-shadow: var(--glow-blue);
    transition: width 0.1s; 
}

.flow-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--text-muted);
}

/* Impact Section */
.impact-section {
    background: #fff1f2;
    border: 2px dashed #fecaca;
    padding: 1rem;
    border-radius: 0.75rem;
}

.incident-btns {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.incident-btn {
    flex: 1;
    min-width: 80px;
    background: white;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 0.5rem;
    font-size: 0.75rem;
    font-weight: 600;
    border-radius: 0.4rem;
    cursor: pointer;
    transition: all 0.2s;
}

.incident-btn:hover {
    background: #ef4444;
    color: white;
}

.solution-section {
    background: #f0fdf4;
    border: 1px solid #dcfce7;
    padding: 1rem;
    border-radius: 0.75rem;
}

.solution-btns {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.solution-btn {
    background: white;
    border: 1px solid #bbf7d0;
    color: #166534;
    padding: 0.6rem;
    font-size: 0.8rem;
    font-weight: 600;
    border-radius: 0.4rem;
    cursor: pointer;
}

.solution-btn:hover {
    background: #22c55e;
    color: white;
}

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

.pulse { animation: pulse 1.5s infinite; }

.pulse-border {
    animation: borderPulse 2s infinite;
}

@keyframes borderPulse {
    0% { border-color: #fecaca; }
    50% { border-color: #ef4444; }
    100% { border-color: #fecaca; }
}

/* Overlay */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.9);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

#analysis-overlay {
    z-index: 1200;
}

#guide-overlay {
    z-index: 1100;
}

.modal {
    background: #0f172a;
    padding: 2.5rem;
    border-radius: 1.5rem;
    max-width: 500px;
    width: 90%;
    border: 1px solid var(--primary);
    box-shadow: var(--glow-blue);
    color: white;
    position: relative;
    z-index: 1300;
}
.modal h2 { color: #991b1b; margin-bottom: 1rem; }
.modal-body { margin-bottom: 2rem; line-height: 1.6; }

/* Energy Arrows */
.energy-arrows-container {
    position: absolute;
    top: 50%;
    left: 40%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    gap: 40px;
    pointer-events: none;
    z-index: 10;
}

.arrow {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    opacity: 0.8;
}

.arrow .line {
    width: 4px;
    height: 60px;
    background: white;
    border-radius: 2px;
}

.arrow .head {
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
}

.pe-arrow .line, .pe-arrow .head { 
    background: var(--primary); 
    border-bottom: 12px solid var(--primary); 
}
.ke-arrow .line, .ke-arrow .head { 
    background: var(--accent-red); 
    border-top: 12px solid var(--accent-red); 
}

.pe-arrow { animation: floatUp 2s infinite ease-in-out; }
.ke-arrow { animation: floatDown 2s infinite ease-in-out; }

@keyframes floatUp {
    0%, 100% { transform: translateY(0); opacity: 0.4; }
    50% { transform: translateY(-20px); opacity: 1; }
}

@keyframes floatDown {
    0%, 100% { transform: translateY(0); opacity: 0.4; }
    50% { transform: translateY(20px); opacity: 1; }
}

.arrow label {
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    color: white;
}

.visualize-toggle {
    margin-left: auto;
    background: rgba(56, 189, 248, 0.1);
    border: 1px solid var(--primary);
    color: var(--primary);
    padding: 0.3rem 0.6rem;
    border-radius: 0.4rem;
    font-size: 0.7rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.visualize-toggle:hover {
    background: var(--primary);
    color: #0f172a;
}

.interactive-header {
    cursor: pointer;
}

.interactive-header:hover h2 {
    color: var(--primary);
}

.close-btn {
    width: 100%;
    padding: 1rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 0.75rem;
    cursor: pointer;
    font-weight: 700;
}

/* Guide Modal */
.guide-modal {
    max-width: 600px;
}

.guide-header {
    margin-bottom: 2rem;
    text-align: center;
}

.guide-body {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    margin-bottom: 2.5rem;
}

.step {
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
}

.step-num {
    background: var(--primary);
    color: #0f172a;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
    box-shadow: var(--glow-blue);
}

.highlight {
    color: var(--primary);
    font-weight: 600;
}

.floating-btn {
    position: fixed;
    right: 2rem;
    top: 2rem;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--panel-bg);
    border: 1px solid var(--primary);
    color: var(--primary);
    font-size: 1.25rem;
    font-weight: 700;
    cursor: pointer;
    z-index: 100;
    transition: all 0.3s;
}

.floating-btn:hover {
    box-shadow: var(--glow-blue);
    transform: scale(1.1);
}

.glow-btn {
    background: var(--primary);
    color: #0f172a;
    text-shadow: none;
    transition: all 0.3s;
}

.glow-btn:hover {
    box-shadow: 0 0 30px var(--primary);
    transform: translateY(-2px);
}

.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    padding: 1rem;
    gap: 1rem;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
}

.hidden {
    display: none !important;
}

.game-footer {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding-top: 1rem;
    border-top: 1px solid var(--border);
    margin-top: auto;
}

/* Responsive Design */
@media (max-width: 1024px) {
    main {
        grid-template-columns: 1fr;
        overflow-y: visible;
        height: auto;
    }

    body {
        overflow-y: auto;
    }

    .app-container {
        height: auto;
    }

    .panel {
        height: auto;
        overflow-y: visible;
    }

    .simulation-container {
        order: -1; /* Keep simulation at the top of controls on mid-screens */
        min-height: 500px;
    }
}

@media (max-width: 768px) {
    header {
        flex-direction: column;
        text-align: center;
        gap: 1.5rem;
        padding: 1.5rem 1rem;
        border-right: 4px solid var(--primary); /* Balanced look */
    }

    .header-content h1 {
        font-size: 1.5rem;
        line-height: 1.2;
    }

    .status-bar {
        display: grid;
        grid-template-columns: 1fr;
        gap: 0.5rem;
        width: 100%;
    }

    .status-item {
        width: 100%;
        justify-content: center;
        padding: 0.8rem;
    }

    .simulation-view {
        padding: 1rem 0;
        min-height: 400px;
    }

    .shaft {
        left: 10px;
        right: 10px;
        top: 30px;
        bottom: 30px;
    }

    .elevator-car {
        width: 60px;
        height: 80px;
    }

    .car-window {
        width: 35px;
        height: 25px;
    }

    .tag-list {
        justify-content: center;
    }

    .telemetry-grid {
        grid-template-columns: 1fr 1fr; /* Keep 2 cols on mobile for compact stats */
        gap: 0.75rem;
    }

    .metric {
        padding: 0.75rem;
    }

    .metric .value {
        font-size: 1.1rem;
    }

    .floating-btn {
        right: 1rem;
        top: 1rem;
        width: 36px;
        height: 36px;
    }

    .modal {
        padding: 1.5rem;
    }

    .guide-modal h2 {
        font-size: 1.2rem;
    }

    .step {
        gap: 1rem;
    }

    .game-footer {
        flex-direction: column;
        align-items: center;
        gap: 0.5rem;
        text-align: center;
    }
}

@media (max-width: 480px) {
    .header-content h1 {
        font-size: 1.25rem;
    }

    .visualize-toggle {
        width: 100%;
        margin-top: 0.5rem;
        margin-left: 0;
        text-align: center;
    }

    .interactive-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .panel {
        padding: 1rem;
    }

    .control-btn {
        padding: 0.75rem 1rem;
        font-size: 0.8rem;
        flex: 1;
    }

    .status-item {
        font-size: 0.75rem;
    }

    #input-mass {
        width: 50px;
    }
}
