/* ===== Reset & Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --color-bg: #f1f4f8;
  --color-surface: #ffffff;
  --color-border: #e2e6ed;
  --color-border-light: #eef1f5;
  --color-text: #141a29;
  --color-text-muted: #8893a7;
  --color-primary: #4466f5;
  --color-primary-hover: #3355e0;
  --color-primary-light: #eef2ff;
  --color-danger: #f44b4b;
  --color-danger-hover: #e03030;
  --color-danger-light: #fff0f0;
  --color-selected: #e9efff;
  --color-selected-border: #4466f5;
  --color-header-bg: #f5f7fa;
  --color-best-bg: #f2faf5;
  --color-best-border: #d1f0da;
  --color-best-text: #0f7a38;
  --color-hover-row: #f8fafc;
  --color-stripe: #fafbfc;
  --color-context-hover: #f5f6f9;
  --color-handle: #c5cad4;
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.03);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 3px 10px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.09);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.11);
  --shadow-blue: 0 2px 8px rgba(68, 102, 245, 0.25);
  --radius-xs: 4px;
  --radius-sm: 7px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --cell-height: 42px;
  --col-checkbox-width: 42px;
  --col-handle-width: 34px;
  --col-name-width: 210px;
  --col-price-width: 155px;
  --col-best-width: 230px;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 13.5px;
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-height: 100vh;
  margin: 0 auto;
  max-width: 100%;
  padding: 12px 20px;
}

@media (min-width: 768px) {
  #app { padding: 16px 28px; }
}

@media (min-width: 1200px) {
  #app { padding: 20px 40px; max-width: 1560px; }
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  flex-shrink: 0;
  gap: 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
}

.app-title {
  font-size: 19px;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.4px;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-logo {
  flex-shrink: 0;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

/* ===== Search Box ===== */
.search-box {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  font-size: 14px;
  pointer-events: none;
  opacity: 0.5;
  z-index: 1;
}

.search-input {
  width: 220px;
  padding: 8px 36px 8px 36px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  color: var(--color-text);
  background: #fafbfc;
  outline: none;
  transition: all 0.18s ease;
}

.search-input:focus {
  border-color: var(--color-selected-border);
  background: white;
  box-shadow: 0 0 0 3px rgba(68, 102, 245, 0.08);
  width: 280px;
}

.search-input::placeholder {
  color: var(--color-text-muted);
  opacity: 0.6;
}

.btn-clear-search {
  position: absolute;
  right: 6px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.12s ease;
  z-index: 1;
}

.btn-clear-search:hover {
  background: #e8eaef;
  color: var(--color-text);
}

.btn-clear-search.hidden {
  display: none;
}

/* Generic hidden helper */
.hidden {
  display: none !important;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 15px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.18s cubic-bezier(0.25, 0.8, 0.25, 1.2);
  white-space: nowrap;
  font-family: inherit;
  line-height: 1.4;
  letter-spacing: 0.1px;
  user-select: none;
}

.btn:active {
  transform: scale(0.97);
}

.btn-icon {
  font-size: 15px;
  font-weight: 600;
  line-height: 1;
}

.btn-primary {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
  box-shadow: var(--shadow-blue);
}

.btn-primary:hover {
  background: var(--color-primary-hover);
  border-color: var(--color-primary-hover);
  box-shadow: 0 4px 14px rgba(68, 102, 245, 0.35);
  transform: translateY(-1px);
}

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
  box-shadow: var(--shadow-xs);
}

.btn-secondary:hover {
  background: #fafbfc;
  border-color: #c8ced8;
  box-shadow: var(--shadow-sm);
}

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

.btn-cancel:hover {
  background: #fafbfc;
  border-color: #c8ced8;
}

.btn-danger {
  background: var(--color-danger);
  color: white;
  border-color: var(--color-danger);
  box-shadow: 0 1px 3px rgba(244, 75, 75, 0.22);
}

.btn-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.25);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
}

.btn-danger:hover {
  background: var(--color-danger-hover);
  border-color: var(--color-danger-hover);
  box-shadow: 0 4px 12px rgba(244, 75, 75, 0.3);
  transform: translateY(-1px);
}

/* ===== Spreadsheet Wrapper ===== */
.spreadsheet-wrapper {
  flex: 1;
  overflow: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  position: relative;
}

/* ===== Spreadsheet Table ===== */
.spreadsheet {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  min-width: 100%;
  background: var(--color-surface);
}

/* ===== Table Header ===== */
.spreadsheet thead th {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--color-header-bg);
  border-bottom: 2px solid var(--color-border);
  font-weight: 600;
  font-size: 11.5px;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0 12px;
  height: var(--cell-height);
  text-align: left;
  white-space: nowrap;
  user-select: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);
}

/* Checkbox column header */
.spreadsheet thead th.col-checkbox {
  position: sticky;
  left: 0;
  top: 0;
  z-index: 8;
  width: var(--col-checkbox-width);
  min-width: var(--col-checkbox-width);
  max-width: var(--col-checkbox-width);
  background: var(--color-header-bg);
  border-right: 1px solid var(--color-border-light);
  padding: 0;
  text-align: center;
  vertical-align: middle;
  cursor: default;
}

.spreadsheet thead th.col-checkbox input[type="checkbox"]:indeterminate {
  accent-color: var(--color-primary);
  opacity: 0.7;
}

.spreadsheet thead th.col-checkbox input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--color-primary);
}

/* Drag handle header */
.spreadsheet thead th.col-handle {
  position: sticky;
  left: var(--col-checkbox-width);
  top: 0;
  z-index: 7;
  width: var(--col-handle-width);
  min-width: var(--col-handle-width);
  max-width: var(--col-handle-width);
  background: var(--color-header-bg);
  border-right: 1px solid var(--color-border-light);
  padding: 0;
  text-align: center;
  cursor: default;
}

/* Header: Tên sản phẩm (frozen left) */
.spreadsheet thead th.col-name {
  position: sticky;
  left: calc(var(--col-handle-width) + var(--col-checkbox-width));
  top: 0;
  z-index: 6;
  width: var(--col-name-width);
  min-width: var(--col-name-width);
  background: var(--color-header-bg);
  border-right: 2px solid var(--color-border);
}

/* Frozen column shadow indicator */
.spreadsheet thead th.col-name::after,
.spreadsheet tbody td.col-name::after {
  content: '';
  position: absolute;
  top: 0;
  right: -8px;
  bottom: 0;
  width: 8px;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

/* Header: Supplier columns */
.spreadsheet thead th.col-supplier {
  width: var(--col-price-width);
  min-width: var(--col-price-width);
  cursor: pointer;
  overflow: visible;
  text-transform: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text);
  letter-spacing: 0;
  text-align: center;
  border-left: 1px solid var(--color-border);
}

/* Supplier name text */
.supplier-name-text {
  pointer-events: none;
}

/* Delete supplier button — hidden until header hover */
.btn-delete-supplier {
  display: none;
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  line-height: 1;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  transition: all 0.15s ease;
  z-index: 2;
}

.btn-delete-supplier:hover {
  background: var(--color-danger-light);
  color: var(--color-danger);
  transform: translateY(-50%) scale(1.1);
}

.spreadsheet thead th.col-supplier:hover .btn-delete-supplier {
  display: inline-flex;
}

/* Header: Giá tốt nhất (last column) */
.spreadsheet thead th.col-best {
  width: var(--col-best-width);
  min-width: var(--col-best-width);
  background: var(--color-best-bg);
  color: var(--color-best-text);
  text-transform: none;
  font-size: 13px;
  border-left: 1px solid var(--color-border);
}

/* ===== Table Body ===== */
.spreadsheet tbody td {
  padding: 0 12px;
  height: var(--cell-height);
  border-bottom: 1px solid var(--color-border);
  font-size: 13.5px;
  transition: background 0.1s ease;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--color-text);
}

/* Alternating row stripes */
.spreadsheet tbody tr:nth-child(even) td {
  background: var(--color-stripe);
}

.spreadsheet tbody tr:nth-child(even) td.col-best {
  background: #f4faf6;
}

/* Checkbox cell */
.spreadsheet tbody td.col-checkbox {
  position: sticky;
  left: 0;
  z-index: 3;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border-light);
  padding: 0;
  text-align: center;
  vertical-align: middle;
  width: 42px;
  min-width: 42px;
  max-width: 42px;
}

.spreadsheet tbody td.col-checkbox input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--color-primary);
}

.spreadsheet tbody tr:nth-child(even) td.col-checkbox {
  background: var(--color-stripe);
}

/* Drag handle cell */
.spreadsheet tbody td.col-handle {
  position: sticky;
  left: var(--col-checkbox-width);
  z-index: 2;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border-light);
  padding: 0;
  text-align: center;
  cursor: grab;
  width: var(--col-handle-width);
  min-width: var(--col-handle-width);
  overflow: visible;
  color: var(--color-handle);
  font-size: 16px;
  letter-spacing: 1px;
  user-select: none;
  transition: color 0.15s ease, background 0.1s ease;
}

.spreadsheet tbody td.col-handle:active {
  cursor: grabbing;
}

.spreadsheet tbody td.col-handle:hover {
  color: var(--color-text-muted);
  background: #f0f2f6;
}

/* Fix stripe for handle */
.spreadsheet tbody tr:nth-child(even) td.col-handle {
  background: var(--color-stripe);
}

.spreadsheet tbody tr:nth-child(even) td.col-handle:hover {
  background: #f0f2f6;
}

/* Tên sản phẩm cell (frozen left, after handle) */
.spreadsheet tbody td.col-name {
  position: sticky;
  left: calc(var(--col-handle-width) + var(--col-checkbox-width));
  z-index: 2;
  background: var(--color-surface);
  border-right: 2px solid var(--color-border);
  font-weight: 500;
  cursor: pointer;
  min-width: var(--col-name-width);
  overflow: visible;
}

/* Fix stripe for name */
.spreadsheet tbody tr:nth-child(even) td.col-name {
  background: var(--color-stripe);
}

/* Product name text */
.product-name-text {
  pointer-events: none;
}

.product-name-text.is-placeholder {
  color: var(--color-text-muted);
  font-style: italic;
  opacity: 0.6;
}

/* Supplier price cells */
.spreadsheet tbody td.col-price {
  min-width: var(--col-price-width);
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--color-text);
  cursor: cell;
  font-feature-settings: "tnum";
  border-left: 1px solid var(--color-border);
}

/* Giá tốt nhất cells (last column) */
.spreadsheet tbody td.col-best {
  background: var(--color-best-bg);
  font-weight: 600;
  color: var(--color-best-text);
  cursor: pointer;
  min-width: var(--col-best-width);
  text-align: center;
  padding-right: 16px;
  border-left: 1px solid var(--color-border);
}

/* Row hover */
.spreadsheet tbody tr:hover td {
  background: #f2f5f9 !important;
}

.spreadsheet tbody tr:hover td.col-best {
  background: #e9f6ee !important;
}

.spreadsheet tbody tr:hover td.col-handle {
  background: #e8ecf2 !important;
}

.spreadsheet tbody tr:hover td.col-checkbox {
  background: #e8ecf2 !important;
}

/* ===== Drag and Drop States ===== */

.spreadsheet tbody tr.dragging {
  opacity: 0.3;
  filter: grayscale(0.5);
}

.spreadsheet tbody tr.dragging td {
  background: var(--color-selected) !important;
}

/* Drop target indicator */
.spreadsheet tbody tr.drag-over td {
  box-shadow: inset 0 2px 0 0 var(--color-primary);
}

/* ===== Delete row button ===== */
.btn-delete-row {
  display: none;
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
  z-index: 3;
  opacity: 0.7;
}

.btn-delete-row:hover {
  background: var(--color-danger-light);
  border-color: #fcc;
  opacity: 1;
  transform: translateY(-50%) scale(1.08);
}

.spreadsheet tbody tr:hover .btn-delete-row {
  display: inline-flex;
}

/* ===== Cell States ===== */

/* Selected cell */
.spreadsheet td.selected {
  box-shadow: inset 0 0 0 2px var(--color-selected-border);
  background: var(--color-selected) !important;
  z-index: 3;
  position: relative;
}

/* Empty price cell */
.spreadsheet td.col-price:empty::after {
  content: '—';
  color: var(--color-text-muted);
  opacity: 0.2;
}

/* Best price empty */
.spreadsheet td.col-best.is-empty {
  color: var(--color-text-muted);
  font-weight: 400;
  font-style: italic;
  cursor: default;
}

/* ===== Edit Mode ===== */
.spreadsheet td.editing {
  position: relative;
  z-index: 4;
}

.spreadsheet td.editing input {
  position: absolute;
  inset: 0;
  border: 2px solid var(--color-selected-border);
  border-radius: 3px;
  padding: 0 10px;
  margin: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  font-size: 13.5px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  text-align: center;
  background: white;
  color: var(--color-text);
  z-index: 1;
  box-shadow: 0 0 0 4px rgba(68, 102, 245, 0.1);
}

/* Header edit mode */
.spreadsheet thead th.editing {
  position: relative;
  z-index: 7;
}

.spreadsheet thead th.editing input {
  position: absolute;
  inset: 0;
  border: 2px solid var(--color-selected-border);
  border-radius: 3px;
  padding: 0 10px;
  margin: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  font-size: 13px;
  font-family: inherit;
  font-weight: 600;
  background: white;
  color: var(--color-text);
  text-align: left;
  z-index: 1;
  box-shadow: 0 0 0 4px rgba(68, 102, 245, 0.1);
}

/* Product name cell edit mode */
.spreadsheet tbody td.col-name.editing {
  position: relative;
  left: auto;
  z-index: 4;
}

.spreadsheet tbody td.col-name.editing input {
  position: absolute;
  inset: 0;
  border: 2px solid var(--color-selected-border);
  border-radius: 3px;
  padding: 0 10px;
  margin: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  font-size: 13.5px;
  font-family: inherit;
  font-weight: 500;
  background: white;
  color: var(--color-text);
  text-align: left;
  z-index: 1;
  box-shadow: 0 0 0 4px rgba(68, 102, 245, 0.1);
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 72px 20px;
  text-align: center;
  color: var(--color-text-muted);
  user-select: none;
}

.empty-state-icon {
  width: 72px;
  height: 72px;
  margin-bottom: 20px;
  border-radius: 20px;
  background: linear-gradient(135deg, var(--color-primary-light), #e8ecff);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}

.empty-state-icon::after {
  content: '📊';
}

.empty-state-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 8px;
}

.empty-state-subtitle {
  font-size: 13.5px;
  color: var(--color-text-muted);
  max-width: 360px;
  line-height: 1.7;
}

.empty-state-subtitle strong {
  color: var(--color-primary);
  font-weight: 600;
}

/* ===== Dialog Overlay ===== */
.dialog-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(10, 12, 20, 0.4);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: overlayIn 0.18s ease;
}

.dialog-overlay.hidden {
  display: none;
}

@keyframes overlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.dialog {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  width: 420px;
  max-width: 92vw;
  animation: dialogIn 0.22s cubic-bezier(0.25, 0.8, 0.25, 1.2);
}

@keyframes dialogIn {
  from {
    opacity: 0;
    transform: translateY(-16px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.dialog[data-dialog] {
  display: none;
}

.dialog[data-dialog].active {
  display: block;
}

.dialog-title {
  font-size: 17px;
  font-weight: 600;
  padding: 24px 28px 0;
  color: var(--color-text);
}

.dialog-body {
  padding: 16px 28px;
}

.dialog-message {
  font-size: 14px;
  color: var(--color-text-muted);
  line-height: 1.65;
}

.dialog-input {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  color: var(--color-text);
  outline: none;
  transition: all 0.18s ease;
  background: #fafbfc;
}

.dialog-input:focus {
  border-color: var(--color-selected-border);
  background: white;
  box-shadow: 0 0 0 4px rgba(68, 102, 245, 0.08);
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 0 28px 24px;
}

/* ===== Scrollbar Styling ===== */
.spreadsheet-wrapper::-webkit-scrollbar {
  width: 7px;
  height: 7px;
}

.spreadsheet-wrapper::-webkit-scrollbar-track {
  background: transparent;
}

.spreadsheet-wrapper::-webkit-scrollbar-thumb {
  background: #d5dae3;
  border-radius: 10px;
}

.spreadsheet-wrapper::-webkit-scrollbar-thumb:hover {
  background: #b8bfca;
}

.spreadsheet-wrapper::-webkit-scrollbar-corner {
  background: transparent;
}

/* ===== Context Menu ===== */
.context-menu {
  position: fixed;
  z-index: 5000;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  padding: 6px 0;
  min-width: 210px;
  animation: contextMenuIn 0.12s ease;
}

@keyframes contextMenuIn {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  font-size: 13px;
  color: var(--color-text);
  cursor: pointer;
  user-select: none;
  transition: background 0.1s ease;
}

.context-menu-item:hover {
  background: var(--color-context-hover);
}

.context-menu-item:active {
  background: var(--color-selected);
}

.context-menu-icon {
  width: 18px;
  text-align: center;
  font-size: 14px;
  flex-shrink: 0;
}

.context-menu-label {
  flex: 1;
  white-space: nowrap;
}

.context-menu-shortcut {
  color: var(--color-text-muted);
  font-size: 11.5px;
  white-space: nowrap;
  margin-left: 12px;
}

.context-menu-divider {
  height: 1px;
  margin: 4px 8px;
  background: var(--color-border-light);
}

.context-menu-item-danger {
  color: var(--color-danger);
}

.context-menu-item-danger:hover {
  background: var(--color-danger-light);
}

/* ===== Loading / Shimmer for future use ===== */
@keyframes shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}
