/*
 * MargSync UI — shared design system
 * ------------------------------------------------------------------
 * Namespaced under .ms-* so linking this file into a page can never
 * silently override that page's own existing selectors (.card, .btn,
 * table, th/td, etc.) before that page has been redesigned. Pages
 * adopt this system one at a time; this file itself is reusable
 * infrastructure, not a per-page stylesheet.
 *
 * Theme: dark is the default (:root), matching this app's existing
 * default (localStorage 'margCollTheme' falls back to 'dark' — that
 * default is intentionally unchanged by this redesign). [data-theme
 * ="light"] overrides. Both palettes aim for the same restrained,
 * enterprise-SaaS register — no neon, no glassmorphism, minimal shadow.
 */

:root {
    --ms-bg: #0b0d14;
    --ms-surface: #12141c;
    --ms-surface-2: #181b26;
    --ms-surface-3: #1f2330;
    --ms-border: rgba(255,255,255,0.10);
    --ms-border-strong: rgba(255,255,255,0.16);

    --ms-text-primary: #e7e9ee;
    --ms-text-secondary: #a3aab9;
    --ms-text-muted: #6b7280;

    --ms-primary: #6366f1;
    --ms-primary-hover: #7678f5;
    --ms-primary-soft: rgba(99,102,241,0.14);
    --ms-primary-text: #a5a8f7;

    --ms-success: #22c55e;
    --ms-success-soft: rgba(34,197,94,0.14);
    --ms-success-text: #4ade80;

    --ms-warning: #f59e0b;
    --ms-warning-soft: rgba(245,158,11,0.14);
    --ms-warning-text: #fbbf24;

    --ms-danger: #ef4444;
    --ms-danger-soft: rgba(239,68,68,0.14);
    --ms-danger-text: #f87171;

    --ms-info: #3b82f6;
    --ms-info-soft: rgba(59,130,246,0.14);
    --ms-info-text: #60a5fa;

    --ms-radius-sm: 6px;
    --ms-radius-md: 8px;
    --ms-radius-lg: 10px;

    --ms-shadow-sm: 0 1px 2px rgba(0,0,0,.24);
    --ms-shadow-card: 0 1px 3px rgba(0,0,0,.3);

    --ms-transition: 150ms ease;
}

[data-theme="light"] {
    --ms-bg: #f5f7fa;
    --ms-surface: #ffffff;
    --ms-surface-2: #f8fafc;
    --ms-surface-3: #eef1f6;
    --ms-border: #e5e7eb;
    --ms-border-strong: #d0d5dd;

    --ms-text-primary: #101828;
    --ms-text-secondary: #475467;
    --ms-text-muted: #98a2b3;

    --ms-primary: #4f46e5;
    --ms-primary-hover: #4338ca;
    --ms-primary-soft: #eef2ff;
    --ms-primary-text: #4338ca;

    --ms-success: #12b76a;
    --ms-success-soft: #ecfdf3;
    --ms-success-text: #067647;

    --ms-warning: #f79009;
    --ms-warning-soft: #fffaeb;
    --ms-warning-text: #b54708;

    --ms-danger: #f04438;
    --ms-danger-soft: #fef3f2;
    --ms-danger-text: #b42318;

    --ms-info: #2e90fa;
    --ms-info-soft: #eff8ff;
    --ms-info-text: #175cd3;

    --ms-shadow-sm: 0 1px 2px rgba(16,24,40,.04);
    --ms-shadow-card: 0 1px 3px rgba(16,24,40,.06);
}

/* ---------------------------------------------------------------- */
/* Base / typography                                                  */
/* ---------------------------------------------------------------- */
.ms-root, .ms-root * , .ms-root *::before, .ms-root *::after { box-sizing: border-box; }
.ms-root {
    font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--ms-text-primary);
    background: var(--ms-bg);
    font-size: 13px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}
.ms-h1 { font-size: 24px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.25; color: var(--ms-text-primary); }
.ms-h2 { font-size: 15px; font-weight: 600; color: var(--ms-text-primary); }
.ms-text-secondary { color: var(--ms-text-secondary); }
.ms-text-muted { color: var(--ms-text-muted); font-size: 12px; }

/* ---------------------------------------------------------------- */
/* App shell: sidebar + topbar + content                              */
/* ---------------------------------------------------------------- */
.ms-shell { display: flex; min-height: 100vh; }

.ms-sidebar {
    width: 210px;
    flex-shrink: 0;
    background: var(--ms-surface);
    border-right: 1px solid var(--ms-border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--ms-border-strong) transparent;
}
.ms-sidebar::-webkit-scrollbar { width: 6px; }
.ms-sidebar::-webkit-scrollbar-thumb { background: var(--ms-border-strong); border-radius: 3px; }
.ms-sidebar::-webkit-scrollbar-track { background: transparent; }
.ms-sidebar-brand { display: flex; align-items: center; gap: 9px; padding: 18px 18px 14px; }
.ms-sidebar-brand .ms-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ms-success); flex-shrink: 0; }
.ms-sidebar-brand-text { font-size: 15px; font-weight: 700; color: var(--ms-text-primary); letter-spacing: -0.01em; }

.ms-nav { flex: 1; padding: 4px 10px 10px; }
.ms-nav-group { margin-bottom: 4px; }
.ms-nav-group-label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ms-text-secondary); opacity: .88; padding: 14px 10px 6px; }
.ms-nav-link {
    display: flex; align-items: center; gap: 9px;
    padding: 8px 10px; margin-bottom: 2px;
    border-radius: var(--ms-radius-sm);
    font-size: 13px; font-weight: 500;
    color: var(--ms-text-secondary);
    text-decoration: none;
    transition: background var(--ms-transition), color var(--ms-transition), box-shadow var(--ms-transition);
}
.ms-nav-link svg { width: 16px; height: 16px; flex-shrink: 0; opacity: 0.85; }
.ms-nav-link:hover { background: var(--ms-surface-2); color: var(--ms-text-primary); }
.ms-nav-link.active { background: var(--ms-primary-soft); color: var(--ms-primary-text); font-weight: 600; box-shadow: inset 2px 0 0 0 var(--ms-primary); }
.ms-nav-link.active svg { opacity: 1; }

.ms-sidebar-footer { padding: 10px; border-top: 1px solid var(--ms-border); }
.ms-nav-link.ms-nav-logout { color: var(--ms-danger-text); }
.ms-nav-link.ms-nav-logout:hover { background: var(--ms-danger-soft); }

.ms-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.ms-topbar {
    height: 56px; flex-shrink: 0;
    display: flex; align-items: center; justify-content: flex-end; gap: 26px;
    padding: 0 24px;
    background: var(--ms-surface);
    border-bottom: 1px solid var(--ms-border);
    position: sticky; top: 0; z-index: 40;
}
.ms-topbar-item { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--ms-text-secondary); white-space: nowrap; }
.ms-topbar-item strong { color: var(--ms-text-primary); font-weight: 600; }
.ms-topbar-item svg { width: 16px; height: 16px; flex-shrink: 0; opacity: .8; }
.ms-topbar-sep { width: 1px; height: 18px; background: var(--ms-border-strong); flex-shrink: 0; }
.ms-topbar-stack-text { display: flex; flex-direction: column; line-height: 1.3; gap: 1px; }
.ms-topbar-stack-text small { font-size: 11px; font-weight: 500; color: var(--ms-text-muted); }
.ms-topbar-stack-text strong { font-size: 12.5px; font-weight: 600; color: var(--ms-text-primary); }
.ms-icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; border-radius: var(--ms-radius-sm);
    border: 1px solid var(--ms-border-strong); background: var(--ms-surface-2);
    color: var(--ms-text-secondary); cursor: pointer; transition: all var(--ms-transition);
}
.ms-icon-btn:hover { background: var(--ms-surface-3); color: var(--ms-text-primary); }
.ms-icon-btn svg { width: 16px; height: 16px; }

.ms-content { flex: 1; padding: 24px; max-width: 1760px; width: 100%; margin: 0 auto; }

/* ---------------------------------------------------------------- */
/* Page header                                                        */
/* ---------------------------------------------------------------- */
.ms-page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.ms-page-header p { font-size: 13px; color: var(--ms-text-secondary); margin-top: 3px; }
.ms-page-header-actions { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

/* ---------------------------------------------------------------- */
/* Cards / KPIs                                                       */
/* ---------------------------------------------------------------- */
.ms-card {
    background: var(--ms-surface);
    border: 1px solid var(--ms-border);
    border-radius: var(--ms-radius-lg);
    box-shadow: var(--ms-shadow-card);
}
.ms-card-pad { padding: 18px 20px; }
.ms-card-title {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    font-size: 13px; font-weight: 600; color: var(--ms-text-primary);
    padding: 16px 20px; border-bottom: 1px solid var(--ms-border);
}
.ms-card-title-sub { font-size: 11px; font-weight: 400; color: var(--ms-text-muted); }
.ms-card-link { font-size: 12px; font-weight: 500; color: var(--ms-primary-text); text-decoration: none; display: inline-flex; align-items: center; gap: 3px; }
.ms-card-link svg { width: 12px; height: 12px; }
.ms-card-link:hover { text-decoration: underline; }

.ms-kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.ms-kpi {
    background: var(--ms-surface);
    border: 1px solid var(--ms-border);
    border-radius: var(--ms-radius-lg);
    box-shadow: var(--ms-shadow-card);
    padding: 16px 18px;
}
.ms-kpi-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.ms-kpi-label { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 600; color: var(--ms-text-secondary); }
.ms-kpi-icon { width: 32px; height: 32px; border-radius: var(--ms-radius-sm); display: flex; align-items: center; justify-content: center; background: var(--ms-icon-bg, var(--ms-primary-soft)); color: var(--ms-icon-fg, var(--ms-primary-text)); flex-shrink: 0; }
.ms-kpi-icon svg { width: 16px; height: 16px; }
.ms-kpi-link { display: inline-flex; align-items: center; gap: 3px; font-size: 11.5px; font-weight: 500; color: var(--ms-text-muted); text-decoration: none; cursor: pointer; }
.ms-kpi-link svg { width: 12px; height: 12px; }
.ms-kpi-link:hover { color: var(--ms-primary-text); }
.ms-kpi-value { font-size: 25px; font-weight: 700; color: var(--ms-text-primary); letter-spacing: -0.4px; margin-bottom: 8px; cursor: default; }
.ms-kpi-foot { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--ms-text-muted); }
.ms-kpi-foot strong { color: var(--ms-text-secondary); font-weight: 600; }

/* ---------------------------------------------------------------- */
/* Filters                                                            */
/* ---------------------------------------------------------------- */
.ms-select, .ms-input {
    height: 36px;
    padding: 0 12px;
    border-radius: var(--ms-radius-sm);
    border: 1px solid var(--ms-border-strong);
    background: var(--ms-surface);
    color: var(--ms-text-primary);
    font-family: inherit;
    font-size: 13px;
    outline: none;
    transition: border-color var(--ms-transition);
}
.ms-select:focus, .ms-input:focus { border-color: var(--ms-primary); }
.ms-select:focus-visible, .ms-input:focus-visible, .ms-btn:focus-visible, .ms-icon-btn:focus-visible, .ms-nav-link:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--ms-primary-soft);
    border-color: var(--ms-primary);
}

/* ---------------------------------------------------------------- */
/* Buttons                                                            */
/* ---------------------------------------------------------------- */
.ms-btn {
    display: inline-flex; align-items: center; gap: 7px;
    height: 36px; padding: 0 14px;
    border-radius: var(--ms-radius-sm);
    font-family: inherit; font-size: 13px; font-weight: 600;
    border: 1px solid transparent; cursor: pointer;
    text-decoration: none; white-space: nowrap;
    transition: all var(--ms-transition);
}
.ms-btn svg { width: 15px; height: 15px; }
.ms-btn-primary { background: var(--ms-primary); color: #fff; }
.ms-btn-primary:hover { background: var(--ms-primary-hover); }
.ms-btn-secondary { background: var(--ms-surface); color: var(--ms-text-primary); border-color: var(--ms-border-strong); }
.ms-btn-secondary:hover { background: var(--ms-surface-2); }

/* ---------------------------------------------------------------- */
/* Tables                                                             */
/* ---------------------------------------------------------------- */
.ms-table-wrap { overflow-x: auto; }
.ms-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.ms-table th {
    background: var(--ms-surface-2); color: var(--ms-text-secondary);
    font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em;
    padding: 10px 16px; text-align: left; white-space: nowrap;
    border-bottom: 1px solid var(--ms-border);
}
.ms-table td { padding: 12px 16px; border-bottom: 1px solid var(--ms-border); vertical-align: middle; color: var(--ms-text-primary); }
.ms-table td strong { font-weight: 600; }
.ms-table tbody tr:hover { background: var(--ms-surface-2); }
.ms-table tbody tr:last-child td { border-bottom: none; }
.ms-num { text-align: right; font-variant-numeric: tabular-nums; }
.ms-empty-row { text-align: center; color: var(--ms-text-muted); padding: 28px !important; }

.ms-code { font-family: ui-monospace, 'SF Mono', Consolas, monospace; font-size: 12px; background: var(--ms-surface-3); padding: 2px 7px; border-radius: 5px; color: var(--ms-primary-text); }

/* ---------------------------------------------------------------- */
/* Badges                                                             */
/* ---------------------------------------------------------------- */
.ms-badge {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 2px 9px; border-radius: 100px;
    font-size: 11px; font-weight: 600;
    background: var(--ms-surface-3); color: var(--ms-text-secondary);
}
.ms-badge-primary { background: var(--ms-primary-soft); color: var(--ms-primary-text); }
.ms-badge-success { background: var(--ms-success-soft); color: var(--ms-success-text); }
.ms-badge-warning { background: var(--ms-warning-soft); color: var(--ms-warning-text); }
.ms-badge-danger  { background: var(--ms-danger-soft);  color: var(--ms-danger-text); }
.ms-badge-info    { background: var(--ms-info-soft);    color: var(--ms-info-text); }

/* ---------------------------------------------------------------- */
/* Responsive                                                         */
/* ---------------------------------------------------------------- */
@media (max-width: 1280px) {
    .ms-kpi-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 900px) {
    .ms-sidebar { position: fixed; left: 0; z-index: 100; transform: translateX(-100%); transition: transform var(--ms-transition); }
    .ms-sidebar.ms-sidebar-open { transform: translateX(0); }
    .ms-content { padding: 16px; }
}
@media (max-width: 640px) {
    .ms-kpi-grid { grid-template-columns: 1fr; }
}
