/* ============================================================
   CodeRunner Question Forge — Moodle Boost Theme
   Faithfully matches Moodle 4.x Boost design tokens
   ============================================================ */

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

:root {
    --primary: #0f6cbf;
    --primary-hover: #0a5da6;
    --secondary: #ced4da;
    --success: #357a32;
    --success-hover: #2d6a2b;
    --danger: #ca3120;
    --danger-hover: #b02a1b;
    --warning: #f0ad4e;
    --warning-hover: #ec971f;
    --info: #008196;
    --bg: #f8f9fa;
    --surface: #ffffff;
    --text: #1d2125;
    --text-light: #6a737b;
    --border: #dee2e6;
    --light-bg: #e9ecef;
    --radius: 0.5rem;
    --radius-sm: 0.375rem;
    --font-size-base: 0.9375rem;
    --spacer: 1rem;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: var(--font-size-base);
    line-height: 1.5;
    overflow-x: hidden;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-hover); text-decoration: underline; }

/* ============================================================
   Header / Navbar — Moodle Boost style
   ============================================================ */
.app-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0;
}

.app-header-top {
    display: flex;
    align-items: center;
    padding: 0.75rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.app-header-top .site-name {
    font-size: 1.125rem;
    font-weight: 700;
    color: var(--text);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.app-header-top .site-name .site-icon {
    width: 28px;
    height: 28px;
    background: var(--primary);
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 700;
    font-size: 0.85rem;
    flex-shrink: 0;
}

/* Secondary navigation — Moodle style horizontal nav links */
.app-header-nav {
    display: flex;
    align-items: stretch;
    padding: 0 1.5rem;
    gap: 0;
    overflow-x: auto;
}

.app-header-nav .nav-item {
    display: inline-flex;
    align-items: center;
    padding: 0.625rem 1rem;
    font-size: 0.875rem;
    font-weight: 400;
    color: var(--text-light);
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    white-space: nowrap;
    transition: color 0.15s, border-color 0.15s;
    text-decoration: none;
    font-family: inherit;
}

.app-header-nav .nav-item:hover {
    color: var(--primary);
    text-decoration: none;
}

.app-header-nav .nav-item.active {
    color: var(--primary);
    font-weight: 600;
    border-bottom-color: var(--primary);
}
.app-header-nav .nav-item-bank {
    font-weight: 600;
    color: #0f766e;
}
.app-header-nav .nav-item-bank.active {
    color: #0f766e;
    border-bottom-color: #0f766e;
}
.nav-divider {
    display: inline-block;
    width: 1px;
    height: 22px;
    margin: 0 0.5rem;
    background: var(--border);
    align-self: center;
}
@media (max-width: 480px) { .nav-divider { display: none; } }

/* ============================================================
   Layout
   ============================================================ */
.container {
    max-width: 1100px;
    margin: 1.5rem auto;
    padding: 0 1.5rem;
}

/* ============================================================
   Cards — Moodle Boost clean card style
   ============================================================ */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: var(--spacer);
}

.card h2 {
    font-size: 1.125rem;
    font-weight: 700;
    margin-bottom: 0.75rem;
    color: var(--text);
}

.card h3 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text);
}

/* ============================================================
   Forms — Bootstrap/Moodle style
   ============================================================ */
.form-row {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
}

.form-group {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 150px;
    overflow: hidden;
}

.form-group label {
    font-size: 0.875rem;
    font-weight: 400;
    margin-bottom: 0.25rem;
    color: var(--text);
}

.form-group input,
.form-group select,
.form-group textarea {
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-base);
    font-family: inherit;
    color: var(--text);
    background-color: var(--surface);
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    line-height: 1.5;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: #80bdff;
    outline: 0;
    box-shadow: 0 0 0 0.2rem rgba(15, 108, 191, 0.25);
}

.form-group textarea {
    resize: vertical;
    min-height: 80px;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}

/* ============================================================
   Buttons — Moodle/Bootstrap style
   ============================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.75rem;
    font-size: var(--font-size-base);
    font-weight: 400;
    font-family: inherit;
    line-height: 1.5;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color 0.15s, background-color 0.15s, border-color 0.15s, box-shadow 0.15s;
    text-decoration: none;
    white-space: nowrap;
}

.btn:focus {
    outline: 0;
    box-shadow: 0 0 0 0.2rem rgba(15, 108, 191, 0.25);
}

.btn-primary {
    background-color: var(--primary);
    border-color: var(--primary);
    color: #fff;
}
.btn-primary:hover { background-color: var(--primary-hover); border-color: var(--primary-hover); }

.btn-success {
    background-color: var(--success);
    border-color: var(--success);
    color: #fff;
}
.btn-success:hover { background-color: var(--success-hover); border-color: var(--success-hover); }

.btn-danger {
    background-color: var(--danger);
    border-color: var(--danger);
    color: #fff;
}
.btn-danger:hover { background-color: var(--danger-hover); border-color: var(--danger-hover); }

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

.btn-outline {
    background-color: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { background-color: var(--light-bg); }

.btn:disabled {
    opacity: 0.65;
    cursor: not-allowed;
    pointer-events: none;
}

.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.8125rem;
}

/* ============================================================
   Question cards — left border status indicator
   ============================================================ */
.question-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    margin-bottom: 0.75rem;
    border-left: 4px solid var(--border);
}

.question-card.validated { border-left-color: var(--success); }
.question-card.failed { border-left-color: var(--danger); }
.question-card.pending { border-left-color: var(--warning); }

.question-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.35rem;
}
.question-header h3 {
    font-size: var(--font-size-base);
    font-weight: 600;
    color: var(--text);
    margin: 0;
    white-space: nowrap;
}

.question-actions {
    display: flex;
    gap: 0.375rem;
    flex-shrink: 0;
}

.question-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    align-items: center;
    margin-bottom: 0.5rem;
}

.q-more-btn { padding: 0.25rem 0.55rem; font-weight: 700; }

/* Submenu inside a popup menu (e.g. Export to Moodle ▸ XML / GIFT / Aiken) */
.cat-ctx-item.has-submenu { position: relative; }
.ctx-submenu {
    display: none;
    position: absolute;
    left: 100%;
    top: -5px;
    background: white;
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    min-width: 200px;
    padding: 4px 0;
    margin-left: 2px;
}
.cat-ctx-item.has-submenu:hover > .ctx-submenu { display: block; }
/* Flip to the left when the parent menu is near the viewport's right edge */
@media (max-width: 480px) { .ctx-submenu { left: auto; right: 100%; margin-left: 0; margin-right: 2px; } }

.ai-review-badge { cursor: help; }
.ai-review-tooltip {
    display: none;
    position: fixed;
    z-index: 2500;
    max-width: 380px;
    min-width: 280px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    padding: 12px 14px;
    font-size: 0.82rem;
    color: var(--text);
    line-height: 1.4;
    pointer-events: none;
}
.ai-tip-header { font-weight: 600; font-size: 0.88rem; margin-bottom: 6px; padding-bottom: 6px; border-bottom: 1px solid #f1f5f9; }
.ai-tip-intro { color: var(--text-light); font-size: 0.78rem; margin-bottom: 8px; }
.ai-tip-list { list-style: none; margin: 0; padding: 0; }
.ai-tip-item { display: block; padding: 3px 0; padding-left: 22px; position: relative; }
.ai-tip-mark { position: absolute; left: 0; top: 3px; font-weight: 700; font-size: 0.85rem; width: 18px; text-align: center; }
.ai-tip-pass { color: var(--text); }
.ai-tip-pass .ai-tip-mark { color: #16a34a; }
.ai-tip-fail { color: #991b1b; background: #fef2f2; border-radius: 4px; padding-right: 6px; margin: 2px 0; }
.ai-tip-fail .ai-tip-mark { color: #dc2626; }
.ai-tip-unknown { color: var(--text-light); }
.ai-tip-unknown .ai-tip-mark { color: #cbd5e1; }
.ai-tip-reason { font-size: 0.75rem; color: #7f1d1d; margin-top: 2px; font-style: italic; }
.ai-tip-notes { margin-top: 8px; padding-top: 8px; border-top: 1px solid #f1f5f9; font-size: 0.78rem; color: var(--text-light); }

/* ============================================================
   Rich-text editor + structured question edit form
   ============================================================ */
.edit-form .rt-field { margin-bottom: 10px; }
.rt-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 4px;
    background: #f1f5f9;
    border: 1px solid var(--border);
    border-bottom: none;
    border-radius: 4px 4px 0 0;
}
.rt-btn {
    background: #fff;
    border: 1px solid transparent;
    border-radius: 3px;
    padding: 2px 7px;
    font-size: 0.82rem;
    line-height: 1;
    color: var(--text);
    cursor: pointer;
    min-width: 26px;
    height: 26px;
}
.rt-btn:hover { background: #e2e8f0; border-color: var(--border); }
.rt-editor {
    min-height: 60px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 0 0 4px 4px;
    background: #fff;
    font-size: 0.9rem;
    line-height: 1.5;
    outline: none;
    overflow-x: auto;
}
.rt-editor:focus { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(15,108,191,0.12); }
.rt-editor code { background: #f1f5f9; padding: 1px 4px; border-radius: 3px; font-family: ui-monospace, monospace; font-size: 0.85em; }
.rt-editor p:first-child { margin-top: 0; }
.rt-editor p:last-child { margin-bottom: 0; }
.rt-editor ul, .rt-editor ol { margin: 6px 0; padding-left: 22px; }

.type-editor { margin: 14px 0; padding: 10px 12px; background: #fff; border: 1px solid var(--border); border-radius: 6px; }
.answer-row {
    border: 1px solid var(--border);
    border-left: 3px solid #cbd5e1;
    border-radius: 4px;
    padding: 8px 10px;
    margin-bottom: 8px;
    background: #fafbfc;
}
.answer-row:has(.ans-correct:checked) { border-left-color: var(--success); background: #f0fdf4; }
.answer-row-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    font-size: 0.82rem;
    color: var(--text-light);
}
.answer-correct-toggle { font-weight: 600; color: var(--text); display: flex; align-items: center; gap: 4px; }
.answer-row .ans-text, .answer-row .ans-value, .answer-row .pair-answer {
    width: 100%;
    padding: 6px 8px;
    font-size: 0.88rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    margin-bottom: 6px;
}

/* ============================================================
   Badges — Moodle Bootstrap pill badges
   ============================================================ */
.badge {
    display: inline-block;
    padding: 0.2em 0.6em;
    font-size: 0.75rem;
    font-weight: 600;
    line-height: 1;
    border-radius: 50rem;
    white-space: nowrap;
    color: #1e293b; /* dark default — beats Bootstrap's white so unstyled badges stay readable */
}

.badge-easy { background-color: var(--success); color: #fff; }
.badge-medium { background-color: var(--warning); color: #1d2125; }
.badge-hard { background-color: var(--danger); color: #fff; }

.badge-validated { background-color: var(--success); color: #fff; }
.badge-failed { background-color: var(--danger); color: #fff; }
.badge-pending { background-color: var(--warning); color: #1d2125; }

/* ============================================================
   Test case rows
   ============================================================ */
.test-case-row {
    display: flex;
    gap: 0.5rem;
    padding: 0.375rem 0;
    border-bottom: 1px solid var(--bg);
    font-size: 0.85rem;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}
.test-case-row .status { width: 24px; text-align: center; }
.test-case-row .stdin,
.test-case-row .expected,
.test-case-row .actual { flex: 1; }

/* ============================================================
   Loading / Spinner
   ============================================================ */
.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    display: inline-block;
    vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.625rem;
    padding: 2.5rem;
    color: var(--text-light);
}

/* ============================================================
   Tabs (content visibility)
   ============================================================ */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ============================================================
   Utility classes
   ============================================================ */
.mt-2 { margin-top: 0.5rem; }
.mt-4 { margin-top: 1rem; }
.text-center { text-align: center; }
.text-light { color: var(--text-light) !important; }
.hidden { display: none !important; }

code {
    background-color: var(--light-bg);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-size: 0.875em;
    color: var(--danger);
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}

/* ============================================================
   CodeRunner Results Table (Moodle native styles)
   ============================================================ */
.cr-results-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.cr-results-table th {
    background: #f5f5f5;
    border: 1px solid #ddd;
    padding: 6px 10px;
    text-align: left;
    font-weight: 600;
    color: #333;
}
.cr-results-table td {
    border: 1px solid #ddd;
    padding: 6px 10px;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.82rem;
    white-space: pre-wrap;
}
.cr-results-table tr.cr-pass { background: #dff0d8; }
.cr-results-table tr.cr-fail { background: #f2dede; }
.cr-results-table tr.cr-pass td { border-color: #d6e9c6; }
.cr-results-table tr.cr-fail td { border-color: #ebccd1; }
.cr-results-table .cr-icon { text-align: center; width: 30px; }
.cr-results-table .cr-icon-pass { color: #3c763d; }
.cr-results-table .cr-icon-fail { color: #a94442; }

/* Feedback styles */
.cr-feedback-correct { background: #dff0d8; border: 1px solid #d6e9c6; color: #3c763d; }
.cr-feedback-partial { background: #fcf8e3; border: 1px solid #faebcc; color: #8a6d3b; }
.cr-feedback-incorrect { background: #f2dede; border: 1px solid #ebccd1; color: #a94442; }

/* ============================================================
   Bank tab — checkbox styling
   ============================================================ */
.question-card input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--primary);
}

/* ============================================================
   Code / pre blocks inside cards (skill tab etc.)
   ============================================================ */
.code-block {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 0.85rem;
}

/* ============================================================
   Details/summary (question settings)
   ============================================================ */
details summary {
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-light);
    user-select: none;
}

details summary:hover {
    color: var(--primary);
}

details[open] summary {
    margin-bottom: 0.5rem;
}

/* ============================================================
   Alert/notice boxes (Moodle style)
   ============================================================ */
.alert-warning {
    background: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: var(--radius-sm);
    padding: 0.75rem 1rem;
    font-size: 0.9rem;
    color: #664d03;
}

/* ============================================================
   Category Manager — Split pane + tree
   ============================================================ */
.qb-split-pane { display: flex; gap: 0; align-items: flex-start; }
.cat-sidebar { width: 300px; flex-shrink: 0; flex-grow: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow-y: auto; max-height: 70vh; }
.cat-resizer { width: 6px; flex-shrink: 0; cursor: ew-resize; align-self: stretch; min-height: 200px; background: transparent; border-radius: 3px; margin: 0 4px; transition: background 0.1s; position: relative; }
.cat-resizer::after { content: ''; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 2px; height: 28px; background: var(--border); border-radius: 1px; transition: background 0.1s; }
.cat-resizer:hover::after,
.cat-resizer.dragging::after { background: var(--primary); }
.cat-resizer:hover { background: rgba(15, 108, 191, 0.08); }
body.resizing-sidebar { cursor: ew-resize; user-select: none; }
body.resizing-sidebar iframe { pointer-events: none; }
.cat-sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.qb-main { flex: 1; min-width: 0; }

.cat-node { }
.cat-label { display: flex; align-items: center; gap: 4px; padding: 5px 10px; cursor: pointer; font-size: 0.85rem; border-radius: 4px; margin: 1px 4px; transition: background 0.1s; user-select: none; }
.cat-label:hover { background: #f0f4f8; }
.cat-label.selected { background: #dbeafe; color: var(--primary); font-weight: 600; }
.cat-label.cat-drag-over { background: rgba(15, 108, 191, 0.1); outline: 2px dashed var(--primary); outline-offset: -2px; }
.cat-chevron-wrap { display: inline-flex; width: 16px; flex-shrink: 0; cursor: pointer; }
.cat-chevron { transition: transform 0.15s; }
.cat-chevron.expanded { transform: rotate(90deg); }
.cat-count { font-size: 0.7rem; background: #f0f4f8; color: var(--text-light); padding: 1px 6px; border-radius: 10px; margin-left: auto; }
.cat-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-tree-children { }
.cat-action { opacity: 0; background: transparent; border: 1px solid transparent; color: var(--text-light); cursor: pointer; font-size: 0.8rem; line-height: 1; padding: 2px 4px; border-radius: 4px; transition: opacity 0.1s, background 0.1s, color 0.1s; flex-shrink: 0; }
.cat-action:hover { background: #fff; border-color: var(--border); color: var(--primary); }
.cat-action-danger:hover { color: var(--danger); }
.cat-action-sparkle { opacity: 1; color: #f59e0b; }
.cat-action-sparkle:hover { background: #fef3c7; border-color: #fbbf24; color: #b45309; }
.cat-label:hover .cat-action,
.cat-label:focus-within .cat-action { opacity: 1; }

/* Context menu hover */
.cat-ctx-item:hover { background: #f0f4f8; }

/* Question card drag hint */
.question-card[draggable="true"] { cursor: grab; }
.question-card[draggable="true"]:active { cursor: grabbing; }

/* ============================================================
   Responsive — Tablet (≤768px)
   ============================================================ */
@media (max-width: 768px) {
    .app-header-top { padding: 0.5rem 1rem; }
    .app-header-top .site-name { font-size: 1rem; }
    .app-header-nav { padding: 0 0.25rem; justify-content: center; overflow-x: visible; flex-wrap: wrap; }
    .app-header-nav .nav-item { padding: 0.5rem 0.625rem; font-size: 0.8125rem; }
    .container { padding: 0 0.75rem; margin: 0.75rem auto; }
    .qb-split-pane { flex-direction: column; }
    .cat-sidebar { width: 100% !important; max-height: none; }
    .cat-resizer { display: none; }
    .card { padding: 1rem; }
    .form-row { flex-direction: column; }
    .form-group { min-width: 100% !important; flex: 1 1 100% !important; }
    .question-header { flex-direction: column; align-items: flex-start; gap: 0.5rem; }
    .question-actions { flex-wrap: wrap; }
    .cr-results-table { font-size: 0.78rem; }
    .cr-results-table th, .cr-results-table td { padding: 4px 6px; }
}

/* ============================================================
   Responsive — Mobile (≤480px)
   ============================================================ */
@media (max-width: 480px) {
    .app-header-top .site-name { font-size: 0.9rem; }
    .app-header-top .site-name img { width: 24px; height: 24px; }
    .app-header-nav { padding: 0 0.25rem; justify-content: center; overflow-x: visible; flex-wrap: wrap; gap: 0; }
    .app-header-nav .nav-item {
        padding: 0.4rem 0.5rem;
        font-size: 0;        /* hide text */
        border-bottom-width: 2px;
    }
    .app-header-nav .nav-item svg {
        width: 18px;
        height: 18px;
        margin-right: 0;     /* no gap when text hidden */
    }
    .app-header-nav .nav-item.active {
        font-size: 0.65rem;  /* show text only for active tab */
    }
    .app-header-nav .nav-item.active svg {
        margin-right: 3px;
    }
    .container { padding: 0 0.5rem; margin: 0.5rem auto; }
    .card { padding: 0.75rem; }
    .card h2 { font-size: 1rem; }
    .btn { padding: 0.3rem 0.6rem; font-size: 0.8125rem; }
    .btn-sm { padding: 0.2rem 0.4rem; font-size: 0.75rem; }
    .badge { font-size: 0.675rem; padding: 0.15em 0.5em; }
    .question-card { padding: 0.75rem; }
    .question-header h3 { font-size: 0.875rem; }
    .question-actions { gap: 0.25rem; }
    .test-case-row { flex-direction: column; gap: 0.25rem; font-size: 0.78rem; }
    .test-case-row .status { width: auto; }

    /* Preview modal — full screen on mobile */
    #preview-overlay > div { margin: 0; border-radius: 0; min-height: 100vh; }
    #preview-editor { height: 250px !important; }

    /* Results table — horizontal scroll on mobile */
    .cr-results-table { display: block; overflow-x: auto; }
    .cr-results-table th, .cr-results-table td { font-size: 0.75rem; padding: 3px 5px; white-space: nowrap; }
}
