/* ========================================
   MATERIALS BUILDUP MODAL
   
   Centralized styling for materials buildup functionality.
   Uses theme variables from variables.css for consistency.
   Responsive design with mobile-first approach.
   
   Theme Variables Used:
   - --bg-primary, --bg-secondary, --bg-hover
   - --text-primary, --text-secondary, --text-muted, --text-inverse
   - --primary-color, --primary-hover
   - --header-bg, --header-text, --header-border
   - --border-color, --radius-md, --radius-lg
   - --spacing-* (xs, sm, md, lg, xl, 2xl)
   - --shadow-* (sm, md, lg, modal)
   - --status-* (success, error)
   - --z-modal, --z-sticky
   ======================================== */

/* Modal overlay - extends base modal system */
.materials-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--theme-overlay);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 99999; /* Extremely high z-index to break out of parent modals */
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding-top: 2vh;
  visibility: hidden;
  opacity: 0;
  transition: all 0.2s ease;
}

.materials-modal-overlay.active {
  display: flex;
  visibility: visible;
  opacity: 1;
}

/* Modal dialog */
.materials-modal {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  max-width: 1400px;
  max-height: 95vh;
  height: 95vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Modal header */
.materials-modal-header {
  padding: var(--spacing-sm);
  border-bottom: 2px solid var(--header-border);
  background-color: var(--header-bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0 !important;
}

.materials-modal-title {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--header-text);
  letter-spacing: 0.5px;
}

.materials-modal-close {
  background: transparent;
  border: none;
  font-size: var(--font-size-xl);
  cursor: pointer;
  color: var(--text-secondary);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius-md);
  transition: all 0.2s ease;
  line-height: 1;
}

.materials-modal-close:hover {
  background-color: var(--bg-hover);
  color: var(--text-primary);
}

/* Modal body */
.materials-modal-body {
  padding: var(--spacing-xs);
  overflow: hidden;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Columns layout */
.materials-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--spacing-md);
  min-height: 0;
  flex: 1;
  overflow: hidden;
}

/* Column styling */
.materials-column {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
  flex: 1;
}

.materials-column-header {
  padding: var(--spacing-md);
  background-color: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  font-weight: 600;
  color: var(--text-primary);
  font-size: var(--font-size-base);
  flex-shrink: 0;
}

.materials-column-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* Search box */
.materials-search-box {
  padding: var(--spacing-md);
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-primary);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
}

.materials-search-input {
  flex: 1;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: var(--font-size-base);
  transition: all 0.2s ease;
  background-color: var(--bg-primary);
  color: var(--text-primary);
}

.materials-search-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(var(--theme-primary-rgb), 0.1);
}

/* Materials table */
.materials-table-wrapper {
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.materials-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-base);
}

.materials-table thead {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background-color: var(--bg-secondary);
}

.materials-table th {
  padding: var(--spacing-sm) var(--spacing-md);
  text-align: left;
  font-weight: 600;
  color: var(--text-primary);
  border-bottom: 2px solid var(--border-color);
  white-space: nowrap;
  background-color: var(--bg-secondary);
}

.materials-table td {
  padding: var(--spacing-sm) var(--spacing-md);
  border-bottom: 1px solid var(--border-color);
  color: var(--text-primary);
}

.materials-table tbody tr {
  transition: background-color 0.15s ease;
  cursor: pointer;
}

.materials-table tbody tr:hover {
  background-color: var(--bg-hover);
}

.materials-table tbody tr.selected {
  background-color: rgba(var(--theme-primary-rgb), 0.1);
}

/* Material code styling */
.materials-table .material-code {
  font-weight: 600;
  color: var(--primary-color);
}

/* Material description */
.materials-table .material-description {
  color: var(--text-secondary);
  max-width: 250px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Material cost */
.materials-table .material-cost {
  text-align: right;
  font-family: monospace;
  color: var(--text-primary);
}

/* Quantity input */
.material-quantity-input {
  width: 80px;
  padding: var(--spacing-xs) var(--spacing-sm);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  text-align: center;
  font-size: var(--font-size-sm);
  transition: all 0.2s ease;
  background-color: var(--bg-primary);
  color: var(--text-primary);
}

.material-quantity-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--theme-primary-rgb), 0.1);
}

/* Unit cost input */
.material-unitcost-input {
  width: 48px;
  padding: var(--spacing-xs) var(--spacing-sm);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  text-align: center;
  font-size: var(--font-size-sm);
  transition: all 0.2s ease;
  background-color: var(--bg-primary);
  color: var(--text-primary);
}

.material-unitcost-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(var(--theme-primary-rgb), 0.1);
}

.materials-table .btn-add {
  background-color: var(--status-success);
}

.materials-table .btn-add:hover {
  background-color: var(--theme-success-hover, var(--theme-success-hover));
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}

/* B-11: `.materials-table .btn-remove` removed in phase 3 — nothing in views/,
   controllers/ or public/js ever applied the class. */

/* Modal footer */
.materials-modal-footer {
  padding: var(--spacing-sm);
  border-top: 1px solid var(--border-color);
  background-color: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0 !important;
}

.materials-summary {
  font-size: var(--font-size-base);
  color: var(--text-secondary);
}

.materials-summary-label {
  font-weight: 600;
  color: var(--text-primary);
}

.materials-summary-value {
  font-weight: 700;
  color: var(--primary-color);
  font-size: var(--font-size-lg);
  font-family: monospace;
}

.materials-modal-actions {
  display: flex;
  gap: var(--spacing-md);
}

.materials-modal-btn {
  padding: var(--spacing-sm) var(--spacing-lg);
  border: none;
  border-radius: var(--radius-md);
  font-size: var(--font-size-base);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.materials-modal-btn-primary {
  background-color: var(--primary-color);
  color: var(--text-inverse);
}

.materials-modal-btn-primary:hover {
  background-color: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.materials-modal-btn-secondary {
  background-color: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

.materials-modal-btn-secondary:hover {
  background-color: var(--bg-hover);
  border-color: var(--text-secondary);
  color: var(--text-primary);
}

/* Empty state */
.materials-empty-state {
  padding: var(--spacing-2xl);
  text-align: center;
  color: var(--text-muted);
}

.materials-empty-state i {
  font-size: 3rem;
  margin-bottom: var(--spacing-md);
  opacity: 0.3;
}

.materials-empty-state p {
  margin: 0;
  font-size: var(--font-size-base);
}

/* Scrollbar styling */
.materials-table-wrapper::-webkit-scrollbar,
.materials-modal-body::-webkit-scrollbar,
.materials-column-body::-webkit-scrollbar {
  width: 8px;
}

.materials-table-wrapper::-webkit-scrollbar-track,
.materials-modal-body::-webkit-scrollbar-track,
.materials-column-body::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

.materials-table-wrapper::-webkit-scrollbar-thumb,
.materials-modal-body::-webkit-scrollbar-thumb,
.materials-column-body::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: var(--radius-md);
}

.materials-table-wrapper::-webkit-scrollbar-thumb:hover,
.materials-modal-body::-webkit-scrollbar-thumb:hover,
.materials-column-body::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Responsive design */
@media (max-width: 768px) {
  .materials-modal {
    width: 95vw;
    max-height: 90vh;
  }

  .materials-columns {
    grid-template-columns: 1fr;
    gap: var(--spacing-md);
  }

  .materials-modal-header {
    padding: var(--spacing-md);
  }

  .materials-modal-body {
    padding: var(--spacing-md);
  }

  .materials-modal-footer {
    flex-direction: column;
    gap: var(--spacing-md);
    align-items: stretch;
  }

  .materials-modal-actions {
    flex-direction: column;
  }

  .materials-modal-btn {
    width: 100%;
  }

  .materials-table .material-description {
    max-width: 150px;
  }
}

@media (max-width: 480px) {
  .materials-modal {
    width: 100vw;
    max-height: 100vh;
    border-radius: 0;
  }

  .materials-modal-overlay {
    padding: 0;
  }

  .materials-table th,
  .materials-table td {
    padding: var(--spacing-xs) var(--spacing-sm);
    font-size: var(--font-size-sm);
  }

  .material-quantity-input {
    width: 60px;
  }
}

.materials-search-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: var(--spacing-sm);
  font-size: var(--font-size-base);
  transition: color 0.2s ease;
}

.materials-search-clear:hover {
  color: var(--text-primary);
}