/*
 * ECOFACTOR Design System v0.2 — canonical layout CSS for the
 * Internal Platform layer at compact density (240px sidebar, 13px nav,
 * accounting-friendly).
 *
 * Pairs with the Jinja2 canonical template at:
 *     templates/internal-platform/dashboard.html.j2
 *
 * This file is served by the design-system CDN at:
 *     https://id.ecofactortech.com/ds/v0.2/presets/layouts/internal-platform-compact.css
 *
 * Consumers add it via a single <link> in the canonical head_extra block:
 *
 *     {% block head_extra %}
 *       <link rel="stylesheet"
 *             href="https://id.ecofactortech.com/ds/v0.2/presets/layouts/internal-platform-compact.css">
 *       <link rel="stylesheet" href="{{ base_path }}/static/css/<product>.css">
 *     {% endblock %}
 *
 * Tokens (--surface-1, --text-primary, --container-action, etc.) come from
 * design-system/presets/base.css which is loaded by the canonical template
 * earlier in the cascade.
 *
 * Reference implementation:  SANDBOX/static/css/sandbox.css lines 1-410
 * (Sandbox is the on-prod canonical compact reference — see LAYERS.md).
 *
 * This file is the SINGLE SOURCE OF TRUTH for Internal Platform layout
 * styling. Sandbox's sandbox.css and Finance's finance.css contain mirrors
 * of the same patterns until they cut over to a pure CDN <link> setup —
 * keep this file and those mirrors in sync.
 */

/* ── Reset (only what the design-system base.css doesn't already do) ─ */
*, *::before, *::after { box-sizing: border-box; }
html, body { overflow-x: hidden; }
body { margin: 0; }

/* ── App shell — sticky sidebar + scrolling content ─────────── */
/* Lock the grid to viewport height; scrolling moves into .page-content
   inside .main. Without height:100vh+overflow:hidden, sticky sidebar
   becomes useless when content is taller than viewport. */
.app-shell {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar (Internal Platform: light, var(--surface-1)) ────── */
.sidebar {
  background: var(--surface-1);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
  border-right: 1px solid var(--outline-secondary);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 14px 12px;
  font-weight: 700;
  font-size: 16px;
  color: var(--text-action);
  letter-spacing: 0.02em;
}
.sidebar-brand .brand-mark {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--container-action);
}

/* Hamburger inside sidebar header — collapses sidebar so the table area
   takes the full viewport. Hidden by default; product opts in. */
.sidebar-toggle {
  background: none;
  border: none;
  padding: 4px;
  margin: -4px 4px -4px -2px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: var(--surface-2); color: var(--text-primary); }
.sidebar-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--state-focus-ring);
}

/* Floating "show sidebar" button — appears only when body.sidebar-collapsed.
   Wire its onclick to toggle body.sidebar-collapsed in product JS. */
.main-sidebar-toggle {
  position: fixed;
  top: 14px; left: 14px;
  z-index: 70;
  width: 36px; height: 36px;
  background: var(--surface-1);
  border: 1px solid var(--outline-secondary);
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-primary);
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
.main-sidebar-toggle:hover { background: var(--surface-2); }
.main-sidebar-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--state-focus-ring);
}

/* ── Mobile topbar — sticky bar on small screens with hamburger + brand. ──
   Hidden on desktop; revealed in @media (max-width: 768px) below. Canonical
   Jinja2 dashboard.html.j2 renders this element automatically. */
.mobile-topbar {
  display: none;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  height: 48px;
  position: sticky;
  top: 0;
  z-index: 80;
  background: var(--surface-1);
  border-bottom: 1px solid var(--outline-secondary);
}
.mobile-topbar .menu-toggle {
  background: none;
  border: none;
  padding: 4px 8px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: var(--text-primary);
}
.mobile-topbar .menu-toggle:hover { color: var(--text-action); }
.mobile-topbar .mobile-brand {
  font-weight: 700;
  color: var(--text-action);
  font-size: 14px;
}
.mobile-topbar .mobile-brand img { height: 20px; width: auto; }

body.sidebar-collapsed .sidebar { display: none; }
body.sidebar-collapsed .app-shell { grid-template-columns: minmax(0, 1fr); }
body.sidebar-collapsed .main-sidebar-toggle { display: inline-flex; }
body.sidebar-collapsed .topbar,
body.sidebar-collapsed .section-header,
body.sidebar-collapsed .subnav,
body.sidebar-collapsed .page-content { padding-left: 60px; }

/* ── Sidebar navigation ─────────────────────────────── */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.sidebar-nav .section-title {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 10px;
  color: var(--text-tertiary);
  padding: 12px 8px 4px;
}
.sidebar-nav .nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  transition: background 150ms ease, color 150ms ease;
  border-left: 3px solid transparent;
}
.sidebar-nav .nav-link:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}
.sidebar-nav .nav-link.active {
  background: color-mix(in srgb, var(--container-action) 10%, transparent);
  color: var(--text-action);
  border-left-color: var(--container-action);
}
.sidebar-nav .nav-link .ico {
  width: 18px;
  text-align: center;
  color: var(--icon-tertiary);
}
.sidebar-nav .nav-link.active .ico { color: var(--icon-action); }
.sidebar-nav .nav-link.disabled {
  color: var(--text-tertiary);
  cursor: default;
  pointer-events: none;
  opacity: 0.65;
}
.sidebar-nav .nav-link.disabled .ico {
  color: var(--text-tertiary);
  opacity: 0.6;
}

/* ── Sidebar bottom ─────────────────────────────────── */
.sidebar-bottom {
  border-top: 1px solid var(--outline-secondary);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface-1);
}
.sidebar-bottom .lang-row {
  display: flex;
  justify-content: flex-start;
  padding: 0 4px;
  position: relative;
}

/* Force lang-switcher dropdown to open UPWARD when at sidebar bottom.
   Without this the absolute-positioned dropdown is clipped by the
   sidebar's bottom edge / next stacking context. */
.sidebar-bottom .ecofactor-lang-switcher .lang-dropdown {
  bottom: 100%;
  top: auto;
  margin-bottom: 4px;
  margin-top: 0;
  left: 0;
  right: auto;
}

/* Default user-row (Sandbox style: single-line) */
.sidebar-bottom .user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  font-size: 13px;
  color: var(--text-secondary);
}
.sidebar-bottom .user-row .user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--container-action) 14%, transparent);
  color: var(--text-action);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
  overflow: hidden;
}
.sidebar-bottom .user-row .user-avatar-img {
  object-fit: cover;
  background: var(--surface-2);
  display: block;
  width: 100%;
  height: 100%;
}
.sidebar-bottom .user-row .user-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-primary);
}
.sidebar-bottom .logout-link {
  font-size: 12px;
  color: var(--container-error);
  text-decoration: none;
  white-space: nowrap;
  margin-left: auto;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
}
.sidebar-bottom .logout-link:hover { text-decoration: underline; }

/* Double-line variant (Finance style: name + role on two lines).
   Activated by wrapping name+role in .user-info inside the override
   block — see LAYOUT-PATTERNS.md "User-block variants". */
.sidebar-bottom .user-row .user-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}
.sidebar-bottom .user-row .user-info .user-name {
  flex: none;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-bottom .user-row .user-info .user-role {
  font-size: 11px;
  color: var(--text-tertiary);
  text-transform: capitalize;
}

/* ── Main content area — sticky topbar + scrolling page-content ─ */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  max-width: 100%;
  height: 100vh;
  background: var(--surface-page);
  overflow: hidden;
}

/* Optional Finance-style topbar (rendered via {% block topbar %}).
   Sandbox doesn't override this block, so by default it's empty and
   page-content sits flush against the top of .main. */
.topbar {
  background: var(--surface-1);
  border-bottom: 1px solid var(--outline-secondary);
  display: flex;
  align-items: center;
  padding: 12px 14px;
  gap: 12px;
  min-height: 52px;
  flex: 0 0 auto;
}
.topbar h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}
.topbar .hamburger {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-secondary);
}

/* .section-header — legacy V1 in-content title (Sandbox uses this).
   Aligned visually with .page-header so Sandbox / Pulse / ID admin all
   render their primary heading identically. Existing template structure
   (h1 + .section-sub span) keeps working — no Sandbox template rewrite needed. */
.section-header {
  background: transparent;
  padding: 0;
  display: block;
  margin-bottom: 16px;
  min-width: 0;
  flex-shrink: 0;
}
.section-header h1 {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.25;
  margin: 0 0 4px;
}
.section-header .section-sub {
  display: block;
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.4;
  margin: 0;
}

.subnav {
  background: var(--surface-1);
  border-bottom: 1px solid var(--outline-secondary);
  padding: 0 14px;
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  white-space: nowrap;
  flex-shrink: 0;
}
.subnav .subnav-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 14px 14px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  border-bottom: 2px solid transparent;
  transition: color 150ms ease, border-color 150ms ease;
}
.subnav .subnav-tab:hover { color: var(--text-primary); }
.subnav .subnav-tab.active {
  color: var(--text-action);
  border-bottom-color: var(--container-action);
}
.subnav .subnav-spacer { flex: 1; }
.subnav .subnav-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.page-content {
  padding: 16px 14px;
  width: 100%;
  flex: 1 1 auto;
  overflow-y: auto;
}

/* ── Page header — primary heading at the top of .page-content ──────
   Canonical structure for "page title + subtitle + actions row" present
   in every list/dashboard page. Flex with wrap so actions can fall to
   their own row on narrow viewports without breaking the title. */
.page-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.page-header__main {
  flex: 1 1 auto;
  min-width: 0;
}
.page-header__title {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.25;
  margin: 0 0 4px;
}
.page-header__subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
  margin: 0;
}
.page-header__subtitle .muted,
.page-header__count {
  color: var(--text-tertiary);
  margin-left: 8px;
  font-weight: 400;
}
.page-header__actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* ── Stat cards — KPI tiles row at the top of dashboard pages ──────
   Canonical structure for "label + value + optional sub-text" tiles.
   Variants add a colored left-border accent for status (action/success/
   warning/error/info). Sandbox legacy `.kpi-grid` / `.kpi-card` /
   `.kpi-label` / `.kpi-value` / `.kpi-sub` are aliased so existing
   Sandbox templates render identically without rewrite. */
.stat-cards,
.stats-grid,
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}
.stat-card,
.kpi-card {
  background: var(--surface-1);
  border: 1px solid var(--outline-secondary);
  border-radius: 12px;
  padding: 16px 18px;
  position: relative;
  text-align: left;
}
.stat-card__label,
.kpi-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-tertiary);
  margin: 0 0 8px;
  font-weight: 500;
}
.stat-card__value,
.kpi-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.1;
  margin: 0;
}
.stat-card__sub,
.kpi-sub {
  font-size: 12px;
  color: var(--text-secondary);
  margin: 6px 0 0;
}

/* Variants — colored left-border accent */
.stat-card--accent,
.kpi-card.accent  { border-left: 3px solid var(--container-action); }
.stat-card--success,
.kpi-card.success { border-left: 3px solid var(--container-success); }
.stat-card--warning,
.kpi-card.warning,
.kpi-card.attention { border-left: 3px solid var(--container-warning); }
.stat-card--error,
.kpi-card.error,
.kpi-card.urgent  { border-left: 3px solid var(--container-error); }
.stat-card--info,
.kpi-card.info    { border-left: 3px solid var(--container-info); }

/* Finance legacy aliases — `.label / .value / .note` inside .stat-card.
   Map onto BEM-style children so Finance dashboard.html renders without
   template rewrite. */
.stat-card > .label  { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em;
                       color: var(--text-tertiary); margin: 0 0 8px; font-weight: 500; }
.stat-card > .value  { font-size: 28px; font-weight: 700; color: var(--text-primary);
                       line-height: 1.1; margin: 0; }
.stat-card > .note   { font-size: 12px; color: var(--text-secondary); margin: 6px 0 0; }

/* ── Filter row — common pattern above tables (search + selects) ──── */
.filters-row {
  display: flex;
  flex-wrap: wrap;          /* mobile: stack on multiple rows */
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
@media (min-width: 640px) {
  /* desktop: stay on a single row — search shrinks, selects keep size */
  .filters-row { flex-wrap: nowrap; }
}
.filters-row .input,
.filters-row select.input,
.filters-row > input,
.filters-row > select {
  height: 36px;
}
.filters-row .filters-row__search {
  flex: 1 1 200px;          /* grow to fill, shrink down to 200px */
  min-width: 0;             /* allow shrink below content size */
}
.filters-row .filters-row__select {
  flex: 0 0 auto;
  min-width: 140px;
  max-width: 200px;
}
.filters-row .filters-row__spacer { flex: 1; }
.filters-row .filters-row__actions {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-left: auto;
}

/* ── Tables — compact density override of base.css .table ─────────
   base.css ships .table with 12-16px padding / 14px header / 16px cells
   (comfortable density). Internal Platform compact pages need tighter
   rows. Override here. Variants:
     .table-wrap         — outer wrapper with .card surface + horizontal scroll
     .table--striped     — alternating row background
     .table--hoverable   — row hover (default behavior, can be opted-out by .no-hover)
     .table--sticky-head — sticky thead inside scrollable .table-wrap
     th[role="button"]   — sortable header (cursor + indicator) */

.table-wrap {
  background: var(--surface-1);
  border: 1px solid var(--outline-secondary);
  border-radius: 12px;
  overflow: hidden;
  /* When a table-wrap should scroll horizontally, parent sets max-width
     and .table-wrap--scroll handles overflow. */
}
.table-wrap--scroll {
  overflow-x: auto;
}

.table {
  width: 100%;
  background: transparent;
  border-radius: 0;
  border-collapse: collapse;
  font-size: 13px;
}
.table thead { background: var(--surface-2); }
.table thead th {
  padding: 8px 12px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
  border-bottom: 1px solid var(--outline-secondary);
  white-space: nowrap;
}
.table tbody td {
  padding: 8px 12px;
  font-size: 13px;
  color: var(--text-primary);
  border-bottom: 1px solid var(--outline-secondary);
  vertical-align: middle;
}
.table tbody tr:last-child td { border-bottom: none; }

.table--hoverable tbody tr,
.table tbody tr {
  transition: background 120ms ease;
}
.table--hoverable tbody tr:hover,
.table tbody tr:not(.no-hover):hover {
  background: var(--surface-2);
}

.table--striped tbody tr:nth-child(odd) {
  background: color-mix(in srgb, var(--surface-2) 40%, transparent);
}
.table--striped tbody tr:nth-child(odd):hover {
  background: var(--surface-2);
}

.table--sticky-head thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
}

/* Sortable header — show pointer + sort caret. Product wires aria-sort. */
.table thead th[role="button"],
.table thead th.sortable {
  cursor: pointer;
  user-select: none;
}
.table thead th[role="button"]:hover,
.table thead th.sortable:hover {
  color: var(--text-primary);
}
.table thead th[aria-sort="ascending"]::after  { content: " ↑"; opacity: 0.7; }
.table thead th[aria-sort="descending"]::after { content: " ↓"; opacity: 0.7; }

/* Numeric cells — right-align */
.table th.num,
.table td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Action column — narrow, right-aligned */
.table th.actions,
.table td.actions { width: 1%; white-space: nowrap; text-align: right; }

/* Status indicator dot (often used in name cell next to text) */
.table .status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 8px;
  flex-shrink: 0;
}
.table .status-dot--online  { background: var(--container-success); }
.table .status-dot--offline { background: var(--container-error); }
.table .status-dot--idle    { background: var(--text-tertiary); }

/* Pagination row — pairs with table at bottom */
.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-top: 1px solid var(--outline-secondary);
  font-size: 12px;
  color: var(--text-secondary);
}
.table-pagination__info { color: var(--text-tertiary); }
.table-pagination__nav {
  display: flex;
  gap: 4px;
  align-items: center;
}
.table-pagination__nav button {
  background: transparent;
  border: 1px solid var(--outline-secondary);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-secondary);
  min-width: 28px;
}
.table-pagination__nav button:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text-primary);
}
.table-pagination__nav button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.table-pagination__nav button.active {
  background: var(--container-action);
  color: var(--text-reversed, #fff);
  border-color: var(--container-action);
}

/* ── Mobile responsive ─────────────────────────────── */
@media (max-width: 768px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 240px;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s;
  }
  .sidebar.open { transform: translateX(0); }
  /* Mobile topbar replaces the desktop sidebar header with hamburger + brand */
  .mobile-topbar { display: flex; }
  /* On mobile, the topbar/mobile-topbar already provides a hamburger.
     Hide the floating .main-sidebar-toggle so we don't double up. */
  body.sidebar-collapsed .main-sidebar-toggle { display: none; }
  .topbar .hamburger { display: inline-flex; }
}
