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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: #333;
  background: #f5f5f5;
  overflow: hidden;
}

/* App Container */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: #fff;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  color: #333;
}

.cell-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
}

#selected-cell {
  font-family: "Courier New", monospace;
  font-size: 14px;
  font-weight: 600;
  color: #666;
  background: #f0f0f0;
  padding: 4px 10px;
  border-radius: 4px;
}

/* Filter Panel */
.filter-panel {
  display: none;
  background: #f8f8f8;
  border-bottom: 1px solid #e0e0e0;
  padding: 16px 20px;
  flex-shrink: 0;
}

.filter-panel.active {
  display: block;
}

.filter-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 800px;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.filter-group label {
  font-weight: 600;
  font-size: 13px;
  color: #333;
}

.filter-input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s ease;
}

.filter-input:focus {
  outline: none;
  border-color: #2196f3;
  box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}

.filter-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.filter-stats {
  font-size: 12px;
  color: #666;
  font-weight: 500;
  min-height: 18px;
}

/* Grid Container */
.grid-container {
  flex: 1;
  overflow: auto;
  background: #fff;
  position: relative;
}

/* Spreadsheet Table */
.spreadsheet-grid {
  border-collapse: collapse;
  table-layout: fixed;
  background: #fff;
}

.spreadsheet-grid th,
.spreadsheet-grid td {
  border: 1px solid #e0e0e0;
  text-align: left;
  user-select: none;
}

/* Corner Cell */
.corner-cell {
  width: 50px;
  min-width: 50px;
  height: 30px;
  background: #f8f8f8;
  position: sticky;
  top: 0;
  left: 0;
  z-index: 3;
  border-right: 2px solid #ccc;
  border-bottom: 2px solid #ccc;
}

/* Column Headers */
.column-header {
  width: 100px;
  min-width: 100px;
  height: 30px;
  background: #f8f8f8;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  text-align: center;
  padding: 6px;
  position: sticky;
  top: 0;
  z-index: 2;
  border-bottom: 2px solid #ccc;
}

/* Column Header Wrapper (for checkbox layout) */
.header-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: 100%;
}

/* Column Total Checkbox */
.column-total-checkbox {
  width: 14px;
  height: 14px;
  cursor: pointer;
  margin: 0;
  accent-color: #2196f3;
}

.column-total-checkbox:hover {
  transform: scale(1.1);
}

.column-total-checkbox:focus {
  outline: 2px solid #2196f3;
  outline-offset: 2px;
}

/* Column Letter in Header */
.column-letter {
  font-weight: 600;
  font-size: 12px;
  color: #666;
}

/* Column Title Row */
.title-corner-cell {
  width: 50px;
  min-width: 50px;
  height: 32px;
  background: #f0f0f0;
  position: sticky;
  top: 30px;
  left: 0;
  z-index: 3;
  border-right: 2px solid #ccc;
  border-bottom: 1px solid #e0e0e0;
}

.column-title-cell {
  width: 100px;
  min-width: 100px;
  height: 32px;
  background: #ffffff;
  font-weight: 500;
  font-size: 13px;
  color: #333;
  text-align: center;
  padding: 6px 8px;
  position: sticky;
  top: 30px;
  z-index: 2;
  border-bottom: 1px solid #e0e0e0;
  cursor: pointer;
  transition: background-color 0.1s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.column-title-cell:hover {
  background: #f9f9f9;
}

.column-title-cell.selected {
  background: #e3f2fd;
  outline: 2px solid #2196f3;
  outline-offset: -1px;
  z-index: 3;
}

.column-title-cell.editing {
  padding: 0;
  background: #fff;
  outline: 2px solid #2196f3;
  outline-offset: -1px;
}

/* Title Input (for editing) */
.title-input {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 6px 8px;
  font-family: inherit;
  font-size: inherit;
  background: transparent;
  text-align: center;
}

/* Show placeholder letter when empty */
.column-title-cell:empty::after {
  content: attr(data-letter);
  color: #999;
  font-weight: 400;
}

/* Row Headers */
.row-header {
  width: 50px;
  min-width: 50px;
  background: #f8f8f8;
  font-weight: 600;
  font-size: 12px;
  color: #666;
  text-align: center;
  padding: 6px;
  position: sticky;
  left: 0;
  z-index: 1;
  border-right: 2px solid #ccc;
}

/* Grid Cells */
.grid-cell {
  width: 100px;
  min-width: 100px;
  height: 30px;
  padding: 6px 8px;
  cursor: cell;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: #fff;
  transition: background-color 0.1s ease;
}

.grid-cell:hover {
  background: #fafafa;
}

.grid-cell.selected {
  background: #e3f2fd;
  outline: 2px solid #2196f3;
  outline-offset: -1px;
  z-index: 1;
}

/* Focus styling for accessibility */
.grid-cell:focus {
  outline: 2px solid #2196f3;
  outline-offset: -2px;
}

/* Hide focus outline when cell is selected (already has visible selection) */
.grid-cell.selected:focus {
  outline: 2px solid #2196f3;
  outline-offset: -1px;
}

.grid-cell.editing {
  padding: 0;
  background: #fff;
  outline: 2px solid #2196f3;
  outline-offset: -1px;
}

/* Hidden filtered rows */
.filtered-row {
  display: none !important;
}

/* Cell Input (for editing) */
.cell-input {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 6px 8px;
  font-family: inherit;
  font-size: inherit;
  background: transparent;
}

/* Status Bar */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 20px;
  background: #f8f8f8;
  border-top: 1px solid #e0e0e0;
  font-size: 12px;
  color: #666;
  flex-shrink: 0;
}

#status-message {
  font-weight: 500;
}

/* Totals Footer Row */
.totals-row {
  background: #f0f8ff;
  border-top: 3px solid #2196f3;
}

.totals-row-header {
  width: 50px;
  min-width: 50px;
  height: 36px;
  background: #e3f2fd;
  font-weight: 700;
  font-size: 11px;
  color: #1976d2;
  text-align: center;
  padding: 8px 6px;
  position: sticky;
  bottom: 0;
  left: 0;
  z-index: 3;
  border-right: 2px solid #ccc;
  letter-spacing: 0.5px;
}

.total-cell {
  width: 100px;
  min-width: 100px;
  height: 36px;
  padding: 8px;
  font-weight: 600;
  font-size: 13px;
  color: #1565c0;
  text-align: right;
  background: #f0f8ff;
  position: sticky;
  bottom: 0;
  z-index: 1;
  border-top: 2px solid #2196f3;
}

.total-cell:empty {
  background: transparent;
}

/* Make tfoot sticky at bottom */
.spreadsheet-grid tfoot {
  position: sticky;
  bottom: 0;
  z-index: 2;
}

/* Scrollbar Styling (Webkit) */
.grid-container::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

.grid-container::-webkit-scrollbar-track {
  background: #f0f0f0;
}

.grid-container::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 6px;
}

.grid-container::-webkit-scrollbar-thumb:hover {
  background: #aaa;
}

/* Header Controls */
.header-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* Icon Button */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: #666;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: #f0f0f0;
  color: #333;
}

.icon-btn:active {
  background: #e0e0e0;
}

.icon-btn.filter-active {
  background: #2196f3;
  color: #fff;
}

.icon-btn.filter-active:hover {
  background: #1976d2;
}

/* Modal Overlay */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal.active {
  display: flex;
}

/* Modal Content */
.modal-content {
  background: #fff;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  animation: modalSlideIn 0.3s ease;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Modal Header */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid #e0e0e0;
}

.modal-header h2 {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.close-btn {
  font-size: 28px;
  color: #999;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.2s ease;
  line-height: 1;
}

.close-btn:hover {
  background: #f0f0f0;
  color: #333;
}

/* Modal Body */
.modal-body {
  padding: 24px;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setting-group label {
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.setting-group input[type="number"] {
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s ease;
}

.setting-group input[type="number"]:focus {
  outline: none;
  border-color: #2196f3;
  box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}

/* Preset Buttons */
.preset-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.preset-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  background: #fff;
  color: #666;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.preset-btn:hover {
  border-color: #2196f3;
  color: #2196f3;
  background: #f0f8ff;
}

.preset-btn:active {
  transform: scale(0.95);
}

.setting-help {
  font-size: 12px;
  color: #666;
  margin: 0;
  line-height: 1.5;
}

/* Modal Footer */
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid #e0e0e0;
  background: #f8f8f8;
  border-radius: 0 0 8px 8px;
}

/* Buttons */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}

.btn-primary {
  background: #2196f3;
  color: #fff;
}

.btn-primary:hover {
  background: #1976d2;
}

.btn-primary:active {
  background: #1565c0;
}

.btn-secondary {
  background: #fff;
  color: #666;
  border: 1px solid #ddd;
}

.btn-secondary:hover {
  background: #f5f5f5;
  border-color: #ccc;
}

.btn-secondary:active {
  background: #e8e8e8;
}

/* Responsive Adjustments */
@media (max-width: 768px) {
  .app-header {
    padding: 10px 15px;
  }

  .app-title {
    font-size: 18px;
  }

  .column-header,
  .grid-cell {
    width: 80px;
    min-width: 80px;
  }

  .modal-content {
    width: 95%;
    max-width: none;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 16px;
  }

  .preset-buttons {
    justify-content: stretch;
  }

  .preset-btn {
    flex: 1;
    min-width: 60px;
  }
}
