/* ============================================================================
   AutoTrade — Shared Responsive Layer
   ----------------------------------------------------------------------------
   Sits on top of Bootstrap 5.1 (loaded as bootstrap.min.css OR bootstrap.rtl.min.css
   depending on the active culture). Owns:
     - Off-canvas sidebar behavior (mobile drawer / desktop fixed pane) for the
       User & Owner dashboard layouts.
     - Responsive Landing navbar tweaks layered over landing-page.css.
     - RTL/LTR direction-aware spacing so existing CSS keeps working under both.
     - Common topbar / page-content rhythm that adapts to phone, tablet, desktop.
   Visual theme (colors, gradients, fonts) is intentionally NOT touched here —
   that lives in landing-page.css / dashboard.css and remains the source of truth.
   ========================================================================= */

:root {
    --at-sidebar-width: 260px;
    --at-topbar-height: 64px;
    --at-navbar-height: 64px;
    --at-content-gutter: 1.25rem;
}

/* Bootstrap Icons baseline: align icons with text and inherit color. */
.bi {
    line-height: 1;
    vertical-align: -0.125em;
    fill: currentColor;
}

/* ============================================================================
   1. LANDING NAVBAR (works with existing <header> in landing-page.css too)
   ========================================================================= */
.at-navbar {
    --bs-navbar-padding-y: 0.6rem;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    position: sticky;
    top: 0;
    z-index: 1030;
}

.at-navbar .navbar-brand {
    font-weight: 700;
    color: #e2e8f0 !important;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.at-navbar .nav-link {
    color: rgba(226, 232, 240, 0.85) !important;
    font-weight: 500;
    transition: color 0.2s ease;
}

.at-navbar .nav-link:hover,
.at-navbar .nav-link:focus {
    color: #22d3ee !important;
}

.at-navbar .navbar-toggler {
    border-color: rgba(255, 255, 255, 0.15);
    color: #e2e8f0;
}

.at-navbar .navbar-toggler:focus {
    box-shadow: 0 0 0 0.2rem rgba(34, 211, 238, 0.25);
}

.at-navbar .form-select-sm.at-lang {
    background-color: rgba(15, 23, 42, 0.6);
    color: #e2e8f0;
    border-color: rgba(255, 255, 255, 0.12);
    min-width: 130px;
}

/* Mobile: stack nav items below brand */
@media (max-width: 991.98px) {
    .at-navbar .navbar-collapse {
        margin-top: 0.75rem;
        padding: 0.75rem 0;
        border-top: 1px solid rgba(255, 255, 255, 0.06);
    }

    .at-navbar .at-auth-area {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
}

/* ============================================================================
   2. DASHBOARD SHELL (User + Owner) with offcanvas sidebar
   ----------------------------------------------------------------------------
   Markup pattern:
     <div class="at-shell">
       <aside class="at-sidebar offcanvas-lg offcanvas-start" id="atSidebar">...</aside>
       <div class="at-main">
         <header class="at-topbar">...</header>
         <main class="at-content">...</main>
       </div>
     </div>
   At ≥lg the offcanvas is forced visible & fixed; below lg it slides in/out.
   ========================================================================= */
.at-shell {
    min-height: 100vh;
    width: 100%;
    /* DO NOT use overflow-x: hidden here — it breaks position:sticky on the
       topbar by establishing a new scrolling/clipping context for descendants.
       overflow-x: clip is the modern equivalent that does NOT break sticky.
       Fallback: any wide elements clamp themselves with max-width: 100%. */
    overflow-x: clip;
}

/* Safari < 16 lacks overflow:clip; degrade gracefully without trapping sticky */
@supports not (overflow-x: clip) {
    .at-shell { overflow-x: visible; }
}

.at-sidebar {
    background: linear-gradient(180deg, #0f172a 0%, #020617 100%);
    color: #e2e8f0;
    border-right: 1px solid rgba(255, 255, 255, 0.06);
    width: var(--at-sidebar-width);
}

[dir="rtl"] .at-sidebar {
    border-right: none;
    border-left: 1px solid rgba(255, 255, 255, 0.06);
}

.at-sidebar .at-sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1.25rem 1.25rem;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.3px;
    color: #fff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.at-sidebar .at-sidebar-brand .bi {
    font-size: 1.5rem;
    color: #22d3ee;
}

.at-sidebar .at-nav {
    list-style: none;
    margin: 0;
    padding: 0.75rem 0.5rem;
    flex: 1 1 auto;
    overflow-y: auto;
}

.at-sidebar .at-nav a {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.7rem 0.95rem;
    border-radius: 0.6rem;
    color: rgba(226, 232, 240, 0.78);
    text-decoration: none;
    font-weight: 500;
    transition: background-color 0.2s ease, color 0.2s ease;
    white-space: nowrap;
}

.at-sidebar .at-nav a .bi {
    font-size: 1.1rem;
    flex-shrink: 0;
}

.at-sidebar .at-nav a:hover {
    background-color: rgba(255, 255, 255, 0.06);
    color: #fff;
}

.at-sidebar .at-nav a.active {
    background: linear-gradient(135deg, rgba(34, 211, 238, 0.2), rgba(59, 130, 246, 0.2));
    color: #22d3ee;
    box-shadow: inset 0 0 0 1px rgba(34, 211, 238, 0.25);
}

.at-sidebar .at-sidebar-footer {
    padding: 0.9rem 1rem;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.at-sidebar .at-user-card {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.at-sidebar .at-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, #22d3ee, #3b82f6);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.at-sidebar .at-user-info {
    overflow: hidden;
    min-width: 0;
}

.at-sidebar .at-user-info h4 {
    font-size: 0.92rem;
    margin: 0;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.at-sidebar .at-user-info p {
    font-size: 0.78rem;
    margin: 0;
    color: rgba(226, 232, 240, 0.55);
}

/* Desktop ≥992px: pin sidebar, push main */
@media (min-width: 992px) {
    .at-sidebar.offcanvas-lg {
        position: fixed;
        top: 0;
        bottom: 0;
        height: 100vh;
        z-index: 1040;
        display: flex;
        flex-direction: column;
        visibility: visible !important;
        transform: none !important;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.25);
    }

    html:not([dir="rtl"]) .at-sidebar.offcanvas-lg { left: 0; }
    html[dir="rtl"] .at-sidebar.offcanvas-lg { right: 0; box-shadow: -4px 0 20px rgba(0, 0, 0, 0.25); }

    .at-main {
        min-height: 100vh;
    }

    html:not([dir="rtl"]) .at-main { margin-left: var(--at-sidebar-width); }
    html[dir="rtl"] .at-main { margin-right: var(--at-sidebar-width); }
}

/* Mobile <992px: offcanvas drawer */
@media (max-width: 991.98px) {
    .at-sidebar {
        width: min(85vw, 320px);
    }

    .at-main {
        margin-left: 0 !important;
        margin-right: 0 !important;
        min-height: 100vh;
    }
}

/* Topbar */
.at-topbar {
    position: sticky;
    top: 0;
    z-index: 1020;
    background: rgba(15, 23, 42, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    min-height: var(--at-topbar-height);
    color: #e2e8f0;
    flex-wrap: wrap;
}

.at-topbar .at-topbar-toggler {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #e2e8f0;
    border-radius: 0.5rem;
    padding: 0.4rem 0.6rem;
    line-height: 1;
}

@media (min-width: 992px) {
    .at-topbar .at-topbar-toggler { display: none; }
}

.at-topbar .at-page-title {
    flex: 1 1 auto;
    min-width: 0;
}

.at-topbar .at-page-title h1 {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    font-weight: 700;
    margin: 0;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.at-topbar .at-page-title p {
    font-size: 0.85rem;
    margin: 0;
    color: rgba(226, 232, 240, 0.6);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.at-topbar .at-topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex-wrap: wrap;
}

.at-topbar .at-badge-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.7rem;
    border-radius: 999px;
    background: rgba(34, 211, 238, 0.12);
    color: #22d3ee;
    border: 1px solid rgba(34, 211, 238, 0.3);
    font-size: 0.82rem;
    font-weight: 600;
}

.at-topbar .at-icon-btn {
    position: relative;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: #e2e8f0;
    border-radius: 0.5rem;
    padding: 0.45rem 0.65rem;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.at-topbar .at-icon-btn:hover {
    background: rgba(255, 255, 255, 0.08);
}

.at-topbar .at-icon-btn .at-count {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ef4444;
    color: #fff;
    font-size: 0.66rem;
    font-weight: 700;
    border-radius: 999px;
    padding: 0 5px;
    min-width: 16px;
    height: 16px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

[dir="rtl"] .at-topbar .at-icon-btn .at-count { right: auto; left: -4px; }

.at-topbar .at-logout {
    color: rgba(226, 232, 240, 0.7);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.85rem;
}

.at-topbar .at-logout:hover { color: #ef4444; }

/* Content area */
.at-content {
    padding: var(--at-content-gutter);
}

@media (min-width: 768px) {
    .at-content { padding: 1.5rem; }
}

@media (min-width: 1200px) {
    .at-content { padding: 2rem; }
}

/* ============================================================================
   3. UTILITIES — make pre-existing custom widgets behave at small widths
   ========================================================================= */

/* Tables: horizontally scroll on narrow screens instead of overflowing */
.at-content table { width: 100%; }
.at-content .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Cards / panels with hardcoded widths get clamped */
.at-content .card,
.at-content .panel { max-width: 100%; }

/* Forms: make every input fluid */
.at-content input,
.at-content textarea,
.at-content select { max-width: 100%; }

/* Toast container — anchor bottom-right (or left in RTL) */
#toast-container {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    z-index: 1080;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    pointer-events: none;
}

[dir="rtl"] #toast-container { right: auto; left: 1rem; }

#toast-container .toast {
    pointer-events: auto;
    background: #1e293b;
    color: #e2e8f0;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 0.5rem;
    padding: 0.75rem 1rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    min-width: 240px;
    max-width: 90vw;
    font-size: 0.9rem;
}

#toast-container .toast-success { border-color: rgba(34, 197, 94, 0.4); }
#toast-container .toast-error { border-color: rgba(239, 68, 68, 0.4); }
#toast-container .toast-info { border-color: rgba(34, 211, 238, 0.4); }

/* ============================================================================
   4. STATS GRID (4 stat cards) — auto-fit, never crowds, never overflows
   ========================================================================= */
.at-stats-grid,
.at-content .stats-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)) !important;
    gap: 1rem !important;
    margin-bottom: 1.5rem !important;
}

@media (min-width: 1400px) {
    .at-stats-grid,
    .at-content .stats-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    }
}

/* ============================================================================
   5. CHARTS GRID — 4 charts: 1col → 2col → 4col with smooth breakpoints
   Markup:
       <div class="at-charts-grid">
         <div class="chart-container">…</div>  × 4
       </div>
   ========================================================================= */
.at-charts-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

@media (min-width: 768px) {
    .at-charts-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.25rem; }
}

@media (min-width: 1400px) {
    .at-charts-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1.5rem; }
}

/* Each chart card: allow grid items to shrink, fluid canvas */
.at-charts-grid > .chart-container,
.at-charts-grid > .chart-card {
    min-width: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.at-charts-grid > .chart-container canvas,
.at-charts-grid > .chart-card canvas {
    max-width: 100% !important;
    width: 100% !important;
    height: auto !important;
    min-height: 220px;
}

/* Scroll variant: keep 4-col layout & overflow horizontally on narrow widths */
.at-charts-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1.5rem;
    padding-bottom: 0.5rem;
}

.at-charts-scroll > .at-charts-grid {
    grid-template-columns: repeat(4, minmax(280px, 1fr));
    margin-bottom: 0;
}

/* ============================================================================
   6. TABLE WRAPPER — horizontal scroll on narrow screens (preserves legacy
      .table-container styling, just adds a scroll boundary inside it).
   ========================================================================= */
.at-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
}

.at-table-wrap > table { min-width: 720px; width: 100%; }

/* ============================================================================
   7. STICKY-SAFE TOPBAR ANCHOR
   The .at-topbar uses position: sticky; top: 0. For sticky to work, NO ancestor
   may use overflow: hidden / scroll / auto on the same axis. We've already
   patched .at-shell to use overflow-x: clip. Add belt-and-suspenders here:
   ensure the immediate parent of the topbar isn't accidentally clipped.
   ========================================================================= */
.at-main { overflow: visible; }

/* Topbar doesn't need a margin-bottom (would create the "gap from top" the
   user reported) — content sits flush below it via .at-content padding. */
.at-topbar { margin: 0; }

/* ============================================================================
   8. CHART-CONTAINER SIZING — Chart.js with maintainAspectRatio:false needs
      the parent box to have an explicit (or flex/grid resolved) height.
      .chart-container is the per-chart card from dashboard.css. We give it a
      sensible default height that works at every breakpoint.
   ========================================================================= */
.at-charts-grid > .chart-container {
    height: 320px;
    padding: 1rem;
}

@media (min-width: 768px) {
    .at-charts-grid > .chart-container { height: 340px; padding: 1.25rem; }
}

@media (min-width: 1400px) {
    .at-charts-grid > .chart-container { height: 360px; padding: 1.5rem; }
}

.at-charts-grid > .chart-container > canvas {
    flex: 1 1 auto;
    min-height: 0;   /* let canvas shrink within flex column */
}

/* Header row inside each chart card never collapses */
.at-charts-grid > .chart-container .chart-header {
    flex-shrink: 0;
}
