/* =========================================================
   1. BASE LAYOUT
   ========================================================= */
body {
  display: flex;
  min-height: 100vh;
  background: var(--main);
  color: var(--foreground);
  font-family: "Nunito", sans-serif;
  font-size: 0.95rem;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--default-link-color);
  text-decoration: none;
  transition: color 160ms ease, background-color 160ms ease, border-color 160ms ease;
}

a:hover, a:focus, a:active {
  text-decoration: underline;
}

button,
.btn,
.form-control,
.form-select {
  transition: color 160ms ease, background-color 160ms ease, border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

button:focus-visible,
a:focus-visible,
.btn:focus-visible,
.form-control:focus-visible,
.form-select:focus-visible,
.form-check-input:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  box-shadow: var(--focus-ring);
}

h1, h2, h3, h4, h5, h6 {
  color: var(--foreground);
  font-weight: 800;
  letter-spacing: -0.025em;
}

h1 { font-size: clamp(1.5rem, 2vw, 2rem); }

label {
  margin-bottom: 0.5rem;
  color: var(--foreground);
  font-weight: 700;
}

code,
.font-monospace,
.technical-code {
  font-family: "Geist Mono", "JetBrains Mono", monospace;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

::selection {
  color: var(--primary-foreground);
  background: var(--primary);
}

/* =========================================================
   2. SIDEBAR
   ========================================================= */

/* Sidebar on the LEFT */
#sidebar {
  order: 1;
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  transition: width 0.25s ease, min-width 0.25s ease;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(38, 24, 9, 0.08);
}

#sidebar .nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

#sidebar.collapsed {
  width: var(--sidebar-width-collapsed);
  min-width: var(--sidebar-width-collapsed);
}

/* -- Logo -- */
#sidebar .logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 16px;
  border-bottom: 1px solid var(--sidebar-border);
  white-space: nowrap;
  width: 100%;
  min-height: 76px;
}
#sidebar .logo .brand-home { display: flex; align-items: center; gap: 12px; color: var(--sidebar-text); text-decoration: none; }
#sidebar .logo .brand-mark { width: 34px; height: 34px; flex: 0 0 34px; }
#sidebar .logo .bi-hexagon-fill { display: none; }
#sidebar .logo .accent-text { color: var(--accent); }
#sidebar .logo .brand-name { color: var(--sidebar-text); font-size: 1rem; letter-spacing: -0.02em; }
#sidebar.collapsed .logo span { display: none; }
#sidebar.collapsed .toggle-sidebar { display: none; }

/* -- Nav links -- */
#sidebar .nav-link {
  color: var(--sidebar-text);
  padding: 11px 14px;
  margin: 2px 10px;
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
  font-weight: 700;
  transition: background-color 160ms ease, color 160ms ease, transform 160ms ease;
}
#sidebar .nav-link:hover { background: var(--sidebar-hover-bg); color: var(--sidebar-text-hover); }
#sidebar .nav-link:active { transform: scale(0.98); }
#sidebar .nav-link i.bi { font-size: 1.1rem; width: 22px; text-align: center; }
#sidebar .nav-link .chevron { margin-left: auto; transition: transform 0.2s; }
#sidebar .nav-link[aria-expanded="true"] .chevron { transform: rotate(90deg); }

#sidebar.collapsed .nav-link span.label,
#sidebar.collapsed .nav-link .chevron { display: none; }

#sidebar .submenu .nav-link { padding-left: 34px; font-size: 0.92rem; }
#sidebar .submenu .submenu .nav-link { padding-left: 52px; font-size: 0.88rem; }
#sidebar .submenu hr { border-color: var(--sidebar-border); opacity: 1; margin: 0.65rem 1rem; }

#sidebar a.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-text-active);
  box-shadow: 0 4px 12px rgba(46, 25, 5, 0.12);
}

/* -- Overlay behind the sidebar on mobile -- */
#sidebarOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1035;
}
#sidebarOverlay.show { display: block; }

/* -- Responsive behavior -- */
@media (min-width: 768px) {
  #sidebar .logo .toggle-sidebar { display: none; }
}

@media (max-width: 768px) {
  /* Sidebar becomes off-canvas, hidden by default */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 1040;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  #sidebar.mobile-open {
    transform: translateX(0);
  }
  /* On mobile the "collapsed" (icon-only) state is not used; width stays full */
  #sidebar.collapsed {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
  }
  #sidebar.collapsed .logo span,
  #sidebar.collapsed .nav-link span.label,
  #sidebar.collapsed .nav-link .chevron {
    display: inline;
  }
}


/* =========================================================
   3. MAIN CONTENT
   ========================================================= */
#main {
  order: 2;
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--content-bg);
  min-width: 0;
}

#topbar {
  position: sticky;
  top: 0;
  z-index: 1020;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--topbar-border);
  padding: 12px clamp(16px, 3vw, 36px);
  align-items: center;
  gap: 12px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.topbar-heading,
.topbar-actions,
.topbar-user {
  display: flex;
  align-items: center;
}

.topbar-heading { gap: 14px; min-width: 0; }
.topbar-heading .toggle-sidebar { border-color: var(--border); color: var(--foreground); }
.topbar-title { min-width: 0; }
.topbar-title .eyebrow {
  display: block;
  color: var(--muted-foreground);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  line-height: 1.1;
  text-transform: uppercase;
}
.topbar-title h1 {
  margin: 3px 0 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar-actions { gap: 14px; }
.topbar-user {
  gap: 9px;
  min-width: 0;
  color: var(--foreground);
  font-weight: 700;
}
.topbar-user .user-avatar {
  display: grid;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--muted);
  color: var(--default-link-color);
}
.topbar-user .user-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.theme-toggle {
  display: inline-flex;
  min-width: 42px;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: var(--card);
  color: var(--foreground);
}
.theme-toggle:hover { border-color: var(--primary); background: var(--muted); }
.theme-toggle:active { transform: scale(0.97); }
.theme-toggle svg { width: 21px; height: 21px; }
.theme-toggle .theme-toggle-label { display: none; }

#content {
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  padding: clamp(18px, 3vw, 36px);
  flex: 1;
}

.widget-content { width: 100%; }
.widget-content > .card,
.widget-content > .card-body {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--card-foreground);
  box-shadow: 0 2px 10px rgba(24, 24, 27, 0.035);
}

.app-footer {
  padding: 16px clamp(18px, 3vw, 36px) 22px;
  border-top: 1px solid var(--border);
  color: var(--muted-foreground);
  font-size: 0.8rem;
  line-height: 1.5;
}
.app-footer p { margin: 0; }
.app-footer .footer-brand { color: var(--foreground); font-weight: 800; }

.flash-messages { margin-bottom: 1.25rem; }
.alert { border-radius: 0.85rem; }

.welcome-panel {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 8px 30px rgba(24, 24, 27, 0.05);
}
.welcome-panel::before {
  position: absolute;
  inset: 0 auto 0 0;
  width: 5px;
  background: var(--primary);
  content: "";
}
.welcome-panel-body { padding: clamp(24px, 4vw, 48px); }
.welcome-panel .eyebrow { color: var(--muted-foreground); font-size: 0.75rem; letter-spacing: 0.08em; }
.welcome-panel h2 { margin-bottom: 0.75rem; color: var(--default-link-color); }
.welcome-panel p { max-width: 680px; color: var(--muted-foreground); font-size: 1.05rem; line-height: 1.7; }
.welcome-links { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-top: 28px; }
.welcome-link {
  display: flex;
  min-height: 72px;
  align-items: center;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 0.9rem;
  background: var(--background);
  color: var(--foreground);
  font-weight: 800;
}
.welcome-link:hover { border-color: var(--primary); background: var(--muted); color: var(--foreground); text-decoration: none; }
.welcome-link:active { transform: scale(0.99); }
.welcome-link .bi { color: var(--default-link-color); font-size: 1.3rem; }

.card,
.modal-content,
.dropdown-menu {
  border-color: var(--border);
  border-radius: 0.9rem;
  background-color: var(--card);
  color: var(--card-foreground);
}
.card { box-shadow: 0 2px 10px rgba(24, 24, 27, 0.035); }
.card-body { padding: 1.5rem; }
.card-title { color: var(--default-link-color); font-weight: 800; }

.table {
  --bs-table-color: var(--foreground);
  --bs-table-bg: transparent;
  --bs-table-border-color: var(--border);
  --bs-table-hover-bg: var(--muted);
  --bs-table-hover-color: var(--foreground);
  color: var(--foreground);
}
.table > :not(caption) > * > * { padding: 0.8rem 0.75rem; }
.table thead th { background: var(--muted); color: var(--foreground); font-size: 0.82rem; font-weight: 800; }
.table-sort-link { color: var(--foreground); }
a.table-sort-link.current-order i { border-color: var(--primary); color: var(--default-link-color); }

.form-control,
.form-select {
  min-height: 42px;
  border-color: var(--input);
  border-radius: 0.7rem;
  background-color: var(--background);
  color: var(--foreground);
}
.form-control::placeholder { color: var(--muted-foreground); opacity: 0.75; }
.form-control:focus,
.form-select:focus {
  border-color: var(--primary);
  background-color: var(--background);
  color: var(--foreground);
  box-shadow: var(--focus-ring);
}
.form-check-input { border-color: var(--input); background-color: var(--background); }
.form-check-input:checked { border-color: var(--primary); background-color: var(--primary); }
.form-text,
.text-muted,
.text-secondary { color: var(--muted-foreground) !important; }

.btn {
  border-radius: 0.7rem;
  font-weight: 800;
  transition-duration: 150ms;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  border-color: var(--primary);
  background-color: var(--primary);
  color: var(--primary-foreground);
}
.btn-primary:hover {
  border-color: var(--primary-hover);
  background-color: var(--primary-hover);
  color: var(--primary-foreground);
}
.btn-success { border-color: var(--success); background-color: var(--success); color: #fff; }
.btn-success:hover { border-color: #116a32; background-color: #116a32; color: #fff; }
.btn-outline-primary {
  border-color: var(--primary);
  color: var(--default-link-color);
}
.btn-outline-primary:hover { border-color: var(--primary); background: var(--primary); color: var(--primary-foreground); }

.badge { border-radius: 0.5rem; font-weight: 800; }
.nav-pills .nav-link { border-radius: 0.75rem; font-weight: 700; }
.pagination { gap: 4px; }
.page-link { border-color: var(--border); border-radius: 0.55rem !important; background: var(--card); color: var(--default-link-color); }
.page-item.active .page-link { border-color: var(--primary); background: var(--primary); color: var(--primary-foreground); }

.dark .card,
.dark .modal-content,
.dark .dropdown-menu,
.dark .list-group-item,
.dark .popover,
.dark .accordion-item {
  border-color: var(--border);
  background-color: var(--card);
  color: var(--card-foreground);
}
.dark .dropdown-item { color: var(--foreground); }
.dark .dropdown-item:hover,
.dark .dropdown-item:focus { background: var(--muted); color: var(--foreground); }
.dark .btn-close { filter: invert(1) grayscale(100%) brightness(200%); }
.dark .text-dark,
.dark .link-dark { color: var(--foreground) !important; }
.dark .table-light,
.dark .table-light > * > * { --bs-table-bg: var(--muted); --bs-table-color: var(--foreground); }
.dark .tom-select .ts-control,
.dark .ts-control,
.dark .ts-dropdown { border-color: var(--input); background: var(--background); color: var(--foreground); }
.dark .ql-toolbar.ql-snow,
.dark .ql-container.ql-snow { border-color: var(--border); }
.dark .ql-editor,
.dark .ql-snow .ql-stroke { color: var(--foreground); stroke: var(--foreground); }
.dark .ql-snow .ql-fill { fill: var(--foreground); }

#sidebarOverlay {
  background: rgba(24, 24, 27, 0.52);
}


/* =========================================================
   4. SEARCH FORM CARD
   ========================================================= */
.card.search-form .card-body {
  padding: 0;
}

.card.search-form .card-body .search-fields {
  padding: 1.25rem;
  background-color: var(--search-fields-bg);
  color: var(--search-fields-color);
}

.card.search-form .card-body .search-fields label {
  color: var(--search-fields-color);
}

.card.search-form .card-title {
  background-color: var(--search-title-bg);
  color: var(--search-title-color);
  padding: 1.25rem 1.25rem 0.25rem;
  margin: 0;
}
.card.search-form h3.card-title {
  font-size: 1.1rem;
}


/* =========================================================
   5. FORMS
   ========================================================= */
.card.object-form {
  border-top: 3px solid var(--primary);
  overflow: hidden;
}

.field_with_errors input,
.field_with_errors select {
  border-color: var(--bs-danger);
}


/* =========================================================
   6. BUTTONS
   ========================================================= */
.btn-pure {
  border: none;
  background: transparent;
  padding: .25rem;
}

.btn-pure:hover {
  background-color: var(--muted);
}


/* =========================================================
   7. TABLES
   ========================================================= */
a.table-sort-link i {
  padding: 2px 4px;
  border: 1px solid transparent;
}

a.table-sort-link.current-order i {
  font-weight: bold;
  background-color: var(--bs-secondary-bg);
  border: 1px solid var(--default-link-color);
}


/* =========================================================
   8. LOADING / SPINNER STATES
   ========================================================= */
@keyframes spin {
  to { transform: rotate(360deg); }
}

.spin {
  display: inline-block;
  animation: spin 1s linear infinite;
}

turbo-frame {
  position: relative;

  &[busy]::after {
    content: "";
    position: absolute;
    inset: 0;
    background: color-mix(in srgb, var(--background) 72%, transparent);
    z-index: 10;
  }

  &[busy]::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2rem;
    height: 2rem;
    margin: -1rem 0 0 -1rem;
    border: 3px solid var(--primary);
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
    z-index: 11;
  }
}

@media (max-width: 767.98px) {
  #topbar { padding: 10px 14px; }
  #content { padding: 18px 14px 24px; }
  .topbar-actions { gap: 8px; }
  .topbar-user .user-name { display: none; }
  .topbar-title .eyebrow { display: none; }
  .card-body { padding: 1.1rem; }
  .app-footer { padding: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
