/* -------------------------------------------------------------------------
   VARIABLES DE TEMA (MODO CLARO/OSCURO)
   ------------------------------------------------------------------------- */
:root {
  --bg-color: #ffffff;
  --text-color: #333333;
  --header-bg: #f0f1f4;
  --header-text: #4a5068;
  --border-color: #d1d5db;
  --corner-bg: #e5e7eb;
  --nav-bg: #1e2030;
  --nav-text: #c8cad8;
  --formula-bg: #f8f9fb;
  --dialog-bg: #ffffff;
  --dialog-text: #1e2030;
  --btn-bg: #ffffff;
  --btn-hover: #f0f5ff;
  --toast-bg: #1e2030;
  --menu-bg: #ffffff;
  --selected-bg: #dbeafe;
  --input-text: #333333;
  --accent-color: #3b82f6;
  --row-header-text: #6b7280;
}

[data-theme="dark"] {
  --bg-color: #1a1b26;
  --text-color: #a9b1d6;
  --header-bg: #24283b;
  --header-text: #787c99;
  --border-color: #414868;
  --corner-bg: #1f2335;
  --nav-bg: #16161e;
  --nav-text: #a9b1d6;
  --formula-bg: #1f2335;
  --dialog-bg: #24283b;
  --dialog-text: #c0caf5;
  --btn-bg: #1f2335;
  --btn-hover: #292e42;
  --toast-bg: #16161e;
  --menu-bg: #24283b;
  --selected-bg: #364a82;
  --input-text: #c0caf5;
  --accent-color: #7aa2f7;
  --row-header-text: #565f89;
}

/* -------------------------------------------------------------------------
   CONFIGURACIÓN GLOBAL Y RESET
   ------------------------------------------------------------------------- */
html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background-color: var(--bg-color);
  color: var(--text-color);
}

* {
  box-sizing: border-box;
}

body {
  font-family: 'DM Sans', sans-serif;
  box-sizing: border-box;
}

/* -------------------------------------------------------------------------
   CONTENEDOR DE LA HOJA DE CÁLCULO
   ------------------------------------------------------------------------- */
.sheet-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background-color: var(--bg-color);
}

.sheet-scroll {
  flex: 1;
  overflow: auto;
  position: relative;
  background-color: var(--bg-color);
}

/* -------------------------------------------------------------------------
   ESTILOS DE LA TABLA Y CELDAS
   ------------------------------------------------------------------------- */
table {
  border-collapse: collapse;
  table-layout: fixed;
  width: auto;
}

th,
td {
  border: 1px solid var(--border-color);
  padding: 0;
  height: 28px;
  width: 100px;
  position: relative;
}

th {
  background: var(--header-bg);
  font-size: 12px;
  font-weight: 600;
  color: var(--header-text);
  position: sticky;
  top: 0;
  z-index: 2;
  user-select: none;
  overflow: hidden;
  padding-right: 20px;
  /* Espacio para el botón de ordenación */
}

.sort-btn {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  opacity: 0.3;
  transition: opacity 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  padding: 2px;
  color: var(--header-text);
}

th:hover .sort-btn {
  opacity: 0.8;
}

.sort-btn:hover {
  opacity: 1 !important;
  color: var(--accent-color);
}

.sort-btn.active {
  opacity: 1;
  color: var(--accent-color);
}

/* Modos de ajuste de texto */
table.mode-clip td {
  overflow: hidden;
  height: 28px;
}

table.mode-clip .cell-display {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

table.mode-overflow td {
  overflow: visible;
  height: 28px;
}

table.mode-overflow .cell-display {
  white-space: nowrap;
  overflow: visible;
  z-index: 3;
}

table.mode-wrap td {
  overflow: visible;
  height: auto;
  min-height: 28px;
}

table.mode-wrap .cell-display {
  position: relative;
  white-space: normal;
  overflow: visible;
  word-break: break-word;
  height: auto;
  min-height: 28px;
  padding: 4px 6px;
  z-index: 1;
  pointer-events: none;
  /* Dejamos pasar los clics al input de abajo */
  line-height: 1.4;
}

table.mode-wrap .cell-display a {
  pointer-events: auto;
  /* Solo los enlaces capturan clics */
}

table.mode-wrap td input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  opacity: 0;
  /* Oculto por defecto para mostrar el texto con wrap */
}

table.mode-wrap td.selected input {
  z-index: 5;
  opacity: 1;
  background: var(--btn-hover);
}

.row-header {
  background: var(--header-bg);
  font-size: 12px;
  font-weight: 500;
  color: var(--row-header-text);
  width: 50px;
  min-width: 50px;
  max-width: 50px;
  text-align: center;
  position: sticky;
  left: 0;
  z-index: 1;
}

.corner {
  position: sticky;
  left: 0;
  top: 0;
  z-index: 3;
  background: var(--corner-bg);
  cursor: pointer;
  transition: background 0.1s;
}

.corner:hover {
  background: var(--border-color);
}

.corner:active {
  background: var(--accent-color);
}

/* -------------------------------------------------------------------------
   REDIMENSIONAMIENTO (RESIZE)
   ------------------------------------------------------------------------- */
.col-resizer {
  position: absolute;
  right: 0;
  top: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
}

.col-resizer:hover,
.col-resizer.resizing {
  background: var(--accent-color);
  opacity: 0.5;
}

.row-resizer {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 8px;
  cursor: row-resize;
  z-index: 10;
  background: transparent;
}

.row-resizer:hover,
.row-resizer.resizing {
  background: var(--accent-color);
  opacity: 0.5;
}

/* -------------------------------------------------------------------------
   INPUTS DENTRO DE CELDAS Y VISUALIZACIÓN
   ------------------------------------------------------------------------- */
td {
  position: relative;
}

.cell-display {
  position: absolute;
  inset: 0;
  padding: 2px 6px;
  font-size: 13px;
  font-family: 'JetBrains Mono', monospace;
  pointer-events: none;
  overflow: hidden;
  white-space: nowrap;
  display: flex;
  align-items: center;
  z-index: 2;
  color: var(--input-text);
}

.cell-display a {
  pointer-events: auto;
  color: var(--accent-color);
  text-decoration: underline;
  cursor: pointer;
  position: relative;
}

.cell-display a:hover {
  color: var(--accent-color);
  filter: brightness(1.2);
}

td input {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 2px 6px;
  font-size: 13px;
  font-family: 'JetBrains Mono', monospace;
  background: transparent;
  position: relative;
  z-index: 1;
  color: transparent;
  /* El texto del input es transparente por defecto */
}

/* Solo ocultamos el display cuando el input tiene el foco (se está editando) */
td:focus-within .cell-display {
  visibility: hidden;
}

/* El texto del input solo es visible cuando tiene el foco */
td input:focus {
  color: var(--input-text);
  background: var(--btn-hover);
  z-index: 5;
}

td.selected {
  background: var(--selected-bg) !important;
  outline: 2px solid var(--accent-color);
  outline-offset: -1px;
  z-index: 5;
  position: relative;
}

td.dependency-highlight {
  background: rgba(59, 130, 246, 0.1) !important;
  outline: 2px dashed var(--accent-color);
  outline-offset: -2px;
  z-index: 4;
}

td.cell-color {
  background: var(--cell-bg, transparent) !important;
}

th input {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: 600;
  background: transparent;
  text-align: center;
  font-family: 'DM Sans', sans-serif;
  color: var(--header-text);
}

/* -------------------------------------------------------------------------
   BARRA DE NAVEGACIÓN (NAVBAR)
   ------------------------------------------------------------------------- */
.navbar {
  background: var(--nav-bg);
  color: var(--nav-text);
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
}

.navbar h1 {
  font-size: 15px;
  font-weight: 700;
  color: var(--nav-text);
  letter-spacing: -0.3px;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--nav-text);
  transition: all .15s;
  opacity: 0.8;
}

.nav-btn:hover {
  background: var(--btn-hover);
  color: var(--dialog-text);
  opacity: 1;
}

.nav-btn i,
.nav-btn svg {
  pointer-events: none;
}

.nav-btn.danger:hover {
  background: #3b1520;
  color: #f87171;
}

/* -------------------------------------------------------------------------
   BARRA DE FÓRMULAS
   ------------------------------------------------------------------------- */
.formula-bar {
  background: var(--formula-bg);
  border-bottom: 1px solid var(--border-color);
  height: 30px;
  display: flex;
  align-items: center;
  padding: 0 8px;
  flex-shrink: 0;
  font-size: 12px;
  color: var(--header-text);
  gap: 8px;
}

.formula-bar .cell-ref {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  color: var(--accent-color);
  min-width: 40px;
}

.formula-bar input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  color: var(--input-text);
}

/* -------------------------------------------------------------------------
   DIÁLOGOS Y OVERLAYS
   ------------------------------------------------------------------------- */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}

.dialog-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.dialog {
  background: var(--dialog-bg);
  border-radius: 12px;
  padding: 28px;
  max-width: 460px;
  width: 90%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .2);
  color: var(--dialog-text);
}

.dialog h2 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--dialog-text);
}

.dialog p {
  font-size: 14px;
  color: var(--text-color);
  line-height: 1.6;
  margin-bottom: 12px;
}

/* -------------------------------------------------------------------------
   BOTONES DE EXPORTACIÓN
   ------------------------------------------------------------------------- */
.export-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--btn-bg);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-color);
  transition: all .15s;
  margin-bottom: 8px;
}

.export-btn:hover {
  background: var(--btn-hover);
  border-color: var(--accent-color);
}

.export-btn .ext {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  background: var(--btn-hover);
  color: var(--accent-color);
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 600;
}

.close-btn {
  margin-top: 12px;
  width: 100%;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--btn-bg);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-color);
}

.close-btn:hover {
  background: var(--btn-hover);
}

/* -------------------------------------------------------------------------
   TOAST NOTIFICACIONES
   ------------------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--nav-text);
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
}

/* -------------------------------------------------------------------------
   MENÚ CONTEXTUAL (CLIC DERECHO)
   ------------------------------------------------------------------------- */
.context-menu {
  position: fixed;
  background: var(--menu-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 2px 0;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .15);
  font-size: 12px;
  min-width: 130px;
  /* Reducido de 160px a 130px */
}

.context-menu button {
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 4px 8px;
  /* Reducido de 12px a 8px */
  cursor: pointer;
  color: var(--text-color);
  transition: background .1s;
  white-space: nowrap;
  /* Evitar saltos de línea */
}

.context-menu button:hover {
  background: var(--btn-hover);
  color: var(--accent-color);
}

.context-menu hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 2px 0;
}

.menu-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 4px 8px 2px;
  /* Reducido padding horizontal */
  opacity: 0.6;
  font-weight: 600;
  color: var(--text-color);
}

.menu-color-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 4px 8px;
  /* Reducido padding horizontal */
}

.menu-color-circle {
  width: 16px;
  height: 18px;
  border-radius: 4px;
  /* Cambiado a rectángulos redondeados para ahorrar espacio vertical */
  cursor: pointer;
  border: 1px solid var(--border-color);
  transition: transform 0.1s, border-color 0.1s;
}

.menu-color-circle:hover {
  transform: scale(1.2);
  border-color: var(--accent-color);
}

/* -------------------------------------------------------------------------
   PALETA DE COLORES
   ------------------------------------------------------------------------- */
.color-swatch {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
  border: 2px solid var(--border-color);
  transition: all .15s;
}

.color-swatch:hover {
  border-color: var(--accent-color);
  transform: scale(1.05);
}

/* -------------------------------------------------------------------------
   BARRA DE ESTADO (STATUS BAR)
   ------------------------------------------------------------------------- */
.status-bar {
  background: var(--nav-bg);
  color: var(--nav-text);
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 16px;
  gap: 20px;
  font-size: 12px;
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.status-item {
  display: flex;
  gap: 6px;
  align-items: center;
}

.status-item .label {
  opacity: 0.7;
  font-weight: 500;
}

.status-item span:not(.label) {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  color: var(--accent-color);
}

/* -------------------------------------------------------------------------
   CUSTOM TOOLTIPS
   ------------------------------------------------------------------------- */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--nav-bg);
  color: var(--nav-text);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: all 0.15s ease;
  z-index: 2000;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border-color);
}

[data-tooltip]:hover::after {
  opacity: 1;
  transform: translateX(-50%) translateY(4px);
}

/* Flecha del tooltip */
[data-tooltip]::before {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(4px);
  border: 4px solid transparent;
  border-bottom-color: var(--border-color);
  opacity: 0;
  pointer-events: none;
  transition: all 0.15s ease;
  z-index: 2000;
}

[data-tooltip]:hover::before {
  opacity: 1;
  transform: translateX(-50%) translateY(0px);
}

/* -------------------------------------------------------------------------
   ENLACES DE APOYO / SOPORTE
   ------------------------------------------------------------------------- */
.support-link {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--btn-bg);
  cursor: pointer;
  transition: all .15s;
  text-decoration: none;
  color: inherit;
}

.support-link:hover {
  border-color: var(--accent-color);
  background: var(--btn-hover);
}

.support-link .name {
  font-weight: 600;
  font-size: 13px;
  color: var(--dialog-text);
}

.support-link .desc {
  font-size: 12px;
  color: var(--row-header-text);
}
