/* =========================================================================
   SPA-feel layer (htmx): top progress bar, busy states, submit spinners.
   Plain CSS on purpose - it must not depend on the Tailwind CDN scanning it.
   ========================================================================= */

/* 1. Top navigation bar -------------------------------------------------- */
/* htmx adds .htmx-request to #nav-progress while a request is in flight
   (wired by hx-indicator="#nav-progress" on <body>). The 120ms delay means
   instant navigations never flash the bar. */
#nav-progress {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 3px;
  z-index: 100;
  overflow: hidden;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s ease;
}
#nav-progress.htmx-request { opacity: 1; transition-delay: .12s; }
#nav-progress::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; left: 0;
  width: 35%;
  border-radius: 0 3px 3px 0;
  background: linear-gradient(90deg, transparent, #f38020 40%, #fb923c);
  animation: nav-progress-slide 1.1s cubic-bezier(.4, 0, .2, 1) infinite;
}
@keyframes nav-progress-slide {
  from { transform: translateX(-100%); }
  to   { transform: translateX(300%); }
}

/* 2. Main content area --------------------------------------------------- */
#main-content { outline: none; scroll-margin-top: 3rem; /* clear the fixed navbar */ }
/* Dim old content only if the server is slow (>150ms) */
#main-content[aria-busy="true"] {
  opacity: .55;
  pointer-events: none;
  transition: opacity .2s ease .15s;
}
#page-content { animation: page-in .18s ease-out; }
@keyframes page-in { from { opacity: 0; } to { opacity: 1; } }

/* In-page fragment swaps (filters, pagination) */
#results { transition: opacity .15s ease; }
#results.htmx-request { opacity: .5; pointer-events: none; transition-delay: .1s; }

/* 3. Submit-button spinner ------------------------------------------------ */
/* JS (static/js/app.js) adds .is-submitting to a <form> while it is being sent,
   for both htmx-boosted and plain forms. No markup change needed per button. */
.is-submitting :is(.btn-primary, .btn-secondary, .btn-danger)[type="submit"] {
  pointer-events: none;
  cursor: progress;
  opacity: .8;
}
.is-submitting :is(.btn-primary, .btn-secondary, .btn-danger)[type="submit"]::before {
  content: "";
  flex: none;
  width: .9em; height: .9em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: btn-spin .6s linear infinite;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }

/* 4. Toast for network / server failures ---------------------------------- */
.app-toast {
  position: fixed; top: 4rem; right: 1rem; z-index: 110;
  max-width: 22rem;
  padding: .75rem 1rem;
  border: 2px solid #e11d48; border-radius: .75rem;
  background: #fff1f2; color: #4c0519;
  font-size: 13px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  animation: page-in .15s ease-out;
}
.dark .app-toast { background: rgba(76, 5, 25, .9); color: #fecdd3; border-color: #f43f5e; }

@media (prefers-reduced-motion: reduce) {
  #page-content, .app-toast { animation: none; }
  #nav-progress::before { animation-duration: 2.4s; }
  .is-submitting :is(.btn-primary, .btn-secondary, .btn-danger)[type="submit"]::before { animation-duration: 1.6s; }
}

/* Sidebar visibility: #sidebar-dynamic[data-has-sidebar="false"] (rendered by components/sidebar.html and
   swapped by htmx on every navigation) hides the sidebar, its spacer and the mobile hamburger. Pure CSS, so
   SPA navigation, Back/Forward and full loads all behave the same. Used by the dashboard (module cards). */
body:has(#sidebar-dynamic[data-has-sidebar="false"]) #console-sidebar,
body:has(#sidebar-dynamic[data-has-sidebar="false"]) #sidebar-spacer,
body:has(#sidebar-dynamic[data-has-sidebar="false"]) [data-drawer-target="console-sidebar"] { display: none !important; }

/* Active sidebar link highlight driven by aria-current (no re-render needed) */
#sidebar-nav a[aria-current="page"] {
  border-left-color: #f38020 !important;
  background-color: rgba(243, 128, 32, 0.1) !important;
  color: #e07019 !important;
  font-weight: 600 !important;
}
#sidebar-nav a[aria-current="page"] > span.shrink-0:first-child {
  color: #f38020 !important;
}
.dark #sidebar-nav a[aria-current="page"] {
  background-color: rgba(243, 128, 32, 0.15) !important;
  color: #fb923c !important;
}
.dark #sidebar-nav a[aria-current="page"] > span.shrink-0:first-child {
  color: #fb923c !important;
}
/* Active dot indicator */
#sidebar-nav a[aria-current="page"]::after {
  content: "";
  margin-left: auto;
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 9999px;
  background-color: #f38020;
  flex-shrink: 0;
}
/* 5. Overlay scrollbar — reusable thin scrollbar, hidden until hover or active scroll */
/* Usage: add class="overlay-scroll" (or data-overlay-scroll) to any scrollable container.
   The JS in static/js/app.js auto-initialises [data-overlay-scroll] elements. */
.overlay-scroll {
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
.overlay-scroll:hover,
.overlay-scroll.is-scrolling {
  scrollbar-color: rgba(145,158,171,.28) transparent;
}
.overlay-scroll::-webkit-scrollbar { width: 6px; background: transparent; }
.overlay-scroll::-webkit-scrollbar-track { background: transparent; margin: 6px 0; }
.overlay-scroll::-webkit-scrollbar-thumb {
  background-color: transparent;
  border-radius: 999px;
  transition: background-color .2s ease;
}
.overlay-scroll:hover::-webkit-scrollbar-thumb,
.overlay-scroll.is-scrolling::-webkit-scrollbar-thumb { background-color: rgba(145,158,171,.28); }
.overlay-scroll::-webkit-scrollbar-thumb:hover { background-color: rgba(145,158,171,.48); }

/* Legacy alias — sidebar still uses sidebar-scroll for backward compat */
.sidebar-scroll {
  flex: 1;
  min-height: 0;
}
.sidebar-scroll {
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
#console-sidebar:hover .sidebar-scroll,
.sidebar-scroll.is-scrolling {
  scrollbar-color: rgba(145,158,171,.28) transparent;
}
.sidebar-scroll::-webkit-scrollbar { width: 6px; background: transparent; }
.sidebar-scroll::-webkit-scrollbar-track { background: transparent; margin: 6px 0; }
.sidebar-scroll::-webkit-scrollbar-thumb {
  background-color: transparent;
  border-radius: 999px;
  transition: background-color .2s ease;
}
#console-sidebar:hover .sidebar-scroll::-webkit-scrollbar-thumb,
.sidebar-scroll.is-scrolling::-webkit-scrollbar-thumb { background-color: rgba(145,158,171,.28); }
.sidebar-scroll::-webkit-scrollbar-thumb:hover { background-color: rgba(145,158,171,.48); }

/* Vertical trunk */
.sidebar-tree {
  position: relative;
  margin-left: 1.25rem;
  padding-left: 1rem;
}

.sidebar-tree::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 1.2rem;        /* stop the trunk before the last item's midpoint */
  width: 1.5px;
  background-color: var(--sidebar-tree-line, rgba(100,116,139,.25));
}

/* Curved L-branch per item */
.sidebar-tree-item {
  position: relative;
}

.sidebar-tree-item::before {
  content: "";
  position: absolute;
  top: -0.4rem;
  left: -1rem;
  width: 0.75rem;
  height: 1.5rem;
  border-left: 1.5px solid var(--sidebar-tree-line, rgba(100,116,139,.25));
  border-bottom: 1.5px solid var(--sidebar-tree-line, rgba(100,116,139,.25));
  border-bottom-left-radius: 8px;
}

/* Smooth collapse using grid trick */
.sidebar-tree-collapse {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.24s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar-tree-collapse.is-open {
  grid-template-rows: 1fr;
}
.sidebar-tree-collapse-inner {
  overflow: hidden;
}
