/*
 * Application design system.
 *
 * Colors are NEVER hardcoded here — every color is a CSS custom property
 * (--c-*) resolved at runtime from /theme.css, which is generated from the
 * active tenant's theme JSON (writable/tenants/{slug}/themes/*.json). This
 * file only defines structure, spacing, typography and behavior.
 */

:root {
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --radius-sm: 8px;
    --radius: 10px;
    --radius-lg: 14px;
    --sidebar-width: 264px;
    --sidebar-collapsed-width: 76px;
    --header-height: 64px;
    --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);

    /* Fallbacks so the shell renders sanely even before /theme.css loads */
    --c-primary: #2563EB;
    --c-primary-hover: #1D4ED8;
    --c-secondary: #14B8A6;
    --c-success: #16A34A;
    --c-danger: #DC2626;
    --c-warning: #F59E0B;
    --c-info: #0EA5E9;
    --c-bg: #F8FAFC;
    --c-surface: #FFFFFF;
    --c-sidebar: #FFFFFF;
    --c-header: #FFFFFF;
    --c-text: #1F2937;
    --c-text-muted: #6B7280;
    --c-border: #E5E7EB;
    --c-menu-hover: #EEF4FF;
    --c-menu-active: #2563EB;
    --c-menu-active-text: #FFFFFF;
    --c-card: #FFFFFF;
    --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
    --shadow-lg: 0 12px 24px -6px rgba(16, 24, 40, .12);
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    font-family: var(--font-sans);
    background: var(--c-bg);
    color: var(--c-text);
    font-size: 14.5px;
    -webkit-font-smoothing: antialiased;
    transition: background-color var(--transition), color var(--transition);
}

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

::selection { background: color-mix(in srgb, var(--c-primary) 25%, transparent); }

/* ---------------------------------------------------------------- layout */

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

.app-sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--c-sidebar);
    border-right: 1px solid var(--c-border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; bottom: 0; left: 0;
    z-index: 1030;
    transition: width var(--transition), transform var(--transition);
}

.app-shell.sidebar-collapsed .app-sidebar { width: var(--sidebar-collapsed-width); }

.app-main {
    flex: 1;
    margin-left: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    transition: margin-left var(--transition);
}

.app-shell.sidebar-collapsed .app-main { margin-left: var(--sidebar-collapsed-width); }

.app-header {
    height: var(--header-height);
    background: var(--c-header);
    border-bottom: 1px solid var(--c-border);
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: 0 1.25rem;
    position: sticky;
    top: 0;
    z-index: 1020;
}

.app-content {
    flex: 1;
    padding: 1.5rem;
}

.app-footer {
    padding: 1rem 1.5rem;
    color: var(--c-text-muted);
    font-size: 12.5px;
    border-top: 1px solid var(--c-border);
}

.app-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
}

.page-title { font-size: 1.35rem; font-weight: 650; letter-spacing: -0.01em; margin: 0; }

.breadcrumb-bar { font-size: 12.5px; color: var(--c-text-muted); margin-bottom: .25rem; }
.breadcrumb-bar a { color: var(--c-text-muted); }
.breadcrumb-bar a:hover { color: var(--c-primary); }

@media (max-width: 991.98px) {
    .app-sidebar { transform: translateX(-100%); }
    .app-shell.sidebar-mobile-open .app-sidebar { transform: translateX(0); box-shadow: var(--shadow-lg); }
    .app-main, .app-shell.sidebar-collapsed .app-main { margin-left: 0; }
    .sidebar-backdrop {
        display: none;
        position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 1025;
    }
    .app-shell.sidebar-mobile-open .sidebar-backdrop { display: block; }
}

/* --------------------------------------------------------------- sidebar */

.sidebar-brand {
    height: var(--header-height);
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 0 1.1rem;
    border-bottom: 1px solid var(--c-border);
    font-weight: 700;
    font-size: 1.05rem;
    white-space: nowrap;
    overflow: hidden;
    color: var(--c-text);
}

.sidebar-brand .brand-mark {
    width: 32px; height: 32px; border-radius: var(--radius-sm);
    background: var(--c-primary); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 800; flex-shrink: 0;
}

.sidebar-nav { flex: 1; overflow-y: auto; padding: .75rem .6rem; }
.sidebar-nav::-webkit-scrollbar { width: 6px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--c-border); border-radius: 3px; }

.sidebar-search { padding: 0 .6rem .5rem; }
.sidebar-search input {
    background: var(--c-bg); border: 1px solid var(--c-border); border-radius: var(--radius-sm);
    font-size: 13px;
}

.nav-section-label {
    font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
    color: var(--c-text-muted); padding: .9rem .6rem .3rem; font-weight: 600;
}

/*
 * Multi-level nav (unlimited nesting). Visual weight steps down with depth
 * so a 3-4 level tree still reads as a hierarchy at a glance:
 *   depth 0  section    — icon, semibold, full-size
 *   depth 1  sub-section — icon, medium weight, 1px smaller
 *   depth 2+ plain link — small dot instead of an icon, regular weight, muted
 * A thin guideline on nested groups (like VS Code / Linear) ties children
 * visually back to their parent without needing heavier borders/boxes.
 */
.nav-link-item {
    position: relative;
    display: flex; align-items: center; gap: .6rem;
    padding: .5rem .65rem; border-radius: var(--radius-sm);
    color: var(--c-text); font-size: 13.5px; font-weight: 500;
    transition: background var(--transition), color var(--transition), padding-left var(--transition);
    cursor: pointer; white-space: nowrap;
}

.nav-link-item:hover { background: var(--c-menu-hover); color: var(--c-primary); }

.nav-link-item.active {
    background: var(--c-menu-active); color: var(--c-menu-active-text);
    font-weight: 600;
}
.nav-link-item.active .nav-icon { color: var(--c-menu-active-text); }
.nav-link-item.active .nav-dot { background: var(--c-menu-active-text); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c-menu-active-text) 30%, transparent); }

.nav-icon { width: 18px; height: 18px; flex-shrink: 0; color: var(--c-text-muted); transition: color var(--transition); }
.nav-link-item.active .nav-icon, .nav-link-item:hover .nav-icon { color: currentColor; }

/* depth 2+: a small dot takes the icon's place — keeps alignment consistent
   without the visual noise of a full icon on every leaf. */
.nav-dot {
    width: 6px; height: 6px; flex-shrink: 0; border-radius: 50%;
    background: var(--c-text-muted); opacity: .55;
    transition: background var(--transition), opacity var(--transition);
}
.nav-link-item:hover .nav-dot { background: var(--c-primary); opacity: 1; }

.nav-label { overflow: hidden; text-overflow: ellipsis; }

/* Sub-sections read slightly quieter than top-level ones until active/hovered. */
.nav-link-item[data-depth="1"] { font-size: 13px; font-weight: 500; color: var(--c-text); }
.nav-link-item[data-depth="1"] .nav-icon { width: 16px; height: 16px; }
.nav-link-item[data-depth="1"].active { font-weight: 600; }

.nav-link-item[data-depth="2"], .nav-link-item[data-depth="3"] {
    font-size: 12.8px; font-weight: 400; color: var(--c-text-muted);
}
.nav-link-item[data-depth="2"]:hover, .nav-link-item[data-depth="3"]:hover,
.nav-link-item[data-depth="2"].active, .nav-link-item[data-depth="3"].active { color: inherit; }

/* A group whose current page is somewhere inside it stays legible even when
   collapsed again — user knows at a glance which branch they're working in. */
.nav-group.has-active-child > .nav-link-item { color: var(--c-primary); font-weight: 600; }
.nav-group.has-active-child > .nav-link-item .nav-icon,
.nav-group.has-active-child > .nav-link-item .nav-dot { color: var(--c-primary); background: var(--c-primary); }

.nav-link-item .chevron { margin-left: auto; width: 14px; height: 14px; flex-shrink: 0; color: var(--c-text-muted); transition: transform var(--transition); }
.nav-group.open > .nav-link-item .chevron { transform: rotate(90deg); }
.nav-group.has-active-child > .nav-link-item .chevron { color: var(--c-primary); }

.nav-badge {
    margin-left: auto; font-size: 10.5px; padding: .1rem .4rem; border-radius: 999px;
    background: var(--c-danger); color: #fff; font-weight: 700;
}

.nav-children {
    max-height: 0; overflow: hidden; transition: max-height var(--transition);
    margin-left: 1.05rem; padding-left: .75rem;
    border-left: 1px solid var(--c-border);
}
.nav-group.open > .nav-children { max-height: 1200px; }
.nav-group.has-active-child > .nav-children { border-left-color: color-mix(in srgb, var(--c-primary) 35%, var(--c-border)); }

.app-shell.sidebar-collapsed .sidebar-brand span,
.app-shell.sidebar-collapsed .nav-link-item span,
.app-shell.sidebar-collapsed .nav-section-label,
.app-shell.sidebar-collapsed .sidebar-search,
.app-shell.sidebar-collapsed .chevron { display: none; }
.app-shell.sidebar-collapsed .nav-link-item { justify-content: center; }
/* Collapsed mode has no flyout for nested items yet — only top-level icons
   are reachable, so hide the (inert) nested tree rather than leave a stray
   guideline with nothing visible under it. */
.app-shell.sidebar-collapsed .nav-children { display: none; }

/* ---------------------------------------------------------------- header */

.header-btn {
    width: 38px; height: 38px; border-radius: var(--radius-sm);
    display: flex; align-items: center; justify-content: center;
    background: transparent; border: 1px solid transparent; color: var(--c-text-muted);
    transition: background var(--transition), border-color var(--transition);
}
.header-btn:hover { background: var(--c-menu-hover); color: var(--c-primary); }
.header-btn svg { width: 19px; height: 19px; }

.header-search {
    max-width: 380px; flex: 1;
    background: var(--c-bg); border: 1px solid var(--c-border); border-radius: var(--radius);
}
.header-search input { background: transparent; border: 0; font-size: 13.5px; }
.header-search input:focus { box-shadow: none; }

.avatar-circle {
    width: 34px; height: 34px; border-radius: 50%;
    background: var(--c-primary); color: #fff; font-weight: 700; font-size: 13px;
    display: flex; align-items: center; justify-content: center;
}

.dot-indicator {
    position: absolute; top: 6px; right: 6px; width: 8px; height: 8px; border-radius: 50%;
    background: var(--c-danger); border: 2px solid var(--c-header);
}

/* ----------------------------------------------------------------- cards */

.card {
    background: var(--c-card);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}
.card-header { background: transparent; border-bottom: 1px solid var(--c-border); font-weight: 600; }

.stat-card { padding: 1.1rem 1.25rem; }
.stat-card .stat-value { font-size: 1.7rem; font-weight: 700; letter-spacing: -0.02em; }
.stat-card .stat-label { color: var(--c-text-muted); font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-card .stat-icon {
    width: 42px; height: 42px; border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center;
    background: color-mix(in srgb, var(--c-primary) 12%, transparent); color: var(--c-primary);
}

/* --------------------------------------------------------------- widgets */

.widget-drag-handle { cursor: grab; color: var(--c-text-muted); }
.widget-placeholder { border: 2px dashed var(--c-border); border-radius: var(--radius-lg); opacity: .5; }

/* ---------------------------------------------------------------- forms */

.form-control, .form-select {
    border-color: var(--c-border);
    border-radius: var(--radius-sm);
    font-size: 13.8px;
}
.form-control:focus, .form-select:focus {
    border-color: var(--c-primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--c-primary) 18%, transparent);
}
.form-floating > label { color: var(--c-text-muted); font-size: 13.5px; }
.form-section { padding: 1.1rem 0; border-top: 1px solid var(--c-border); }
.form-section:first-child { border-top: 0; padding-top: 0; }
.form-section-title { font-size: 12.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--c-text-muted); margin-bottom: .75rem; font-weight: 700; }

.meta-field[data-hidden-by-rule="1"] { display: none !important; }

/* ----------------------------------------------------------------- grid */

.data-grid-wrap { border: 1px solid var(--c-border); border-radius: var(--radius-lg); overflow: hidden; background: var(--c-card); }
.data-grid-toolbar { padding: .85rem 1rem; border-bottom: 1px solid var(--c-border); display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.data-grid { width: 100%; border-collapse: collapse; font-size: 13.3px; }
.data-grid thead th {
    position: sticky; top: 0; background: var(--c-bg); z-index: 1;
    text-align: left; font-weight: 650; color: var(--c-text-muted);
    padding: .65rem .85rem; border-bottom: 1px solid var(--c-border);
    white-space: nowrap; user-select: none; cursor: pointer;
}
.data-grid thead th.sortable:hover { color: var(--c-primary); }
.data-grid tbody td { padding: .6rem .85rem; border-bottom: 1px solid var(--c-border); vertical-align: middle; }
.data-grid tbody tr:hover { background: var(--c-menu-hover); }
.data-grid tbody tr:last-child td { border-bottom: 0; }
.data-grid-pagination { padding: .7rem 1rem; display: flex; align-items: center; justify-content: space-between; border-top: 1px solid var(--c-border); font-size: 12.8px; color: var(--c-text-muted); }

.badge-soft-success { background: color-mix(in srgb, var(--c-success) 15%, transparent); color: var(--c-success); }
.badge-soft-danger { background: color-mix(in srgb, var(--c-danger) 15%, transparent); color: var(--c-danger); }
.badge-soft-warning { background: color-mix(in srgb, var(--c-warning) 18%, transparent); color: var(--c-warning); }
.badge-soft-info { background: color-mix(in srgb, var(--c-info) 15%, transparent); color: var(--c-info); }
.badge-soft-secondary { background: color-mix(in srgb, var(--c-text-muted) 15%, transparent); color: var(--c-text-muted); }

/* -------------------------------------------------------------- skeleton */

.skeleton {
    background: linear-gradient(90deg, var(--c-border) 25%, color-mix(in srgb, var(--c-border) 50%, transparent) 37%, var(--c-border) 63%);
    background-size: 400% 100%;
    animation: skeleton-loading 1.4s ease infinite;
    border-radius: var(--radius-sm);
}
@keyframes skeleton-loading { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }

/* --------------------------------------------------------------- toasts */

.toast-stack { position: fixed; top: 1.25rem; right: 1.25rem; z-index: 1080; display: flex; flex-direction: column; gap: .6rem; }

/* ----------------------------------------------------------- animations */

.fade-in { animation: fadeIn .2s ease both; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/*
 * .page-transition sits on .app-content — the wrapper every page's Bootstrap
 * modals are written inside. Two hazards for such a wrapper, both hit here:
 *
 *  1. transform keyframes make the element the containing block for
 *     `position: fixed` descendants (modals get positioned/clipped relative
 *     to the wrapper instead of the viewport);
 *  2. an opacity animation whose effect KEEPS APPLYING after it finishes
 *     (`animation-fill-mode: both`/`forwards`) creates a *permanent*
 *     stacking context, trapping the modal's z-index inside the wrapper —
 *     the sidebar (z-1030) and Bootstrap's body-level backdrop (z-1050)
 *     then paint over the modal (z-1055-but-only-locally) and clicks land
 *     on the backdrop instead of the modal's buttons.
 *
 * So: opacity-only keyframes AND no persistent fill. `backwards` covers the
 * pre-animation frame only; once the 180ms run ends the animation effect is
 * fully dropped and no stacking context remains. app.js additionally
 * reparents any opening modal to <body>, which immunizes modals against any
 * future ancestor transform/opacity/filter regardless of this rule.
 */
@keyframes pageFadeIn { from { opacity: 0; } to { opacity: 1; } }
.page-transition { animation: pageFadeIn .18s ease backwards; }

/* ---------------------------------------------------------- auth screens */

.auth-shell {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: var(--c-bg); padding: 1.5rem;
}
.auth-card {
    width: 100%; max-width: 400px; background: var(--c-card); border: 1px solid var(--c-border);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: 2.25rem;
}
.auth-logo { width: 46px; height: 46px; border-radius: var(--radius); background: var(--c-primary); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 800; margin: 0 auto 1rem; }

/* ------------------------------------------------------------ utilities */

.text-muted-c { color: var(--c-text-muted) !important; }
.bg-surface { background: var(--c-surface) !important; }
.border-c { border-color: var(--c-border) !important; }
.rounded-lg { border-radius: var(--radius-lg) !important; }
.shadow-soft { box-shadow: var(--shadow) !important; }

.btn-primary { background: var(--c-primary); border-color: var(--c-primary); }
.btn-primary:hover, .btn-primary:focus { background: var(--c-primary-hover); border-color: var(--c-primary-hover); }
.btn-outline-primary { color: var(--c-primary); border-color: var(--c-primary); }
.btn-outline-primary:hover { background: var(--c-primary); border-color: var(--c-primary); }
.text-primary-c { color: var(--c-primary) !important; }

.mobile-bottom-nav {
    display: none;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1040;
    background: var(--c-header); border-top: 1px solid var(--c-border);
    padding: .4rem .5rem calc(.4rem + env(safe-area-inset-bottom));
}
.mobile-bottom-nav .nav-link-item { flex-direction: column; gap: .15rem; font-size: 10.5px; padding: .35rem; }
@media (max-width: 767.98px) {
    .mobile-bottom-nav { display: flex; justify-content: space-around; }
    .app-content { padding-bottom: 4.5rem; }
}

.fab {
    position: fixed; right: 1.25rem; bottom: 5rem; z-index: 1045;
    width: 54px; height: 54px; border-radius: 50%;
    background: var(--c-primary); color: #fff; display: none;
    align-items: center; justify-content: center; box-shadow: var(--shadow-lg); border: 0;
}

/* Help Center article body: Markdown-rendered content needs its own type scale. */
.help-article-body h2 { font-size: 1.15rem; margin-top: 1.5rem; }
.help-article-body h3 { font-size: 1rem; margin-top: 1.25rem; }
.help-article-body img { max-width: 100%; border-radius: var(--radius-md, 6px); }
.help-article-body pre { background: var(--c-surface-alt, #f4f5f7); padding: .75rem; border-radius: 6px; overflow-x: auto; }
.help-article-body table { width: 100%; }

@media print {
    .app-sidebar, .app-header, .app-footer, .sidebar-backdrop,
    .breadcrumb-bar, .app-toolbar, .col-lg-3, .list-group-item-action[href]:not(.help-article-body a) { display: none !important; }
    .app-main { margin-left: 0 !important; }
    .app-content { padding: 0 !important; }
    .card { border: 0 !important; box-shadow: none !important; }
}
@media (max-width: 767.98px) { .fab.fab-mobile { display: flex; } }
