/* ==========================================================================
   Rachel's Place — Fortnightly KPI Tracker (staff tool)
   Uses site tokens from base.css; light-theme optimised for data entry.
   ========================================================================== */

/* Hide empty extra-css link if no file (defensive) */
link#page-extra-css[href$="/"] {
  display: none;
}

body[data-page="kpi-tracker"] {
  background: var(--color-bg);
}

body[data-page="kpi-tracker"] .site-footer {
  margin-top: var(--space-16);
}

/* Snapshot strip */
.kpi-snapshot {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
  margin-top: var(--space-8);
  margin-bottom: var(--space-8);
  position: relative;
  z-index: 1;
}

@media (min-width: 640px) {
  .kpi-snapshot {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 1024px) {
  .kpi-snapshot {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1440px) {
  .kpi-snapshot {
    grid-template-columns: repeat(6, 1fr);
  }
}

.kpi-stat {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}

.kpi-stat .label {
  display: block;
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.kpi-stat .value {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--color-primary-ink);
  line-height: 1.15;
}

.kpi-stat .meta {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}

.kpi-stat .value-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

/* Toolbar */
.kpi-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.kpi-tabs {
  display: inline-flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  padding: var(--space-1);
  background: var(--color-surface-2);
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-border);
}

.kpi-tab {
  appearance: none;
  border: 0;
  background: transparent;
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-pill);
  cursor: pointer;
  min-height: 2.5rem;
}

.kpi-tab:hover {
  color: var(--color-text);
}

.kpi-tab[aria-selected="true"] {
  background: var(--color-surface);
  color: var(--color-primary-ink);
  box-shadow: var(--shadow-sm);
}

.kpi-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.kpi-actions .kpi-row-lock,
.kpi-lock-bar .kpi-row-lock {
  width: 1.75rem;
  height: 1.75rem;
  margin-right: -0.2rem;
  flex: none;
}

.kpi-lock-bar .kpi-row-lock {
  margin-right: 0;
}

.kpi-actions .btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.kpi-actions .btn {
  min-height: 2.5rem;
  padding-inline: var(--space-4);
}

.kpi-panel[hidden] {
  display: none !important;
}

/* Legend */
.kpi-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.kpi-legend span {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.kpi-dot {
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.kpi-dot-ok { background: #2f6b4f; }
.kpi-dot-watch { background: #c4922a; }
.kpi-dot-action { background: #b4552e; }
.kpi-dot-input { background: #fff1b8; border: 1px solid #e0d5c2; }

/* Status pills */
.kpi-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 4.5rem;
  padding: 0.2rem 0.55rem;
  border-radius: var(--radius-pill);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  line-height: 1.2;
}

.kpi-status-ok {
  background: #dfe8de;
  color: #1e4634;
}

.kpi-status-watch {
  background: #f6e9c8;
  color: #7a5a12;
}

.kpi-status-action {
  background: #f6e3d7;
  color: #8d3f1d;
}

.kpi-status-empty {
  background: var(--color-surface-2);
  color: var(--color-text-faint);
}

/* Table shell */
.kpi-table-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.kpi-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Mirrored scrollbar above the table (kept in sync with the main wrap by JS) */
.kpi-table-topscroll {
  overflow-x: auto;
  overflow-y: hidden;
  height: 16px;
  border-bottom: 1px solid var(--line, rgba(0, 0, 0, 0.08));
}

.kpi-table-topscroll-inner {
  height: 1px;
}

/* Keep both horizontal scrollbars visible (overlay scrollbars hide otherwise) */
.kpi-table-topscroll,
.kpi-table-wrap {
  scrollbar-width: thin;
  scrollbar-color: rgba(30, 70, 52, 0.45) rgba(30, 70, 52, 0.08);
}

.kpi-table-topscroll::-webkit-scrollbar,
.kpi-table-wrap::-webkit-scrollbar {
  height: 10px;
}

.kpi-table-topscroll::-webkit-scrollbar-track,
.kpi-table-wrap::-webkit-scrollbar-track {
  background: rgba(30, 70, 52, 0.08);
}

.kpi-table-topscroll::-webkit-scrollbar-thumb,
.kpi-table-wrap::-webkit-scrollbar-thumb {
  background: rgba(30, 70, 52, 0.45);
  border-radius: 999px;
}

.kpi-table-topscroll::-webkit-scrollbar-thumb:hover,
.kpi-table-wrap::-webkit-scrollbar-thumb:hover {
  background: rgba(30, 70, 52, 0.65);
}

[data-theme='dark'] .kpi-table-topscroll,
[data-theme='dark'] .kpi-table-wrap {
  scrollbar-color: rgba(247, 243, 236, 0.4) rgba(247, 243, 236, 0.08);
}

[data-theme='dark'] .kpi-table-topscroll::-webkit-scrollbar-track,
[data-theme='dark'] .kpi-table-wrap::-webkit-scrollbar-track {
  background: rgba(247, 243, 236, 0.08);
}

[data-theme='dark'] .kpi-table-topscroll::-webkit-scrollbar-thumb,
[data-theme='dark'] .kpi-table-wrap::-webkit-scrollbar-thumb {
  background: rgba(247, 243, 236, 0.4);
}

.kpi-table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.8125rem;
}

.kpi-table th,
.kpi-table td {
  padding: 0.45rem 0.4rem;
  border-bottom: 1px solid var(--color-divider);
  border-right: 1px solid var(--color-divider);
  vertical-align: middle;
  text-align: center;
  white-space: nowrap;
}

.kpi-table th:last-child,
.kpi-table td:last-child {
  border-right: 0;
}

.kpi-table thead th {
  background: var(--color-primary);
  color: var(--color-text-inverse);
  font-weight: 600;
  font-size: 0.7rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  position: sticky;
  top: 0;
  z-index: 2;
}

.kpi-table thead tr.kpi-group th {
  background: #163527;
  font-size: 0.68rem;
  letter-spacing: 0.06em;
  padding-block: 0.55rem;
}

.kpi-table tbody th {
  background: var(--color-surface-2);
  color: var(--color-text);
  font-weight: 600;
  text-align: left;
  position: sticky;
  left: 0;
  z-index: 1;
  min-width: 8.9rem;
  padding-inline: 0.65rem;
  white-space: nowrap;
}

/* Per-row padlock — rows are read-only until unlocked */
.kpi-row-lock {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.45rem;
  height: 1.45rem;
  margin-right: 0.45rem;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 0.4rem;
  background: transparent;
  color: var(--color-text-muted);
  cursor: pointer;
  vertical-align: middle;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.kpi-row-lock:hover {
  color: var(--color-primary);
  border-color: var(--color-primary);
}

.kpi-row-lock.is-unlocked {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}

.kpi-table tbody th .kpi-num {
  vertical-align: middle;
}

.kpi-table input[type="number"]:disabled,
.kpi-table input[type="text"]:disabled {
  background: transparent;
  border-color: var(--color-divider);
  color: var(--color-text-muted);
  cursor: not-allowed;
}

/* Keep the "Period start" header pinned with its column during sideways scroll */
.kpi-table thead tr.kpi-group th:first-child {
  left: 0;
  z-index: 4;
}

.kpi-table tbody tr:nth-child(even) td {
  background: rgba(241, 233, 220, 0.35);
  background: rgba(127, 127, 127, 0.12);
  background: color-mix(in srgb, var(--color-surface-2) 35%, transparent);
}

.kpi-table tbody tr:hover td,
.kpi-table tbody tr:hover th {
  background: var(--color-primary-soft);
}

.kpi-table .kpi-num {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

.kpi-table .kpi-muted {
  color: var(--color-text-faint);
}

.kpi-table input[type="number"],
.kpi-table input[type="text"] {
  width: 3.4rem;
  min-height: 2rem;
  padding: 0.25rem 0.3rem;
  border-radius: var(--radius-sm);
  border: 1.5px solid #e8d9a8;
  background: #fff8e7;
  text-align: center;
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  box-shadow: none;
}

.kpi-table input.kpi-money {
  width: 5.5rem;
}

.kpi-table input:focus {
  border-color: var(--color-primary);
  background: #fffdf8;
  box-shadow: 0 0 0 2px rgba(30, 70, 52, 0.18);
  box-shadow: 0 0 0 2px rgba(30, 70, 52, 0.18);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--color-primary) 18%, transparent);
}

.kpi-table input::-webkit-outer-spin-button,
.kpi-table input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.kpi-table input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

/* Targets grid */
/* Lock bar — targets are read-only until explicitly unlocked */
.kpi-lock-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3) var(--space-5);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-left: 4px solid var(--color-accent);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-5);
  margin-bottom: var(--space-5);
}

.kpi-lock-bar[data-locked="0"] {
  border-left-color: var(--color-primary);
}

.kpi-lock-note {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  max-width: 56ch;
}

.kpi-lock-note strong {
  color: var(--color-text);
}

.kpi-lock-bar .btn {
  flex-shrink: 0;
}

.kpi-field-row input:disabled {
  background: var(--color-surface);
  border-color: var(--color-border);
  color: var(--color-text-muted);
  cursor: not-allowed;
}

.kpi-targets-grid {
  display: grid;
  gap: var(--space-6);
}

@media (min-width: 900px) {
  .kpi-targets-grid {
    grid-template-columns: 1fr 1fr;
  }
}

.kpi-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: clamp(var(--space-5), 3vw, var(--space-8));
  box-shadow: var(--shadow-sm);
}

.kpi-card h2 {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  margin: 0 0 var(--space-2);
  letter-spacing: -0.02em;
}

.kpi-card .lede-sm {
  margin: 0 0 var(--space-5);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.kpi-field-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--color-divider);
}

.kpi-field-row:last-child {
  border-bottom: 0;
}

.kpi-field-row label {
  font-size: var(--text-sm);
  font-weight: 600;
  margin: 0;
}

.kpi-field-row .hint {
  display: block;
  font-size: var(--text-xs);
  font-weight: 400;
  color: var(--color-text-muted);
  margin-top: 0.15rem;
}

.kpi-field-row input {
  width: 6.5rem;
  text-align: right;
  background: #fff8e7;
  border-color: #e8d9a8;
  min-height: 2.5rem;
  font-variant-numeric: tabular-nums;
}

.kpi-calc-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--color-primary-soft);
  color: var(--color-primary-ink);
  border-radius: var(--radius-pill);
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
  font-weight: 600;
  margin-top: var(--space-4);
}

/* Guide */
.kpi-guide {
  display: grid;
  gap: var(--space-5);
}

@media (min-width: 800px) {
  .kpi-guide {
    grid-template-columns: 1.2fr 0.8fr;
  }
}

.kpi-guide ol,
.kpi-guide ul {
  margin: 0;
  padding-left: 1.2rem;
  color: var(--color-text-muted);
}

.kpi-guide li {
  margin-bottom: var(--space-2);
}

.kpi-guide li strong {
  color: var(--color-text);
}

.kpi-rule {
  border-left: 3px solid var(--color-accent);
  padding-left: var(--space-4);
  margin: var(--space-4) 0 0;
}

.kpi-rule p {
  margin: 0 0 var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* Charts */
.kpi-charts {
  display: grid;
  gap: var(--space-5);
  margin-top: var(--space-8);
  margin-bottom: var(--space-6);
}

@media (min-width: 900px) {
  .kpi-charts {
    grid-template-columns: 1fr 1fr;
  }
}

.kpi-chart-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}

.kpi-chart-card h3 {
  margin: 0 0 var(--space-4);
  font-size: var(--text-base);
}

.kpi-chart-card canvas {
  width: 100% !important;
  max-height: 220px;
}

.kpi-empty-chart[hidden] {
  display: none;
}

.kpi-empty-chart {
  min-height: 160px;
  display: grid;
  place-items: center;
  color: var(--color-text-faint);
  font-size: var(--text-sm);
  background: var(--color-surface-2);
  border-radius: var(--radius-md);
}

/* Mobile card fallback note */
.kpi-mobile-hint {
  display: none;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin: 0 0 var(--space-3);
}

@media (max-width: 720px) {
  .kpi-mobile-hint {
    display: block;
  }
}

/* ---------- Mobile (stacked cards) ----------
   Below 860px the fortnight log becomes one card per row, matching the pattern
   used for the accounts table on Manage users: header hidden, each cell labelled
   via data-label, comfortable tap targets, no sideways scrolling required. */
@media (max-width: 860px) {
  body[data-page="kpi-tracker"] .kpi-mobile-hint {
    display: none !important;
  }

  .kpi-toolbar,
  .kpi-actions,
  .kpi-tabs {
    flex-wrap: wrap;
  }

  .kpi-table-wrap {
    overflow: visible;
    padding: var(--space-3);
  }

  .kpi-table-topscroll {
    display: none;
  }

  .kpi-table:not(.chd-table) {
    width: 100%;
    min-width: 0;
  }

  .kpi-table:not(.chd-table) thead {
    display: none;
  }

  .kpi-table:not(.chd-table),
  .kpi-table:not(.chd-table) tbody,
  .kpi-table:not(.chd-table) tr,
  .kpi-table:not(.chd-table) th,
  .kpi-table:not(.chd-table) td {
    display: block;
    width: 100%;
  }

  .kpi-table:not(.chd-table) tr {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-3);
  }

  /* Row header (period start + padlock) becomes the card title */
  .kpi-table:not(.chd-table) tbody th {
    position: static;
    background: none;
    padding: 0 0 var(--space-2);
    margin-bottom: var(--space-2);
    border-bottom: 1px solid var(--color-divider);
    font-size: var(--text-base);
    min-width: 0;
    white-space: normal;
    display: flex;
    align-items: center;
  }

  .kpi-table:not(.chd-table) .kpi-row-lock {
    width: 2.5rem;
    height: 2.5rem;
  }

  .kpi-table:not(.chd-table) td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    border: 0;
    border-top: 1px solid var(--color-divider);
    padding: var(--space-2) 0;
    text-align: right;
    white-space: normal;
  }

  .kpi-table:not(.chd-table) td:first-of-type {
    border-top: 0;
  }

  /* Label on the left, value/input on the right */
  .kpi-table:not(.chd-table) td::before {
    content: attr(data-label);
    flex: 1 1 auto;
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: none;
    letter-spacing: normal;
  }

  .kpi-table:not(.chd-table) td > * {
    flex: none;
  }

  .kpi-table:not(.chd-table) input[type="number"],
  .kpi-table:not(.chd-table) input[type="text"] {
    width: 7rem;
    min-height: 2.5rem;
    text-align: right;
  }

  .kpi-table:not(.chd-table) input.kpi-money {
    width: 7rem;
  }
}

/* Print */
@media print {
  .site-header,
  .site-footer,
  .kpi-staff-banner,
  .kpi-toolbar,
  .kpi-actions,
  .theme-toggle,
  .burger {
    display: none !important;
  }

  body[data-page="kpi-tracker"] {
    background: #fff;
  }

  .kpi-table-card,
  .kpi-card {
    box-shadow: none;
    break-inside: avoid;
  }
}

/* Full-width tracker container — spans the whole page, matching the workbook layout */
.kpi-wrap-full {
  width: 100%;
  max-width: none;
  margin-inline: auto;
  padding-inline: clamp(var(--space-3), 1vw, var(--space-5));
}

/* Variance & notes columns */
.kpi-table .kpi-var-good {
  color: #1e6b43;
}

.kpi-table .kpi-var-bad {
  color: #a63d1c;
}

.kpi-table td.kpi-period-start {
  color: var(--color-text-muted);
  font-weight: 500;
}

/* ==========================================================================
   Dark theme — overrides for the hard-coded light data-entry colours
   ========================================================================== */

[data-theme='dark'] .kpi-dot-input {
  background: #4a3f1e;
  border-color: #6b5c2e;
}

[data-theme='dark'] .kpi-status-ok {
  background: #23301f;
  color: #a6d2b6;
}

[data-theme='dark'] .kpi-status-watch {
  background: #383018;
  color: #e0b866;
}

[data-theme='dark'] .kpi-status-action {
  background: #34231a;
  color: #eeaa86;
}

[data-theme='dark'] .kpi-table thead tr.kpi-group th {
  background: #79b18f;
}

[data-theme='dark'] .kpi-row-lock.is-unlocked {
  color: var(--color-text-inverse);
}

[data-theme='dark'] .kpi-table input[type='number'],
[data-theme='dark'] .kpi-table input[type='text'],
[data-theme='dark'] .kpi-field-row input {
  background: #2c2716;
  border-color: #5a4f28;
  color: var(--color-text);
}

[data-theme='dark'] .kpi-table input:focus {
  background: #332d1a;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(140, 192, 160, 0.25);
}

[data-theme='dark'] .kpi-table input[type='number']:disabled,
[data-theme='dark'] .kpi-table input[type='text']:disabled,
[data-theme='dark'] .kpi-field-row input:disabled {
  background: transparent;
  border-color: var(--color-divider);
  color: var(--color-text-muted);
}

[data-theme='dark'] .kpi-table .kpi-var-good {
  color: #7fc9a0;
}

[data-theme='dark'] .kpi-table .kpi-var-bad {
  color: #e8977a;
}

