/* ============================================================
   BASE.CSS — FIFO-ENGINE STRUCTURAL STYLES
   Physical structure only. No hardcoded colors.
   Colors come from the active theme.css via CSS variables.
   ============================================================ */

/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after {
  /* v10.31: switched from `thin` to `auto` so scrollbars stay visible on
     Firefox / Windows Chrome. Combined with the boosted webkit rules below,
     scrollbars are now always clearly visible instead of near-transparent. */
  scrollbar-width: auto;
  scrollbar-color: var(--scrollbar-thumb-strong, var(--scrollbar-thumb)) var(--scrollbar-track);
  box-sizing: border-box;
}
/* v10.31: bigger track + high-contrast thumb so users can actually see
   where the scrollbar is and grab it. Applies to every scrollable element
   in the app unless a page CSS overrides it. */
::-webkit-scrollbar { width: 14px; height: 14px; }
::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.06);
}
::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb-strong, var(--scrollbar-thumb));
  border: 2px solid var(--scrollbar-track);
  border-radius: 8px;
  min-height: 40px;
  min-width: 40px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.15);
}
::-webkit-scrollbar-thumb:hover  { background: var(--scrollbar-thumb-hover); }
::-webkit-scrollbar-thumb:active { background: var(--scrollbar-thumb-hover); }
::-webkit-scrollbar-corner { background: var(--scrollbar-track); }

html { height: 100%; }
body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  line-height: var(--line-height);
  margin: 0;
  height: 100%;
}

/* ============================================================
   APP SHELL LAYOUT
   ============================================================ */
.layout {
  display: flex;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

.page-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  height: 100dvh;
}

.content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 24px;
  min-width: 0;
}

/* ============================================================
   SIDEBAR — DESKTOP
   Width-only transition. NO opacity transition on desktop.
   Opacity + backdrop-filter animated together causes a
   browser repaint stutter that freezes the sidebar glass.
   ============================================================ */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  overflow-y: auto;
  overflow-x: hidden;
  /* desktop: only animate width, never opacity */
  transition: width 0.22s cubic-bezier(0.4,0,0.2,1),
              min-width 0.22s cubic-bezier(0.4,0,0.2,1);
  z-index: 200;
  flex-shrink: 0;
  /* ensure pointer-events are always on unless explicitly disabled */
  pointer-events: auto;
  visibility: visible;
}

/* Desktop collapsed: hide by width, no opacity trick */
.layout.sidebar-collapsed .sidebar {
  width: 0 !important;
  min-width: 0 !important;
  border-right: none;
  overflow: hidden;
  pointer-events: none;
}

/* ============================================================
   SIDEBAR CONTENT
   ============================================================ */
.logo-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--sidebar-border);
  flex-shrink: 0;
}
.logo-text {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-bold);
  color: var(--color-primary);
  letter-spacing: -0.02em;
}
.logo-sub {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  margin-top: 2px;
}
.nav-section {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  color: var(--sidebar-section-color);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 16px 16px 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  color: var(--sidebar-text);
  text-decoration: none;
  border-radius: var(--border-radius-sm);
  margin: 1px 8px;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  transition: var(--transition);
  white-space: nowrap;
  overflow: hidden;
  min-height: 44px;
}
.nav-item:hover  { background: var(--sidebar-item-hover);  color: var(--sidebar-text-active); }
.nav-item.active { background: var(--sidebar-item-active); color: var(--sidebar-text-active); }

.sidebar-bottom {
  margin-top: auto;
  padding: 12px 8px;
  border-top: 1px solid var(--sidebar-border);
  flex-shrink: 0;
}

/* ============================================================
   TOPBAR
   ============================================================ */
.page-head {
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--topbar-border);
  height: var(--topbar-height);
  min-height: var(--topbar-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  position: sticky;
  top: 0;
  z-index: 100;
  flex-shrink: 0;
  gap: 8px;
}
.topbar-left  { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.topbar-app   { font-size: var(--font-size-sm); font-weight: var(--font-weight-semibold); color: var(--topbar-text); white-space: nowrap; }
.topbar-user  {
  display: flex; align-items: center; gap: 8px;
  font-size: var(--font-size-sm); color: var(--color-text-secondary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 180px;
}
.topbar-page  { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-toggle {
  background: transparent;
  border: none;
  height: 40px;
  width: 40px;
  min-width: 40px;
  padding: 0;
  border-radius: var(--border-radius-sm);
  cursor: pointer;
  color: var(--color-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: var(--sidebar-item-hover); color: var(--color-primary); }

/* ============================================================
   CARD
   ============================================================ */
.card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--card-radius);
  box-shadow: var(--card-shadow);
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  padding: 16px 20px;
  margin-bottom: 16px;
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn,
button[type=submit],
button:not(.tab-btn):not(.nav-item):not(.sidebar-toggle) {
  height: var(--btn-height);
  padding: var(--btn-padding);
  border-radius: var(--btn-radius);
  font-size: var(--btn-font-size);
  font-weight: var(--btn-font-weight);
  border: 1px solid rgba(255,255,255,0.50);
  cursor: pointer;
  transition: var(--transition);
  background: var(--btn-primary-bg);
  color: var(--btn-primary-text);
  backdrop-filter: blur(8px);
  min-height: 36px;
  white-space: nowrap;
  flex-shrink: 0;
}
.btn:hover, button[type=submit]:hover { background: var(--btn-primary-hover); }
.btn-secondary { background: var(--btn-secondary-bg) !important; color: var(--btn-secondary-text) !important; border: 1px solid var(--btn-secondary-border) !important; }
.btn-danger, .btn-sm.btn-danger { background: var(--btn-danger-bg) !important; color: var(--btn-danger-text) !important; }
.btn-sm { height: 28px; padding: 0 10px; font-size: var(--font-size-xs); border-radius: 8px; }

/* ============================================================
   INPUTS
   ============================================================ */
input, select, textarea {
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--input-radius);
  color: var(--input-text);
  font-size: var(--font-size-sm);
  height: var(--input-height);
  padding: var(--input-padding);
  transition: var(--transition);
  width: 100%;
  outline: none;
  backdrop-filter: blur(8px);
  font-size: max(16px, var(--font-size-sm));
  -webkit-appearance: none;
  appearance: none;
}
textarea { height: auto; padding: 10px 12px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--input-border-focus); box-shadow: var(--input-shadow-focus); }
input::placeholder { color: var(--input-placeholder); }

/* ============================================================
   TABLE
   ============================================================ */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--table-bg);
  border-radius: var(--table-radius);
  overflow: hidden;
  font-size: var(--font-size-sm);
  backdrop-filter: var(--backdrop-blur);
  min-width: 480px;
}
th {
  background: var(--table-header-bg);
  color: var(--table-header-text);
  font-weight: var(--font-weight-semibold);
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--table-border);
  font-size: var(--font-size-xs);
  text-transform: uppercase;
;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--table-border);
  color: var(--table-text);
}
tr:hover td { background: var(--table-row-hover); }
tr:last-child td { border-bottom: none; }

/* ============================================================
   BADGES
   ============================================================ */
.badge { display: inline-block; padding: var(--badge-padding); border-radius: var(--badge-radius); font-size: var(--badge-font-size); font-weight: var(--font-weight-medium); }
.badge-active       { background: var(--badge-active-bg);   color: var(--badge-active-text); }
.badge-inactive     { background: var(--badge-inactive-bg); color: var(--badge-inactive-text); }
.badge-pending      { background: var(--badge-pending-bg);  color: var(--badge-pending-text); }
.badge-in_progress  { background: var(--badge-pending-bg);  color: var(--badge-pending-text); }
.badge-completed    { background: var(--badge-active-bg);   color: var(--badge-active-text); }
.badge-on_hold      { background: var(--badge-inactive-bg); color: var(--badge-inactive-text); }
.badge-danger, .badge-quarantined { background: var(--badge-danger-bg); color: var(--badge-danger-text); }

/* ============================================================
   TABS
   ============================================================ */
.tabs {
  display: flex;
  gap: 4px;
  padding: 12px 16px 0;
  border-bottom: 1px solid var(--border-color);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-shrink: 0;
}
.tabs::-webkit-scrollbar { display: none; }
.tab-btn {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 8px 14px;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-muted);
  cursor: pointer;
  border-radius: 0;
  height: auto;
  min-height: 40px;
  transition: var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
}
.tab-btn:hover  { color: var(--color-text); background: transparent; }
.tab-btn.active { color: var(--color-primary); border-bottom-color: var(--color-primary); }
.tab-panel { display: none; padding: 16px; }
.tab-panel.active { display: block; }

/* ============================================================
   PERMISSION MATRIX
   ============================================================ */
.matrix-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.matrix-table { font-size: var(--font-size-xs); min-width: 600px; }
.matrix-table .role-header { font-size: var(--font-size-xs); text-align: center; white-space: nowrap; }
.matrix-table .group-row { background: var(--table-header-bg); font-weight: var(--font-weight-semibold); color: var(--color-text-secondary); }
.matrix-table .perm-cell { text-align: center; }

/* ============================================================
   MODAL OVERLAY
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.40);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 16px;
}
.modal-box {
  width: 100%;
  max-width: 540px;
  max-height: 90dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ============================================================
   UTILITIES
   ============================================================ */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.span-2 { grid-column: span 2; }
.hint   { color: var(--color-text-muted); font-size: var(--font-size-sm); margin: 0 0 12px; }
.back   { color: var(--color-primary); text-decoration: none; font-size: var(--font-size-sm); margin-right: 12px; }
.hidden { display: none !important; }
.stack  { display: flex; flex-direction: column; gap: 12px; }
.row    { display: flex; gap: 8px; flex-wrap: wrap; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.toolbar input { max-width: 260px; flex: 1 1 180px; }

.progress-wrap  { margin-top: 12px; }
.progress-bar   { background: var(--color-bg-secondary); border-radius: 99px; height: 6px; min-width: 60px; overflow: hidden; }
.progress-fill  { background: var(--color-primary); height: 100%; border-radius: 99px; transition: width 0.3s ease; }
.progress-label { display: flex; justify-content: space-between; font-size: var(--font-size-xs); color: var(--color-text-muted); margin-bottom: 4px; }

.quick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  margin-top: 8px;
}
.quick-tile {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 14px 8px;
  background: var(--color-primary-light);
  color: var(--color-primary);
  border-radius: var(--border-radius);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  text-decoration: none;
  transition: var(--transition);
  min-height: 56px;
}
.quick-tile:hover { background: var(--btn-primary-bg); color: #fff; }

.tiles-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.tile { text-align: center; padding: 14px 8px; }
.tile-val { font-size: var(--font-size-2xl); font-weight: var(--font-weight-bold); color: var(--color-primary); }
.tile-lbl { font-size: var(--font-size-xs); color: var(--color-text-muted); margin-top: 4px; }

.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.info-field label { display: block; font-size: var(--font-size-xs); color: var(--color-text-muted); margin-bottom: 2px; text-transform: uppercase; letter-spacing: 0.04em; }
.info-field span  { font-size: var(--font-size-sm); color: var(--color-text); }
.info-field.span-2 { grid-column: span 2; }

/* ============================================================
   RESPONSIVE — tablet (max 900px)
   ============================================================ */
@media (max-width: 900px) {
  .two-col { grid-template-columns: 1fr; }
  .grid-3  { grid-template-columns: 1fr 1fr; }
  .info-grid { grid-template-columns: 1fr 1fr; }
}

/* ============================================================
   RESPONSIVE — MOBILE (max 768px)
   On mobile the sidebar overlays the content (fixed position).
   We use transform instead of width for smooth animation.
   ============================================================ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0;
    height: 100dvh;
    width: var(--sidebar-width) !important;
    min-width: var(--sidebar-width) !important;
    z-index: 300;
    /* on mobile: use transform for animation (no backdrop-filter conflict) */
    transform: translateX(0);
    transition: transform 0.22s cubic-bezier(0.4,0,0.2,1);
    box-shadow: var(--shadow-lg);
    /* no opacity animation on mobile either — avoids glass freeze */
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }
  .layout.sidebar-collapsed .sidebar {
    transform: translateX(-100%);
    /* pointer-events off immediately via JS — not via CSS delay */
    pointer-events: none !important;
    width: var(--sidebar-width) !important;
    min-width: var(--sidebar-width) !important;
    border-right: none;
  }
  .page-wrap { width: 100%; }
  .content { padding: 12px; }
  .card    { padding: 12px 14px; border-radius: 14px; }
  .grid-2  { grid-template-columns: 1fr; }
  .grid-3  { grid-template-columns: 1fr; }
  .span-2  { grid-column: span 1; }
  .two-col { grid-template-columns: 1fr; }
  .info-grid { grid-template-columns: 1fr; }
  .info-field.span-2 { grid-column: span 1; }
  .card table, section table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar input { max-width: 100%; }
  .topbar-user { display: none; }
  .topbar-sep  { display: none; }
  .quick-grid { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); }
  .tiles-grid { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); }
  .tab-btn { padding: 8px 10px; font-size: 0.8rem; }
}

/* ============================================================
   RESPONSIVE — small phones (max 390px)
   ============================================================ */
@media (max-width: 390px) {
  .topbar-app { display: none; }
  .page-head  { padding: 0 10px; }
  .content    { padding: 8px; }
  .card       { padding: 10px 12px; }
}
