/* components.css — UI component styles.
 * Loaded AFTER tokens.css and base.css.
 * Source of truth: PHASE_1_4_SPEC.md §6. */

/* ============================================================ */
/* Buttons                                                       */
/* ============================================================ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
  padding: 0 var(--space-5);
  border-radius: var(--radius-md);
  font-size: var(--font-size-body);
  font-weight: var(--font-weight-medium);
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--duration-fast) var(--easing),
    color var(--duration-fast) var(--easing),
    border-color var(--duration-fast) var(--easing),
    opacity var(--duration-fast) var(--easing);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--color-accent);
  color: var(--color-text-on-accent);
}
.btn-primary:hover:not(:disabled)  { background: var(--color-accent-hover); }
.btn-primary:active:not(:disabled) { background: var(--color-accent-active); }

.btn-secondary {
  background: var(--color-bg-surface);
  color: var(--color-accent);
  border: 1px solid var(--color-border);
}
.btn-secondary:hover:not(:disabled)  { background: var(--color-accent-bg); border-color: var(--color-accent); }
.btn-secondary:active:not(:disabled) { background: var(--color-accent-bg); }

.btn-tertiary {
  background: transparent;
  color: var(--color-accent);
}
.btn-tertiary:hover:not(:disabled)  { background: var(--color-accent-bg); }
.btn-tertiary:active:not(:disabled) { background: var(--color-accent-bg); }

.btn-danger {
  background: var(--color-danger);
  color: var(--color-text-on-accent);
}
.btn-danger:hover:not(:disabled)  { filter: brightness(0.92); }
.btn-danger:active:not(:disabled) { filter: brightness(0.85); }

.btn-block { width: 100%; }

.btn-sm {
  min-height: 36px;
  padding: 0 var(--space-4);
  font-size: var(--font-size-subhead);
}

/* ============================================================ */
/* Form inputs                                                   */
/* ============================================================ */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.label {
  font-size: var(--font-size-footnote);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
}

.input,
.select select {
  width: 100%;
  min-height: var(--touch-target);
  padding: 0 var(--space-4);
  background: var(--color-bg-input);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--font-size-body);
  color: var(--color-text-primary);
  transition:
    border-color var(--duration-fast) var(--easing),
    box-shadow var(--duration-fast) var(--easing);
}

.input:focus,
.select select:focus {
  outline: none;
  border-color: var(--color-border-focus);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.18);
}

.input::placeholder {
  color: var(--color-text-tertiary);
}

.input[aria-invalid="true"] {
  border-color: var(--color-danger);
}

/* Native select wrapper. Adds the chevron without breaking native UX. */
.select {
  position: relative;
  display: block;
}
.select select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: var(--space-10);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'><path d='M1 1.5l5 5 5-5' stroke='%238E8E93' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right var(--space-4) center;
  cursor: pointer;
}

.field-error {
  font-size: var(--font-size-footnote);
  color: var(--color-danger);
}

.field-helper {
  font-size: var(--font-size-footnote);
  color: var(--color-text-secondary);
}

/* Password input with show/hide toggle. */
.input-password {
  position: relative;
}
.input-password .input { padding-right: var(--space-12); }
.input-password__toggle {
  position: absolute;
  top: 50%;
  right: var(--space-2);
  transform: translateY(-50%);
  height: 36px;
  padding: 0 var(--space-3);
  font-size: var(--font-size-footnote);
  color: var(--color-accent);
  border-radius: var(--radius-sm);
}
.input-password__toggle:hover { background: var(--color-accent-bg); }

/* ============================================================ */
/* Cards & lists                                                 */
/* ============================================================ */

.card {
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}

/* iOS-style grouped list: rows separated by hairline borders. */
.card-grouped {
  background: var(--color-bg-grouped);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.list-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: var(--touch-target);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg-grouped);
  transition: background-color var(--duration-fast) var(--easing);
}
.list-row:last-child { border-bottom: none; }
a.list-row, button.list-row { width: 100%; text-align: left; cursor: pointer; }
a.list-row:hover, button.list-row:hover { background: var(--color-bg-base); }
.list-row[aria-disabled="true"], .list-row.is-inactive { opacity: 0.55; }

.list-row__main { flex: 1; min-width: 0; }
.list-row__title {
  font-size: var(--font-size-callout);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
}
.list-row__sub {
  font-size: var(--font-size-footnote);
  color: var(--color-text-secondary);
}
.list-row__chevron {
  color: var(--color-text-tertiary);
  flex-shrink: 0;
}

.divider {
  height: 1px;
  background: var(--color-border);
  border: 0;
  margin: var(--space-5) 0;
}

/* ============================================================ */
/* Stat card (dashboard tiles)                                   */
/* ============================================================ */

.stat-card {
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.stat-card__label {
  font-size: var(--font-size-caption-1);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.stat-card__value {
  font-size: var(--font-size-title-2);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-3);
}
@media (min-width: 768px) {
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
}

/* ============================================================ */
/* Avatars & badges                                              */
/* ============================================================ */

.avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-pill);
  background: var(--color-accent-bg);
  color: var(--color-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: var(--font-size-footnote);
  font-weight: var(--font-weight-semibold);
  flex-shrink: 0;
  text-transform: uppercase;
}
.avatar-lg { width: 56px; height: 56px; font-size: var(--font-size-callout); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  border-radius: var(--radius-pill);
  font-size: var(--font-size-footnote);
  font-weight: var(--font-weight-medium);
  background: var(--color-bg-base);
  color: var(--color-text-secondary);
  white-space: nowrap;
}
.badge-owner   { background: var(--color-accent-bg); color: var(--color-accent); }
.badge-sales   { background: var(--color-success-bg); color: var(--color-success); }
.badge-account { background: var(--color-warning-bg); color: var(--color-warning); }
.badge-active   { background: var(--color-success-bg); color: var(--color-success); }
.badge-inactive { background: var(--color-pending-bg); color: var(--color-pending); }

/* ============================================================ */
/* Empty state                                                   */
/* ============================================================ */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: var(--space-10) var(--space-4);
  gap: var(--space-2);
}
.empty-state__icon {
  width: 44px; height: 44px;
  border-radius: var(--radius-pill);
  background: var(--color-bg-base);
  color: var(--color-text-tertiary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--space-2);
}
.empty-state__title {
  font-size: var(--font-size-callout);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
}
.empty-state__description {
  font-size: var(--font-size-footnote);
  color: var(--color-text-secondary);
  max-width: 320px;
}

/* ============================================================ */
/* Modal                                                         */
/* ============================================================ */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  z-index: 1000;
}
.modal {
  background: var(--color-bg-elevated);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 480px;
  max-height: calc(100dvh - var(--space-8));
  overflow-y: auto;
}
.modal__header {
  padding: var(--space-5) var(--space-5) var(--space-3);
  font-size: var(--font-size-title-3);
  font-weight: var(--font-weight-semibold);
}
.modal__body { padding: 0 var(--space-5) var(--space-3); }
.modal__footer {
  padding: var(--space-3) var(--space-5) var(--space-5);
  display: flex;
  gap: var(--space-3);
  justify-content: flex-end;
}

/* ============================================================ */
/* Toast                                                         */
/* ============================================================ */

.toast-container {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-bar-height) + var(--space-4));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 1100;
  pointer-events: none;
}
@media (min-width: 768px) {
  .toast-container { bottom: var(--space-6); }
}
.toast {
  background: var(--color-text-primary);
  color: var(--color-text-on-accent);
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-pill);
  font-size: var(--font-size-subhead);
  box-shadow: var(--shadow-md);
  max-width: 90vw;
  pointer-events: auto;
}
.toast-success { background: var(--color-success); }
.toast-error   { background: var(--color-danger); }
.toast-info    { background: var(--color-text-primary); }

/* ============================================================ */
/* Spinner & skeleton                                            */
/* ============================================================ */

.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.spinner-lg { width: 28px; height: 28px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }

.page-loader {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--color-bg-base);
  z-index: 50;
  color: var(--color-text-tertiary);
}

.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-bg-base) 0%,
    rgba(0, 0, 0, 0.04) 50%,
    var(--color-bg-base) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ============================================================ */
/* Top bar / sidebar / tab bar                                   */
/* ============================================================ */

.top-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--top-bar-height);
  padding: 0 var(--space-4);
  background: var(--color-bg-surface);
  border-bottom: 1px solid var(--color-border);
}
.top-bar__brand {
  font-size: var(--font-size-headline);
  font-weight: var(--font-weight-semibold);
  letter-spacing: -0.01em;
}
.top-bar__menu {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  position: relative;
}

.user-menu-trigger {
  background: none;
  border: none;
  padding: var(--space-1);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--easing);
}
.user-menu-trigger:hover { background: var(--color-bg-base); }

.user-menu {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  min-width: 220px;
  background: var(--color-bg-elevated);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: var(--space-2);
  z-index: 20;
}
.user-menu__header {
  padding: var(--space-2) var(--space-3) var(--space-3);
  border-bottom: 1px solid var(--color-border);
  margin-bottom: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.user-menu__name {
  font-size: var(--font-size-callout);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
}
.user-menu__sub {
  font-size: var(--font-size-footnote);
  color: var(--color-text-secondary);
  text-transform: capitalize;
}
.user-menu__item {
  display: block;
  width: 100%;
  padding: var(--space-3);
  text-align: left;
  font-size: var(--font-size-callout);
  color: var(--color-text-primary);
  border-radius: var(--radius-sm);
  background: none;
  border: none;
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--easing);
}
.user-menu__item:hover { background: var(--color-bg-base); }
.user-menu__item--danger { color: var(--color-danger); }
.user-menu__item--danger:hover { background: var(--color-danger-bg); }

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  height: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--color-bg-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: stretch;
}
.tab-bar__item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: var(--font-size-caption-2);
  color: var(--color-text-tertiary);
  background: none;
  border: none;
  cursor: pointer;
}
.tab-bar__item.is-active { color: var(--color-accent); }

.sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--sidebar-width);
  background: var(--color-bg-surface);
  border-right: 1px solid var(--color-border);
  padding: var(--space-5) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.sidebar__brand {
  padding: 0 var(--space-3) var(--space-5);
  font-size: var(--font-size-title-3);
  font-weight: var(--font-weight-semibold);
}
.sidebar__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-primary);
  font-size: var(--font-size-callout);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  background: none;
  border: none;
  text-align: left;
}
.sidebar__item:hover { background: var(--color-bg-base); }
.sidebar__item.is-active { background: var(--color-accent-bg); color: var(--color-accent); }

/* ============================================================ */
/* Layout — mobile first, sidebar at >=768px                     */
/* ============================================================ */

.app-shell { min-height: 100dvh; display: flex; flex-direction: column; }

.app-content {
  flex: 1;
  width: 100%;
  max-width: var(--content-max-width);
  margin: 0 auto;
  padding: var(--space-5) var(--space-4) calc(var(--tab-bar-height) + var(--space-8));
}
@media (min-width: 768px) {
  .app-shell { flex-direction: row; }
  .app-content {
    margin-left: var(--sidebar-width);
    padding: var(--space-8);
    max-width: calc(var(--content-max-width) + var(--sidebar-width));
  }
}

/* Visibility helpers tied to the breakpoint. */
.mobile-only  { display: block; }
.desktop-only { display: none; }
@media (min-width: 768px) {
  .mobile-only  { display: none; }
  .desktop-only { display: block; }
}

/* ============================================================ */
/* Misc                                                          */
/* ============================================================ */

.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}
.section-header h2 {
  font-size: var(--font-size-title-3);
  font-weight: var(--font-weight-semibold);
}

.stack { display: flex; flex-direction: column; }
.stack-2 { gap: var(--space-2); }
.stack-3 { gap: var(--space-3); }
.stack-4 { gap: var(--space-4); }
.stack-5 { gap: var(--space-5); }
.stack-6 { gap: var(--space-6); }

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