/* ================================================================
   Clear To Run Admin — Design System
   Built on Pico CSS
   ================================================================ */

:root {
    /* Brand palette */
    --bb-navy: #003057;
    --bb-navy-light: #004080;
    --bb-navy-muted: #0a3d6b;
    --bb-red: #e31937;
    --bb-red-light: #ff4d6a;

    /* Functional colors */
    --bb-green: #16a34a;
    --bb-green-bg: #dcfce7;
    --bb-yellow: #ca8a04;
    --bb-yellow-bg: #fef9c3;
    --bb-red-bg: #fee2e2;
    --bb-blue: #2563eb;
    --bb-blue-bg: #dbeafe;
    --bb-gray: #6b7280;
    --bb-gray-light: #f3f4f6;
    --bb-gray-border: #e5e7eb;

    /* Surfaces */
    --bb-sidebar-bg: #0f172a;
    --bb-sidebar-text: #cbd5e1;
    --bb-sidebar-active: rgba(255,255,255,0.12);
    --bb-sidebar-hover: rgba(255,255,255,0.06);
    --bb-sidebar-accent: #38bdf8;
    --bb-page-bg: #f8fafc;

    /* Shadows */
    --bb-shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --bb-shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
    --bb-shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.04);
}

/* ----------------------------------------------------------------
   Layout
   ---------------------------------------------------------------- */

body {
    background: var(--bb-page-bg);
    margin: 0;
}

.layout {
    display: flex;
    min-height: 100vh;
}

/* ----------------------------------------------------------------
   Sidebar
   ---------------------------------------------------------------- */

.sidebar {
    width: 250px;
    background: var(--bb-sidebar-bg);
    color: var(--bb-sidebar-text);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-right: none;
    padding: 0;
    overflow-y: auto;
    position: sticky;
    top: 0;
    height: 100vh;
}

.sidebar-brand {
    padding: 1.25rem 1.25rem 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.sidebar-brand svg {
    width: 28px;
    height: 28px;
    color: var(--bb-sidebar-accent);
    flex-shrink: 0;
}

.sidebar-brand-text h3 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
}

.sidebar-brand-text small {
    font-size: 0.7rem;
    color: #64748b;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.sidebar-nav {
    padding: 0.75rem 0.75rem;
    flex: 1;
}

.sidebar-section {
    margin-bottom: 1.25rem;
}

.sidebar-section-label {
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #475569;
    padding: 0 0.5rem;
    margin-bottom: 0.35rem;
}

.sidebar-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: block !important;
}

.sidebar-nav li {
    margin-bottom: 2px;
    display: block !important;
    width: 100%;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.5rem 0.65rem;
    border-radius: 6px;
    text-decoration: none;
    color: var(--bb-sidebar-text);
    font-size: 0.875rem;
    font-weight: 450;
    transition: background 0.15s, color 0.15s;
}

.sidebar-nav a:hover {
    background: var(--bb-sidebar-hover);
    color: #f1f5f9;
}

.sidebar-nav a[aria-current="page"] {
    background: var(--bb-sidebar-active);
    color: #fff;
    font-weight: 600;
}

.sidebar-nav a[aria-current="page"] .nav-icon {
    color: var(--bb-sidebar-accent);
}

.nav-icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    color: #64748b;
    transition: color 0.15s;
}

.sidebar-nav a:hover .nav-icon {
    color: #94a3b8;
}

/* Sidebar user info */
.sidebar-user {
    padding: 0.75rem 1rem;
    border-top: 1px solid rgba(255,255,255,0.08);
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--bb-sidebar-accent);
    color: #0f172a;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 700;
    flex-shrink: 0;
}

.user-info {
    flex: 1;
    min-width: 0;
}

.user-name {
    font-size: 0.8rem;
    font-weight: 600;
    color: #e2e8f0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-role {
    font-size: 0.65rem;
    color: #64748b;
}

.user-logout {
    color: #64748b;
    padding: 0.25rem;
    border-radius: 4px;
    transition: color 0.15s, background 0.15s;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.user-logout:hover {
    color: #f87171;
    background: rgba(248,113,113,0.1);
}

/* Mobile sidebar toggle */
.sidebar-toggle {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 1000;
    background: var(--bb-navy);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 0.5rem;
    cursor: pointer;
    box-shadow: var(--bb-shadow-md);
}

.sidebar-toggle svg {
    width: 22px;
    height: 22px;
}

/* ----------------------------------------------------------------
   Main Content
   ---------------------------------------------------------------- */

main.content {
    flex: 1;
    padding: 2rem 2.5rem;
    max-width: 1200px;
    min-width: 0;
}

/* Page header */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.75rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.page-header h1 {
    font-size: 1.65rem;
    font-weight: 700;
    color: var(--bb-navy);
    margin: 0;
}

.page-header p {
    margin: 0.25rem 0 0;
    color: var(--bb-gray);
    font-size: 0.9rem;
}

.page-actions {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

/* ----------------------------------------------------------------
   Cards
   ---------------------------------------------------------------- */

.card {
    background: #fff;
    border-radius: 10px;
    box-shadow: var(--bb-shadow-sm);
    border: 1px solid var(--bb-gray-border);
    overflow: hidden;
}

.card-body {
    padding: 1.25rem 1.5rem;
}

/* Stat cards (dashboard) */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stat-card {
    background: #fff;
    border-radius: 10px;
    box-shadow: var(--bb-shadow-sm);
    border: 1px solid var(--bb-gray-border);
    padding: 1.25rem 1.5rem;
    display: flex;
    align-items: flex-start;
    gap: 1rem;
}

.stat-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.stat-icon svg {
    width: 22px;
    height: 22px;
}

.stat-icon.blue { background: var(--bb-blue-bg); color: var(--bb-blue); }
.stat-icon.green { background: var(--bb-green-bg); color: var(--bb-green); }
.stat-icon.yellow { background: var(--bb-yellow-bg); color: var(--bb-yellow); }
.stat-icon.red { background: var(--bb-red-bg); color: var(--bb-red); }

.stat-content {
    min-width: 0;
}

.stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--bb-navy);
    line-height: 1.15;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--bb-gray);
    margin-top: 0.15rem;
}

/* ----------------------------------------------------------------
   Tables
   ---------------------------------------------------------------- */

.table-wrap {
    background: #fff;
    border-radius: 10px;
    box-shadow: var(--bb-shadow-sm);
    border: 1px solid var(--bb-gray-border);
    overflow: hidden;
    margin-bottom: 1.5rem;
}

.table-wrap table {
    margin: 0;
    border-collapse: collapse;
    width: 100%;
}

.table-wrap thead th {
    background: var(--bb-gray-light);
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--bb-gray);
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--bb-gray-border);
    text-align: left;
    white-space: nowrap;
}

.table-wrap tbody td {
    padding: 0.7rem 1rem;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.875rem;
    vertical-align: middle;
}

.table-wrap tbody tr:last-child td {
    border-bottom: none;
}

.table-wrap tbody tr:hover {
    background: #f8fafc;
}

.table-empty {
    text-align: center;
    padding: 2.5rem 1rem !important;
    color: var(--bb-gray);
}

.table-empty svg {
    display: block;
    margin: 0 auto 0.75rem;
    width: 40px;
    height: 40px;
    color: #d1d5db;
}

/* ----------------------------------------------------------------
   Buttons
   ---------------------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
    font-weight: 550;
    border-radius: 7px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
    white-space: nowrap;
    line-height: 1.4;
}

.btn svg {
    width: 16px;
    height: 16px;
}

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

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

.btn-secondary {
    background: #fff;
    color: var(--bb-navy);
    border-color: var(--bb-gray-border);
}

.btn-secondary:hover {
    background: var(--bb-gray-light);
    border-color: #d1d5db;
}

.btn-sm {
    padding: 0.3rem 0.65rem;
    font-size: 0.78rem;
}

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

.btn-danger:hover {
    background: var(--bb-red-bg);
}

/* ----------------------------------------------------------------
   Badges / Tags
   ---------------------------------------------------------------- */

.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    white-space: nowrap;
    gap: 0.3rem;
}

.badge-blue { background: var(--bb-blue-bg); color: #1d4ed8; }
.badge-green { background: var(--bb-green-bg); color: #15803d; }
.badge-yellow { background: var(--bb-yellow-bg); color: #a16207; }
.badge-red { background: var(--bb-red-bg); color: #dc2626; }
.badge-gray { background: var(--bb-gray-light); color: var(--bb-gray); }

/* Event type specific */
.event-access_granted { background: var(--bb-green-bg); color: #15803d; }
.event-access_denied { background: var(--bb-red-bg); color: #dc2626; }
.event-badge_unknown { background: var(--bb-yellow-bg); color: #a16207; }
.event-role_assigned { background: var(--bb-blue-bg); color: #1d4ed8; }
.event-employee_inactive { background: var(--bb-yellow-bg); color: #a16207; }
.event-employee_not_found { background: var(--bb-yellow-bg); color: #a16207; }
.event-endpoint_not_found { background: var(--bb-gray-light); color: var(--bb-gray); }

/* Status dots */
.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 0.4rem;
    flex-shrink: 0;
}

.status-dot.green { background: var(--bb-green); box-shadow: 0 0 0 3px var(--bb-green-bg); }
.status-dot.yellow { background: var(--bb-yellow); box-shadow: 0 0 0 3px var(--bb-yellow-bg); }
.status-dot.red { background: var(--bb-red); box-shadow: 0 0 0 3px var(--bb-red-bg); }
.status-dot.gray { background: #9ca3af; box-shadow: 0 0 0 3px var(--bb-gray-light); }

/* Endpoint type badges */
.type-hmi { background: #ede9fe; color: #6d28d9; }
.type-forklift { background: #fef3c7; color: #92400e; }
.type-door { background: #e0f2fe; color: #0369a1; }

/* Failsafe badges */
.failsafe-fail_closed { background: var(--bb-red-bg); color: #dc2626; }
.failsafe-fail_open { background: var(--bb-yellow-bg); color: #a16207; }

/* Active/Inactive status */
.status-active { background: var(--bb-green-bg); color: #15803d; }
.status-inactive { background: var(--bb-red-bg); color: #dc2626; }

/* ----------------------------------------------------------------
   Forms (inline, search bars)
   ---------------------------------------------------------------- */

.search-bar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.search-bar input[type="text"],
.search-bar input[type="search"] {
    flex: 1;
    padding: 0.55rem 0.85rem;
    font-size: 0.875rem;
    border: 1px solid var(--bb-gray-border);
    border-radius: 7px;
    background: #fff;
    margin: 0;
}

.search-bar input:focus {
    outline: none;
    border-color: var(--bb-navy);
    box-shadow: 0 0 0 3px rgba(0,48,87,0.08);
}

.search-bar select {
    padding: 0.55rem 0.85rem;
    font-size: 0.875rem;
    border: 1px solid var(--bb-gray-border);
    border-radius: 7px;
    background: #fff;
    margin: 0;
    min-width: 160px;
}

.inline-form {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}

.inline-form select {
    padding: 0.3rem 0.5rem;
    font-size: 0.8rem;
    border: 1px solid var(--bb-gray-border);
    border-radius: 5px;
    margin: 0;
}

/* Form cards (create/edit) */
.form-card {
    background: #fff;
    border: 1px solid var(--bb-gray-border);
    border-radius: 10px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--bb-shadow-sm);
}

.form-card fieldset {
    border: none;
    padding: 0;
    margin: 0;
}

.form-card label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: #374151;
    margin-bottom: 0.75rem;
}

.form-card input,
.form-card select,
.form-card textarea {
    margin-top: 0.25rem;
    padding: 0.55rem 0.75rem;
    font-size: 0.875rem;
    border: 1px solid var(--bb-gray-border);
    border-radius: 7px;
    width: 100%;
}

.form-card textarea {
    min-height: 80px;
    resize: vertical;
}

.form-card .form-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1rem;
}

/* ----------------------------------------------------------------
   HTMX Loading Indicator
   ---------------------------------------------------------------- */

.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-flex;
}

/* Top progress bar */
#page-loader {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--bb-sidebar-accent);
    z-index: 9999;
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.3s ease;
    pointer-events: none;
}

body.htmx-request #page-loader {
    transform: scaleX(0.8);
    transition: transform 8s cubic-bezier(0.4, 0, 0, 1);
}

/* Spinner */
.spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--bb-gray-border);
    border-top-color: var(--bb-navy);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ----------------------------------------------------------------
   Section dividers
   ---------------------------------------------------------------- */

.section-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.75px;
    color: var(--bb-gray);
    margin: 2rem 0 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-label::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--bb-gray-border);
}

/* ----------------------------------------------------------------
   Empty States
   ---------------------------------------------------------------- */

.empty-state {
    text-align: center;
    padding: 3rem 2rem;
    color: var(--bb-gray);
}

.empty-state svg {
    width: 48px;
    height: 48px;
    color: #d1d5db;
    margin-bottom: 1rem;
}

.empty-state h3 {
    font-size: 1rem;
    font-weight: 600;
    color: #374151;
    margin: 0 0 0.35rem;
}

.empty-state p {
    font-size: 0.875rem;
    margin: 0 0 1.25rem;
}

/* ----------------------------------------------------------------
   Status page cards
   ---------------------------------------------------------------- */

.status-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 1rem;
}

.status-card {
    background: #fff;
    border: 1px solid var(--bb-gray-border);
    border-radius: 10px;
    padding: 1.25rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    box-shadow: var(--bb-shadow-sm);
}

.status-card-icon {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: var(--bb-gray-light);
    color: var(--bb-gray);
}

.status-card-icon svg {
    width: 20px;
    height: 20px;
}

.status-card-info {
    flex: 1;
    min-width: 0;
}

.status-card-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: #1f2937;
    word-break: normal;
    overflow-wrap: normal;
    white-space: nowrap;
}

.status-card-detail {
    font-size: 0.78rem;
    color: var(--bb-gray);
    margin-top: 0.1rem;
    word-break: normal;
    overflow-wrap: normal;
}

.status-card-badge {
    flex-shrink: 0;
}

/* ----------------------------------------------------------------
   Training tree
   ---------------------------------------------------------------- */

.tree-view {
    padding: 1rem 0;
}

.tree-view ul {
    list-style: none;
    padding-left: 1.25rem;
    margin: 0;
    border-left: 2px solid var(--bb-gray-border);
}

.tree-view > ul {
    border-left: none;
    padding-left: 0;
}

.tree-view li {
    padding: 0.35rem 0;
    position: relative;
}

.tree-operator {
    display: inline-flex;
    align-items: center;
    padding: 0.15rem 0.45rem;
    border-radius: 4px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.tree-operator.op-and { background: var(--bb-blue-bg); color: #1d4ed8; }
.tree-operator.op-or { background: #fae8ff; color: #a21caf; }

.tree-leaf {
    font-size: 0.85rem;
    color: #374151;
}

.tree-leaf code {
    background: var(--bb-gray-light);
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    font-size: 0.8rem;
}

/* ----------------------------------------------------------------
   Utility classes
   ---------------------------------------------------------------- */

.text-muted { color: var(--bb-gray); }
.text-sm { font-size: 0.8rem; }
.text-xs { font-size: 0.72rem; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.font-mono { font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace; font-size: 0.85em; }
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.flex { display: flex; }
.items-center { align-items: center; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }
.hidden { display: none; }

/* ----------------------------------------------------------------
   Responsive
   ---------------------------------------------------------------- */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -260px;
        top: 0;
        bottom: 0;
        z-index: 999;
        transition: left 0.25s ease;
        box-shadow: var(--bb-shadow-lg);
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-toggle {
        display: flex;
    }

    main.content {
        padding: 1rem;
        padding-top: 3.5rem;
    }

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

    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .search-bar {
        flex-direction: column;
    }

    .search-bar select {
        min-width: auto;
    }
}

/* ----------------------------------------------------------------
   Override Pico defaults that conflict
   ---------------------------------------------------------------- */

article {
    margin: 0;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    background: transparent;
}

table {
    --pico-border-color: transparent;
}

h1, h2, h3, h4 {
    --pico-typography-spacing-vertical: 0;
}

select, input, textarea {
    --pico-border-radius: 7px;
}

/* Pico sets grid on nav ul - override for sidebar */
nav.sidebar ul {
    display: block !important;
    grid-template-columns: none !important;
}

nav.sidebar li {
    display: block !important;
    padding: 0 !important;
}

/* Pico sets width constraints on inputs - override for search bars */
.search-bar input,
.search-bar select,
.search-bar button {
    width: auto;
    margin-bottom: 0 !important;
    --pico-form-element-spacing-vertical: 0.55rem;
    --pico-form-element-spacing-horizontal: 0.85rem;
}

/* Pico button overrides */
.btn {
    --pico-form-element-spacing-vertical: 0;
    --pico-form-element-spacing-horizontal: 0;
    width: auto;
    margin-bottom: 0;
}

/* Pico select/input margin reset for inline forms */
.inline-form select,
.inline-form input,
.inline-form button {
    margin-bottom: 0 !important;
}

/* Status card grid - ensure horizontal layout */
.status-grid {
    grid-template-columns: repeat(2, 1fr);
}

.status-card {
    min-width: 0;
    overflow: hidden;
}

/* Form card inputs - reset Pico margin */
.form-card input,
.form-card select,
.form-card textarea {
    margin-bottom: 0;
}
