/*
Copyright (C) 2025 Seth Johnston

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/* ===== CSS Reset & Base Styles ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary-color: #2c3e50;
  --secondary-color: #3498db;
  --success-color: #27ae60;
  --error-color: #e74c3c;
  --warning-color: #f39c12;
  --background: #ecf0f1;
  --card-background: #ffffff;
  --border-color: #bdc3c7;
  --text-color: #2c3e50;
  --text-light: #7f8c8d;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.15);
  --transition: all 0.3s ease;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--background);
  color: var(--text-color);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header {
  background: var(--primary-color);
  color: white;
  padding: 1rem 2rem;
  box-shadow: var(--shadow);
  position: relative;
  display: flex;
  align-items: center;
  gap: 1rem;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 600;
}

main {
  flex: 1;
  padding: 2rem;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

footer {
  background: var(--primary-color);
  color: white;
  text-align: center;
  padding: 1rem;
  margin-top: auto;
  font-size: 0.9rem;
}

/* ===== Status Indicator ===== */
#status-indicator {
  font-size: 1.5rem;
  margin-left: auto;
  transition: var(--transition);
}

.status-online {
  color: var(--success-color);
  text-shadow: 0 0 8px var(--success-color);
}

.status-offline {
  color: var(--error-color);
  text-shadow: 0 0 8px var(--error-color);
}

/* ===== Update Banner ===== */
.update-banner {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--warning-color);
  color: white;
  padding: 1rem 2rem;
  border-radius: 8px;
  box-shadow: var(--shadow-hover);
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 1rem;
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    top: -100px;
    opacity: 0;
  }
  to {
    top: 70px;
    opacity: 1;
  }
}

.update-banner button {
  background: white;
  color: var(--warning-color);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.update-banner button:hover {
  transform: scale(1.05);
}

/* ===== Cards ===== */
.card {
  background: var(--card-background);
  border-radius: 12px;
  padding: 2rem;
  margin-bottom: 2rem;
  box-shadow: var(--shadow);
  transition: var(--transition);
}

.card:hover {
  box-shadow: var(--shadow-hover);
}

.card h2 {
  margin-bottom: 1rem;
  color: var(--primary-color);
  font-size: 1.5rem;
}

.card h3 {
  margin-bottom: 0.75rem;
  color: var(--primary-color);
  font-size: 1.2rem;
}

/* ===== Privacy Notice ===== */
.privacy-notice {
  background: #e8f5e9;
  border-left: 4px solid var(--success-color);
  padding: 1rem;
  margin-bottom: 1.5rem;
  border-radius: 4px;
  color: var(--text-color);
}

/* ===== File Input ===== */
.file-input-wrapper {
  margin-bottom: 1rem;
}

#file-input {
  display: none;
}

.file-label {
  display: inline-block;
  padding: 0.75rem 2rem;
  background: var(--secondary-color);
  color: white;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--transition);
  font-weight: 500;
}

.file-label:hover {
  background: #2980b9;
  transform: translateY(-2px);
  box-shadow: var(--shadow-hover);
}

.file-info {
  margin-top: 1rem;
  padding: 1rem;
  border-radius: 8px;
  background: #f8f9fa;
}

.file-info p {
  margin: 0.5rem 0;
}

.file-info .success {
  color: var(--success-color);
  font-weight: 500;
}

.file-info .error {
  color: var(--error-color);
  font-weight: 500;
}

/* ===== Config Section ===== */
.config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

.config-item label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--primary-color);
}

.config-item select {
  width: 100%;
  padding: 0.75rem;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  font-size: 1rem;
  background: white;
  cursor: pointer;
  transition: var(--transition);
}

.config-item select:focus {
  outline: none;
  border-color: var(--secondary-color);
}

/* ===== Filter Section ===== */
.filter-section {
  background: #f8f9fa;
  padding: 1.5rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
  border: 2px solid var(--border-color);
}

.filter-section h3 {
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.optional-label {
  font-size: 0.85rem;
  color: var(--text-light);
  font-weight: normal;
}

.filter-description {
  color: var(--text-light);
  font-size: 0.95rem;
  margin-bottom: 1rem;
}

.filter-group {
  background: white;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
  position: relative;
}

.filter-group-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}

.filter-group-title {
  font-weight: 600;
  color: var(--primary-color);
}

.filter-group-logic {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.filter-group-logic label {
  font-size: 0.9rem;
  color: var(--text-light);
}

.filter-group-logic select {
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.9rem;
}

.remove-group-btn {
  background: var(--error-color);
  color: white;
  border: none;
  padding: 0.25rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: var(--transition);
}

.remove-group-btn:hover {
  background: #c0392b;
}

.filter-condition {
  display: grid;
  grid-template-columns: 1fr 120px 1fr auto auto;
  gap: 0.5rem;
  align-items: end;
  margin-bottom: 0.75rem;
  padding: 0.75rem;
  background: #f8f9fa;
  border-radius: 6px;
}

.filter-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.filter-field label {
  font-size: 0.85rem;
  color: var(--text-color);
  font-weight: 500;
}

.filter-field select,
.filter-field input {
  padding: 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.9rem;
}

.filter-field select:focus,
.filter-field input:focus {
  outline: none;
  border-color: var(--secondary-color);
}

.regex-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
}

.regex-toggle input[type=\"checkbox\"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.regex-toggle label {
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
}

.remove-condition-btn {
  background: var(--error-color);
  color: white;
  border: none;
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: var(--transition);
}

.remove-condition-btn:hover {
  background: #c0392b;
}

.add-condition-btn {
  background: var(--secondary-color);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: var(--transition);
  margin-top: 0.5rem;
}

.add-condition-btn:hover {
  background: #2980b9;
}

.filter-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.filter-group-separator {
  text-align: center;
  font-weight: 600;
  color: var(--secondary-color);
  padding: 0.5rem 0;
  font-size: 0.9rem;
}

.no-filters {
  color: var(--text-light);
  font-style: italic;
  padding: 1rem;
  text-align: center;
  background: white;
  border-radius: 6px;
  border: 1px dashed var(--border-color);
}

.help-btn {
  background: var(--warning-color);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: var(--transition);
  margin-left: auto;
}

.help-btn:hover {
  background: #e67e22;
  transform: translateY(-2px);
}

/* ===== Modal ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 1rem;
}

.modal-content {
  background: white;
  border-radius: 12px;
  max-width: 900px;
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 2px solid var(--border-color);
  background: var(--primary-color);
  color: white;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.3rem;
}

.close-modal {
  background: none;
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  padding: 0;
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: var(--transition);
}

.close-modal:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  flex: 1;
}

.modal-body h3 {
  color: var(--primary-color);
  margin-top: 1.5rem;
  margin-bottom: 1rem;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.help-table {
  width: 100%;
  margin: 1rem 0;
  border-collapse: collapse;
}

.help-table th,
.help-table td {
  padding: 0.75rem;
  border: 1px solid var(--border-color);
  text-align: left;
}

.help-table th {
  background: #f8f9fa;
  color: var(--primary-color);
  font-weight: 600;
  position: static;
}

.help-table code {
  background: #f8f9fa;
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  color: var(--error-color);
  font-size: 0.9em;
}

.modal-body ul {
  margin: 1rem 0;
  padding-left: 2rem;
}

.modal-body li {
  margin: 0.5rem 0;
}

.modal-body li code {
  background: #f8f9fa;
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  color: var(--error-color);
}

/* Reference Modal Styles */
.reference-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.reference-count {
  font-weight: 600;
  color: var(--text-color);
  margin: 0;
}

#reference-textarea {
  width: 100%;
  padding: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
  line-height: 1.6;
  resize: vertical;
  background: #f8f9fa;
}

#copy-references-btn {
  align-self: flex-start;
}

.no-references {
  color: var(--text-light);
  font-style: italic;
  text-align: center;
  padding: 2rem;
}

/* ===== Buttons ===== */
.primary-btn,
.secondary-btn {
  padding: 0.75rem 2rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
}

.primary-btn {
  background: var(--secondary-color);
  color: white;
}

.primary-btn:hover:not(:disabled) {
  background: #2980b9;
  transform: translateY(-2px);
  box-shadow: var(--shadow-hover);
}

.primary-btn:disabled {
  background: var(--border-color);
  cursor: not-allowed;
  opacity: 0.6;
}

.secondary-btn {
  background: var(--border-color);
  color: var(--text-color);
}

.secondary-btn:hover {
  background: #95a5a6;
  transform: translateY(-2px);
  box-shadow: var(--shadow-hover);
}

/* ===== Section Header ===== */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.pivot-controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ===== Pivot Visibility Controls ===== */
.pivot-visibility-section {
  background: #f8f9fa;
  border: 2px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

/* ===== Info Sections ===== */
.pivot-info,
.datasheet-info {
  background: #e3f2fd;
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
  border-left: 4px solid var(--secondary-color);
}

/* ===== Tables ===== */
.table-wrapper {
  overflow-x: auto;
  margin-top: 1rem;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  background: white;
}

th,
td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}

th {
  background: var(--primary-color);
  color: white;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 10;
}

tr:hover {
  background: #f8f9fa;
}

tr:last-child td {
  border-bottom: none;
}

/* ===== Pivot Table Specific ===== */
#pivot-table th:first-child {
  background: var(--primary-color);
}

#pivot-table th {
  text-align: center;
  min-width: 120px;
}

#pivot-table td {
  text-align: center;
}

.cell-btn {
  background: var(--secondary-color);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: var(--transition);
  font-weight: 500;
}

.cell-btn:hover {
  background: #2980b9;
  transform: scale(1.05);
}

.empty-cell {
  color: var(--text-light);
  font-style: italic;
}

/* ===== Datasheet Header ===== */
.header-cell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.header-controls {
  display: flex;
  gap: 0.25rem;
}

.move-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: none;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: var(--transition);
}

.move-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* ===== Datasheet Controls ===== */
.datasheet-controls {
  margin-bottom: 1.5rem;
  position: relative;
}

#show-hidden-columns-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

#show-hidden-columns-btn:disabled {
  cursor: not-allowed;
}

.btn-icon {
  font-size: 1.1rem;
}

.hidden-columns-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.5rem;
  background: white;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 250px;
  max-width: 400px;
  max-height: 400px;
  overflow-y: auto;
  z-index: 1000;
}

.dropdown-header {
  padding: 0.75rem 1rem;
  font-weight: 600;
  color: var(--primary-color);
  border-bottom: 1px solid var(--border-color);
  background: #f8f9fa;
  font-size: 0.9rem;
}

.dropdown-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #f0f0f0;
  transition: var(--transition);
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background: rgba(52, 152, 219, 0.1);
}

.dropdown-item span {
  flex: 1;
  font-size: 0.9rem;
}

.show-column-btn {
  background: var(--secondary-color);
  color: white;
  border: none;
  padding: 0.35rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.show-column-btn:hover {
  background: #2980b9;
  transform: scale(1.05);
}

/* ===== Datasheet Table Headers ===== */
.draggable-header {
  cursor: move;
  user-select: none;
  position: relative;
  transition: var(--transition);
}

.draggable-header.dragging {
  opacity: 0.5;
}

.draggable-header.drag-over {
  background: rgba(52, 152, 219, 0.2);
  border-left: 3px solid var(--secondary-color);
}

.draggable-header .header-cell {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.25rem;
}

.drag-handle {
  color: #95a5a6;
  cursor: grab;
  font-size: 1rem;
  line-height: 1;
  transition: var(--transition);
}

.drag-handle:hover {
  color: var(--secondary-color);
}

.draggable-header:active .drag-handle {
  cursor: grabbing;
}

.header-label {
  flex: 1;
  font-weight: 600;
}

.hide-column-btn {
  background: transparent;
  border: none;
  color: #95a5a6;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 3px;
  font-size: 1rem;
  line-height: 1;
  transition: var(--transition);
  opacity: 0;
}

.draggable-header:hover .hide-column-btn {
  opacity: 1;
}

.hide-column-btn:hover {
  background: rgba(231, 76, 60, 0.1);
  color: #e74c3c;
  transform: scale(1.1);
}

/* ===== Utility Classes ===== */
.hidden {
  display: none !important;
}

/* ===== Responsive Design ===== */
@media (max-width: 768px) {
  header {
    padding: 1rem;
  }

  header h1 {
    font-size: 1.2rem;
  }

  main {
    padding: 1rem;
  }

  .card {
    padding: 1.5rem;
  }

  .config-grid {
    grid-template-columns: 1fr;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .column-toggles {
    grid-template-columns: 1fr;
  }

  th,
  td {
    padding: 0.5rem;
    font-size: 0.9rem;
  }

  .update-banner {
    left: 1rem;
    right: 1rem;
    transform: none;
    font-size: 0.9rem;
  }

  .filter-condition {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .filter-actions {
    flex-direction: column;
  }

  .help-btn {
    margin-left: 0;
  }

  .modal-content {
    max-width: 100%;
    margin: 0.5rem;
  }
}

@media (max-width: 480px) {
  .file-label {
    width: 100%;
    text-align: center;
  }

  .primary-btn,
  .secondary-btn {
    width: 100%;
  }
}

/* ===== Print Styles ===== */
@media print {
  header,
  footer,
  .privacy-notice,
  .file-input-wrapper,
  .config-grid,
  .primary-btn,
  .secondary-btn,
  .column-controls,
  .update-banner {
    display: none;
  }

  body {
    background: white;
  }

  .card {
    box-shadow: none;
    page-break-inside: avoid;
  }

  table {
    page-break-inside: auto;
  }

  tr {
    page-break-inside: avoid;
    page-break-after: auto;
  }
}
