/* fp-widgets — grid + card frame for the configurable dashboard engine.
   Each widget is a self-contained card. The grid arranges them in 4 columns
   on desktop, 1 column on mobile. Widgets opt into widths via size modifiers:
     --small  = 1 col on desktop (4-up rows)
     --medium = 2 cols (2-up rows)
     --wide   = full row */

.fp-widgets {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-3);
}

@media (min-width: 1024px) {
  .fp-widgets { grid-template-columns: repeat(4, 1fr); }
}

.fp-widget {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0; /* allow truncation inside grid cells */
}

/* Dark variant — for the focal/signature widget on the page */
.fp-widget--dark {
  background: #0a0a0c;
  border-color: #232328;
  color: #fafafa;
}
[data-theme="light"] .fp-widget--dark { background: #1c1c20; color: #fafafa; }

@media (min-width: 1024px) {
  .fp-widget--small  { grid-column: span 1; }
  .fp-widget--medium { grid-column: span 2; }
  .fp-widget--wide   { grid-column: 1 / -1; }
}

/* Widget head — title + optional menu/action */
.fp-widget__head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-2);
}

.fp-widget__title {
  font-size: var(--fs-md);
  font-weight: var(--fw-bold);
  line-height: 1.2;
}

.fp-widget--dark .fp-widget__title { color: #fafafa; }

.fp-widget__sub {
  font-size: var(--fs-xs);
  color: var(--text-tertiary);
  margin-top: 2px;
}

/* Widget body — flex-grow so widgets in the same row align */
.fp-widget__body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* Empty state when there's no data yet */
.fp-widget__empty {
  text-align: center;
  padding: var(--space-5) var(--space-3);
  color: var(--text-tertiary);
  font-size: var(--fs-sm);
}

/* === Widget edit affordances (Session 2) === */

/* "···" menu button in the widget head */
.fp-widget__menu-wrap { position: relative; display: inline-flex; flex-shrink: 0; }
.fp-widget__menu-btn {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-tertiary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 18px;
  font-weight: var(--fw-bold);
  line-height: 1;
  letter-spacing: 0;
  transition: var(--transition-color);
}
@media (hover: hover) and (pointer: fine) {
  .fp-widget__menu-btn:hover { background: var(--bg-subtle); color: var(--text-primary); }
}
.fp-widget--dark .fp-widget__menu-btn { color: rgba(255,255,255,0.45); }
@media (hover: hover) and (pointer: fine) {
  .fp-widget--dark .fp-widget__menu-btn:hover { background: rgba(255,255,255,0.06); color: #fafafa; }
}

/* Per-widget dropdown menu — positioned below-right of the trigger */
.fp-widget__menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-1);
  min-width: 180px;
  z-index: var(--z-overlay);
  animation: fp-menu-in 140ms cubic-bezier(0.4, 0, 0.2, 1);
  transform-origin: top right;
}
@media (prefers-reduced-motion: reduce) { .fp-widget__menu { animation: none; } }

/* Hide Resize on mobile/tablet-portrait. Resize cycles small/medium/wide
   which only affect the desktop 4-column grid; on smaller screens widgets
   are always full-width so the action would silently do nothing. */
@media (max-width: 1023px) {
  .fp-widget__menu [data-action="resize"] { display: none; }
}

/* "+ Add widget" tile — looks like a placeholder slot at the end of the grid */
.fp-widget--add {
  background: transparent;
  border: 1px dashed var(--border-default);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: var(--space-2);
  min-height: 140px;
  color: var(--text-tertiary);
  transition: var(--transition-color);
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
}
@media (hover: hover) and (pointer: fine) {
  .fp-widget--add:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--accent-primary-soft);
  }
}
.fp-widget--add svg { width: 24px; height: 24px; }

/* Widget picker drawer — bottom sheet on mobile, centered modal on desktop */
.fp-picker {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  pointer-events: none;
}
.fp-picker.is-open { pointer-events: auto; }

.fp-picker__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  transition: opacity 200ms cubic-bezier(0.4, 0, 0.2, 1);
}
.fp-picker.is-open .fp-picker__backdrop { opacity: 1; }

.fp-picker__panel {
  position: relative;
  background: var(--bg-surface);
  border-top-left-radius: var(--radius-xl);
  border-top-right-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  padding: var(--space-3) var(--space-5) calc(var(--space-5) + env(safe-area-inset-bottom));
  max-height: 80dvh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 240ms cubic-bezier(0.4, 0, 0.2, 1);
}
.fp-picker.is-open .fp-picker__panel { transform: translateY(0); }

@media (min-width: 768px) {
  .fp-picker { justify-content: center; align-items: center; }
  .fp-picker__panel {
    max-width: 640px;
    width: calc(100% - var(--space-8));
    border-radius: var(--radius-xl);
    transform: translateY(20px) scale(0.97);
    opacity: 0;
    transition: transform 200ms cubic-bezier(0.4, 0, 0.2, 1), opacity 200ms;
  }
  .fp-picker.is-open .fp-picker__panel { transform: translateY(0) scale(1); opacity: 1; }
}

.fp-picker__handle {
  width: 40px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--border-default);
  margin: 0 auto var(--space-3);
}
@media (min-width: 768px) { .fp-picker__handle { display: none; } }

.fp-picker__head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}
.fp-picker__title { font-family: var(--font-display); font-size: var(--fs-xl); font-weight: var(--fw-bold); letter-spacing: var(--tracking-snug); }
.fp-picker__sub { font-size: var(--fs-sm); color: var(--text-tertiary); margin-top: 2px; }
.fp-picker__close {
  width: 36px; height: 36px; font-size: var(--fs-xl);
  background: var(--bg-base); border: 1px solid var(--border-default);
  border-radius: var(--radius-md); color: var(--text-secondary); cursor: pointer;
  font-family: inherit; flex-shrink: 0;
}

.fp-picker__list { display: grid; grid-template-columns: 1fr; gap: var(--space-2); }
@media (min-width: 768px) { .fp-picker__list { grid-template-columns: 1fr 1fr; } }

.fp-picker__option {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3);
  background: var(--bg-base);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: inherit;
  transition: var(--transition-color), transform 100ms;
}
@media (hover: hover) and (pointer: fine) {
  .fp-picker__option:hover { border-color: var(--accent-primary); background: var(--accent-primary-soft); }
}
.fp-picker__option:active { transform: scale(0.98); }
.fp-picker__option-icon {
  width: 36px; height: 36px; border-radius: var(--radius-md);
  background: var(--accent-primary-soft); color: var(--accent-primary);
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.fp-picker__option-icon svg { width: 18px; height: 18px; }
.fp-picker__option-name { font-size: var(--fs-sm); font-weight: var(--fw-bold); }
.fp-picker__option-desc { font-size: var(--fs-2xs); color: var(--text-tertiary); margin-top: 2px; }

.fp-picker__foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: var(--space-4);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border-subtle);
}
.fp-picker__reset {
  background: transparent;
  border: none;
  color: var(--accent-negative);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  cursor: pointer;
  font-family: inherit;
  padding: var(--space-1);
}

/* Configure form fields — used inside the configure drawer */
.fp-configure-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.fp-configure-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.fp-configure-field__label {
  font-size: var(--fs-2xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  font-weight: var(--fw-bold);
}
.fp-configure-field__input {
  padding: var(--space-2) var(--space-3);
  background: var(--bg-base);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-primary);
  font-family: inherit;
  cursor: pointer;
  /* Native select arrow shows on every platform — no need to fight it */
  appearance: auto;
  -webkit-appearance: auto;
}
.fp-configure-field__input:focus {
  border-color: var(--accent-primary);
  outline: none;
}

/* Skeleton placeholders — shown while data is loading. Pulse animation
   gives the user a "something is coming" cue rather than a frozen page. */
.fp-skeleton-bar {
  height: 14px;
  background: var(--bg-subtle);
  border-radius: var(--radius-sm);
  animation: fp-skeleton-pulse 1.5s ease-in-out infinite;
}
.fp-skeleton-bar--title { width: 55%; height: 16px; }
.fp-skeleton-bar--sub   { width: 35%; height: 10px; }
.fp-skeleton-bar--big   { height: 80px; margin-top: var(--space-2); }

@keyframes fp-skeleton-pulse {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 0.25; }
}

@media (prefers-reduced-motion: reduce) {
  .fp-skeleton-bar { animation: none; opacity: 0.4; }
}

/* === Widget-specific styles === */

/* KPI tile widget */
.fp-w-kpi {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-2) 0;
}
.fp-w-kpi__label {
  font-size: var(--fs-2xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  font-weight: var(--fw-bold);
}
.fp-w-kpi__value {
  font-family: var(--font-display);
  font-size: 2.5rem;
  font-weight: var(--fw-bold);
  letter-spacing: -0.03em;
  line-height: 1;
}
.fp-w-kpi__delta {
  font-size: var(--fs-xs);
  font-weight: var(--fw-bold);
}
.fp-w-kpi__delta--pos { color: var(--accent-positive); }
.fp-w-kpi__delta--neg { color: var(--accent-negative); }
.fp-w-kpi__delta--neutral { color: var(--text-tertiary); }

/* Recent activity feed */
.fp-w-feed-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border-subtle);
  text-decoration: none;
  color: inherit;
}
.fp-w-feed-item:last-child { border-bottom: none; }
.fp-w-feed-date {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.fp-w-feed-date strong {
  font-size: var(--fs-md);
  font-weight: var(--fw-bold);
  line-height: 1;
}
.fp-w-feed-date span {
  font-size: 9px;
  text-transform: uppercase;
  color: var(--text-tertiary);
  font-weight: var(--fw-bold);
  letter-spacing: 0.04em;
}
.fp-w-feed-name {
  font-size: var(--fs-sm);
  font-weight: var(--fw-bold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.fp-w-feed-sub {
  font-size: var(--fs-2xs);
  color: var(--text-tertiary);
  margin-top: 2px;
}
.fp-w-feed-units {
  font-family: var(--font-display);
  font-size: var(--fs-md);
  font-weight: var(--fw-bold);
  font-variant-numeric: tabular-nums;
}

/* Monthly bar chart */
.fp-w-bars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 140px;
  padding: var(--space-2) 0 24px;
  position: relative;
}
.fp-w-bar-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: flex-end;
  position: relative;
  min-width: 0;
}
.fp-w-bar {
  width: 100%;
  background: var(--accent-primary);
  border-radius: 3px 3px 0 0;
  min-height: 2px;
  transition: opacity var(--transition-fast);
}
.fp-w-bar-group:hover .fp-w-bar { opacity: 0.85; }
.fp-w-bar-label {
  font-size: 10px;
  color: var(--text-tertiary);
  font-weight: var(--fw-semibold);
  margin-top: 4px;
  position: absolute;
  bottom: -20px;
  white-space: nowrap;
}

/* Donut + legend */
.fp-w-donut-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) 0;
}
.fp-w-donut {
  width: 110px;
  height: 110px;
  flex-shrink: 0;
}
.fp-w-donut-legend {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 0;
}
.fp-w-leg {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 6px;
  align-items: center;
  font-size: 11px;
}
.fp-w-leg-dot { width: 8px; height: 8px; border-radius: 2px; }
.fp-w-leg-name {
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.fp-w-leg-pct {
  color: var(--text-primary);
  font-weight: var(--fw-bold);
  font-variant-numeric: tabular-nums;
}

/* Compact data table (workload + truck history) */
.fp-w-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-sm);
}
.fp-w-table th {
  text-align: left;
  padding: var(--space-2) var(--space-3);
  font-size: var(--fs-2xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  font-weight: var(--fw-bold);
  border-bottom: 1px solid var(--border-default);
  white-space: nowrap;
}
.fp-w-table th.right { text-align: right; }
.fp-w-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
}
.fp-w-table td.right {
  text-align: right;
  font-family: var(--font-display);
  font-weight: var(--fw-semibold);
}
.fp-w-table tbody tr:last-child td { border-bottom: none; }
.fp-w-table tbody tr {
  cursor: pointer;
  transition: background var(--transition-fast);
}
@media (hover: hover) and (pointer: fine) {
  .fp-w-table tbody tr:hover { background: var(--bg-subtle); }
}
.fp-w-table .fp-w-tn { font-weight: var(--fw-bold); color: var(--text-primary); }
.fp-w-table .fp-w-muted { color: var(--text-tertiary); }
.fp-w-table .fp-w-pos { color: var(--accent-positive); }
.fp-w-table .fp-w-neg { color: var(--accent-negative); }

.fp-w-table-wrap {
  overflow-x: auto;
  margin: 0 calc(var(--space-4) * -1);
  padding: 0 var(--space-4);
}
.fp-w-table-wrap .fp-w-table { min-width: 500px; }

/* Type pill in tables (RP/SP/FLX) */
.fp-w-type {
  font-size: 10px;
  font-weight: var(--fw-bold);
  padding: 2px 6px;
  border-radius: var(--radius-pill);
  display: inline-block;
}
.fp-w-type--rp  { background: var(--accent-primary-soft); color: var(--accent-primary); }
.fp-w-type--sp  { background: var(--accent-warning-soft); color: var(--accent-warning); }
.fp-w-type--flx { background: var(--accent-info-soft);    color: var(--accent-info); }
.fp-w-type--unknown { background: var(--bg-subtle); color: var(--text-tertiary); }
