/*
 * NIRA Adaptive UI — Expandable Sidebar Rail (Tablet + Desktop)
 * Breakpoint: >= 768px
 *
 * BEHAVIOR:
 *  — Collapsed: slim 52px icon-only rail (Apple Maps style)
 *  — Expanded:  rail widens IN-PLACE to 204px showing icon + label
 *    (NOT a separate Tier 2 panel — the rail itself animates wider)
 *  — Tier 3 (glassmorphism detail panel) slides in from rail right edge
 *  — Map always stays full-screen behind everything
 */

/* ==========================================
   SHARED: Only show on tablet/desktop
   ========================================== */
@media (min-width: 768px) {

  /* Override mobile body scroll */
  body.bg-background,
  body {
    overflow: hidden !important;
  }

  /* Hide the Tier 2 flat-menu — it is superseded by in-place rail expand */
  .flat-menu {
    display: none !important;
    pointer-events: none !important;
  }

  /* ==========================================
     TIER 1 — COLLAPSIBLE ICON RAIL
     Collapsed: 52px wide, icon only
     Expanded:  204px wide, icon + label
     ========================================== */
  .sidebar-rail {
    position: fixed;
    left: 16px;
    top: 16px;
    width: 52px;                          /* collapsed default */
    height: calc(100vh - 32px);
    height: calc(100dvh - 32px);
    background: rgba(255, 255, 255, 0.97);
    border: 1px solid rgba(0, 0, 0, 0.07);
    border-radius: 16px;
    box-shadow:
      0 2px 20px rgba(0, 0, 0, 0.09),
      0 1px 4px rgba(0, 0, 0, 0.05);
    z-index: 62;
    display: flex !important;
    flex-direction: column;
    align-items: center;
    padding: 8px 0 12px;                  /* generous bottom padding to raise icons */
    box-sizing: border-box;
    overflow: hidden;                     /* clips labels when collapsed */
    /* Smooth width animation */
    transition: width 0.30s cubic-bezier(0.4, 0, 0.2, 1),
                align-items 0.30s ease;
  }

  /* Short screen optimizations (e.g. tablet landscape / small laptops) */
  @media (max-height: 720px) {
    .sidebar-rail {
      padding: 4px 0 6px;
    }
    .rail-logo {
      padding: 2px 10px 2px;
    }
    .rail-divider {
      margin: 2px 10px 4px;
    }
    .rail-nav {
      gap: 3px;
    }
    .rail-nav-btn,
    .rail-action-btn,
    .rail-expand-btn {
      height: 27px;
      min-height: 27px;
    }
    .rail-avatar {
      width: 27px;
      height: 27px;
      min-height: 27px;
    }
    .rail-footer-divider {
      margin: 2px 4px;
    }
  }

  /* Expanded state — rail widens in-place */
  .sidebar-rail.rail-expanded {
    width: 204px;
    align-items: flex-start;
  }

  /* ── Logo area ── */
  .rail-logo {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 2px 10px 4px;
    flex-shrink: 0;
    box-sizing: border-box;
    overflow: hidden;
  }

  .rail-logo-img {
    width: 32px;                          /* Enlarged for premium branding look when collapsed/expanded */
    height: 32px;
    object-fit: contain;
    border-radius: 8px;
    flex-shrink: 0;
  }

  .rail-app-name {
    font-size: 20px;                      /* Larger size for distinct presence */
    font-weight: 900;
    color: #006857;
    letter-spacing: 0.5px;
    white-space: nowrap;
    opacity: 0;
    max-width: 0;
    overflow: hidden;
    transition: opacity 0.20s ease 0.10s, max-width 0.30s ease;
    font-family: 'Kerisdipa', serif;      /* Custom premium branding font */
  }

  .sidebar-rail.rail-expanded .rail-app-name {
    opacity: 1;
    max-width: 120px;
  }

  /* ── Divider ── */
  .rail-divider {
    width: calc(100% - 20px);
    height: 1px;
    background: rgba(0, 0, 0, 0.08);
    margin: 4px 10px 10px;                /* Added clean gap separating branding from navigation buttons */
    flex-shrink: 0;
    align-self: center;
  }

  /* ── Nav area (Strictly non-scrollable for pixel-perfect proportionality) ── */
  .rail-nav {
    display: flex;
    flex-direction: column;
    gap: 5px;                             /* Premium breathable spacing between buttons */
    width: 100%;
    padding: 0 6px;
    flex: 1;
    overflow: hidden !important;        /* Disable scroll completely */
    box-sizing: border-box;
  }

  .rail-nav::-webkit-scrollbar {
    display: none;
  }

  /* ── Individual nav buttons ── */
  .rail-nav-btn,
  .rail-action-btn {
    display: flex;
    align-items: center;
    justify-content: center;            /* icon centered when collapsed */
    gap: 0;
    width: 40px;
    height: 29px;
    min-height: 29px;
    background: transparent;
    border: none;
    border-radius: 7px;
    cursor: pointer;
    color: #94a3b8;
    transition:
      background 0.16s ease,
      color 0.16s ease,
      width 0.30s cubic-bezier(0.4, 0, 0.2, 1),
      justify-content 0.30s ease,
      gap 0.20s ease,
      padding 0.30s ease,
      transform 0.14s ease;
    padding: 0;
    flex-shrink: 0;
    overflow: hidden;
    white-space: nowrap;
    box-sizing: border-box;
    position: relative;
  }

  /* Expanded: buttons go full rail width, left-aligned */
  .sidebar-rail.rail-expanded .rail-nav-btn,
  .sidebar-rail.rail-expanded .rail-action-btn {
    width: 100%;
    justify-content: flex-start;
    gap: 10px;
    padding: 0 10px;
  }

  /* Icon inside buttons */
  .rail-nav-btn .material-symbols-outlined,
  .rail-action-btn .material-symbols-outlined {
    font-size: 19px;
    font-variation-settings: 'FILL' 0, 'wght' 350;
    transition: font-variation-settings 0.14s ease;
    flex-shrink: 0;
  }

  /* Hover */
  .rail-nav-btn:hover,
  .rail-action-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #1e293b;
    transform: scale(1.04);
  }

  .sidebar-rail.rail-expanded .rail-nav-btn:hover,
  .sidebar-rail.rail-expanded .rail-action-btn:hover {
    transform: none;                    /* don't scale when expanded */
  }

  /* Active: icon panel open */
  .rail-nav-btn.active {
    background: rgba(0, 229, 160, 0.11);
    color: #00c589;
  }

  .rail-nav-btn.active .material-symbols-outlined {
    font-variation-settings: 'FILL' 1, 'wght' 400;
  }

  /* Tier 3 also open → stronger highlight */
  .rail-nav-btn.active.tier3-active {
    background: rgba(0, 229, 160, 0.18);
    color: #00c589;
    box-shadow: 0 0 0 2px rgba(0, 229, 160, 0.30);
  }

  /* ── Rail label text ── */
  .rail-label {
    font-size: 13.5px;                    /* Slightly larger for a highly legible, premium look */
    font-weight: 600;                     /* Bolder for a high-end startup aesthetic */
    color: #1e293b;                       /* Rich slate-800 color for solid contrast */
    white-space: nowrap;
    opacity: 0;
    max-width: 0;
    overflow: hidden;
    pointer-events: none;
    transition: opacity 0.18s ease 0.12s, max-width 0.30s ease;
  }

  .sidebar-rail.rail-expanded .rail-label {
    opacity: 1;
    max-width: 150px;
    pointer-events: auto;
  }

  /* Active button label color */
  .sidebar-rail.rail-expanded .rail-nav-btn.active .rail-label {
    color: #006857;
    font-weight: 700;                     /* Extra bold active state */
  }

  /* ── Spacer ── */
  .rail-spacer {
    flex-shrink: 0;
    min-height: 4px;
  }

  /* ── Rail footer (expand btn + avatar + settings) ── */
  .rail-footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    width: 100%;
    padding: 0 6px 4px;
    flex-shrink: 0;
    box-sizing: border-box;
  }

  .sidebar-rail.rail-expanded .rail-footer {
    align-items: flex-start;
  }

  .rail-footer-divider {
    width: calc(100% - 8px);
    height: 1px;
    background: rgba(0, 0, 0, 0.08);
    margin: 3px 4px;
    align-self: center;
  }

  /* ── Expand / Collapse toggle button ── */
  .rail-expand-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
    width: 40px;
    height: 29px;
    min-height: 29px;
    background: rgba(0, 0, 0, 0.03);
    border: 1px solid rgba(0, 0, 0, 0.06);
    border-radius: 7px;
    cursor: pointer;
    color: #64748b;
    transition:
      background 0.16s ease,
      color 0.16s ease,
      width 0.30s cubic-bezier(0.4, 0, 0.2, 1),
      justify-content 0.30s ease,
      gap 0.20s ease,
      padding 0.30s ease;
    overflow: hidden;
    white-space: nowrap;
    box-sizing: border-box;
    flex-shrink: 0;
  }

  .sidebar-rail.rail-expanded .rail-expand-btn {
    width: 100%;
    justify-content: flex-start;
    gap: 10px;
    padding: 0 10px;
  }

  .rail-expand-btn .material-symbols-outlined {
    font-size: 19px;
    flex-shrink: 0;
    transition: transform 0.25s ease;
  }

  .rail-expand-btn.expanded .material-symbols-outlined {
    transform: rotate(0deg);            /* icon already changes text to chevron_left */
  }

  .rail-expand-btn:hover {
    background: rgba(0, 104, 87, 0.08);
    color: #006857;
  }

  /* Avatar */
  .rail-avatar {
    display: flex;
    align-items: center;
    gap: 0;
    width: 29px;
    height: 29px;
    min-height: 29px;
    border-radius: 50%;
    cursor: pointer;
    overflow: hidden;
    border: 1.5px solid rgba(0, 104, 87, 0.22);
    transition:
      width 0.30s cubic-bezier(0.4, 0, 0.2, 1),
      border-radius 0.30s ease,
      gap 0.20s ease,
      box-shadow 0.15s ease;
    flex-shrink: 0;
    box-sizing: border-box;
    align-self: center;
  }

  .rail-avatar:hover {
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
  }

  .sidebar-rail.rail-expanded .rail-avatar {
    width: 100%;
    border-radius: 7px;
    align-self: flex-start;
    gap: 10px;
    padding: 0 10px;
  }

  .rail-avatar img {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
  }

  .sidebar-rail.rail-expanded .rail-avatar img {
    border-radius: 5px;
  }

  /* ==========================================
     TIER 3 — GLASSMORPHISM DETAIL PANEL
     ========================================== */
  .detail-panel {
    position: fixed;
    top: 16px;
    left: 80px;                         /* default: right of collapsed 52px rail + gap */
    height: calc(100vh - 32px);
    height: calc(100dvh - 32px);
    min-height: 0;
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(28px) saturate(180%);
    -webkit-backdrop-filter: blur(28px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 16px;
    z-index: 58;
    box-shadow:
      0 8px 40px rgba(0, 104, 87, 0.08),
      0 2px 12px rgba(0, 0, 0, 0.07);
    display: flex !important;
    flex-direction: column;
    box-sizing: border-box;
    overflow: hidden;

    /* Hidden state */
    pointer-events: none;
    opacity: 0;
    transform: translateX(-20px);
    transition:
      transform 0.30s cubic-bezier(0.4, 0, 0.2, 1),
      opacity 0.25s ease,
      left 0.30s cubic-bezier(0.4, 0, 0.2, 1);
  }

  /* Visible state */
  .detail-panel.panel-open {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }

  /* When rail is expanded, detail panel shifts right to not overlap */
  body.rail-expanded .detail-panel {
    left: 232px;                        /* 16px margin + 204px rail + 12px gap */
  }

  /* Scoped Tier 3 Sections */
  .tier3-section {
    display: none;
    height: 100%;
    width: 100%;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
  }

  #desktop-nira-container {
    min-height: 0;
  }

  .tier3-section.active {
    display: flex;
  }

  /* ==========================================
     DARK MODE — All Tiers
     ========================================== */
  .dark .sidebar-rail {
    background: rgba(26, 26, 28, 0.98);
    border-color: rgba(255, 255, 255, 0.07);
    box-shadow: 0 2px 20px rgba(0, 0, 0, 0.40), 0 1px 4px rgba(0,0,0,0.25);
  }

  .dark .rail-divider,
  .dark .rail-footer-divider {
    background: rgba(255, 255, 255, 0.11);
  }

  .dark .rail-app-name {
    color: #00E5A0;
  }

  .dark .rail-nav-btn,
  .dark .rail-action-btn {
    color: rgba(255, 255, 255, 0.38);
  }

  .dark .rail-label {
    color: rgba(255, 255, 255, 0.55);
  }

  .dark .rail-nav-btn:hover,
  .dark .rail-action-btn:hover {
    background: rgba(255, 255, 255, 0.07);
    color: rgba(255, 255, 255, 0.90);
  }

  .dark .rail-nav-btn.active {
    background: rgba(0, 229, 160, 0.14);
    color: #00E5A0;
  }

  .dark .sidebar-rail.rail-expanded .rail-nav-btn.active .rail-label {
    color: #00E5A0;
  }

  .dark .rail-expand-btn {
    background: rgba(255, 255, 255, 0.04);
    border-color: rgba(255, 255, 255, 0.07);
    color: rgba(255, 255, 255, 0.45);
  }

  .dark .rail-expand-btn:hover {
    background: rgba(0, 229, 160, 0.10);
    color: #00E5A0;
  }

  .dark .rail-avatar {
    border-color: rgba(0, 229, 160, 0.22);
  }

  .dark .detail-panel {
    background: rgba(26, 26, 28, 0.86);
    border-color: rgba(255, 255, 255, 0.09);
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.45), 0 2px 12px rgba(0,0,0,0.30);
  }

  /* ==========================================
     SEARCH RESULTS — Inline in Tier 3 panel
     ========================================== */
  .detail-panel .search-results {
    position: static !important;
    max-height: calc(100vh - 190px) !important;
    max-height: calc(100dvh - 190px) !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    opacity: 1 !important;
    transform: none !important;
    pointer-events: auto !important;
    box-shadow: none !important;
    border: none !important;
    background: transparent !important;
    padding: 0 !important;
    margin: 8px 0 0 0 !important;
    display: none;
    scrollbar-width: thin !important;
  }

  .detail-panel .search-results::-webkit-scrollbar {
    width: 5px !important;
    display: block !important;
  }

  .detail-panel .search-results::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.12) !important;
    border-radius: 99px !important;
  }

  .detail-panel .search-results::-webkit-scrollbar-track {
    background: transparent !important;
  }

  .detail-panel .search-results.active {
    display: block !important;
  }

  .detail-panel .search-result-item {
    background: rgba(255, 255, 255, 0.55) !important;
    border: 1px solid rgba(0, 0, 0, 0.04) !important;
    margin-bottom: 8px !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.02) !important;
  }

  .detail-panel .search-result-item:hover {
    background: linear-gradient(135deg, #f0fdf4, #ecfdf5) !important;
  }

  .dark .detail-panel .search-result-item {
    background: rgba(255, 255, 255, 0.04) !important;
    border-color: rgba(255, 255, 255, 0.08) !important;
  }

  .dark .detail-panel .search-result-item:hover {
    background: rgba(0, 229, 160, 0.08) !important;
  }

  .detail-panel .search-hint {
    display: none !important;
  }

  /* ==========================================
     PREMIUM TRANSITION SYSTEM — Color Wipe
     ========================================== */
  .wipe-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: linear-gradient(135deg, #006857, #00c589);
    z-index: 999999 !important;          /* On top of absolutely everything */
    pointer-events: none;
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.55s cubic-bezier(0.86, 0, 0.07, 1);
    will-change: transform;
  }

  .wipe-overlay.wipe-active {
    transform: scaleX(1);
    pointer-events: auto;                /* Blocks clicks while wiping */
  }
}

/* ==========================================
   DESKTOP OVERRIDES (>= 1200px)
   Slightly wider panels
   ========================================== */
@media (min-width: 1200px) {
  .sidebar-rail.rail-expanded {
    width: 220px;
  }

  .detail-panel {
    left: 80px;
    width: 360px;
  }

  body.rail-expanded .detail-panel {
    left: 248px;                        /* 16px + 220px + 12px */
  }
}
