:root {
    --bg: #09090b;
    --surface: rgba(255,255,255,0.075);
    --surface-hover: rgba(255,255,255,0.115);
    --surface-active: rgba(255,255,255,0.17);
    --border: rgba(255,255,255,0.14);
    --border-subtle: rgba(255,255,255,0.085);
    --text-primary: #f0f0f0;
    --text-secondary: rgba(255,255,255,0.70);
    --text-tertiary: rgba(255,255,255,0.50);
    --text-hint: rgba(255,255,255,0.40);
    --text-muted: rgba(255,255,255,0.32);
    --text-faint: rgba(255,255,255,0.22);
    --sidebar-w: 220px;
    --sidebar-bg: #09090b;
    --avatar-bg: rgba(255,255,255,0.14);
    /* Accent colors */
    --green: #34d399;
    --green-dim: rgba(52,211,153,0.15);
    --blue: #60a5fa;
    --blue-dim: rgba(96,165,250,0.15);
    --amber: #fbbf24;
    --amber-dim: rgba(251,191,36,0.14);
    --red: #f87171;
    --red-dim: rgba(248,113,113,0.14);
    --cyan: #22d3ee;
    --cyan-dim: rgba(34,211,238,0.12);
    /* Components */
    --chart-text: rgba(255,255,255,0.60);
    --chart-grid: rgba(255,255,255,0.08);
    --tooltip-bg: rgba(24,24,27,0.96);
    --tooltip-border: rgba(255,255,255,0.15);
    --overlay: rgba(0,0,0,0.6);
    --badge-red: #ef4444;
    --shadow: 0 16px 48px rgba(0,0,0,0.5);
    --btn-radius: 10px;
    --btn-pill-radius: 999px;
    --btn-border-blue: rgba(96,165,250,0.36);
    --btn-border-red: rgba(248,113,113,0.42);
    --btn-shadow-focus: 0 0 0 3px rgba(96,165,250,0.18);
}

[data-theme="light"] {
    --bg: #f0f0f2;
    --surface: #ffffff;
    --surface-hover: rgba(0,0,0,0.04);
    --surface-active: rgba(0,0,0,0.07);
    --border: rgba(0,0,0,0.12);
    --border-subtle: rgba(0,0,0,0.07);
    --text-primary: #111111;
    --text-secondary: rgba(0,0,0,0.62);
    --text-tertiary: rgba(0,0,0,0.48);
    --text-hint: rgba(0,0,0,0.40);
    --text-muted: rgba(0,0,0,0.34);
    --text-faint: rgba(0,0,0,0.25);
    --sidebar-bg: #ffffff;
    --avatar-bg: rgba(0,0,0,0.09);
    --green: #059669;
    --green-dim: rgba(5,150,105,0.1);
    --blue: #2563eb;
    --blue-dim: rgba(37,99,235,0.1);
    --amber: #d97706;
    --amber-dim: rgba(217,119,6,0.1);
    --red: #dc2626;
    --red-dim: rgba(220,38,38,0.1);
    --cyan: #0891b2;
    --cyan-dim: rgba(8,145,178,0.1);
    --chart-text: rgba(0,0,0,0.55);
    --chart-grid: rgba(0,0,0,0.08);
    --tooltip-bg: rgba(255,255,255,0.98);
    --tooltip-border: rgba(0,0,0,0.12);
    --overlay: rgba(0,0,0,0.25);
    --badge-red: #dc2626;
    --shadow: 0 8px 32px rgba(0,0,0,0.12);
    --btn-border-blue: rgba(37,99,235,0.28);
    --btn-border-red: rgba(220,38,38,0.30);
    --btn-shadow-focus: 0 0 0 3px rgba(37,99,235,0.14);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    background-color: var(--bg);
    color: var(--text-primary);
    min-height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', sans-serif;
}

.sidebar-account-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: 2px;
}
.sidebar-account-row .sidebar-user { flex: 1; min-width: 0; }
.workspace-view-toggle {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: 5px;
    color: var(--text-hint);
    cursor: pointer;
    text-decoration: none;
}
.workspace-view-toggle:hover { color: var(--text-primary); background: var(--surface-hover); }
.workspace-view-toggle i { font-size: 0.76rem; }
.workspace-current-view { color: var(--text-secondary); font-weight: 600; }

/* ===== Unified button language ===== */
button, a {
    -webkit-tap-highlight-color: transparent;
}
.ui-btn,
.btn-save,
.btn-reset,
.btn-submit,
.btn-action,
.btn-browse,
.btn-resolve,
.btn-delete-product,
.btn-mini,
.btn-appeal,
.btn-ghost,
.date-sidebar-submit,
.ds-period-btn,
.period-chip,
.filter-pill,
.filter-btn,
.filter-reset,
.pill-menu-item,
.rank-toggle-btn,
.tab-btn,
.qv-chip,
.qv-detail-link,
.page-btn,
.ip-btn,
.ip-detect-btn,
.ip-page-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border-radius: var(--btn-radius);
    border: 1px solid var(--border);
    font-family: inherit;
    font-weight: 650;
    line-height: 1;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition: background 0.14s ease, border-color 0.14s ease, color 0.14s ease, box-shadow 0.14s ease, transform 0.14s ease;
}
.ui-btn:focus-visible,
.btn-save:focus-visible,
.btn-reset:focus-visible,
.btn-submit:focus-visible,
.btn-action:focus-visible,
.btn-browse:focus-visible,
.btn-resolve:focus-visible,
.btn-delete-product:focus-visible,
.btn-mini:focus-visible,
.btn-appeal:focus-visible,
.btn-ghost:focus-visible,
.date-sidebar-submit:focus-visible,
.ds-period-btn:focus-visible,
.period-chip:focus-visible,
.filter-pill:focus-visible,
.filter-btn:focus-visible,
.filter-reset:focus-visible,
.pill-menu-item:focus-visible,
.rank-toggle-btn:focus-visible,
.tab-btn:focus-visible,
.qv-chip:focus-visible,
.qv-detail-link:focus-visible,
.page-btn:focus-visible,
.ip-btn:focus-visible,
.ip-detect-btn:focus-visible,
.ip-page-btn:focus-visible {
    outline: none;
    box-shadow: var(--btn-shadow-focus);
}
.ui-btn:hover,
.btn-reset:hover,
.btn-action:hover,
.btn-browse:hover,
.btn-resolve:hover,
.btn-mini:hover,
.btn-ghost:hover,
.filter-reset:hover,
.tab-btn:hover,
.qv-detail-link:hover,
.page-btn:hover,
.ip-page-btn:hover {
    background: var(--surface-hover);
    color: var(--text-primary);
    border-color: var(--text-faint);
}
.ui-btn.primary,
.btn-save,
.btn-submit,
.date-sidebar-submit,
.btn-primary-dark,
.ip-btn:not(.secondary) {
    background: var(--blue);
    border-color: var(--blue);
    color: #fff;
}
.ui-btn.primary:hover,
.btn-save:hover,
.btn-submit:hover,
.date-sidebar-submit:hover,
.btn-primary-dark:hover,
.ip-btn:not(.secondary):hover {
    background: var(--blue);
    border-color: var(--blue);
    color: #fff;
    filter: brightness(1.05);
}
.ui-btn.secondary,
.btn-reset,
.btn-secondary-dark,
.btn-browse,
.btn-resolve,
.btn-mini,
.btn-ghost,
.filter-reset,
.page-btn,
.qv-detail-link,
.ip-btn.secondary {
    background: var(--surface);
    color: var(--text-secondary);
    border-color: var(--border);
}
.ui-btn.danger,
.btn-delete-product,
.btn-mini.danger,
.btn-resolve.danger,
.ip-detect-btn {
    background: transparent;
    color: var(--red);
    border-color: var(--btn-border-red);
}
.ui-btn.danger:hover,
.btn-delete-product:hover,
.btn-mini.danger:hover,
.btn-resolve.danger:hover,
.ip-detect-btn:hover {
    background: var(--red-dim);
    color: var(--red);
    border-color: var(--red);
}
.ui-btn.pill,
.ds-period-btn,
.period-chip,
.filter-pill,
.filter-btn,
.rank-toggle-btn,
.tab-btn,
.qv-chip {
    border-radius: var(--btn-pill-radius);
    background: var(--surface);
    color: var(--text-tertiary);
    border-color: var(--border);
}
.ui-btn.pill.active,
.ds-period-btn.active,
.period-chip.active,
.filter-pill.active,
.filter-btn.active,
.rank-toggle-btn.active,
.tab-btn.active,
.qv-chip.active,
.qv-chip.source-ai.active,
.qv-chip.source-original.active {
    background: var(--blue-dim);
    color: var(--blue);
    border-color: var(--btn-border-blue);
}
.btn-appeal {
    background: var(--blue-dim);
    color: var(--blue);
    border-color: var(--btn-border-blue);
}
.btn-appeal:hover {
    background: var(--blue);
    color: #fff;
    border-color: var(--blue);
}
.btn-appeal.has-pending {
    background: var(--amber-dim);
    color: var(--amber);
    border-color: rgba(251,191,36,0.36);
}
.btn-appeal.has-pending:hover {
    background: var(--amber);
    color: #111;
    border-color: var(--amber);
}
.ui-btn[disabled],
.ui-btn.disabled,
.btn-save:disabled,
.btn-submit:disabled,
.btn-action:disabled,
.btn-resolve:disabled,
.btn-appeal:disabled,
.date-sidebar-submit:disabled,
.ip-detect-btn:disabled {
    opacity: 0.48;
    cursor: not-allowed;
    pointer-events: none;
}

/* ===== Sidebar ===== */
.sidebar {
    position: fixed; left: 0; top: 0;
    width: var(--sidebar-w); height: 100vh;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    z-index: 100;
    transition: transform 0.25s;
}
body.preview-open .sidebar { z-index: 1; }
.sidebar-brand {
    padding: 20px 20px 16px;
    font-weight: 700; font-size: 1rem; letter-spacing: 0.3px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border);
}
.sidebar-nav {
    flex: 1; overflow-y: auto; padding: 12px 10px;
}
.sidebar-section {
    font-size: 0.65rem; color: var(--text-faint); text-transform: uppercase;
    letter-spacing: 0.8px; padding: 12px 10px 6px; font-weight: 500;
}
.sidebar-link {
    display: flex; align-items: center; justify-content: space-between;
    padding: 9px 12px; border-radius: 8px; font-size: 0.85rem;
    color: var(--text-secondary); text-decoration: none;
    transition: all 0.15s; margin-bottom: 2px;
}
.sidebar-link:hover { color: var(--text-primary); background: var(--surface-hover); }
.sidebar-link.active { color: var(--text-primary); background: var(--surface-active); font-weight: 500; }
.sidebar-badge {
    font-size: 0.65rem; font-weight: 600; min-width: 18px; height: 18px;
    line-height: 18px; text-align: center; border-radius: 9px; padding: 0 5px;
    background: var(--badge-red); color: #fff;
}
.sidebar-divider { height: 1px; background: var(--border); margin: 8px 10px; }

/* Sidebar footer / user */
.sidebar-footer {
    padding: 14px 16px; border-top: 1px solid var(--border);
}
.sidebar-user {
    display: flex; align-items: center; gap: 10px;
    padding: 6px 4px; border-radius: 8px; transition: background 0.15s;
}
.sidebar-user:hover { background: var(--surface-hover); }
.user-avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--avatar-bg);
    display: flex; align-items: center; justify-content: center;
    font-size: 0.85rem; font-weight: 600; color: var(--text-primary);
    flex-shrink: 0; cursor: pointer; overflow: hidden;
    transition: opacity 0.15s;
}
.user-avatar:hover { opacity: 0.8; }
.user-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 0.85rem; font-weight: 500; color: var(--text-primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 0.68rem; color: var(--text-hint); }
.sidebar-logout {
    display: block; margin-top: 10px; padding: 7px 12px; border-radius: 8px;
    font-size: 0.8rem; color: var(--text-tertiary); text-decoration: none;
    transition: all 0.15s; text-align: center;
}
.sidebar-logout:hover { color: var(--red); background: var(--red-dim); }
.theme-toggle {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 12px; border-radius: 8px; margin-top: 6px;
    font-size: 0.8rem; color: var(--text-tertiary); text-decoration: none;
    transition: all 0.15s; cursor: pointer; border: none; background: none; width: 100%;
}
.theme-toggle:hover { color: var(--text-primary); background: var(--surface-hover); }

/* ===== Main area ===== */
.main-wrapper {
    margin-left: var(--sidebar-w);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
.main-content {
    flex: 1 0 auto;
    padding: 28px 32px; min-width: 0;
}
.footer {
    flex: 0 0 auto;
    border-top: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 14px 32px;
    text-align: center;
}

/* Unified lightweight loading for shell and deferred panels */
.pg-shell-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: calc(100vh - 126px);
    padding: 0;
}
.pg-shell-spinner {
    position: relative;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 3px solid color-mix(in srgb, var(--text-faint) 42%, transparent);
    border-top-color: var(--amber);
    border-right-color: color-mix(in srgb, var(--amber) 72%, var(--blue));
    box-shadow: 0 0 0 1px color-mix(in srgb, var(--surface-active) 70%, transparent);
    animation: pgShellSpin 0.72s linear infinite;
}
.pg-shell-spinner::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--amber);
    transform: translate(19px, 2px);
    box-shadow: 0 0 14px color-mix(in srgb, var(--amber) 48%, transparent);
}
@keyframes pgShellSpin {
    to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
    .pg-shell-spinner {
        animation: none;
    }
}

/* Flash alerts */
.flash-container { padding: 16px 32px 0; }
.alert {
    background: var(--surface-hover); border: 1px solid var(--border);
    color: var(--text-secondary); border-radius: 10px;
    padding: 12px 16px; font-size: 0.85rem; margin-bottom: 12px;
    display: flex; align-items: center; justify-content: space-between;
}
.alert-danger { background: var(--red-dim); border-color: var(--red-dim); color: var(--red); }
.alert-success { background: var(--surface-hover); border-color: var(--border); }
.alert .close-btn { background: none; border: none; color: inherit; cursor: pointer; font-size: 1.1rem; opacity: 0.5; }
.alert .close-btn:hover { opacity: 1; }
.storage-banner {
    margin: 16px 32px 0;
    border: 1px solid var(--red-dim);
    background: var(--red-dim);
    color: var(--red);
    border-radius: 10px;
    padding: 11px 14px;
    font-size: 0.84rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.storage-banner .storage-title { font-weight: 700; color: var(--red); margin-right: 8px; }
.storage-banner button {
    border: 1px solid currentColor;
    background: transparent;
    color: inherit;
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.76rem;
    cursor: pointer;
    white-space: nowrap;
}
.scan-run-banner {
    position: fixed;
    top: auto;
    bottom: 18px;
    right: 24px;
    z-index: 1080;
    display: flex;
    width: 88px;
    max-width: calc(100vw - 48px);
    min-height: 28px;
    border: 1px solid var(--border-subtle);
    background: var(--surface);
    color: var(--text-secondary);
    border-radius: 999px;
    padding: 6px 8px;
    align-items: center;
    justify-content: space-between;
    gap: 7px;
    box-shadow: 0 8px 22px rgba(15,23,42,0.10);
    opacity: 0;
    transform: translateY(8px);
    pointer-events: none;
    transition: opacity 0.16s ease, transform 0.16s ease, width 0.18s ease, border-radius 0.18s ease, padding 0.18s ease, box-shadow 0.18s ease;
    backdrop-filter: blur(10px);
    cursor: default;
    outline: none;
}
.scan-run-banner.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
.scan-run-banner:hover,
.scan-run-banner:focus,
.scan-run-banner:focus-within {
    width: min(560px, calc(100vw - var(--sidebar-w) - 56px));
    border-radius: 14px;
    padding: 10px 14px;
    gap: 14px;
    box-shadow: 0 14px 34px rgba(15,23,42,0.16);
}
.scan-run-banner:focus-visible {
    box-shadow: 0 0 0 3px rgba(59,130,246,0.18), 0 14px 34px rgba(15,23,42,0.16);
}
.scan-run-banner.stalled {
    border-color: rgba(251,191,36,0.38);
    background: var(--amber-dim);
}
.scan-run-banner-main {
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.82rem;
    line-height: 1.35;
    flex: 1 1 auto;
}
.scan-run-banner-dot {
    width: 7px;
    height: 7px;
    border-radius: 999px;
    background: var(--blue);
    box-shadow: 0 0 0 4px rgba(59,130,246,0.10);
    flex: 0 0 auto;
}
.scan-run-banner-title {
    font-weight: 700;
    color: var(--blue);
    white-space: nowrap;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 0;
    opacity: 0;
    transform: translateX(4px);
    transition: max-width 0.18s ease, opacity 0.14s ease, transform 0.18s ease;
}
.scan-run-banner:hover .scan-run-banner-title,
.scan-run-banner:focus .scan-run-banner-title,
.scan-run-banner:focus-within .scan-run-banner-title {
    max-width: 190px;
    opacity: 1;
    transform: translateX(0);
}
.scan-run-banner.stalled .scan-run-banner-title {
    color: var(--amber);
}
.scan-run-banner-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-secondary);
    max-width: 0;
    opacity: 0;
    transform: translateX(-4px);
    transition: max-width 0.18s ease, opacity 0.14s ease, transform 0.18s ease;
}
.scan-run-banner:hover .scan-run-banner-text,
.scan-run-banner:focus .scan-run-banner-text,
.scan-run-banner:focus-within .scan-run-banner-text {
    max-width: 390px;
    opacity: 1;
    transform: translateX(0);
}
.scan-run-banner-progress {
    width: 58px;
    height: 4px;
    border-radius: 999px;
    background: rgba(59,130,246,0.16);
    overflow: hidden;
    flex: 0 0 auto;
    transition: width 0.18s ease;
}
.scan-run-banner:hover .scan-run-banner-progress,
.scan-run-banner:focus .scan-run-banner-progress,
.scan-run-banner:focus-within .scan-run-banner-progress {
    width: 120px;
}
.scan-run-banner-progress span {
    display: block;
    height: 100%;
    width: 0;
    border-radius: inherit;
    background: var(--blue);
    transition: width 0.35s ease;
}
.scan-run-banner.stalled .scan-run-banner-dot,
.scan-run-banner.stalled .scan-run-banner-progress span {
    background: var(--amber);
}
.scan-run-banner.stalled .scan-run-banner-dot {
    box-shadow: 0 0 0 4px rgba(251,191,36,0.12);
}
.scan-run-banner.stalled .scan-run-banner-progress {
    background: rgba(251,191,36,0.18);
}

/* Grade badges */
.badge-grade { font-size: 0.78rem; padding: 3px 10px; border-radius: 6px; font-weight: 600; }
.badge-grade-aplus { background: var(--blue-dim) !important; color: var(--blue) !important; }
.badge-grade-a     { background: var(--green-dim) !important; color: var(--green) !important; }
.badge-grade-b     { background: var(--amber-dim) !important; color: var(--amber) !important; }
.badge-grade-c     { background: var(--red-dim) !important; color: var(--red) !important; }

/* Status colors */
.status-completed { color: var(--green); }
.status-running   { color: var(--blue); }
.status-error     { color: var(--red); }
.status-pending   { color: var(--text-muted); }

/* Generic card */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; color: var(--text-primary); }

/* Table override */
.table { color: var(--text-secondary); width: 100%; border-collapse: collapse; }
.table th { font-weight: 500; font-size: 0.78rem; color: var(--text-tertiary) !important; text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid var(--border); background: transparent !important; padding: 10px 16px; }
.table td { border-bottom: 1px solid var(--border-subtle); vertical-align: middle; padding: 12px 16px; }
.table-hover tbody tr:hover { background: var(--surface) !important; }
.text-muted { color: var(--text-muted) !important; }

/* Mobile toggle */
.mobile-toggle {
    display: none; position: fixed; top: 12px; left: 12px; z-index: 200;
    width: 36px; height: 36px; border-radius: 8px;
    background: var(--surface-active); border: 1px solid var(--border);
    color: var(--text-primary); font-size: 1.1rem;
    cursor: pointer; align-items: center; justify-content: center;
}
.mobile-overlay {
    display: none; position: fixed; inset: 0; background: var(--overlay); z-index: 99;
}

@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .main-wrapper { margin-left: 0; }
    .main-content { padding: 20px 16px; padding-top: 56px; }
    .flash-container { padding: 56px 16px 0; }
    .scan-run-banner {
        bottom: 14px;
        right: 14px;
        left: auto;
        width: 86px;
        max-width: calc(100vw - 28px);
    }
    .scan-run-banner:hover,
    .scan-run-banner:focus,
    .scan-run-banner:focus-within {
        left: 16px;
        right: 16px;
        width: auto;
        max-width: none;
        align-items: stretch;
        flex-direction: column;
    }
    .scan-run-banner:hover .scan-run-banner-progress,
    .scan-run-banner:focus .scan-run-banner-progress,
    .scan-run-banner:focus-within .scan-run-banner-progress { width: 100%; }
    .scan-run-banner:hover .scan-run-banner-text,
    .scan-run-banner:focus .scan-run-banner-text,
    .scan-run-banner:focus-within .scan-run-banner-text { max-width: 100%; }
    .mobile-toggle { display: flex; }
    .mobile-overlay.show { display: block; }
}
