/* ================================================================
   ReaderApp – Global Styles
   ================================================================ */
:root {
    --bg: #1a1a2e;
    --bg-card: #16213e;
    --bg-panel: #0f0f1a;
    --text: #e0e0e0;
    --text-dim: #8888aa;
    --primary: #4a9eff;
    --primary-hover: #3388ee;
    --success: #4caf50;
    --danger: #ef5350;
    --warning: #ffc107;
    --border: #2a2a4a;
    --radius: 8px;
    --shadow: 0 2px 12px rgba(0,0,0,0.3);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
    height: 100%;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow-x: hidden;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Navbar ───────────────────────────────────────────────────── */
.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 1.5rem;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    z-index: 1000;
}
.nav-brand { font-weight: 700; font-size: 1rem; color: var(--primary); }
.nav-links { display: flex; gap: 1rem; align-items: center; font-size: 0.85rem; }
.nav-user { color: var(--text-dim); }

/* ── Flash messages ───────────────────────────────────────────── */
.flash-container { padding: 0.5rem 1.5rem; }
.flash {
    padding: 0.6rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 0.4rem;
    font-size: 0.9rem;
}
.flash-success { background: #1b5e20; color: #c8e6c9; }
.flash-danger  { background: #b71c1c; color: #ffcdd2; }
.flash-warning { background: #f57f17; color: #fff9c4; }
.flash-info    { background: #1565c0; color: #bbdefb; }

/* ── Main content ─────────────────────────────────────────────── */
.main-content {
    max-width: 960px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}

/* ── Buttons ──────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.55rem 1.2rem;
    font-size: 0.9rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    text-decoration: none;
    color: #fff;
}
.btn:hover { transform: translateY(-1px); text-decoration: none; }
.btn:active { transform: translateY(0); }
.btn-primary { background: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { background: rgba(255,255,255,0.05); }
.btn-danger { background: var(--danger); }
.btn-block { width: 100%; }

/* ── Auth pages ───────────────────────────────────────────────── */
.auth-container {
    display: flex;
    justify-content: center;
    padding-top: 3rem;
}
.auth-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    width: 380px;
    box-shadow: var(--shadow);
}
.auth-card h2 { margin-bottom: 1.5rem; font-size: 1.3rem; }
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.3rem; font-size: 0.85rem; color: var(--text-dim); }
.form-group input {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    font-size: 0.95rem;
    outline: none;
}
.form-group input:focus { border-color: var(--primary); }
.auth-footer { margin-top: 1rem; text-align: center; font-size: 0.85rem; color: var(--text-dim); }

/* ── Dashboard ────────────────────────────────────────────────── */
.dashboard h1 { margin-bottom: 0.5rem; }
.dashboard-subtitle { color: var(--text-dim); margin-bottom: 2rem; font-size: 0.95rem; }

.session-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow);
}
.session-card h2 { margin-bottom: 1rem; font-size: 1.2rem; }
.session-locked { opacity: 0.5; }
.locked-text { color: var(--text-dim); font-style: italic; }
.lock-icon { font-size: 0.9em; }

.session-parts { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.part-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
}
.part-card h3 { font-size: 0.95rem; margin-bottom: 0.75rem; }

.progress-bar-container {
    height: 8px;
    background: var(--border);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 0.4rem;
}
.progress-bar {
    height: 100%;
    background: var(--success);
    border-radius: 4px;
    transition: width 0.3s;
}
.progress-text { font-size: 0.8rem; color: var(--text-dim); margin-bottom: 0.5rem; }

.badge {
    display: inline-block;
    padding: 0.25rem 0.6rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}
.badge-success { background: #1b5e20; color: #c8e6c9; }

/* ── Intro page ───────────────────────────────────────────────── */
.intro-container { display: flex; justify-content: center; }
.intro-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    max-width: 720px;
    box-shadow: var(--shadow);
}
.intro-card h1 { margin-bottom: 1.5rem; font-size: 1.4rem; }
.intro-body h3 { margin-top: 1.2rem; margin-bottom: 0.5rem; color: var(--primary); font-size: 1rem; }
.intro-body p, .intro-body li { font-size: 0.92rem; color: var(--text); line-height: 1.65; }
.intro-body ul, .intro-body ol { padding-left: 1.5rem; margin-bottom: 0.5rem; }
.intro-actions { display: flex; gap: 1rem; margin-top: 2rem; justify-content: flex-end; }

/* ── Complete page ────────────────────────────────────────────── */
.complete-container { display: flex; justify-content: center; padding-top: 4rem; }
.complete-card { text-align: center; max-width: 500px; }
.complete-icon { font-size: 4rem; color: var(--success); margin-bottom: 1rem; }
.complete-card h1 { margin-bottom: 0.5rem; }
.complete-card p { color: var(--text-dim); margin-bottom: 0.5rem; }
.complete-actions { margin-top: 2rem; }

/* ================================================================
   VIEWER PAGE
   ================================================================ */
.viewer-page {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 44px);
}

/* ── Toolbar ──────────────────────────────────────────────────── */
.viewer-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.35rem 0.75rem;
    background: #111128;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.toolbar-left, .toolbar-center, .toolbar-right {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}
.toolbar-label { font-size: 0.8rem; color: var(--text-dim); font-weight: 600; }
.toolbar-separator { color: var(--border); margin: 0 0.2rem; }

.toolbar-btn {
    padding: 0.3rem 0.65rem;
    font-size: 0.78rem;
    font-weight: 600;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.toolbar-btn:hover { background: rgba(74,158,255,0.12); border-color: var(--primary); }
.toolbar-btn.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
.toolbar-btn:disabled { opacity: 0.4; cursor: default; }
.toolbar-btn.btn-danger { border-color: var(--danger); color: var(--danger); }
.toolbar-btn.btn-danger:hover { background: rgba(239,83,80,0.15); }

/* ── Viewer body (viewer + sidebar) ───────────────────────────── */
.viewer-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.viewer-area {
    flex: 1;
    overflow: hidden;
    background: #000;
    position: relative;
}

/* ── Viewer grid ──────────────────────────────────────────────── */
.viewer-grid {
    display: grid;
    width: 100%;
    height: 100%;
    gap: 2px;
    background: #222;
}
.viewer-grid.layout-all {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-auto-rows: minmax(180px, 1fr);
}
.viewer-grid.layout-2x2 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.viewer-grid.layout-3x2 { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }
.viewer-grid.layout-4x2 { grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }
.viewer-grid.layout-1x2 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
.viewer-grid.layout-2x1 { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
.viewer-grid.layout-1x1 { grid-template-columns: 1fr; grid-template-rows: 1fr; }

/* ── Individual panel ─────────────────────────────────────────── */
.viewer-panel {
    position: relative;
    background: #000;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
}

.panel-canvas {
    width: 100%;
    height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
    display: block;
}

/* Panel controls overlay */
.panel-controls {
    position: absolute;
    top: 4px;
    left: 4px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    z-index: 10;
}
.panel-btn {
    padding: 2px 6px;
    font-size: 0.68rem;
    background: rgba(0,0,0,0.65);
    color: #ccc;
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 3px;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s;
}
.panel-btn:hover { background: rgba(74,158,255,0.3); color: #fff; }

/* Panel sequence dropdown */
.panel-dropdown {
    position: absolute;
    top: 28px;
    left: 4px;
    background: rgba(20,20,40,0.95);
    border: 1px solid var(--border);
    border-radius: 4px;
    max-height: 300px;
    overflow-y: auto;
    z-index: 20;
    display: none;
    min-width: 160px;
}
.panel-dropdown.visible { display: block; }
.panel-dropdown-item {
    padding: 4px 10px;
    font-size: 0.78rem;
    cursor: pointer;
    white-space: nowrap;
    color: var(--text);
}
.panel-dropdown-item:hover { background: rgba(74,158,255,0.2); }
.panel-dropdown-item.selected { color: var(--primary); font-weight: 600; }

/* Sequence label */
.panel-label {
    position: absolute;
    bottom: 4px;
    left: 4px;
    font-size: 0.72rem;
    font-weight: 600;
    color: #ffcc00;
    text-shadow: 0 0 4px #000, 0 0 8px #000;
    z-index: 5;
    pointer-events: none;
}

/* Slice info */
.panel-slice-info {
    position: absolute;
    bottom: 4px;
    right: 4px;
    font-size: 0.68rem;
    color: #aaa;
    text-shadow: 0 0 4px #000;
    z-index: 5;
    pointer-events: none;
}

/* WL info */
.panel-wl-info {
    position: absolute;
    top: 4px;
    right: 4px;
    font-size: 0.65rem;
    color: #888;
    text-shadow: 0 0 4px #000;
    z-index: 5;
    pointer-events: none;
}

/* Crosshair – FPS-style small cross with center gap */
.crosshair-fps {
    position: absolute;
    z-index: 8;
    pointer-events: none;
    display: none;
    /* positioned via left/top percent from JS */
}
.crosshair-fps .ch-arm {
    position: absolute;
    background: rgba(0, 255, 0, 0.85);
}
.crosshair-fps .ch-top,
.crosshair-fps .ch-bottom {
    width: 2px;
    height: 14px;
    left: -1px;
}
.crosshair-fps .ch-left,
.crosshair-fps .ch-right {
    height: 2px;
    width: 14px;
    top: -1px;
}
.crosshair-fps .ch-top    { bottom: 5px; }
.crosshair-fps .ch-bottom { top: 5px; }
.crosshair-fps .ch-left   { right: 5px; }
.crosshair-fps .ch-right  { left: 5px; }

/* ROI box overlay for right-click W/L */
.roi-box {
    position: absolute;
    border: 2px solid #00ccff;
    background: rgba(0, 204, 255, 0.12);
    pointer-events: none;
    display: none;
    z-index: 15;
}

/* Loading indicator per panel */
.panel-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-dim);
    font-size: 0.85rem;
    z-index: 6;
    pointer-events: none;
}

/* ================================================================
   QUESTIONNAIRE SIDEBAR
   ================================================================ */
.questionnaire-sidebar {
    width: 420px;
    min-width: 280px;
    max-width: 800px;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}
.questionnaire-sidebar.collapsed {
    width: 36px !important;
    min-width: 36px;
    max-width: 36px;
    overflow: hidden;
}

/* Resize drag handle on the left edge of sidebar */
.sidebar-resize-handle {
    position: absolute;
    top: 0;
    left: -4px;
    width: 8px;
    height: 100%;
    cursor: col-resize;
    z-index: 30;
    background: transparent;
}
.sidebar-resize-handle:hover,
.sidebar-resize-handle.dragging {
    background: var(--primary);
    opacity: 0.4;
}

.questionnaire-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.8rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.questionnaire-header h3 { font-size: 0.95rem; white-space: nowrap; }

.toggle-sidebar {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1rem;
    cursor: pointer;
    padding: 0.2rem;
    flex-shrink: 0;
}

/* When collapsed, show only the re-expand button */
.questionnaire-sidebar.collapsed .questionnaire-header {
    justify-content: center;
    padding: 0.6rem 0;
}
.questionnaire-sidebar.collapsed .questionnaire-header h3 { display: none; }
.questionnaire-sidebar.collapsed #questionnaire-form { display: none; }
.questionnaire-sidebar.collapsed .sidebar-resize-handle { display: none; }

#questionnaire-form {
    padding: 0.8rem;
    flex: 1;
}

.question-block {
    margin-bottom: 1.2rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.question-label {
    display: block;
    font-size: 0.85rem;
    margin-bottom: 0.5rem;
    line-height: 1.5;
}
.q-number {
    font-weight: 700;
    color: var(--primary);
    margin-right: 0.3rem;
}

/* ── Likert scale ─────────────────────────────────────────────── */
.likert-scale {
    display: flex;
    flex-direction: column;
    gap: 0;
}
.likert-options {
    display: flex;
    flex-direction: column;
    gap: 0;
}
.likert-option {
    cursor: pointer;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    padding: 0.3rem 0.4rem;
    border-radius: 6px;
    transition: background 0.15s;
}
.likert-option:hover {
    background: rgba(74, 158, 255, 0.08);
}
.likert-option input { display: none; }
.likert-circle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    min-width: 28px;
    border: 2px solid var(--border);
    border-radius: 50%;
    font-size: 0.78rem;
    font-weight: 600;
    transition: all 0.2s;
}
.likert-option:hover .likert-circle { border-color: var(--primary); color: var(--primary); }
.likert-option input:checked + .likert-circle {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
.likert-desc {
    font-size: 0.72rem;
    color: var(--text-dim);
    line-height: 1.35;
}
.likert-option input:checked ~ .likert-desc {
    color: var(--text);
    font-weight: 500;
}

/* ── Number & select inputs ───────────────────────────────────── */
.number-input { display: flex; align-items: center; gap: 0.5rem; }
.number-input input {
    width: 80px;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    font-size: 0.9rem;
}
.number-range { font-size: 0.75rem; color: var(--text-dim); }

.select-input select {
    width: 100%;
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
    font-size: 0.88rem;
    cursor: pointer;
}
.select-input select option { background: var(--bg-card); }

/* ── Form actions ─────────────────────────────────────────────── */
.form-actions {
    padding-top: 1rem;
    border-top: 1px solid var(--border);
    margin-top: 0.5rem;
}

/* ── Loading spinner ──────────────────────────────────────────── */
.spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Responsive ───────────────────────────────────────────────── */
@media (max-width: 900px) {
    .viewer-body { flex-direction: column; }
    .questionnaire-sidebar { width: 100%; min-width: 100%; max-width: 100%; max-height: 40vh; }
    .session-parts { grid-template-columns: 1fr; }
}
