/* Custom styles for Elevator Management System */

/* RTL Support */
[dir="rtl"] {
    direction: rtl;
    text-align: right;
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Status badges */
.status-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}

.status-badge.active {
    background-color: #d1fae5;
    color: #065f46;
}

.status-badge.expired {
    background-color: #fee2e2;
    color: #991b1b;
}

.status-badge.pending {
    background-color: #fef3c7;
    color: #92400e;
}

.status-badge.cancelled {
    background-color: #f3f4f6;
    color: #374151;
}

/* Sync status badges */
.sync-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}

.sync-badge.synced {
    background-color: #d1fae5;
    color: #065f46;
}

.sync-badge.pending {
    background-color: #fef3c7;
    color: #92400e;
}

.sync-badge.syncing {
    background-color: #dbeafe;
    color: #1e40af;
}

.sync-badge.failed {
    background-color: #fee2e2;
    color: #991b1b;
}

/* Card hover effects */
.card-hover {
    transition: all 0.3s ease;
}

.card-hover:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
}

/* Loading spinner */
.spinner {
    border: 3px solid #f3f3f3;
    border-top: 3px solid #3498db;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Toast notifications live in #toast-container (see base.html + toasts.js).
   Positioning/stacking/animation are handled by the container and the
   Tailwind classes on each toast — nothing fixed at toast level. */

/* Table styles */
.table-container {
    overflow-x: auto;
}

.table-container table {
    min-width: 100%;
}

/* Form styles */
.form-input {
    width: 100%;
    padding: 0.5rem 1rem;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.form-input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Button styles */
.btn {
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-weight: 500;
    transition: all 0.2s;
    cursor: pointer;
    border: none;
}

.btn-primary {
    background-color: #3b82f6;
    color: white;
}

.btn-primary:hover {
    background-color: #2563eb;
}

.btn-secondary {
    background-color: #6b7280;
    color: white;
}

.btn-secondary:hover {
    background-color: #4b5563;
}

.btn-success {
    background-color: #10b981;
    color: white;
}

.btn-success:hover {
    background-color: #059669;
}

.btn-danger {
    background-color: #ef4444;
    color: white;
}

.btn-danger:hover {
    background-color: #dc2626;
}

/* Responsive adjustments */
.mobile-sidebar,
.mobile-sidebar-overlay {
    top: 3rem;
    bottom: 0;
}

@media (min-width: 640px) {
    .mobile-sidebar,
    .mobile-sidebar-overlay {
        top: 3.5rem;
    }
}

@media (min-width: 1024px) {
    .mobile-sidebar {
        top: auto;
        bottom: auto;
        height: 100%;
    }
}

@media (max-width: 640px) {
    .table-container {
        font-size: 0.875rem;
    }
    
    .btn {
        padding: 0.375rem 0.75rem;
        font-size: 0.875rem;
    }
}

/* =============================================
   App-shell layout (fixed viewport + inner scroll)
   Desktop/tablet ONLY. On phones (≤640px) base.html
   switches to natural document scrolling — locking the
   body to 100vh/dvh there hides content behind the
   browser toolbars and makes the bottom of the page
   unreachable.
   ============================================= */
@media (min-width: 641px) {
    body {
        display: flex;
        flex-direction: column;
        height: 100vh;
        height: 100dvh; /* dynamic viewport height for mobile browsers */
        overflow: hidden;
    }

    /* Header: reduce height on small screens */
    header {
        flex-shrink: 0;
    }

    /* Flex container below header */
    body > .flex.flex-1 {
        flex: 1;
        min-height: 0; /* critical: allows flex children to shrink */
        overflow: hidden;
    }

    /* Main content area */
    main {
        flex: 1;
        min-height: 0; /* critical: allows overflow-y-auto to work */
        overflow: hidden;
    }

    #main-content {
        height: 100%;
        overflow-y: auto;
        scroll-padding-top: 4rem; /* account for sticky header */
    }
}

/* Mobile header: smaller padding, more compact */
@media (max-width: 640px) {
    header .max-w-7xl {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
    
    /* Header inner row is a flex container (menu+logo | search+user) */
    header .max-w-7xl > .flex.items-center.justify-between {
        min-height: 2.75rem;
    }
    
    /* Compact notification dropdown on mobile (anchored left in RTL header) */
    header .absolute.left-0.mt-2.w-72 {
        width: calc(100vw - 2rem);
        left: 0;
        right: auto;
        max-width: 20rem;
    }
    
    /* Compact profile dropdown on mobile (anchored left in RTL header) */
    header .absolute.left-0.mt-2.w-56 {
        width: calc(100vw - 2rem);
        left: 0;
        right: auto;
        max-width: 16rem;
    }
    
    /* Help modal: full width on mobile */
    header .fixed.inset-0.z-50 .max-w-4xl {
        max-width: calc(100vw - 1rem);
    }
}

/* Sidebar: starts below header on mobile */
.mobile-sidebar {
    top: 3rem !important;
    bottom: 0 !important;
    height: auto !important;
}

/* Overlay covers full viewport including behind header */
.mobile-sidebar-overlay {
    top: 0 !important;
    bottom: 0 !important;
    height: 100% !important;
}

@media (min-width: 640px) {
    .mobile-sidebar {
        top: 3.5rem !important;
    }
}

@media (min-width: 1024px) {
    .mobile-sidebar {
        top: auto !important;
        bottom: auto !important;
        height: auto !important;
    }
}

/* Page content: ensure scrollable areas work on mobile */
@media (max-width: 640px) {
    /* Reduce padding on mobile for more content space */
    main {
        padding: 0.5rem;
    }
    
    #main-content {
        padding-top: 0.75rem;
    }
    
    /* Tables: horizontal scroll */
    .table-container {
        max-width: 100%;
        overflow-x: auto;
    }
    
    /* Grid layouts: single column on mobile */
    .grid-cols-1.md\:grid-cols-2,
    .grid-cols-1.md\:grid-cols-2.lg\:grid-cols-3 {
        grid-template-columns: 1fr;
    }
}

/* Tablet adjustments */
@media (min-width: 641px) and (max-width: 1023px) {
    main {
        padding: 1rem;
    }
    
    #main-content {
        padding-top: 1rem;
    }
}

/* Fix touch scrolling on iOS */
@supports (-webkit-touch-callout: none) {
    #main-content {
        -webkit-overflow-scrolling: touch;
    }
}

/* Prevent body scroll when sidebar is open on mobile */
body.sidebar-open {
    overflow: hidden;
}

/* Mobile-friendly form inputs */
@media (max-width: 640px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    textarea,
    select {
        font-size: 16px !important; /* prevent iOS zoom on focus */
    }
}

/* Mobile-friendly buttons: full width on small screens */
@media (max-width: 640px) {
    .btn-primary,
    .btn-success,
    .btn-danger {
        width: 100%;
        justify-content: center;
    }
}

/* Pagination: stack on mobile */
@media (max-width: 640px) {
    .bg-white.px-4.py-3.rounded-xl {
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
    }
}

/* Search bar: full width on mobile */
@media (max-width: 640px) {
    .flex.flex-col.sm\:flex-row.gap-4 {
        flex-direction: column;
    }
    
    .flex.flex-col.sm\:flex-row.gap-4 .flex-1 {
        width: 100%;
    }

    /* Filter forms: every control becomes an easy full-width tap target */
    form.flex.flex-col.sm\:flex-row.gap-4 select,
    form.flex.flex-col.sm\:flex-row.gap-4 input[type="text"] {
        width: 100%;
    }

    form.flex.flex-col.sm\:flex-row.gap-4 button[type="submit"] {
        width: 100%;
        justify-content: center;
    }
}

/* Fix header dropdowns positioning on RTL mobile:
   dropdowns anchored to left-side triggers must extend rightward */
@media (max-width: 640px) {
    [dir="rtl"] header .absolute.left-0 {
        left: 0;
        right: auto;
    }
}

/* Map container: responsive height */
@media (max-width: 640px) {
    #location-map,
    #building-map {
        height: 250px !important;
        min-height: 200px;
    }
}

/* Card grid: single column on mobile */
@media (max-width: 767px) {
    .grid.grid-cols-1.md\:grid-cols-2.lg\:grid-cols-3 {
        grid-template-columns: 1fr;
    }
    
    /* Reduce card height on mobile */
    .h-32.bg-gradient-to-br {
        height: 6rem;
    }
}

/* Help modal: scrollable on mobile */
@media (max-width: 640px) {
    .fixed.inset-0.z-50 .max-h-\[90vh\] {
        max-height: 95vh;
    }

    .fixed.inset-0.z-50 .max-h-\[calc\(90vh-80px\)\] {
        max-height: calc(95vh - 60px);
    }

    .fixed.inset-0.z-50 .p-6 {
        padding: 1rem;
    }
}

/* =============================================
   Desktop (lg+): the sidebar is a FIXED COLUMN IN
   THE LAYOUT — its own stable place beside the
   content — and the header toggle simply opens and
   closes it. It must never float above the page as
   an overlay (that was reported as "بهم ریخته و
   رفته رو هوا").

   Below lg the slide-in drawer + backdrop stay.
   !important is required to beat both Tailwind
   utilities (.fixed/.z-40/.shadow-xl) and the inline
   styles Alpine's x-transition writes while animating.
   ============================================= */
@media (min-width: 1024px) {
    #main-sidebar {
        position: static !important;
        inset: auto !important;
        z-index: auto !important;
        flex-shrink: 0 !important;
        height: auto !important;
        transform: none !important;
        box-shadow: none !important;
        transition: none !important;
        border-left: 1px solid #e5e7eb; /* gray-200 — separator toward the content (RTL) */
    }

    /* No backdrop behind the desktop column */
    .mobile-sidebar-overlay {
        display: none !important;
    }
}

/* =============================================
   SIDEBAR SCROLLING & SHORT-VIEWPORT MENU
   («منو اسکرول نمی‌خوره» / «سواگر و پایینی‌هاش
   را نمی‌بینم»)

   Root cause: the full-size menu is ~681px tall
   (12 items + section header) while a laptop
   viewport offers ~643px beside the header. The
   bottom section (پنل ادمین جنگو + Swagger) fell
   below the fold and the tiny leftover overflow
   was practically undiscoverable — no visible
   scrollbar, users concluded "the menu does not
   scroll".

   Strategy, in order:
   1. On short viewports the menu COMPACTS itself
      (tighter paddings/gaps/icons) so the WHOLE
      menu — Swagger included — fits with no
      scrolling at all.
   2. If even the compact menu cannot fit (very
      short windows), the sidebar scrolls with an
      ALWAYS-VISIBLE, friendly scrollbar and
      contained overscroll, so scrolling is both
      possible and obvious.
   3. JS scrolls the active link into view after
      every navigation (see base.html).
   ============================================= */

/* Always-visible, soft scrollbar for the sidebar
   (works for the desktop column and the drawer —
   both are the same #main-sidebar element). */
#main-sidebar {
    overscroll-behavior: contain; /* menu scroll must not chain into the page */
    scrollbar-gutter: stable;     /* no layout jump when a scrollbar appears */
    scrollbar-width: thin;        /* Firefox */
    scrollbar-color: #cbd5e1 transparent;
}
#main-sidebar::-webkit-scrollbar {
    width: 10px;
}
#main-sidebar::-webkit-scrollbar-track {
    background: transparent;
}
#main-sidebar::-webkit-scrollbar-thumb {
    background: #cbd5e1; /* gray-300 — visible on white, softer than the global #888 */
    border-radius: 8px;
    border: 2px solid #ffffff;
}
#main-sidebar::-webkit-scrollbar-thumb:hover {
    background: #94a3b8; /* gray-400 */
}

/* Compact menu — desktop column (lg+) on short
   viewports. Full-size menu needs ~700px of column
   height; everything below ~800px viewport height
   gets the dense variant (~480px) which fits even
   a 600px-tall window. */
@media (min-width: 1024px) and (max-height: 800px) {
    #main-sidebar nav {
        padding: 0.5rem 0.625rem !important;
    }
    #main-sidebar nav > * + * {
        margin-top: 0.25rem !important; /* beats space-y-1 sm:space-y-2 */
    }
    #main-sidebar nav a {
        padding-top: 0.4375rem !important;    /* beats sm:py-3 */
        padding-bottom: 0.4375rem !important;
        gap: 0.5rem !important;               /* beats sm:gap-3 */
    }
    #main-sidebar nav a span {
        font-size: 0.8125rem !important;      /* 13px, beats sm:text-sm */
        line-height: 1.125rem !important;
    }
    #main-sidebar nav a svg {
        width: 1.0625rem !important;          /* 17px, beats sm:w-5 */
        height: 1.0625rem !important;
    }
    #main-sidebar nav div.pt-3 {
        padding-top: 0.5rem !important;       /* «مدیریت سیستم» divider */
        margin-top: 0.5rem !important;
    }
    #main-sidebar nav p {
        margin-bottom: 0.25rem !important;
    }
}

/* Compact menu — drawer (below lg) on short
   viewports: landscape phones and small tablets
   where the sm:-sized items (~700px) cannot fit
   either. */
@media (max-width: 1023.9px) and (max-height: 740px) {
    #main-sidebar nav {
        padding: 0.5rem !important;
    }
    #main-sidebar nav > * + * {
        margin-top: 0.25rem !important;
    }
    #main-sidebar nav a {
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
    }
    #main-sidebar nav a span {
        font-size: 0.75rem !important;
        line-height: 1rem !important;
    }
    #main-sidebar nav a svg {
        width: 1rem !important;
        height: 1rem !important;
    }
    #main-sidebar nav div.pt-3 {
        padding-top: 0.5rem !important;
        margin-top: 0.5rem !important;
    }
    #main-sidebar nav p {
        margin-bottom: 0.25rem !important;
    }
}
