/* Reset und Basis-Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --success-color: #22c55e;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    --bg-color: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-color: #f1f5f9;
    --text-muted: #94a3b8;
    --border-color: #475569;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
    --radius: 8px;
    --touch-target: 44px;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.5;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* ==========================================
   Header - Mobile first (compact)
   ========================================== */
#header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.5rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
    gap: 0.25rem;
}

.header-left h1 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 110px;
}

.header-center {
    display: flex;
    align-items: center;
}

#timer {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.3rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    font-size: 1rem;
    font-family: 'Consolas', 'Courier New', monospace;
    font-weight: bold;
}

.timer-icon {
    font-size: 1rem;
}

.header-right {
    display: flex;
    gap: 0.35rem;
}

/* Hide button text on mobile, show only icon */
.btn-text {
    display: none;
}

/* ==========================================
   Buttons - Mobile first (touch-friendly)
   ========================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.6rem 0.85rem;
    font-size: 1rem;
    font-weight: 500;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: var(--touch-target);
}

.btn:hover {
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-help {
    background: var(--warning-color);
    color: #1a1a1a;
}

.btn-help:hover {
    background: #d97706;
}

.btn-reset {
    background: var(--bg-tertiary);
    color: var(--text-color);
    border: 1px solid var(--border-color);
}

.btn-reset:hover {
    background: var(--border-color);
}

.btn-large {
    padding: 0.75rem 1.5rem;
    font-size: 1.1rem;
    width: 100%;
    justify-content: center;
}

.btn-hint {
    background: var(--bg-tertiary);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    font-size: 1rem;
    margin-top: 0.5rem;
}

.btn-hint:hover {
    background: var(--warning-color);
    color: #1a1a1a;
}

.btn-hint.revealed {
    background: var(--success-color);
    color: white;
}

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-color);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--border-color);
}

.btn-secondary:disabled {
    opacity: 0.4;
}

/* ==========================================
   Main Layout - Mobile first (column)
   ========================================== */
#main {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

/* ==========================================
   Canvas Container
   ========================================== */
#canvas-container {
    flex: 1 1 58vh;
    min-height: 0;
    position: relative;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
}

#scene {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

#instructions-overlay {
    position: absolute;
    bottom: 0.75rem;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.85);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    font-size: 1.15rem;
    font-weight: 600;
    color: #ffffff;
    max-width: 95%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.3s ease;
}

/* ==========================================
   Sidebar / Bottom Panel - Mobile first
   ========================================== */
#sidebar {
    flex: 0 0 auto;
    max-height: 42vh;
    width: 100%;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Scroll hint handle - mobile only */
.scroll-hint {
    display: block;
    width: 40px;
    height: 4px;
    background: var(--border-color);
    border-radius: 2px;
    margin: 6px auto 2px;
    flex-shrink: 0;
}

.panel {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.panel h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-color);
}

.panel-description {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

/* ==========================================
   Cable Cores - Mobile first (4 columns)
   ========================================== */
#cable-cores {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.4rem;
}

#cable-cores .level3-instructions {
    grid-column: 1 / -1;
}

.cable-core {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border: 2px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 0.95rem;
    min-height: var(--touch-target);
}

.cable-core:hover:not(.used) {
    border-color: var(--primary-color);
    transform: scale(1.02);
}

.cable-core.selected {
    border-color: var(--primary-color);
    background: rgba(37, 99, 235, 0.2);
    box-shadow: 0 0 10px rgba(37, 99, 235, 0.5);
}

.cable-core.used {
    opacity: 0.4;
    cursor: not-allowed;
}

.cable-core .color-indicator {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid var(--border-color);
    flex-shrink: 0;
}

.cable-core .core-name {
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ==========================================
   Socket Selector & Status
   ========================================== */
#socket-selector {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.socket-btn {
    flex: 1;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border: 2px solid transparent;
    border-radius: var(--radius);
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 0.9rem;
    min-height: var(--touch-target);
}

.socket-btn:hover {
    border-color: var(--border-color);
}

.socket-btn.active {
    border-color: var(--primary-color);
    background: rgba(37, 99, 235, 0.2);
}

#socket-status {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.socket-status-item {
    display: flex;
    justify-content: space-between;
    padding: 0.4rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    font-size: 1rem;
}

.socket-status-item span:last-child {
    font-weight: 600;
    color: var(--primary-color);
}

.socket-status-item.active-cable {
    background: rgba(37, 99, 235, 0.2);
    border: 1px solid var(--primary-color);
}

.socket-status-item.completed-cable {
    background: rgba(34, 197, 94, 0.15);
    border: 1px solid var(--success-color);
}

.socket-status-item.completed-cable span:last-child {
    color: var(--success-color);
}

.info-note {
    background: rgba(37, 99, 235, 0.1);
    padding: 0.5rem;
    border-radius: var(--radius);
    border-left: 3px solid var(--primary-color);
    font-size: 0.9rem;
    margin: 0.5rem 0;
}

/* Action Panel */
#action-panel {
    margin-top: auto;
    background: var(--bg-color);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

#action-panel .btn {
    width: 100%;
}

/* ==========================================
   Modal - Mobile first (near full-screen)
   ========================================== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 0.5rem;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-secondary);
    border-radius: var(--radius);
    max-width: 100%;
    width: 100%;
    max-height: 95vh;
    overflow-y: auto;
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

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

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.2s;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    color: var(--text-color);
}

.modal-body {
    padding: 1rem;
}

/* Help Levels */
.help-levels {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.help-level {
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    padding: 0.75rem;
    border-left: 4px solid var(--warning-color);
}

.help-level h3 {
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
}

.help-content {
    padding: 0.75rem;
    background: var(--bg-color);
    border-radius: var(--radius);
    margin-top: 0.5rem;
    font-size: 0.85rem;
}

.help-content.hidden {
    display: none;
}

.help-content p {
    margin-bottom: 0.5rem;
}

.help-content ul, .help-content ol {
    margin-left: 1.5rem;
    margin-bottom: 0.5rem;
}

.help-content li {
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.mnemonic {
    font-size: 1rem;
    color: var(--warning-color);
    padding: 0.5rem;
    background: rgba(245, 158, 11, 0.1);
    border-radius: var(--radius);
    text-align: center;
}

.solution-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.solution-table th, .solution-table td {
    padding: 0.4rem;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.solution-table th {
    background: var(--bg-secondary);
    font-weight: 600;
}

.solution-table td {
    display: table-cell;
}

.color-dot {
    display: inline-block;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    vertical-align: middle;
}

/* Start Modal */
.start-info {
    text-align: center;
    margin-bottom: 1rem;
}

.start-info h3 {
    margin: 0.75rem 0 0.5rem;
    text-align: left;
}

.start-info ol {
    text-align: left;
    margin-left: 1.5rem;
    margin-bottom: 1rem;
}

.start-info li {
    margin-bottom: 0.5rem;
}

.start-info .tip {
    background: rgba(245, 158, 11, 0.1);
    padding: 0.75rem;
    border-radius: var(--radius);
    border-left: 4px solid var(--warning-color);
    text-align: left;
}

/* Result Modal */
#result-icon {
    font-size: 3rem;
    text-align: center;
    margin-bottom: 0.75rem;
}

#result-score {
    font-size: 1.5rem;
    font-weight: bold;
    text-align: center;
    margin-bottom: 0.5rem;
}

#result-time {
    font-size: 1rem;
    text-align: center;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

#result-details {
    text-align: center;
    margin-bottom: 0.75rem;
    padding: 0.75rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
}

#result-errors {
    margin-bottom: 0.75rem;
    padding: 0.75rem;
    background: rgba(239, 68, 68, 0.1);
    border-radius: var(--radius);
    border-left: 4px solid var(--error-color);
}

#result-errors h4 {
    color: var(--error-color);
    margin-bottom: 0.5rem;
}

#result-errors ul {
    margin-left: 1.5rem;
    font-size: 0.85rem;
}

#result-retry {
    width: 100%;
}

/* Score Colors */
.score-perfect {
    color: var(--success-color);
}

.score-good {
    color: #84cc16;
}

.score-medium {
    color: var(--warning-color);
}

.score-poor {
    color: var(--error-color);
}

/* Animations */
@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

.pulse {
    animation: pulse 0.5s ease;
}

.shake {
    animation: shake 0.3s ease;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: var(--bg-color);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--border-color);
}

/* ==========================================
   Level Select Modal
   ========================================== */
.level-select-content {
    max-width: 100%;
}

.level-intro {
    text-align: center;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

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

.level-btn {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    background: var(--bg-tertiary);
    border: 2px solid var(--border-color);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    color: var(--text-color);
    min-height: 60px;
    text-align: left;
}

.level-btn:hover:not(.locked) {
    border-color: var(--primary-color);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.level-btn:active:not(.locked) {
    transform: scale(0.98);
}

.level-btn.locked {
    opacity: 0.5;
    cursor: not-allowed;
}

.level-btn .level-icon {
    font-size: 2rem;
    flex-shrink: 0;
}

.level-btn .level-info h3 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0.15rem;
}

.level-btn .level-info p {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.level-btn .level-desc {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.level-btn .lock-overlay {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    font-size: 1.25rem;
}

.level-hint {
    text-align: center;
    font-size: 0.85rem;
    color: var(--warning-color);
    background: rgba(245, 158, 11, 0.1);
    padding: 0.5rem;
    border-radius: var(--radius);
}

/* ==========================================
   Level 1 Actions
   ========================================== */
.level1-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.level1-actions .btn {
    width: 100%;
    justify-content: center;
}

.level1-actions .btn.active {
    background: var(--success-color);
    border-color: var(--success-color);
}

/* ==========================================
   Level 3 Styles
   ========================================== */
.level3-instructions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.connection-guide {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    margin: 0.25rem 0;
}

.port-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.4rem 0.6rem;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border-color);
    min-width: 70px;
}

.port-indicator .port-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.port-indicator .port-value {
    font-size: 1.05rem;
    font-weight: bold;
    color: var(--text-color);
}

.port-indicator .port-value.selected {
    color: var(--primary-color);
}

.connection-guide .arrow {
    font-size: 1.25rem;
    color: var(--success-color);
}

.hint-text {
    font-size: 0.9rem;
    color: var(--text-muted);
    text-align: center;
    font-style: italic;
}

.timer-display {
    font-family: 'Consolas', 'Courier New', monospace;
}

#level3-timer {
    font-weight: bold;
    font-size: 1rem;
}

#level3-timer.warning {
    color: var(--warning-color);
    animation: pulse-warning 1s infinite;
}

#level3-timer.critical {
    color: var(--error-color);
    animation: pulse-critical 0.5s infinite;
}

@keyframes pulse-warning {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

@keyframes pulse-critical {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.8; transform: scale(1.05); }
}

.socket-status-item.reference {
    background: rgba(34, 197, 94, 0.1);
    border: 1px solid rgba(34, 197, 94, 0.3);
    border-radius: var(--radius);
    padding: 0.5rem;
    font-size: 0.8rem;
}

/* Level 3 Switch Assignment Styles */
.switch-assignment {
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    padding: 0.5rem;
    margin: 0.25rem 0;
}

.assignment-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.3rem 0;
    font-size: 1rem;
}

.assignment-row:first-child {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.4rem;
    margin-bottom: 0.2rem;
}

.port-range {
    font-weight: bold;
    color: var(--text-color);
}

.switch-name {
    font-weight: bold;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
}

.switch-name.switch1 {
    background: rgba(68, 136, 204, 0.25);
    color: #6db3f2;
}

.switch-name.switch2 {
    background: rgba(153, 85, 204, 0.25);
    color: #b86df2;
}

.switch1-status {
    border-left: 3px solid #4488cc;
}

.switch2-status {
    border-left: 3px solid #9955cc;
}

.level3-progress-grid {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.progress-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.3rem 0.5rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    font-size: 1rem;
}

.progress-label {
    flex-shrink: 0;
    width: 60px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.progress-bar-container {
    flex: 1;
    height: 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    background: var(--success-color);
    border-radius: 4px;
    transition: width 0.3s ease;
}

.progress-bar.switch1-bar {
    background: #4488cc;
}

.progress-bar.switch2-bar {
    background: #9955cc;
}

.progress-count {
    flex-shrink: 0;
    font-weight: bold;
    font-size: 0.9rem;
    min-width: 32px;
    text-align: right;
}

.connection-arrow {
    font-size: 1.25rem;
    color: var(--success-color);
}

.socket-status-item.completed {
    background: rgba(34, 197, 94, 0.15);
}

.socket-status-item.completed span:last-child {
    color: var(--success-color);
    font-weight: bold;
}

/* ==========================================
   Landscape Hint Overlay
   ========================================== */
.landscape-hint {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
    z-index: 2000;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-color);
    font-size: 1.1rem;
    gap: 1rem;
    text-align: center;
    padding: 2rem;
}

.landscape-hint.visible {
    display: flex;
}

.landscape-hint .rotate-icon {
    font-size: 3rem;
    animation: rotate-hint 2s ease-in-out infinite;
}

@keyframes rotate-hint {
    0%, 100% { transform: rotate(0deg); }
    50% { transform: rotate(90deg); }
}

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

/* ==========================================
   DESKTOP OVERRIDES - structural only
   ========================================== */
@media (min-width: 769px) {
    #header {
        padding: 0.75rem 1.5rem;
    }

    .header-left h1 {
        font-size: 1.25rem;
        max-width: none;
    }

    .btn-text {
        display: inline;
    }

    /* Main layout - horizontal on desktop */
    #main {
        flex-direction: row;
    }

    #canvas-container {
        flex: 1;
    }

    #sidebar {
        width: 320px;
        max-height: none;
        border-top: none;
        border-left: 1px solid var(--border-color);
    }

    .scroll-hint {
        display: none;
    }

    #cable-cores {
        grid-template-columns: repeat(2, 1fr);
    }

    .modal-content {
        max-width: 600px;
    }

    .level-select-content {
        max-width: 700px;
    }

    .level-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .level-btn {
        flex-direction: column;
        text-align: center;
    }

    .level-btn .level-icon {
        margin-bottom: 0.75rem;
    }

    .level-btn:hover:not(.locked) {
        transform: translateY(-3px);
    }
}

/* ============================================
   Level 4: Ping Terminal & Connection Status
   ============================================ */

.ping-terminal {
    background: #0c0c0c;
    color: #00ff41;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.75rem;
    padding: 0.75rem;
    border-radius: var(--radius);
    white-space: pre-wrap;
    line-height: 1.5;
    max-height: 200px;
    overflow-y: auto;
    margin-top: 0.5rem;
    border: 1px solid #333;
}

.ping-terminal .prompt {
    color: #cccccc;
}

.ping-terminal .reply {
    color: #00ff41;
}

.ping-terminal .stats {
    color: #ffaa00;
}

.ping-terminal .info {
    color: #88aaff;
}

.connection-status-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin: 0.75rem 0;
}

.conn-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    font-size: 0.9rem;
}

.conn-item.done {
    background: rgba(34, 197, 94, 0.15);
    border-left: 3px solid var(--success-color);
}

.conn-icon {
    font-size: 1.1rem;
}
