/* ============================================================
   ECP Admin UI — global styles + components
   Depends on: tokens.css (must load first)
   ============================================================ */

/* ===== Reset / base ===== */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand); text-decoration: none; }
a:hover { color: var(--brand-hover); text-decoration: underline; }

/* Keyboard focus ring — applies to all interactive elements. Matches the
   brand color and stays visible only when navigating via keyboard. */
:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 3px;
}
.btn:focus-visible, .pagination__btn:focus-visible {
  outline-offset: 3px;
}

/* Skip link — hidden offscreen until focused, lands keyboard users on main. */
.skip-link {
  position: fixed;
  top: var(--space-2);
  left: var(--space-2);
  padding: var(--space-2) var(--space-4);
  background: var(--brand);
  color: var(--brand-fg);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  z-index: var(--z-overlay);
  transform: translateY(-200%);
  transition: transform var(--transition-fast);
}
.skip-link:focus {
  transform: translateY(0);
  text-decoration: none;
  color: var(--brand-fg);
}

/* Submit-in-progress state — set by base.html form-submit hook. */
.btn.is-submitting {
  opacity: 0.7;
  pointer-events: none;
  position: relative;
}
.btn.is-submitting::after {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-left: var(--space-2);
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: btn-spin 0.8s linear infinite;
}
@keyframes btn-spin { to { transform: rotate(360deg); } }

h1, h2, h3, h4 { margin: 0 0 var(--space-3); font-weight: var(--weight-semibold); line-height: var(--leading-tight); }
h1 { font-size: var(--text-3xl); }
h2 { font-size: var(--text-2xl); }
h3 { font-size: var(--text-xl); }
h4 { font-size: var(--text-lg); }
p  { margin: 0 0 var(--space-3); }

button { font-family: inherit; font-size: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: inherit; }

/* ===== Layout shell ===== */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--header-height) 1fr;
  grid-template-areas:
    "sidebar header"
    "sidebar main";
  min-height: 100vh;
}

.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-6);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}

.app-header__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}

.app-header__user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.app-header__user a { color: var(--color-text-muted); }
.app-header__user a:hover { color: var(--brand); }

.app-sidebar {
  grid-area: sidebar;
  background: var(--brand);
  color: var(--brand-fg);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.app-sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-5);
  height: var(--header-height);
  font-size: var(--text-lg);
  font-weight: var(--weight-bold);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  letter-spacing: 0.5px;
}

.app-sidebar__nav {
  display: flex;
  flex-direction: column;
  padding: var(--space-3) 0;
  gap: 2px;
}

.app-sidebar__link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  color: rgba(255, 255, 255, 0.85);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.app-sidebar__link:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  text-decoration: none;
}

.app-sidebar__link.is-active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border-left-color: #fff;
}

.app-main {
  grid-area: main;
  padding: var(--space-6);
  overflow-x: auto;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-5);
  gap: var(--space-3);
  flex-wrap: wrap;
}

.page-header__title {
  font-size: var(--text-2xl);
  font-weight: var(--weight-semibold);
  margin: 0;
}

.page-header__actions {
  display: flex;
  gap: var(--space-2);
}

/* ===== Card ===== */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card__header {
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--color-divider);
  font-weight: var(--weight-semibold);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card__body { padding: var(--space-5); }

/* ===== Button ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1.2;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: var(--color-surface);
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast),
              color var(--transition-fast);
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { text-decoration: none; }

.btn--primary {
  background: var(--brand);
  color: var(--brand-fg);
  border-color: var(--brand);
}
.btn--primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); color: var(--brand-fg); }
.btn--primary:active { background: var(--brand-active); }

.btn--secondary {
  background: var(--color-surface);
  border-color: var(--color-border-strong);
  color: var(--color-text);
}
.btn--secondary:hover { background: var(--color-surface-alt); border-color: var(--brand); color: var(--brand); }

.btn--ghost {
  background: transparent;
  color: var(--color-text-muted);
  border-color: transparent;
}
.btn--ghost:hover { background: var(--color-divider); color: var(--color-text); }

.btn--danger {
  background: var(--color-danger);
  color: #fff;
  border-color: var(--color-danger);
}
.btn--danger:hover { background: #991b1b; border-color: #991b1b; color: #fff; }

.btn--sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }
.btn--lg { padding: var(--space-3) var(--space-5); font-size: var(--text-base); }

.btn[disabled], .btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  pointer-events: none;
}

/* ===== Form controls ===== */
.form-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}
.form-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 160px;
}
.form-field__label {
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.input, .select {
  height: 34px;
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.input:focus, .select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(31, 66, 135, 0.15);
}
.input::placeholder { color: var(--color-text-faint); }

/* ===== Table ===== */
.table-wrap {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
}
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
.table th, .table td {
  padding: var(--space-3) var(--space-4);
  text-align: left;
  border-bottom: 1px solid var(--color-divider);
  vertical-align: middle;
}
.table th {
  background: var(--color-surface-alt);
  color: var(--color-text-muted);
  font-weight: var(--weight-semibold);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}
.table tbody tr:hover { background: var(--brand-soft); }
.table tbody tr:last-child td { border-bottom: none; }
.table--compact th, .table--compact td { padding: var(--space-2) var(--space-3); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ===== Status chip ===== */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  border-radius: var(--radius-pill);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  line-height: 1.4;
}
.chip--created   { color: var(--status-created-fg);   background: var(--status-created-bg); }
.chip--approved  { color: var(--status-approved-fg);  background: var(--status-approved-bg); }
.chip--shipped   { color: var(--status-shipped-fg);   background: var(--status-shipped-bg); }
.chip--delivered { color: var(--status-delivered-fg); background: var(--status-delivered-bg); }
.chip--returned  { color: var(--status-returned-fg);  background: var(--status-returned-bg); }
.chip--refused   { color: var(--status-refused-fg);   background: var(--status-refused-bg); }
.chip--cancelled { color: var(--status-cancelled-fg); background: var(--status-cancelled-bg); }
.chip--lost      { color: var(--status-lost-fg);      background: var(--status-lost-bg); }
.chip--neutral   { color: var(--color-text-muted);    background: var(--color-divider); }
.chip--success   { color: var(--color-success);       background: var(--color-success-soft); }
.chip--muted     { color: var(--color-text-muted);    background: var(--color-divider); }

/* ===== KPI card ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}
.kpi {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-5);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.kpi__label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: var(--weight-medium);
}
.kpi__value {
  font-size: var(--text-3xl);
  font-weight: var(--weight-bold);
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.kpi__hint {
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}
.kpi--accent { border-top: 3px solid var(--brand); }

/* ===== Empty state ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-10) var(--space-5);
  text-align: center;
  color: var(--color-text-muted);
  gap: var(--space-2);
}
.empty-state__icon {
  font-size: 36px;
  opacity: 0.45;
}
.empty-state__title {
  font-size: var(--text-lg);
  color: var(--color-text);
  font-weight: var(--weight-semibold);
  margin: 0;
}
.empty-state__hint { font-size: var(--text-sm); }

/* ===== Pagination ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border-top: 1px solid var(--color-divider);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  flex-wrap: wrap;
}
.pagination__info { display: flex; align-items: center; gap: var(--space-2); }
.pagination__pages { display: flex; align-items: center; gap: var(--space-1); }
.pagination__btn {
  min-width: 32px;
  height: 32px;
  padding: 0 var(--space-2);
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
.pagination__btn:hover {
  border-color: var(--brand);
  color: var(--brand);
  text-decoration: none;
}
.pagination__btn.is-active {
  background: var(--brand);
  border-color: var(--brand);
  color: var(--brand-fg);
}
.pagination__btn[disabled], .pagination__btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}
.pagination__size .select { height: 32px; }

/* ===== Alerts ===== */
.alert {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  margin-bottom: var(--space-4);
  font-size: var(--text-sm);
}
.alert--info { background: var(--color-info-soft); color: var(--color-info); border-color: var(--color-info); }
.alert--success { background: var(--color-success-soft); color: var(--color-success); border-color: var(--color-success); }
.alert--warning { background: var(--color-warning-soft); color: var(--color-warning); border-color: var(--color-warning); }
.alert--danger { background: var(--color-danger-soft); color: var(--color-danger); border-color: var(--color-danger); }

/* ===== Bulk action bar (floating, revealed when rows selected) ===== */
.bulk-action-bar {
  position: sticky;
  bottom: var(--space-4);
  z-index: 30;
  display: flex;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-3) var(--space-4);
  margin-top: var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}
.bulk-action-bar[hidden] { display: none; }
.bulk-action-bar__count {
  margin-right: auto;
  font-size: var(--text-sm);
  color: var(--color-text);
}

/* ===== Utilities ===== */
.muted { color: var(--color-text-muted); }
.faint { color: var(--color-text-faint); }
.mono  { font-family: var(--font-mono); }
.tnum  { font-variant-numeric: tabular-nums; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }

/* ===== Error page (404 / 403 / 500 — no shell) ===== */
.error-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-5);
  background: var(--color-bg);
}
.error-page__inner {
  max-width: 480px;
  text-align: center;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-8) var(--space-6);
  box-shadow: var(--shadow-md);
}
.error-page__code {
  font-size: 64px;
  font-weight: var(--weight-bold);
  color: var(--brand);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  margin-bottom: var(--space-4);
}
.error-page__title { font-size: var(--text-2xl); margin-bottom: var(--space-3); }
.error-page__message { color: var(--color-text-muted); margin-bottom: var(--space-5); }
.error-page__actions {
  display: flex;
  gap: var(--space-2);
  justify-content: center;
  flex-wrap: wrap;
}

/* ===== Login page (special: no shell) ===== */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-active) 100%);
  padding: var(--space-5);
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-8) var(--space-6);
}
.auth-card h1 {
  text-align: center;
  margin-bottom: var(--space-6);
  font-size: var(--text-2xl);
  color: var(--brand);
}
.auth-card .form-field { width: 100%; margin-bottom: var(--space-3); }
.auth-card .input { width: 100%; }
.auth-card .btn { width: 100%; margin-top: var(--space-3); }

/* ============================================================
   Mobile responsive (<= 768px)
   ============================================================ */
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: var(--header-height) 1fr;
    grid-template-areas:
      "header"
      "main";
  }
  .app-sidebar {
    position: fixed;
    top: 0; left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    transform: translateX(-100%);
    transition: transform var(--transition-base);
    z-index: var(--z-overlay);
    box-shadow: var(--shadow-lg);
  }
  .app-sidebar.is-open { transform: translateX(0); }
  .app-shell.has-overlay::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.4);
    z-index: calc(var(--z-overlay) - 1);
  }
  .app-header__menu-btn { display: inline-flex !important; }
  .app-main { padding: var(--space-4); }
  .page-header { flex-direction: column; align-items: stretch; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: var(--space-3); }
  .kpi__value { font-size: var(--text-2xl); }
  .table-wrap { overflow-x: auto; }
  .table th, .table td { white-space: nowrap; }
  .form-row { flex-direction: column; align-items: stretch; }
  .form-field { min-width: 0; }
  .pagination { flex-direction: column; align-items: stretch; gap: var(--space-2); }
  .bulk-action-bar {
    flex-wrap: wrap;
    gap: var(--space-2);
    bottom: var(--space-2);
  }
  .bulk-action-bar .btn { flex: 1 1 auto; min-width: 120px; }
}

.app-header__menu-btn {
  display: none;
  background: transparent;
  border: none;
  font-size: 22px;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: var(--space-2);
  border-radius: var(--radius-md);
}
.app-header__menu-btn:hover { background: var(--color-divider); color: var(--brand); }

/* ============================================================
   Tabbed workspace shell
   ============================================================ */
.tab-bar {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: var(--space-2) var(--space-3) 0;
  background: var(--color-surface-alt);
  border-bottom: 1px solid var(--color-border);
  overflow-x: auto;
  scrollbar-width: thin;
  min-height: 40px;
}
.tab-bar__item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-bottom: none;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  transition: background var(--transition-fast), color var(--transition-fast);
}
.tab-bar__item:hover { background: var(--brand-soft); color: var(--color-text); }
.tab-bar__item.is-active {
  background: var(--color-bg);
  color: var(--color-text);
  font-weight: var(--weight-semibold);
  border-bottom: 2px solid var(--brand);
  margin-bottom: -1px;
}
.tab-bar__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px; height: 16px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--color-text-faint);
  border-radius: var(--radius-sm);
  font-size: 14px;
  line-height: 1;
}
.tab-bar__close:hover { background: var(--color-danger-soft); color: var(--color-danger); }
.tab-bar__item.is-pinned .tab-bar__close { display: none; }
.tab-bar__close-all {
  margin-left: auto;
  padding: var(--space-1) var(--space-3);
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  white-space: nowrap;
}
.tab-bar__close-all:hover {
  background: var(--color-danger-soft);
  color: var(--color-danger);
  border-color: var(--color-danger-soft);
}

.tab-panels {
  position: relative;
  background: var(--color-bg);
  min-height: calc(100vh - var(--header-height) - 40px);
}
.tab-panel {
  display: none;
  width: 100%;
  height: calc(100vh - var(--header-height) - 40px);
  border: 0;
  background: var(--color-bg);
}
.tab-panel.is-active { display: block; }

.tab-menu {
  position: fixed;
  z-index: var(--z-overlay);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--space-1) 0;
  min-width: 140px;
}
.tab-menu__item {
  display: block;
  width: 100%;
  padding: var(--space-2) var(--space-4);
  background: none;
  border: none;
  text-align: left;
  font-size: var(--text-sm);
  color: var(--color-text);
  cursor: pointer;
}
.tab-menu__item:hover:not(:disabled) { background: var(--brand-soft); }
.tab-menu__item:disabled { color: var(--color-text-faint); cursor: not-allowed; }

/* When tabs.js runs at the top frame, it adds .has-tabs to <body>. That
   swaps the server-rendered page body out for the iframe panels. When
   tabs.js is absent or errored, .tab-fallback remains visible so the page
   still functions. */
html:not(.embedded) body.has-tabs .tab-fallback { display: none; }
html:not(.embedded) body:not(.has-tabs) .tab-bar,
html:not(.embedded) body:not(.has-tabs) .tab-panels { display: none; }

/* Embedded mode — we're inside a tab iframe. Hide shell chrome so the
   iframe shows only the feature content. */
html.embedded .app-sidebar,
html.embedded .app-header,
html.embedded .tab-bar,
html.embedded .tab-panels,
html.embedded .skip-link { display: none !important; }
html.embedded .app-shell {
  display: block !important;
  grid-template: none !important;
  min-height: 100vh;
}
html.embedded .app-main { padding: var(--space-4); }
