/* ===== CSS Variables ===== */
:root {
    --color-bg: #f5f6fa;
    --color-surface: #ffffff;
    --color-sidebar: #1e293b;
    --color-sidebar-hover: #334155;
    --color-sidebar-active: #3b82f6;
    --color-primary: #3b82f6;
    --color-primary-hover: #2563eb;
    --color-danger: #ef4444;
    --color-danger-hover: #dc2626;
    --color-success: #22c55e;
    --color-warning: #f59e0b;
    --color-text: #1e293b;
    --color-text-light: #64748b;
    --color-border: #e2e8f0;
    --sidebar-width: 240px;
    --sidebar-collapsed: 0px;
    --topbar-height: 56px;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
    --font-mono: 'Consolas', 'Monaco', 'Courier New', monospace;
}

/* ===== Dark Theme ===== */
[data-theme="dark"] {
    --color-bg: #0f172a;
    --color-surface: #1e293b;
    --color-sidebar: #0f172a;
    --color-sidebar-hover: #1e293b;
    --color-text: #e2e8f0;
    --color-text-light: #94a3b8;
    --color-border: #334155;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
}
[data-theme="dark"] th { background: #1a2332; }
[data-theme="dark"] tr:hover { background: #253348; }
[data-theme="dark"] .log-output { background: #020617; }
[data-theme="dark"] .badge-success { background: #052e16; color: #4ade80; }
[data-theme="dark"] .badge-danger { background: #450a0a; color: #f87171; }
[data-theme="dark"] .badge-warning { background: #451a03; color: #fbbf24; }
[data-theme="dark"] .badge-info { background: #172554; color: #60a5fa; }
[data-theme="dark"] .toast-warning { color: #fff; }

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    display: flex;
    min-height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-width);
    background: var(--color-sidebar);
    color: #fff;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transition: transform 0.2s ease;
}
.sidebar.collapsed { transform: translateX(-100%); }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar-title {
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sidebar-toggle {
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px;
}
.sidebar-toggle:hover { color: #fff; }

.sidebar-nav {
    flex: 1;
    padding: 8px 0;
    overflow-y: auto;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    color: #cbd5e1;
    text-decoration: none;
    font-size: 0.93rem;
    transition: background 0.15s, color 0.15s;
}
.nav-item:hover {
    background: var(--color-sidebar-hover);
    color: #fff;
}
.nav-item.active {
    background: var(--color-sidebar-active);
    color: #fff;
    font-weight: 500;
}
.nav-icon { font-size: 1.1rem; width: 20px; text-align: center; }

/* ===== Main Content ===== */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    min-height: 100vh;
    transition: margin-left 0.2s ease;
}
body.sidebar-collapsed .main-content { margin-left: 0; }

.topbar {
    height: var(--topbar-height);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    padding: 0 24px;
    gap: 16px;
    position: sticky;
    top: 0;
    z-index: 50;
}
.menu-btn {
    display: none;
    background: none;
    border: none;
    font-size: 1.4rem;
    cursor: pointer;
    color: var(--color-text);
}
body.sidebar-collapsed .menu-btn { display: block; }

.page-title { font-size: 1.25rem; font-weight: 600; }

.content { padding: 24px; }

/* ===== Cards ===== */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}
.card-title { font-size: 1.05rem; font-weight: 600; }

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 16px;
}

.stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 16px 20px;
    box-shadow: var(--shadow);
}
.stat-card .stat-value { font-size: 2rem; font-weight: 700; color: var(--color-primary); }
.stat-card .stat-label { font-size: 0.85rem; color: var(--color-text-light); margin-top: 4px; }

/* ===== Tables ===== */
.table-wrapper { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.93rem;
}
th, td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
}
th {
    background: var(--color-bg);
    font-weight: 600;
    color: var(--color-text-light);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
tr:hover { background: #f8fafc; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, box-shadow 0.15s;
    text-decoration: none;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); }

.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--color-danger-hover); }

.btn-outline {
    background: transparent;
    color: var(--color-primary);
    border: 1px solid var(--color-primary);
}
.btn-outline:hover:not(:disabled) { background: var(--color-primary); color: #fff; }

.btn-sm { padding: 5px 10px; font-size: 0.8rem; }
.btn-icon { padding: 6px 8px; }

/* ===== Forms ===== */
.form-group { margin-bottom: 14px; }
.form-label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 4px;
    color: var(--color-text-light);
}
.form-input,
.form-select,
.form-textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--color-surface);
    color: var(--color-text);
    transition: border-color 0.15s;
}
.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-input-mono { font-family: var(--font-mono); font-size: 0.85rem; }

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
}
.badge-success { background: #dcfce7; color: #166534; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-info { background: #dbeafe; color: #1e40af; }

/* ===== Tabs ===== */
.tabs {
    display: flex;
    border-bottom: 2px solid var(--color-border);
    margin-bottom: 20px;
    gap: 0;
}
.tab-btn {
    padding: 10px 20px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--color-text-light);
    transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--color-text); }
.tab-btn.active {
    color: var(--color-primary);
    border-bottom-color: var(--color-primary);
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ===== Modal ===== */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 200;
    align-items: center;
    justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    padding: 24px;
}
.modal-lg { max-width: 900px; }
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
.modal-title { font-size: 1.1rem; font-weight: 600; }
.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--color-text-light);
    line-height: 1;
}

/* ===== Log Output ===== */
.log-output {
    background: #0f172a;
    color: #e2e8f0;
    font-family: var(--font-mono);
    font-size: 0.8rem;
    padding: 16px;
    border-radius: var(--radius);
    height: 400px;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
    line-height: 1.5;
}
.log-output .log-error { color: #f87171; }
.log-output .log-warn { color: #fbbf24; }
.log-output .log-info { color: #60a5fa; }

/* ===== Toast Notifications ===== */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 300;
    display: flex;
    flex-direction: column-reverse;
    gap: 8px;
}
.toast {
    padding: 12px 20px;
    border-radius: 6px;
    font-size: 0.9rem;
    color: #fff;
    box-shadow: var(--shadow-lg);
    animation: toastIn 0.3s ease;
    max-width: 400px;
}
.toast-success { background: var(--color-success); }
.toast-error { background: var(--color-danger); }
.toast-warning { background: var(--color-warning); color: #1e293b; }
.toast-info { background: var(--color-primary); }

@keyframes toastIn {
    from { opacity: 0; transform: translateY(16px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Utility ===== */
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-8 { gap: 8px; }
.gap-16 { gap: 16px; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }
.text-light { color: var(--color-text-light); }
.text-mono { font-family: var(--font-mono); }
.text-sm { font-size: 0.85rem; }
.hidden { display: none !important; }

.spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    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); } }

/* ===== Desktop-only: hide mobile elements ===== */
.bottom-nav { display: none; }
.sidebar-backdrop { display: none; }
.mobile-action-bar { display: none; }

/* ============================================================
   RESPONSIVE — 768px (tablet / phone)
   ============================================================ */
@media (max-width: 768px) {

    /* ----- Layout ----- */
    body { display: block; }
    .main-content { margin-left: 0 !important; max-width: 100vw; }

    /* ----- Sidebar drawer ----- */
    .sidebar {
        position: fixed;
        transform: translateX(-100%);
        z-index: 300;
        box-shadow: none;
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0,0,0,0.35);
    }
    .sidebar .sidebar-toggle { display: none; }

    /* ----- Backdrop ----- */
    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.45);
        z-index: 250;
        -webkit-tap-highlight-color: transparent;
    }
    .sidebar-backdrop.active { display: block; }

    /* ----- Topbar ----- */
    .topbar { padding: 0 16px; gap: 12px; }
    .menu-btn {
        display: block !important;
        min-width: 44px;
        min-height: 44px;
        font-size: 1.3rem;
    }

    /* ----- Content ----- */
    .content {
        padding: 12px;
        padding-bottom: calc(56px + 12px);
        max-width: 100%;
        box-sizing: border-box;
    }

    /* ----- Cards ----- */
    .card-grid { grid-template-columns: 1fr; gap: 12px; }
    .card { padding: 16px; margin-bottom: 12px; min-width: 0; max-width: 100%; box-sizing: border-box; }
    .card table { width: 100%; table-layout: fixed; }
    .card table td { overflow: hidden; text-overflow: ellipsis; }
    .stat-card .stat-value { font-size: 1.6rem; }

    /* ----- Tables — always horizontal scroll ----- */
    .table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .table-wrapper table { min-width: 480px; }

    /* ----- Table action buttons — stack vertically ----- */
    .table-actions .flex {
        flex-direction: column;
        gap: 4px;
        align-items: stretch;
    }
    .table-actions .btn { width: 100%; justify-content: center; }

    /* ----- Tabs — horizontal scroll ----- */
    .tabs {
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }
    .tabs::-webkit-scrollbar { display: none; }
    .tab-btn {
        white-space: nowrap;
        padding: 10px 16px;
        min-height: 44px;
        flex-shrink: 0;
    }

    /* ----- Forms — single column ----- */
    .flex.gap-16 { flex-wrap: wrap; }
    .flex.gap-16 > .form-group {
        flex: 1 1 100% !important;
        min-width: 0 !important;
    }

    /* ----- Log output — shorter ----- */
    .log-output {
        height: min(200px, 40vh);
        font-size: 0.75rem;
    }

    /* ----- Buttons — touch targets ----- */
    .btn { min-height: 44px; padding: 10px 16px; }
    .btn-sm { min-height: 38px; padding: 8px 12px; font-size: 0.82rem; }
    .btn-icon { min-height: 0; min-width: 0; padding: 4px 8px; font-size: 0.85rem; }

    /* ----- Modals — bottom sheet style ----- */
    .modal-overlay { align-items: flex-end; }
    .modal {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 90vh;
        border-radius: 16px 16px 0 0;
        padding: 20px 16px;
        margin: 0;
    }
    .modal-lg { max-width: 100% !important; }
    #reportModal .modal { height: 90vh; max-height: 90vh; }

    /* ----- Toasts — above bottom nav ----- */
    .toast-container {
        bottom: calc(56px + 12px);
        right: 12px;
        left: 12px;
    }
    .toast { max-width: 100%; }

    /* ----- Bottom Navigation Bar ----- */
    .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--color-surface);
        border-top: 1px solid var(--color-border);
        display: flex;
        align-items: center;
        z-index: 200;
        box-shadow: 0 -1px 4px rgba(0,0,0,0.07);
    }
    .bottom-nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-height: 56px;
        text-decoration: none;
        color: var(--color-text-light);
        font-size: 0.65rem;
        font-weight: 500;
        transition: color 0.15s;
        -webkit-tap-highlight-color: transparent;
    }
    .bottom-nav-item.active { color: var(--color-primary); }
    .bottom-nav-item .bnav-icon { font-size: 1.2rem; }
    .bottom-nav-more {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        min-height: 56px;
        background: none;
        border: none;
        color: var(--color-text-light);
        font-size: 0.65rem;
        font-weight: 500;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }
    .bottom-nav-more .bnav-icon { font-size: 1.2rem; }

    /* ----- Mobile action bar (test runner) ----- */
    .mobile-action-bar {
        position: fixed;
        bottom: 56px;
        left: 0;
        right: 0;
        background: var(--color-surface);
        border-top: 1px solid var(--color-border);
        padding: 10px 16px;
        display: flex;
        gap: 8px;
        z-index: 100;
        box-shadow: 0 -2px 8px rgba(0,0,0,0.08);
    }
    .mobile-action-bar .btn { flex: 1; }
    .has-mobile-bar #inlineRunnerBtns { display: none; }
}

/* ============================================================
   RESPONSIVE — 480px (small phones)
   ============================================================ */
@media (max-width: 480px) {
    html { font-size: 13px; }
    .topbar { padding: 0 12px; }
    .content { padding: 10px; padding-bottom: calc(44px + 10px); }
    .card { padding: 14px 12px; }

    .page-title {
        font-size: 1rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        flex: 1;
    }

    /* Bottom nav — icons only */
    .bottom-nav-item .bnav-label,
    .bottom-nav-more .bnav-label { display: none; }
    .bottom-nav-item { min-height: 44px; }
    .bottom-nav { height: 44px; }
    .toast-container { bottom: calc(44px + 10px); }
    .mobile-action-bar { bottom: 44px; }

    th, td { padding: 8px 10px; }
}

/* ============================================================
   DARK THEME — mobile overrides
   ============================================================ */
[data-theme="dark"] .sidebar-backdrop { background: rgba(0,0,0,0.65); }
[data-theme="dark"] .mobile-action-bar { box-shadow: 0 -2px 8px rgba(0,0,0,0.3); }
