/* ============================================================
   MagDyn Base — Design System Stylesheet
   ----------------------------------------------------------------
   Drop-in stylesheet for any MagDyn web application. Provides
   design tokens (CSS variables) and the core component classes
   that make every MagDyn app look like part of the same family.

   USAGE
   -----
   1. Drop magdyn-base.css into your app's assets/css/ folder.
   2. Link it from your page head:
        <link rel="stylesheet" href="assets/css/magdyn-base.css">
   3. (Optional) Add an app-specific stylesheet AFTER this one for
      any page-specific or feature-specific styles. Always reuse
      the CSS variables defined here — never introduce new colours.

   The classes below cover ~95% of typical admin-tool UI:
   layout shell, page header, cards, buttons, tables, forms,
   status pills, alerts, stat cards for dashboards, modals,
   tabs, and print styles. The accompanying design-system
   prompt explains the personality and the recipes.
   ============================================================ */

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    /* ---- Surfaces ---- */
    --bg:            #f4f5f7;   /* page background */
    --surface:       #ffffff;   /* cards, inputs */
    --surface-alt:   #f8f9fb;   /* table headers, hover tints */
    --border:        #e3e5ea;   /* default borders */
    --border-strong: #d0d4dc;   /* input borders, dividers needing emphasis */

    /* ---- Text ---- */
    --text:       #1a1f2e;     /* primary body text */
    --text-muted: #6b7280;     /* labels, secondary info */
    --text-light: #9aa1ad;     /* tertiary placeholders */

    /* ---- Brand ---- */
    --primary:       #1e3a8a;
    --primary-dark:  #172a63;
    --primary-light: #eef2ff;

    /* ---- Status colours (semantic only) ---- */
    --success:    #047857;   --success-bg: #d1fae5;
    --warn:       #b45309;   --warn-bg:    #fef3c7;
    --danger:     #b91c1c;   --danger-bg:  #fee2e2;
    --info:       #1d4ed8;   --info-bg:    #dbeafe;

    /* ---- Sidebar (dark scheme) ---- */
    --sidebar-bg:        #0f172a;
    --sidebar-bg-hover:  #1e293b;
    --sidebar-bg-active: #1e3a8a;
    --sidebar-text:      #cbd5e1;
    --sidebar-text-dim:  #94a3b8;
    --sidebar-text-very-dim: #64748b;
    --sidebar-border:    #1e293b;

    /* ---- Shape ---- */
    --shadow:    0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-lg: 0 10px 25px rgba(15, 23, 42, 0.1);
    --radius:    6px;
    --radius-lg: 10px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
                 "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

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

h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 18px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }

kbd {
    display: inline-block;
    padding: 1px 6px;
    background: var(--surface-alt);
    border: 1px solid var(--border-strong);
    border-bottom-width: 2px;
    border-radius: 4px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 11px;
    color: var(--text);
}
code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12.5px;
    background: var(--surface-alt);
    padding: 1px 5px;
    border-radius: 3px;
}

/* ============================================================
   Layout shell — sidebar + main content
   ============================================================ */
.layout {
    display: flex;
    min-height: 100vh;
}
.main {
    flex: 1;
    padding: 24px 28px;
    min-width: 0;
    overflow-x: auto;
}

/* ---- Sidebar (dark) ---- */
.sidebar {
    width: 240px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    height: 100vh;
    position: sticky;
    top: 0;
    overflow: hidden;
    transition: width 0.18s ease;
}
.sidebar.collapsed { width: 64px; }

.brand {
    padding: 16px 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--sidebar-border);
    flex-shrink: 0;
    position: relative;
}
.brand-mark {
    width: 38px; height: 38px;
    border-radius: 8px;
    background: transparent;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}
.brand-mark img {
    width: 100%; height: 100%;
    object-fit: contain;
    display: block;
}
.brand-mark.big { width: 64px; height: 64px; border-radius: 12px; }
.brand-text { flex: 1; min-width: 0; overflow: hidden; }
.brand-title {
    color: white; font-weight: 600; font-size: 15px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.brand-sub {
    color: var(--sidebar-text-very-dim); font-size: 11px;
    text-transform: uppercase; letter-spacing: 0.06em;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.sidebar-toggle {
    position: absolute;
    top: 50%; right: 8px;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--sidebar-text-dim);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    font-size: 14px;
    line-height: 1;
}
.sidebar-toggle:hover { background: var(--sidebar-bg-hover); color: white; }

.sidebar.collapsed .brand { padding: 16px 8px; justify-content: center; }
.sidebar.collapsed .brand-text { display: none; }

.nav {
    flex: 1;
    overflow-y: auto;
    padding: 10px 8px;
}
.nav::-webkit-scrollbar { width: 6px; }
.nav::-webkit-scrollbar-track { background: transparent; }
.nav::-webkit-scrollbar-thumb { background: #334155; border-radius: 3px; }
.nav::-webkit-scrollbar-thumb:hover { background: #475569; }
.nav { scrollbar-width: thin; scrollbar-color: #334155 transparent; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    margin: 2px 0;
    color: var(--sidebar-text);
    border-radius: 6px;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-decoration: none;
    transition: background 0.12s, color 0.12s;
}
.nav-item:hover { background: var(--sidebar-bg-hover); color: white; text-decoration: none; }
.nav-item.active { background: var(--sidebar-bg-active); color: white; }
.nav-item .ico { width: 18px; text-align: center; opacity: 0.85; font-size: 14px; flex-shrink: 0; }
.nav-label { overflow: hidden; text-overflow: ellipsis; }

.sidebar.collapsed .nav-item {
    justify-content: center;
    padding: 8px;
    position: relative;
}
.sidebar.collapsed .nav-label { display: none; }
.sidebar.collapsed .nav-item::after {
    /* tooltip on hover when collapsed */
    content: attr(data-label);
    position: absolute;
    left: 100%;
    margin-left: 8px;
    padding: 4px 8px;
    background: var(--sidebar-bg-hover);
    color: white;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s;
    z-index: 10;
}
.sidebar.collapsed .nav-item:hover::after { opacity: 1; }

.nav-section {
    margin-top: 14px;
    padding: 4px 12px;
    font-size: 10px;
    color: var(--sidebar-text-very-dim);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 600;
}
.sidebar.collapsed .nav-section {
    padding: 6px 0;
    text-align: center;
    position: relative;
}
.sidebar.collapsed .nav-section span { display: none; }
.sidebar.collapsed .nav-section::before {
    content: '';
    display: block;
    width: 24px; height: 1px;
    background: var(--sidebar-border);
    margin: 0 auto;
}

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--sidebar-border);
    flex-shrink: 0;
}
.user-card { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; min-width: 0; }
.user-avatar {
    width: 32px; height: 32px;
    border-radius: 50%;
    background: #475569; color: white;
    display: flex; align-items: center; justify-content: center;
    font-size: 13px; font-weight: 600;
    flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; overflow: hidden; }
.user-name { color: white; font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { color: var(--sidebar-text-very-dim); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }

.logout {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px;
    color: var(--sidebar-text-dim);
    font-size: 12.5px;
    text-decoration: none;
    border-radius: 5px;
    transition: background 0.12s;
}
.logout:hover { background: var(--sidebar-bg-hover); color: white; text-decoration: none; }

.account-link {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px;
    margin-bottom: 4px;
    color: var(--sidebar-text-dim);
    font-size: 12.5px;
    text-decoration: none;
    border-radius: 5px;
    transition: background 0.12s;
}
.account-link:hover { background: var(--sidebar-bg-hover); color: white; text-decoration: none; }

.sidebar.collapsed .sidebar-footer { padding: 10px 8px; }
.sidebar.collapsed .user-meta { display: none; }
.sidebar.collapsed .user-card { justify-content: center; margin-bottom: 8px; }
.sidebar.collapsed .logout { justify-content: center; padding: 8px; }
.sidebar.collapsed .account-link { justify-content: center; padding: 8px; }

/* ============================================================
   Page header — H1 + actions on right
   ============================================================ */
.page-head {
    display: flex; justify-content: space-between; align-items: flex-end;
    margin-bottom: 22px; gap: 16px;
}
.page-head h1 { margin-bottom: 2px; }
.page-head .muted { font-size: 13px; }
.head-actions { display: flex; gap: 8px; align-items: center; }

/* ============================================================
   Cards
   ============================================================ */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: 18px;
}
.card-head {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    display: flex; justify-content: space-between; align-items: center;
}
.card-body { padding: 18px; }

/* ============================================================
   Dashboard stat cards
   ============================================================ */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 22px;
}
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 18px;
    box-shadow: var(--shadow);
    border-left: 3px solid var(--primary);
}
.stat-card.stat-warn { border-left-color: var(--warn); }
.stat-card.stat-success { border-left-color: var(--success); }
.stat-card.stat-info { border-left-color: var(--info); }
.stat-card.stat-danger { border-left-color: var(--danger); }
.stat-label {
    font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em;
    color: var(--text-muted); font-weight: 600;
    margin-bottom: 6px;
}
.stat-value { font-size: 22px; font-weight: 700; color: var(--text); letter-spacing: -0.01em; }
.stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ============================================================
   Grid utilities
   ============================================================ */
.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}
.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 18px;
}
@media (max-width: 1024px) {
    .grid-2 { grid-template-columns: 1fr; }
    .grid-3 { grid-template-columns: 1fr; }
}

/* ============================================================
   Data tables
   ============================================================ */
.data-table {
    width: 100%;
    /* `border-collapse: separate` (not `collapse`) is essential for
       `position: sticky` on thead th to work. With `collapse`, the
       browser merges adjacent borders into a single rendered border,
       which interacts badly with sticky positioning — the thead loses
       its pin and scrolls along with rows. `separate` + `border-spacing: 0`
       produces visually identical borders (since we set 0 spacing) while
       leaving sticky intact. */
    border-collapse: separate;
    border-spacing: 0;
    font-size: 13px;
}
.data-table th {
    text-align: left;
    padding: 10px 14px;
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}
.data-table td {
    padding: 11px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: #fafbfc; }
.data-table .r { text-align: right; }
/* Header alignment is forced LEFT for every column regardless of the
   .r / .c body-cell modifier. The numeric-column convention right-
   aligns the cell content but the column LABEL reads better
   left-aligned alongside its peers. Override comes after .data-table
   .r so it wins by source order. */
.data-table thead th.r,
.data-table thead th.c {
    text-align: left;
}
.data-table .c { text-align: center; }
.data-table .empty { text-align: center; color: var(--text-muted); padding: 30px; }
.data-table .small { font-size: 11.5px; }

/* ============================================================
   Status pills
   ============================================================ */
.pill {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
/* Generic semantic pills — use these or define your own following the same pattern */
.pill-draft     { background: #e5e7eb;          color: #374151; }
.pill-pending   { background: var(--warn-bg);   color: var(--warn); }
.pill-active    { background: var(--info-bg);   color: var(--info); }
.pill-success   { background: var(--success-bg);color: var(--success); }
.pill-paid      { background: var(--success-bg);color: var(--success); }
.pill-cancelled { background: var(--danger-bg); color: var(--danger); }
.pill-warn      { background: var(--warn-bg);   color: var(--warn); }
.pill-info      { background: var(--info-bg);   color: var(--info); }
.pill-danger    { background: var(--danger-bg); color: var(--danger); }
.pill-neutral   { background: #e5e7eb;          color: #374151; }

/* ============================================================
   Buttons
   ============================================================ */
.btn {
    display: inline-block;
    padding: 8px 16px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.12s ease;
    line-height: 1.4;
}
.btn:hover { background: #f5f6f8; text-decoration: none; }
.btn:disabled, .btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); color: white; }
.btn-success { background: var(--success); color: white; border-color: var(--success); }
.btn-success:hover { background: #036046; color: white; }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-danger:hover { background: #991717; color: white; }
.btn-warn { background: var(--warn); color: white; border-color: var(--warn); }
.btn-warn:hover { background: #92400e; color: white; }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-alt); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 11px 22px; font-size: 14px; }
.btn-block { display: block; width: 100%; }

.link { font-size: 12.5px; font-weight: 500; }

/* ============================================================
   Forms
   ============================================================ */
.form-card { padding: 22px; }
.form-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}
.form-grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}
.field { display: flex; flex-direction: column; }
.field.span-2 { grid-column: span 2; }
.field.span-3 { grid-column: span 3; }
.field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.field input[type="text"],
.field input[type="email"],
.field input[type="number"],
.field input[type="date"],
.field input[type="datetime-local"],
.field input[type="time"],
.field input[type="password"],
.field input[type="url"],
.field input[type="tel"],
.field input[type="search"],
.field select,
.field textarea,
.modal input[type="text"],
.modal textarea,
.search input,
.search select {
    padding: 8px 11px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    font-size: 13.5px;
    font-family: inherit;
    background: white;
    color: var(--text);
    transition: border-color 0.12s, box-shadow 0.12s;
}
.field input:focus, .field select:focus, .field textarea:focus,
.modal input:focus, .modal textarea:focus,
.search input:focus, .search select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(30, 58, 138, 0.1);
}
.field textarea { resize: vertical; min-height: 80px; }

.field-error { color: var(--danger); font-size: 12px; margin-top: 4px; }
.field-hint  { color: var(--text-muted); font-size: 12px; margin-top: 4px; }

.switch { display: flex; align-items: center; gap: 8px; padding-top: 6px; cursor: pointer; }
.switch input { width: 16px; height: 16px; }

.form-actions {
    margin-top: 22px;
    padding-top: 18px;
    border-top: 1px solid var(--border);
    display: flex; gap: 8px;
}

.section-title {
    margin-top: 22px;
    margin-bottom: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

/* ============================================================
   Totals box (for invoice-like documents)
   ============================================================ */
.totals {
    margin-top: 18px;
    margin-left: auto;
    width: 320px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 18px;
    background: #fafbfc;
}
.totals-row {
    display: flex; justify-content: space-between;
    padding: 5px 0;
    font-size: 13.5px;
}
.total-grand {
    border-top: 1px solid var(--border-strong);
    margin-top: 6px; padding-top: 9px;
    font-size: 15px;
    font-weight: 700;
}

/* ============================================================
   Search input bar (typically lives in head-actions)
   ============================================================ */
.search { display: flex; gap: 6px; }
.search input { width: 240px; }

/* ============================================================
   Alerts
   ============================================================ */
.alert {
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 13.5px;
    border-left: 3px solid;
}
.alert-success { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.alert-error   { background: var(--danger-bg);  color: var(--danger);  border-color: var(--danger); }
.alert-warn    { background: var(--warn-bg);    color: var(--warn);    border-color: var(--warn); }
.alert-info    { background: var(--info-bg);    color: var(--info);    border-color: var(--info); }

/* ============================================================
   Modal (uses .modal.open to show)
   ============================================================ */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.6);
    z-index: 1000;
    align-items: center; justify-content: center;
}
.modal.open { display: flex; }
.modal-content {
    background: white;
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 90%; max-width: 460px;
    box-shadow: var(--shadow-lg);
}
.modal-content h3 { margin-bottom: 8px; font-size: 17px; }
.modal-content p { color: var(--text-muted); margin-bottom: 14px; font-size: 13.5px; }
.modal-content label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 5px;
    margin-top: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.modal-content input,
.modal-content textarea {
    width: 100%;
    padding: 8px 11px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    font-size: 13.5px;
    font-family: inherit;
}

/* ============================================================
   Tabs
   ============================================================ */
.tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 18px;
}
.tab {
    padding: 8px 16px;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    text-decoration: none;
}
.tab:hover { color: var(--text); text-decoration: none; }
.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

/* ============================================================
   Login page (centred card on dark backdrop)
   ============================================================ */
.login-body {
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    min-height: 100vh;
    display: flex; align-items: center; justify-content: center;
    padding: 20px;
}
.login-wrap { width: 100%; max-width: 400px; }
.login-card {
    background: white;
    border-radius: 14px;
    padding: 36px 32px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-brand { text-align: center; margin-bottom: 26px; }
.login-brand .brand-mark { margin: 0 auto 14px; }
.login-brand .brand-mark.big { width: 80px; height: 80px; }
.login-brand .brand-mark.big img { width: 100%; height: 100%; object-fit: contain; display: block; }
.login-brand h1 { font-size: 22px; }
.login-brand p { color: var(--text-muted); font-size: 13px; margin-top: 4px; }
.login-form label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 5px;
    margin-top: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.login-form input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    font-size: 14px;
    margin-bottom: 4px;
}
.login-form button { margin-top: 22px; }
.login-hint {
    margin-top: 22px;
    padding: 12px;
    background: var(--surface-alt);
    border-radius: var(--radius);
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
}

/* ============================================================
   Helpers
   ============================================================ */
.muted { color: var(--text-muted); }
.text-warn    { color: var(--warn);    font-weight: 600; }
.text-danger  { color: var(--danger);  font-weight: 600; }
.text-success { color: var(--success); font-weight: 600; }
.text-info    { color: var(--info);    font-weight: 600; }
.small { font-size: 12px; }
.mono  { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }

/* ============================================================
   Print
   ============================================================ */
@media print {
    .sidebar, .page-head, .head-actions, .no-print, .alert { display: none !important; }
    .layout { display: block; height: auto; min-height: 0; }
    .main { padding: 0; height: auto; overflow: visible; }
    body { background: white; }
}
