/**
 * app.css — layout and component styles.
 *
 * Written entirely in terms of the design tokens in tokens.css. The overall
 * page frame uses CSS GRID (header / body / footer, and sidebar / main within
 * the body); the task columns and cards use FLEXBOX. It is built mobile-first
 * and verified at 375px (sidebar collapses to the top, no horizontal page
 * scroll) and 1280px (two-pane layout with a scrollable board).
 */

/* ---- Reset & box model ---------------------------------------------------
   A deliberate box-sizing reset so padding and borders never expand an
   element past the width we gave it — the whole layout reasons in border-box. */
*,
*::before,
*::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

html,
body {
  height: 100%;
}

body {
  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;
}

/* Visually hidden but available to screen readers (labels for icon controls). */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---- App frame (CSS Grid) ------------------------------------------------ */
.app {
  display: grid;
  grid-template-rows: var(--header-height) 1fr auto;
  min-height: 100vh;
}

.app__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: 0 var(--space-5);
  background: var(--color-surface);
  border-bottom: var(--border-width) solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: var(--weight-bold);
  font-size: var(--text-lg);
}
.brand__mark {
  font-size: var(--text-xl);
}
.header-hint {
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
}

/* Body: sidebar + main. Grid on wide screens, stacked on narrow. */
.app__body {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  width: 100%;
  max-width: var(--content-max-width);
  margin: 0 auto;
  min-height: 0; /* let children scroll instead of overflowing the grid */
}

.app__footer {
  padding: var(--space-4) var(--space-5);
  text-align: center;
  color: var(--color-text-subtle);
  font-size: var(--text-sm);
  border-top: var(--border-width) solid var(--color-border);
  background: var(--color-surface);
}

/* ---- Sidebar ------------------------------------------------------------- */
.sidebar {
  padding: var(--space-4);
  border-right: var(--border-width) solid var(--color-border);
  background: var(--color-surface-2);
  /* min-width:0 lets this grid item shrink below its content's intrinsic width,
     so the horizontal project strip (below) scrolls INSIDE the sidebar instead
     of stretching the whole page — the fix for horizontal scroll at 375px. */
  min-width: 0;
}
.sidebar__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}
.sidebar__title {
  font-size: var(--text-sm);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
}
.sidebar__count {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  background: var(--color-border);
  border-radius: 999px;
  padding: 0 var(--space-2);
}
.project-list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.project-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  width: 100%;
  text-align: left;
  padding: var(--space-3);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
  cursor: pointer;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.project-card:hover {
  border-color: var(--color-border-strong);
}
.project-card--active {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 1px var(--color-primary);
}
.project-card__name {
  font-weight: var(--weight-semibold);
}
.project-card__client {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}
.project-card__meta {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  margin-top: var(--space-1);
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  display: inline-block;
  background: var(--color-text-subtle);
}
.status-dot--active {
  background: var(--color-status-active);
}
.status-dot--on_hold {
  background: var(--color-status-on_hold);
}
.status-dot--completed {
  background: var(--color-status-completed);
}

/* ---- Main ---------------------------------------------------------------- */
.app__main {
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  min-width: 0; /* critical so the flex board can shrink and scroll internally */
}
.project-head__name {
  font-size: var(--text-2xl);
  line-height: var(--leading-tight);
}
.project-head__client {
  color: var(--color-text-muted);
}

/* ---- Dashboard: derived figures + budget bar ----------------------------- */
.dashboard {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
  gap: var(--space-3);
}
.stat-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-4);
  background: var(--color-surface);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
}
.stat-card--warning {
  border-color: var(--color-warning);
  background: var(--color-warning-soft);
}
.stat-card--over {
  border-color: var(--color-danger);
  background: var(--color-danger-soft);
}
.stat-card__label {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
}
.stat-card__value {
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
}
.stat-card__hint {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
}

.budget {
  padding: var(--space-4);
  background: var(--color-surface);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
}
.budget__header {
  display: flex;
  justify-content: space-between;
  margin-bottom: var(--space-2);
}
.budget__title {
  font-weight: var(--weight-medium);
}
.budget__percent {
  font-weight: var(--weight-bold);
}
.budget__track {
  height: 12px;
  background: var(--color-border);
  border-radius: 999px;
  overflow: hidden;
}
.budget__fill {
  height: 100%;
  background: var(--color-ok);
  transition: width var(--transition-base);
}
.budget__track--warning .budget__fill {
  background: var(--color-warning);
}
.budget__track--over .budget__fill {
  background: var(--color-danger);
}
.budget__caption {
  margin-top: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* ---- Toolbar ------------------------------------------------------------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: var(--space-3);
}
.toolbar__search {
  flex: 1 1 14rem;
}
.toolbar__filters {
  display: flex;
  gap: var(--space-3);
}
.toolbar__actions {
  display: flex;
  gap: var(--space-2);
  margin-left: auto;
}
.filter {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* ---- Board (Flexbox columns + cards) ------------------------------------- */
.board {
  display: flex;
  gap: var(--space-4);
  align-items: flex-start;
  overflow-x: auto; /* board scrolls internally; the PAGE never scrolls sideways */
  padding-bottom: var(--space-2);
}
.column {
  flex: 1 0 15rem;
  max-width: 22rem;
  display: flex;
  flex-direction: column;
  background: var(--color-surface-2);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
}
.column__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3);
  border-bottom: var(--border-width) solid var(--color-border);
}
.column__title {
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}
.column__count {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  background: var(--color-border);
  border-radius: 999px;
  padding: 0 var(--space-2);
}
.column__cards {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-3);
  min-height: 4rem;
}

.task-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--color-surface);
  border: var(--border-width) solid var(--color-border);
  border-left: 3px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}
.task-card:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}
.task-card--overdue {
  border-left-color: var(--color-danger);
}
.task-card__top {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: var(--space-2);
}
.task-card__title {
  font-size: var(--text-base);
  font-weight: var(--weight-semibold);
  overflow-wrap: anywhere; /* very long / unbroken titles must not overflow */
}
.task-card__meta {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  font-size: var(--text-sm);
}
.task-card__meta-row {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
}
.task-card__meta dt {
  color: var(--color-text-subtle);
}
.task-card__meta dd {
  color: var(--color-text);
}
.task-card__actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex-wrap: wrap;
  margin-top: var(--space-1);
}
.task-card__move-select {
  font-size: var(--text-xs);
  padding: var(--space-1) var(--space-2);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
}

/* ---- Side panels: crew, materials, activity ------------------------------ */
.side-panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: var(--space-4);
}
.panel {
  padding: var(--space-4);
  background: var(--color-surface);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
}
.panel__title {
  font-size: var(--text-sm);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-3);
}

.crew-list,
.ledger,
.activity__list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.crew-row,
.ledger__row,
.activity__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-2);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
}
.crew-row__info,
.ledger__main {
  display: flex;
  flex-direction: column;
}
.crew-row__name,
.ledger__name {
  font-weight: var(--weight-medium);
}
.crew-row__role,
.ledger__qty,
.crew-row__load {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
}
.ledger__cost {
  font-weight: var(--weight-semibold);
}
.activity__item {
  flex-direction: row;
}
.activity__text {
  font-size: var(--text-sm);
}
.activity__date {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  white-space: nowrap;
}
.activity__error {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  color: var(--color-danger);
  font-size: var(--text-sm);
}

.log-form {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}
.log-form__error {
  flex-basis: 100%;
  color: var(--color-danger);
  font-size: var(--text-xs);
}

/* ---- Buttons ------------------------------------------------------------- */
.btn {
  font: inherit;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: var(--border-width) solid transparent;
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn--primary {
  background: var(--color-primary);
  color: #fff;
}
.btn--primary:hover:not(:disabled) {
  background: var(--color-primary-strong);
}
.btn--default {
  background: var(--color-surface);
  border-color: var(--color-border-strong);
}
.btn--ghost {
  background: transparent;
  color: var(--color-text-muted);
  border-color: var(--color-border);
}
.btn--ghost:hover:not(:disabled) {
  background: var(--color-surface-2);
}
.btn--ghost-danger {
  background: transparent;
  color: var(--color-danger);
  border-color: transparent;
}
.btn--ghost-danger:hover:not(:disabled) {
  background: var(--color-danger-soft);
}
.btn--danger {
  background: var(--color-danger);
  color: #fff;
}
.btn--icon {
  background: transparent;
  border: none;
  font-size: var(--text-lg);
  color: var(--color-text-muted);
  padding: var(--space-1) var(--space-2);
}
.btn--sm {
  padding: var(--space-1) var(--space-2);
  font-size: var(--text-xs);
}

/* ---- Badges -------------------------------------------------------------- */
.badge {
  display: inline-block;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: capitalize;
  padding: 2px var(--space-2);
  border-radius: 999px;
  color: #fff;
  background: var(--color-text-subtle);
  white-space: nowrap;
}
.badge--priority-high {
  background: var(--color-priority-high);
}
.badge--priority-medium {
  background: var(--color-priority-medium);
}
.badge--priority-low {
  background: var(--color-priority-low);
}

/* ---- Inputs & selects ---------------------------------------------------- */
.input,
.select {
  font: inherit;
  font-size: var(--text-sm);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: var(--border-width) solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
}
.input:focus-visible,
.select:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}
.input--sm {
  flex: 1 1 6rem;
  width: auto;
}

/* ---- Forms & field errors ------------------------------------------------ */
.form-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.form-field__label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
}
.form-field__req {
  color: var(--color-danger);
}
.form-field__error {
  font-size: var(--text-xs);
  color: var(--color-danger);
}
.form-field--invalid .input,
.form-field--invalid .select {
  border-color: var(--color-danger);
}

/* ---- Modal (task form overlay) ------------------------------------------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(35, 32, 27, 0.45);
}
.modal__panel {
  width: min(32rem, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4);
  border-bottom: var(--border-width) solid var(--color-border);
}
.task-form__fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-3);
  padding: var(--space-4);
}
.task-form__fields .form-field:first-child {
  grid-column: 1 / -1; /* title spans full width */
}
.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4);
  border-top: var(--border-width) solid var(--color-border);
}

/* ---- Empty / loading / error states -------------------------------------- */
.empty-state {
  padding: var(--space-5) var(--space-3);
  text-align: center;
  color: var(--color-text-subtle);
}
.empty-state__message {
  font-size: var(--text-sm);
}
.empty-state__hint {
  font-size: var(--text-xs);
  margin-top: var(--space-1);
}
.loading {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-5);
  color: var(--color-text-muted);
  justify-content: center;
}
.loading__spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--color-border-strong);
  border-top-color: var(--color-primary);
  border-radius: 999px;
  animation: spin 700ms linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
.error-state {
  padding: var(--space-5);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  color: var(--color-danger);
}

/* ---- Responsive: collapse to a single column on narrow screens ----------- */
@media (max-width: 640px) {
  .app__body {
    grid-template-columns: 1fr; /* sidebar stacks above the board */
  }
  .sidebar {
    border-right: none;
    border-bottom: var(--border-width) solid var(--color-border);
  }
  .project-list {
    flex-direction: row;
    overflow-x: auto; /* horizontal project strip, scrolls within itself */
  }
  .project-card {
    min-width: 12rem;
  }
  .board {
    flex-direction: column; /* columns stack; no sideways page scroll at 375px */
  }
  .column {
    max-width: none;
    flex-basis: auto;
  }
  .toolbar__actions {
    margin-left: 0;
  }
  .task-form__fields {
    grid-template-columns: 1fr; /* single-column form on small screens */
  }
  .header-hint {
    display: none; /* the tip would overflow the narrow header — hide it */
  }
}

/* ---- Top navigation (app shell) ------------------------------------------ */
.app__nav {
  display: flex;
  gap: var(--space-2);
}
.app__nav-link {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
  text-decoration: none;
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
}
.app__nav-link:hover {
  background: var(--color-surface-2);
}
.app__nav-link--active {
  color: var(--color-primary);
  background: var(--color-primary-soft);
}
.app__account-inner {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.app__user-email {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.brand {
  text-decoration: none;
  color: inherit;
}

/* ---- Section headings (title + plain-English subtitle) -------------------- */
.section-heading {
  margin-top: var(--space-2);
}
.section-heading__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
  line-height: var(--leading-tight);
}
.section-heading__subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  margin-top: 2px;
  max-width: 60ch;
}

/* On narrow screens the top nav wraps under the brand instead of overflowing. */
@media (max-width: 640px) {
  .app__header {
    flex-wrap: wrap;
    height: auto;
    padding: var(--space-2) var(--space-4);
    gap: var(--space-2);
  }
  .app__user-email {
    max-width: 8rem;
  }
}

/* Respect users who prefer reduced motion. */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
