/* ============================================================
   AppDiags Dashboard - Dark Theme
   ============================================================ */

/* ----- Design Tokens ----- */
:root {
    --bg:               #0f172a;
    --surface:          #1e293b;
    --surface-elevated: #334155;
    --border:           #475569;
    --text-primary:     #f1f5f9;
    --text-secondary:   #94a3b8;
    --accent:           #3b82f6;
    --accent-hover:     #2563eb;
    --accent-subtle:    rgba(59, 130, 246, 0.12);
    --success:          #22c55e;
    --success-subtle:   rgba(34, 197, 94, 0.12);
    --warning:          #f59e0b;
    --warning-subtle:   rgba(245, 158, 11, 0.12);
    --error:            #ef4444;
    --error-subtle:     rgba(239, 68, 68, 0.12);
    --fatal:            #dc2626;
    --fatal-subtle:     rgba(220, 38, 38, 0.15);

    --sidebar-width:    250px;
    --radius:           8px;
    --radius-sm:        4px;
    --radius-lg:        12px;
    --spacing:          24px;
    --transition:       150ms ease;
    --font-sans:        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                        Helvetica, Arial, sans-serif, "Apple Color Emoji";
    --font-mono:        "SF Mono", Menlo, Monaco, "Cascadia Code", Consolas,
                        monospace;
}

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

html {
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
}

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

img {
    max-width: 100%;
}

/* ----- Scrollbar ----- */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}
::-webkit-scrollbar-track {
    background: var(--bg);
}
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Firefox */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--border) var(--bg);
}

/* ============================================================
   Sidebar
   ============================================================ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform var(--transition);
}

.sidebar-header {
    padding: 20px var(--spacing);
    border-bottom: 1px solid var(--border);
}

.sidebar-logo {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.025em;
}
.sidebar-logo:hover {
    color: var(--accent);
    text-decoration: none;
}

/* Navigation */
.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
}

.sidebar-nav ul {
    list-style: none;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px var(--spacing);
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
    border-left: 3px solid transparent;
    transition: color var(--transition), background var(--transition),
                border-color var(--transition);
}
.nav-link:hover {
    color: var(--text-primary);
    background: var(--accent-subtle);
    text-decoration: none;
}
.nav-link.active {
    color: var(--accent);
    background: var(--accent-subtle);
    border-left-color: var(--accent);
}

.nav-icon {
    width: 18px;
    text-align: center;
    font-size: 0.7rem;
    opacity: 0.7;
}

/* Sidebar footer / user info */
.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 16px var(--spacing);
}

.sidebar-user {
    margin-bottom: 8px;
}

.user-email {
    font-size: 0.8rem;
    color: var(--text-secondary);
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout-btn {
    width: 100%;
    background: none;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
    padding: 6px 0;
    text-align: left;
    justify-content: flex-start;
}

/* ============================================================
   Main Content
   ============================================================ */
.main-content {
    margin-left: var(--sidebar-width);
    padding: var(--spacing);
    min-height: 100vh;
}

.page-header {
    margin-bottom: var(--spacing);
}

.page-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* ============================================================
   Cards
   ============================================================ */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--spacing);
    margin-bottom: var(--spacing);
}

.card-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 16px;
}

/* ============================================================
   Stat Cards
   ============================================================ */
.stat-cards,
.stats-summary {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
    margin-bottom: var(--spacing);
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    transition: border-color var(--transition);
}
.stat-card:hover {
    border-color: var(--accent);
}

.stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.2;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* ============================================================
   Filter Bar
   ============================================================ */
.filter-bar {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 20px;
    margin-bottom: var(--spacing);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.filter-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 160px;
}

.filter-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.filter-actions {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    flex-shrink: 0;
}

/* ============================================================
   Form Elements
   ============================================================ */
.form-group {
    margin-bottom: 16px;
}

.form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
}
.form-row .form-group {
    flex: 1;
    min-width: 180px;
}

.form-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-input,
.form-select,
.form-textarea,
input[type="text"],
input[type="email"],
input[type="password"],
input[type="datetime-local"],
input[type="search"],
input[type="number"],
select,
textarea {
    width: 100%;
    padding: 8px 12px;
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text-primary);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus,
input:focus,
select:focus,
textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}

.form-input::placeholder,
input::placeholder,
textarea::placeholder {
    color: var(--text-secondary);
    opacity: 0.5;
}

.form-select,
select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

.form-textarea,
textarea {
    resize: vertical;
    min-height: 64px;
}

.form-actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 500;
    line-height: 1.4;
    color: var(--text-primary);
    background: var(--surface-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition), border-color var(--transition),
                color var(--transition);
    white-space: nowrap;
    user-select: none;
}
.btn:hover {
    background: var(--border);
    text-decoration: none;
}
.btn:active {
    transform: translateY(1px);
}
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

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

.btn-secondary {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text-secondary);
}
.btn-secondary:hover {
    color: var(--text-primary);
    background: var(--surface-elevated);
}

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

.btn-sm {
    padding: 4px 10px;
    font-size: 0.78rem;
}

/* ============================================================
   Tables - Log Table & Data Table
   ============================================================ */
.log-table-container,
.table-responsive {
    overflow-x: auto;
}

.log-table,
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.log-table th,
.data-table th {
    position: sticky;
    top: 0;
    background: var(--surface-elevated);
    color: var(--text-secondary);
    font-weight: 600;
    text-align: left;
    padding: 10px 12px;
    border-bottom: 2px solid var(--border);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
    z-index: 5;
}

.log-table td,
.data-table td {
    padding: 8px 12px;
    border-bottom: 1px solid rgba(71, 85, 105, 0.3);
    color: var(--text-primary);
    vertical-align: top;
}

/* Alternating rows */
.log-table tbody tr:nth-child(even),
.data-table tbody tr:nth-child(even) {
    background: rgba(30, 41, 59, 0.5);
}
.log-table tbody tr:nth-child(odd),
.data-table tbody tr:nth-child(odd) {
    background: transparent;
}

/* Hover */
.log-table tbody tr:hover,
.data-table tbody tr:hover {
    background: var(--accent-subtle);
}

/* Column widths */
.col-time     { width: 160px; }
.col-level    { width: 80px; }
.col-category { width: 120px; }
.col-message  { min-width: 200px; }
.col-details  { width: 60px; text-align: center; }
.col-actions  { width: 120px; text-align: right; }

/* Expanded row */
.log-row-expanded {
    display: none;
}
.log-row-expanded.visible {
    display: table-row;
}
.log-row-expanded td {
    background: var(--surface);
    padding: 16px 24px;
    border-left: 3px solid var(--accent);
}
.log-row-expanded pre {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: var(--text-secondary);
    white-space: pre-wrap;
    word-break: break-all;
    line-height: 1.5;
    margin: 0;
}

/* Scroll sentinel for infinite scroll */
.scroll-sentinel {
    height: 1px;
}

/* Table empty / loading states */
.table-empty,
.table-loading {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* ============================================================
   Badges
   ============================================================ */
.badge {
    display: inline-block;
    padding: 2px 8px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-radius: 10px;
    line-height: 1.6;
    white-space: nowrap;
}

.badge-debug {
    background: rgba(148, 163, 184, 0.15);
    color: var(--text-secondary);
}
.badge-info {
    background: var(--accent-subtle);
    color: var(--accent);
}
.badge-warning {
    background: var(--warning-subtle);
    color: var(--warning);
}
.badge-error {
    background: var(--error-subtle);
    color: var(--error);
}
.badge-fatal {
    background: var(--fatal-subtle);
    color: var(--fatal);
}

/* ============================================================
   Alerts
   ============================================================ */
.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    line-height: 1.5;
    margin-bottom: 16px;
    border: 1px solid;
}

.alert-success {
    background: var(--success-subtle);
    border-color: rgba(34, 197, 94, 0.3);
    color: var(--success);
}

.alert-error {
    background: var(--error-subtle);
    border-color: rgba(239, 68, 68, 0.3);
    color: var(--error);
}

.alert-warning {
    background: var(--warning-subtle);
    border-color: rgba(245, 158, 11, 0.3);
    color: var(--warning);
}

/* ============================================================
   Key Display
   ============================================================ */
.key-display {
    background: var(--bg);
    border: 1px dashed var(--accent);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    color: var(--accent);
    word-break: break-all;
    line-height: 1.6;
    margin: 12px 0;
    position: relative;
    user-select: all;
}

/* ============================================================
   Chart Container
   ============================================================ */
.chart-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: var(--spacing);
    position: relative;
}
.chart-container canvas {
    width: 100% !important;
    max-height: 300px;
}

/* ============================================================
   Live Indicator
   ============================================================ */
.live-indicator {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    vertical-align: middle;
    transition: background var(--transition);
}
.live-indicator.active {
    background: var(--success);
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5); }
    50%      { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
}

/* ============================================================
   Empty State
   ============================================================ */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 64px 24px;
    text-align: center;
    color: var(--text-secondary);
}
.empty-state p {
    max-width: 360px;
    font-size: 0.9rem;
    line-height: 1.6;
}

/* ============================================================
   Loading Spinner
   ============================================================ */
.loading-spinner {
    display: inline-block;
    width: 28px;
    height: 28px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

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

/* ============================================================
   Modal
   ============================================================ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition);
}
.modal-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--spacing);
    width: 90%;
    max-width: 520px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    transform: translateY(8px);
    transition: transform var(--transition);
}
.modal-overlay.visible .modal {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.modal-title {
    font-size: 1.1rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.25rem;
    padding: 4px;
    line-height: 1;
    transition: color var(--transition);
}
.modal-close:hover {
    color: var(--text-primary);
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

/* ============================================================
   Login Page
   ============================================================ */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: var(--spacing);
}

.login-container {
    width: 100%;
    max-width: 380px;
}

.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 32px;
}

.login-header {
    text-align: center;
    margin-bottom: 24px;
}

.login-logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.login-subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.login-form .form-group {
    margin-bottom: 16px;
}

.login-submit {
    width: 100%;
    margin-top: 8px;
}

/* ============================================================
   Create-form card (Apps page)
   ============================================================ */
.create-app-card {
    border-left: 3px solid var(--accent);
}

/* ============================================================
   Responsive - Mobile
   ============================================================ */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
    }

    .main-content {
        margin-left: 0;
        padding: 16px;
    }

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

    .filter-row {
        flex-direction: column;
    }

    .filter-group {
        min-width: 100%;
    }

    .form-row {
        flex-direction: column;
    }
    .form-row .form-group {
        min-width: 100%;
    }

    .log-table th:nth-child(3),
    .log-table td:nth-child(3) {
        display: none;
    }
}

@media (max-width: 480px) {
    .stat-cards,
    .stats-summary {
        grid-template-columns: 1fr;
    }
}

/* ============================================================
   Accessibility
   ============================================================ */

/* Screen-reader only - visually hidden but accessible */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Skip-to-content link - visible on focus for keyboard users */
.sr-skip-link {
    position: absolute;
    top: -100%;
    left: 8px;
    z-index: 9999;
    padding: 8px 16px;
    background: var(--accent);
    color: #fff;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    text-decoration: none;
    transition: top 0.15s ease;
}
.sr-skip-link:focus {
    top: 8px;
    outline: 2px solid #fff;
    outline-offset: 2px;
}

/* Focus indicators for keyboard navigation */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Log rows that are keyboard-focusable */
.log-row[tabindex] {
    cursor: pointer;
}
.log-row[tabindex]:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

/* Ensure disabled buttons are not confusing */
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
