/* ==========================================================================
   Physics 471 - Chapter 1 Interactive 3D Crystal Structure Lab
   University of Jordan | Solid State Physics (Kittel Ch. 1)
   ========================================================================== */

:root {
    --bg-main: #0b1120;
    --bg-panel: #111827;
    --bg-card: #1e293b;
    --bg-card-hover: #263449;
    --bg-input: #0f172a;
    --border-color: #334155;
    --border-highlight: #3b82f6;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --accent-blue: #3b82f6;
    --accent-cyan: #06b6d4;
    --accent-amber: #f59e0b;
    --accent-emerald: #10b981;
    --accent-purple: #8b5cf6;
    --accent-rose: #f43f5e;
    --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    height: 100%;
    background-color: var(--bg-main);
    color: var(--text-primary);
    font-family: 'Segoe UI', Tahoma, 'Cairo', Arial, sans-serif;
    direction: rtl;
    text-align: right;
    overflow-x: hidden;
    line-height: 1.6;
}

/* Header height varies (tabs wrap), so the workstation takes whatever height is left */
body {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

bdi {
    unicode-bidi: isolate;
}

/* Vector notation: arrow drawn by CSS above the letter (the combining character U+20D7
   is misplaced or missing in most fonts, which showed up as stray symbols) */
.vec {
    display: inline-block;
    position: relative;
    font-style: normal;
    padding-top: 0.32em;
    line-height: 1;
}
.vec::after {
    content: '\2192';
    position: absolute;
    left: 50%;
    top: -0.05em;
    transform: translateX(-50%) scaleX(1.15);
    font-size: 0.72em;
    line-height: 1;
    font-weight: 700;
    font-family: 'Segoe UI Symbol', 'Cambria Math', 'Segoe UI', sans-serif;
    pointer-events: none;
}

/* A vector and its subscript (a₁) never split across lines */
.nw {
    white-space: nowrap;
}

/* Negative Miller index: overline (bar) above the digit */
.bar {
    text-decoration: overline;
    text-decoration-thickness: 1.5px;
}

sub {
    font-size: 0.72em;
    line-height: 0;
    vertical-align: -0.25em;
}

/* Header & Navigation Tabs */
.app-header {
    background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 60%, #172554 100%);
    border-bottom: 2px solid var(--accent-amber);
    padding: 10px 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    box-shadow: var(--shadow-lg);
    position: sticky;
    top: 0;
    z-index: 100;
    flex: 0 0 auto;
}

.header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
}

.brand-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-badge {
    background: linear-gradient(135deg, var(--accent-amber), #d97706);
    color: #0f172a;
    font-weight: 900;
    font-size: 14px;
    padding: 6px 12px;
    border-radius: 8px;
    letter-spacing: 0.5px;
    box-shadow: 0 2px 8px rgba(245, 158, 11, 0.35);
}

.brand-title {
    font-size: 19px;
    font-weight: 800;
    color: #ffffff;
}

.brand-subtitle {
    font-size: 12.5px;
    color: #fde68a;
    font-weight: 600;
}

.header-meta {
    font-size: 12px;
    color: #e2e8f0;
    background: rgba(15, 23, 42, 0.55);
    padding: 5px 12px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.14);
}

/* Module Tabs */
.nav-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    padding-bottom: 2px;
}

.nav-tab {
    background: rgba(15, 23, 42, 0.65);
    color: var(--text-secondary);
    border: 1px solid rgba(148, 163, 184, 0.25);
    padding: 5px 10px;
    border-radius: 7px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 5px;
    font-family: inherit;
}

.nav-tab:hover {
    background: rgba(59, 130, 246, 0.22);
    color: #ffffff;
    border-color: var(--accent-blue);
}

.nav-tab.active {
    background: linear-gradient(135deg, var(--accent-blue), #1d4ed8);
    color: #ffffff;
    border-color: #93c5fd;
    box-shadow: 0 0 12px rgba(59, 130, 246, 0.45);
}

.tab-num {
    background: rgba(255, 255, 255, 0.18);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 11px;
}

/* Main Workstation Grid */
.workstation {
    display: grid;
    grid-template-columns: 440px 1fr;
    flex: 1 1 auto;
    min-height: 480px;
}

/* Right Side: Controls & Explanation Panel (First in RTL grid) */
.side-panel {
    background: var(--bg-panel);
    border-left: 1px solid var(--border-color);
    padding: 16px;
    overflow-y: auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.panel-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 14px 16px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
    margin-bottom: 11px;
}

.card-title {
    font-size: 15px;
    font-weight: 800;
    color: #93c5fd;
    display: block; /* not flex: mixed Arabic/Latin titles must flow as one line of text */
}

.slide-ref {
    font-size: 11px;
    background: rgba(245, 158, 11, 0.16);
    color: #fcd34d;
    border: 1px solid rgba(245, 158, 11, 0.35);
    padding: 2px 7px;
    border-radius: 5px;
    font-weight: 700;
    direction: ltr;
    white-space: nowrap;
    flex-shrink: 0;
}

.card-header {
    gap: 8px;
}

/* Controls UI */
.control-group {
    margin-bottom: 11px;
}

.control-group:last-child {
    margin-bottom: 0;
}

.control-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12.8px;
    font-weight: 700;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.control-val {
    font-family: 'Cambria Math', 'Consolas', monospace;
    direction: ltr;
    unicode-bidi: isolate;
    background: var(--bg-input);
    color: #38bdf8;
    padding: 1px 7px;
    border-radius: 4px;
    font-size: 12.5px;
    border: 1px solid #1e3a8a;
    white-space: nowrap;
}

.btn-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(105px, 1fr));
    gap: 6px;
}

.btn-grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
}

.btn-grid-4 {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.ctrl-btn {
    background: var(--bg-input);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
    padding: 7px 9px;
    border-radius: 7px;
    font-size: 12.2px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: inherit;
    text-align: center;
}

.ctrl-btn:hover {
    background: #1e3a8a;
    color: #ffffff;
    border-color: #60a5fa;
}

.ctrl-btn.active {
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    color: #ffffff;
    border-color: #93c5fd;
    box-shadow: 0 0 8px rgba(37, 99, 235, 0.4);
}

.ctrl-btn.amber.active {
    background: linear-gradient(135deg, #d97706, #b45309);
    border-color: #fde68a;
}

.ctrl-btn.emerald.active {
    background: linear-gradient(135deg, #059669, #047857);
    border-color: #6ee7b7;
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent-blue);
    cursor: pointer;
    height: 6px;
    background: var(--bg-input);
    border-radius: 4px;
}

select.ctrl-select {
    width: 100%;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 7px 10px;
    border-radius: 7px;
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
}

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border-color);
    padding: 7px 11px;
    border-radius: 7px;
    margin-bottom: 6px;
    cursor: pointer;
    font-size: 12.6px;
    font-weight: 600;
}

.toggle-row:hover {
    border-color: #60a5fa;
}

.toggle-row input[type="checkbox"] {
    width: 17px;
    height: 17px;
    accent-color: var(--accent-emerald);
    cursor: pointer;
}

/* Metrics Grid */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-top: 6px;
}

.metric-box {
    background: rgba(15, 23, 42, 0.75);
    border: 1px solid var(--border-color);
    border-right: 3px solid var(--accent-cyan);
    border-radius: 7px;
    padding: 7px 10px;
}

.metric-label {
    font-size: 11.2px;
    color: var(--text-muted);
    font-weight: 600;
}

.metric-val {
    font-size: 14px;
    font-weight: 800;
    color: #38bdf8;
    font-family: 'Cambria Math', 'Consolas', serif;
    direction: ltr;
    unicode-bidi: isolate;
    text-align: left;
    margin-top: 2px;
}

/* Math & Theory Cards */
.math-inline {
    direction: ltr;
    unicode-bidi: isolate;
    display: inline-block;
    font-family: 'Cambria Math', 'Times New Roman', serif;
    font-weight: 700;
    color: #fde68a;
    background: rgba(15, 23, 42, 0.75);
    padding: 0 6px;
    border-radius: 4px;
    border: 1px solid rgba(253, 230, 138, 0.2);
    font-size: 13.5px;
}

.formula-box {
    direction: ltr;
    unicode-bidi: isolate;
    text-align: center;
    font-family: 'Cambria Math', 'Times New Roman', serif;
    font-size: 14.8px;
    font-weight: 700;
    background: #0f172a;
    color: #f8fafc;
    border: 1px solid #334155;
    border-left: 4px solid var(--accent-amber);
    border-radius: 7px;
    padding: 9px 12px;
    margin: 8px 0;
    overflow-x: auto;
}

.theory-text {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.7;
}

.theory-text ul {
    padding-right: 18px;
    margin: 6px 0;
}

.theory-text li {
    margin-bottom: 5px;
}

.highlight-note {
    background: rgba(16, 185, 129, 0.12);
    border: 1px solid rgba(16, 185, 129, 0.35);
    border-right: 4px solid var(--accent-emerald);
    padding: 8px 11px;
    border-radius: 7px;
    font-size: 12.5px;
    color: #d1fae5;
    margin-top: 8px;
}

.warn-note {
    background: rgba(244, 63, 94, 0.12);
    border: 1px solid rgba(244, 63, 94, 0.35);
    border-right: 4px solid var(--accent-rose);
    padding: 8px 11px;
    border-radius: 7px;
    font-size: 12.5px;
    color: #ffe4e6;
    margin-top: 8px;
}

/* 3D Viewport Area */
.viewport-area {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;
    background: radial-gradient(circle at center, #1e293b 0%, #090d16 100%);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    container-type: size;
    container-name: viewport;
}

#canvas3d-container {
    width: 100%;
    height: 100%;
    position: relative;
}

#canvas3d-container canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
    outline: none;
}

/* Floating 3D Labels Overlay */
#labels-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    overflow: hidden;
    z-index: 10;
}

.label-3d {
    position: absolute;
    transform: translate(-50%, -50%);
    font-family: 'Segoe UI', Tahoma, 'Cambria Math', sans-serif;
    font-size: 12.2px;
    font-weight: 800;
    padding: 2px 8px;
    border-radius: 5px;
    background: rgba(15, 23, 42, 0.88);
    color: #ffffff;
    border: 1px solid rgba(148, 163, 184, 0.45);
    white-space: nowrap;
    direction: rtl;
    unicode-bidi: isolate;
    box-shadow: 0 2px 6px rgba(0,0,0,0.45);
}

/* Viewport Top HUD */
.viewport-hud-top {
    position: absolute;
    top: 14px;
    right: 14px;
    left: 14px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    pointer-events: none;
    z-index: 20;
    gap: 10px;
    flex-wrap: wrap;
}

.hud-badge {
    background: rgba(15, 23, 42, 0.82);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(59, 130, 246, 0.45);
    border-right: 4px solid var(--accent-blue);
    padding: 8px 14px;
    border-radius: 9px;
    pointer-events: auto;
    max-width: 480px;
}

.hud-title {
    font-size: 15px;
    font-weight: 800;
    color: #ffffff;
}

.hud-sub {
    font-size: 12px;
    color: #93c5fd;
    margin-top: 2px;
}

.hud-legend {
    background: rgba(15, 23, 42, 0.82);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border-color);
    padding: 7px 12px;
    border-radius: 9px;
    font-size: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    pointer-events: auto;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 7px;
    color: var(--text-secondary);
    font-weight: 600;
}

.legend-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    display: inline-block;
    border: 1px solid rgba(255,255,255,0.5);
}

/* 2D Overlay Canvas (for 2D Cross-Section / Planar Density / 5-fold Tiling) */
.overlay-2d-box {
    position: absolute;
    bottom: 14px;
    left: 14px;
    background: rgba(15, 23, 42, 0.92);
    backdrop-filter: blur(8px);
    border: 1.5px solid #3b82f6;
    border-radius: 10px;
    padding: 10px;
    z-index: 25;
    box-shadow: var(--shadow-lg);
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.overlay-2d-box.visible {
    display: flex;
}

.overlay-2d-title {
    font-size: 12.2px;
    font-weight: 800;
    color: #fde68a;
}

#canvas2d-aux {
    background: #090d16;
    border: 1px solid #334155;
    border-radius: 6px;
}

/* Bottom Camera Toolbar */
/* Sits in the bottom-right corner; the 2D overlay owns the bottom-left corner */
.camera-toolbar {
    position: absolute;
    bottom: 14px;
    right: 14px;
    background: rgba(15, 23, 42, 0.86);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border-color);
    padding: 6px 8px;
    border-radius: 10px;
    display: flex;
    gap: 5px;
    z-index: 20;
    box-shadow: var(--shadow-lg);
    flex-wrap: wrap;
    justify-content: center;
    max-width: calc(100% - 28px);
}

/* Narrow 3D viewport: lift the 2D overlay above the toolbar instead of overlapping it */
@container viewport (max-width: 820px) {
    .overlay-2d-box {
        bottom: 66px;
    }
}

@container viewport (max-width: 560px) {
    /* One compact row of camera buttons centred at the bottom */
    .camera-toolbar {
        right: 50%;
        transform: translateX(50%);
        width: max-content;
        flex-wrap: nowrap;
        gap: 3px;
        padding: 4px 5px;
        bottom: 10px;
    }
    .cam-btn {
        font-size: 11px;
        padding: 4px 6px;
        white-space: nowrap;
    }
    .cam-ico {
        display: none;
    }
    .overlay-2d-box {
        padding: 5px;
        gap: 2px;
        left: 8px;
        bottom: 52px;
    }
    #canvas2d-aux {
        width: 165px;
        height: auto;
    }
    .overlay-2d-title {
        font-size: 10.5px;
        max-width: 165px;
        text-align: center;
        line-height: 1.35;
    }
    .hud-sub {
        display: none;
    }
    .hud-badge {
        padding: 6px 10px;
    }
    .hud-title {
        font-size: 13px;
    }
    .hud-legend {
        font-size: 11px;
        padding: 5px 9px;
    }
    /* Smaller 3D labels so neighbouring labels do not cover each other on phones */
    .label-3d {
        font-size: 10px;
        padding: 1px 5px;
    }
}

.cam-btn {
    background: #1e293b;
    color: var(--text-secondary);
    border: 1px solid #475569;
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.15s;
}

.cam-btn:hover, .cam-btn.active {
    background: var(--accent-blue);
    color: #ffffff;
    border-color: #93c5fd;
}

/* Miller Spinner Row */
.miller-spinners {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    direction: ltr;
}

.spinner-box {
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: 7px;
    padding: 6px;
    text-align: center;
}

.spinner-box label {
    display: block;
    font-size: 12px;
    font-weight: 800;
    color: #93c5fd;
    margin-bottom: 4px;
    font-family: 'Cambria Math', serif;
}

.spinner-box input[type="number"] {
    width: 100%;
    background: #1e293b;
    color: #ffffff;
    border: 1px solid #475569;
    border-radius: 5px;
    padding: 4px;
    text-align: center;
    font-size: 14px;
    font-weight: 800;
    font-family: 'Cambria Math', monospace;
}

/* Responsive Design */
@media (max-width: 1024px) {
    body {
        display: block;
        height: auto;
        min-height: 100vh;
    }
    .app-header {
        position: static;
    }
    .workstation {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;
        height: auto;
        min-height: 0;
    }
    .viewport-area {
        order: 1;
        height: 58vh;
        min-height: 380px;
    }
    .side-panel {
        order: 2;
        border-left: none;
        border-top: 2px solid var(--border-color);
        height: auto;
    }
}

/* Phones: compact header so the 3D view is visible without scrolling */
@media (max-width: 700px) {
    .app-header {
        padding: 8px 10px;
        gap: 6px;
    }
    .header-meta {
        display: none; /* mouse instructions are irrelevant on touch screens */
    }
    .brand-badge {
        font-size: 11px;
        padding: 4px 8px;
        white-space: nowrap;
    }
    .brand-title {
        font-size: 14px;
        line-height: 1.4;
    }
    .brand-subtitle {
        font-size: 10.5px;
    }
    /* One swipeable row of lab tabs instead of nine stacked rows */
    .nav-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        padding-bottom: 4px;
    }
    .nav-tab {
        flex: 0 0 auto;
    }
    .side-panel {
        padding: 10px;
    }
    .btn-grid-4 {
        grid-template-columns: repeat(2, 1fr);
    }
}
