/**
 * Shared Form Page Styles
 * Applies to LOX and Tracking forms.
 */

/* Layout */
.form-content {
  padding: var(--space-lg, 16px);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: var(--space-xl);
  margin-bottom: var(--space-xl);
}

.form-row.compact {
  margin-bottom: var(--space-sm);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.required {
  color: var(--color-error);
}

/* Facility autocomplete */
.facility-autocomplete {
  position: relative;
  width: 100%;
}

/* Facility rows for LOX */
.facility-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}

.facility-row .facility-autocomplete {
  flex: 1;
}

/* Per-facility pop-in checkbox */
.facility-pop-in-label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 4px 0;
  flex-shrink: 0;
}

.facility-pop-in-label:hover {
  color: var(--color-text-primary);
}

.facility-pop-in {
  margin: 0;
  width: 16px;
  height: 16px;
  cursor: pointer;
}

@media (max-width: 480px) {
  .facility-row {
    flex-wrap: wrap;
  }
  .facility-pop-in-label {
    margin-left: auto;
  }
}

.btn-remove-facility {
  padding: 8px 12px;
  background: var(--color-error);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  min-width: 32px;
  flex-shrink: 0;
}

.btn-remove-facility:hover {
  background: var(--color-error-dark);
}

.facility-validation-error {
  color: var(--color-error);
  font-size: 14px;
  margin-top: 4px;
  display: none;
}

.facility-validation-error.show {
  display: block;
}

/* Duplicate facility warning - gold/amber highlight */
input.duplicate {
  border-color: var(--color-warning);
  border-width: 2px;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.25);
}

.facility-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 280px;
  overflow-y: auto;
  background: var(--color-bg-container);
  border-radius: 6px;
  box-shadow:
    0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -4px rgba(0, 0, 0, 0.1);
  outline: 1px solid var(--color-border-light);
  z-index: var(--z-dropdown);
  /* Animation states */
  opacity: 0;
  transform: scale(0.95);
  transform-origin: top;
  pointer-events: none;
  transition:
    opacity 0.1s ease-out,
    transform 0.1s ease-out;
}

.facility-dropdown.show {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.facility-option {
  display: block;
  padding: 12px 16px;
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  color: var(--color-text-secondary);
  cursor: pointer;
  border-bottom: 1px solid var(--color-border-light);
  transition: background-color 0.1s ease, color 0.1s ease;
}

.facility-option:last-child {
  border-bottom: none;
}

.facility-option:hover,
.facility-option.highlighted {
  background: var(--color-bg-page);
  color: var(--color-text-primary);
}

/* Dark mode for facility dropdown */
html.dark-mode .facility-dropdown {
  box-shadow:
    0 10px 15px -3px rgba(0, 0, 0, 0.4),
    0 4px 6px -4px rgba(0, 0, 0, 0.3);
}

/* Checkboxes */
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xl);
  align-items: center;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  cursor: pointer;
}

/* Sections */
.section {
  margin-top: var(--space-3xl);
  padding-top: var(--space-2xl);
  border-top: 1px solid var(--color-border-light);
}

.section-title {
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  margin-bottom: var(--space-lg);
}

.section-title.collapsible {
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: 0;
  padding: var(--space-sm) 0;
}

.toggle-icon {
  transition: transform var(--transition-base);
  display: inline-block;
}

.toggle-icon.open {
  transform: rotate(90deg);
}

.collapsible-content {
  margin-top: var(--space-md);
  overflow: hidden;
}

.subsection {
  margin-bottom: var(--space-2xl);
}

.subsection-title {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
  margin-bottom: var(--space-md);
}

/* Bases table */
.bases-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: var(--space-lg);
}

.bases-table th {
  background: var(--color-bg-elevated);
  padding: var(--space-md) var(--space-sm);
  text-align: left;
  font-weight: var(--font-weight-semibold);
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
  border-bottom: 2px solid var(--color-border-light);
}

.bases-table td {
  padding: var(--space-md) var(--space-sm);
  border-bottom: 1px solid var(--color-border-light);
}

.bases-table th:nth-child(1) {
  width: 40%;
}

.bases-table th:nth-child(2),
.bases-table th:nth-child(3),
.bases-table th:nth-child(4) {
  width: 15%;
}

.bases-table th:nth-child(5) {
  width: 15%;
}

.bases-table input {
  width: 100%;
}

@media (max-width: 640px) {
  .form-content {
    padding: calc(var(--space-xl) / 10);
  }

  .bases-table th,
  .bases-table td {
    padding: var(--space-sm) var(--space-xs);
  }

  .bases-table th:nth-child(1) {
    width: 32%;
  }

  .bases-table th:nth-child(2),
  .bases-table th:nth-child(3),
  .bases-table th:nth-child(4),
  .bases-table th:nth-child(5) {
    width: 17%;
  }
}

.total-filled {
  padding: var(--space-md) var(--space-lg);
  background: var(--color-info-bg);
  border: 2px solid var(--color-info);
  border-radius: var(--radius-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--color-info);
  white-space: nowrap;
}

.bases-footer {
  display: flex;
  gap: var(--space-lg);
  align-items: center;
  flex-wrap: wrap;
}

/* Validation */
.validation-error {
  padding: var(--space-md);
  background: var(--color-error-bg);
  border: 1px solid var(--color-error);
  border-radius: var(--radius-sm);
  color: var(--color-error);
  margin-bottom: var(--space-md);
  display: none;
}

.validation-error.show {
  display: block;
}

.section .validation-error {
  margin-top: var(--space-md);
}

/* Asset lists */
.asset-list {
  margin-top: var(--space-sm);
  margin-left: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-bottom: var(--space-lg);
}

.asset-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
}

.asset-item input {
  flex: 1;
  max-width: 320px;
}

/* Buttons */
.btn-remove {
  background: var(--color-btn-danger);
  color: var(--color-text-inverse);
  padding: var(--space-sm) var(--space-md);
  min-width: 32px;
}

/* Photos */
.photo-section {
  margin-top: var(--space-lg);
}

.photo-input {
  display: none;
}

.photo-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-2xl);
  background: var(--color-btn-primary);
  color: var(--color-text-inverse);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: var(--font-weight-medium);
  margin-bottom: var(--space-md);
}

.photo-preview {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: var(--space-lg);
  margin-top: var(--space-md);
}

.photo-item {
  position: relative;
  border: 1px solid var(--color-border-medium);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--color-bg-elevated);
  cursor: grab;
  transition: transform var(--transition-base),
    box-shadow var(--transition-base), border-color var(--transition-base);
}

.photo-item:active {
  cursor: grabbing;
}

.photo-item.dragging {
  opacity: 0.8;
  transform: scale(1.05);
  box-shadow: var(--shadow-xl);
  z-index: 1000;
  pointer-events: none; /* Important for elementFromPoint to see what's underneath */
}

.photo-item.drop-target {
  border: 2px dashed var(--color-btn-primary);
  transform: scale(1.02);
}

.photo-item img {
  width: 100%;
  height: 160px;
  object-fit: cover;
  display: block;
}

.photo-item button.photo-remove {
  top: 8px;
  right: 8px;
  z-index: 10;
}

.photo-item .retry-btn,
.photo-item .loading-text {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: var(--space-sm);
  text-align: center;
  background: rgba(0, 0, 0, 0.65);
  color: white;
  font-size: var(--font-size-sm);
}

.photo-item .loading-text {
  color: white;
}

/* Image modal */
.image-modal {
  display: none;
  position: fixed;
  z-index: var(--z-modal);
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0, 0, 0, 0.9);
}

.image-modal-content {
  margin: 5% auto;
  display: block;
  max-width: 90%;
  max-height: 80vh;
}

.image-modal img {
  width: 100%;
  height: auto;
  display: block;
}

.close-modal {
  position: fixed;
  top: 20px;
  right: 20px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
  z-index: calc(var(--z-modal) + 1);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}

/* ============================================
   Tailwind UI-Inspired Custom Select Dropdown
   ============================================ */
.custom-select {
  position: relative;
  display: inline-block;
  width: 100%;
}

/* Trigger Button - Light Mode */
.custom-select-trigger {
  display: inline-flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--color-bg-container);
  border-radius: 6px;
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
  cursor: pointer;
  user-select: none;
  min-height: 44px;
  /* Inset ring effect */
  box-shadow:
    inset 0 0 0 1px var(--color-border-medium),
    0 1px 2px 0 rgba(0, 0, 0, 0.05);
  transition: background-color 0.15s ease;
}

.custom-select-trigger:hover {
  background: var(--color-bg-page);
}

.custom-select-trigger:focus {
  outline: none;
  box-shadow:
    inset 0 0 0 1px var(--color-border-medium),
    0 0 0 2px rgba(33, 148, 221, 0.4);
}

/* Chevron Icon */
.custom-select-trigger svg {
  width: 20px;
  height: 20px;
  color: var(--color-text-muted);
  flex-shrink: 0;
  transition: transform 0.15s ease;
}

.custom-select.open .custom-select-trigger svg {
  transform: rotate(180deg);
}

/* Dropdown Menu */
.custom-select-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  min-width: 100%;
  max-height: 280px;
  overflow-y: auto;
  background: var(--color-bg-container);
  border-radius: 6px;
  box-shadow:
    0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -4px rgba(0, 0, 0, 0.1);
  outline: 1px solid var(--color-border-light);
  z-index: var(--z-dropdown);
  /* Animation states */
  opacity: 0;
  transform: scale(0.95);
  transform-origin: top;
  pointer-events: none;
  transition:
    opacity 0.1s ease-out,
    transform 0.1s ease-out;
}

.custom-select.open .custom-select-dropdown {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

/* Options Container */
.custom-select-options {
  padding: 4px 0;
}

/* Option Items */
.custom-select-option {
  display: block;
  padding: 12px 16px;
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  color: var(--color-text-secondary);
  cursor: pointer;
  border-bottom: 1px solid var(--color-border-light);
  transition: background-color 0.1s ease, color 0.1s ease;
}

.custom-select-option:last-child {
  border-bottom: none;
}

.custom-select-option:hover,
.custom-select-option:focus,
.custom-select-option.highlighted {
  background: var(--color-bg-page);
  color: var(--color-text-primary);
  outline: none;
}

.custom-select-option.selected {
  background: var(--color-bg-page);
  color: var(--color-text-primary);
  font-weight: var(--font-weight-medium);
}

.custom-select-option.selected:hover {
  background: var(--color-border-light);
}

/* ============================================
   Dark Mode Styles
   ============================================ */
html.dark-mode .custom-select-trigger {
  box-shadow: inset 0 0 0 1px var(--color-border-light);
}

html.dark-mode .custom-select-trigger:focus {
  box-shadow:
    inset 0 0 0 1px var(--color-border-medium),
    0 0 0 2px rgba(96, 165, 250, 0.4);
}

html.dark-mode .custom-select-dropdown {
  box-shadow:
    0 10px 15px -3px rgba(0, 0, 0, 0.4),
    0 4px 6px -4px rgba(0, 0, 0, 0.3);
}

html.dark-mode .custom-select-option.selected {
  background: var(--color-bg-elevated);
}

html.dark-mode .custom-select-option.selected:hover {
  background: var(--color-border-light);
}

/* Hidden native select for form submission */
.custom-select select {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}

/* Utility */
.hidden {
  display: none;
}

/* Loading overlay for uploads */
#loadingOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  color: var(--color-text-inverse);
  z-index: var(--z-overlay);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: var(--space-md);
  text-align: center;
  padding: var(--space-2xl);
}

#loadingOverlay .spinner {
  width: 48px;
  height: 48px;
  border: 6px solid rgba(255, 255, 255, 0.35);
  border-top-color: var(--color-text-inverse);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
