/* Tema oscuro base */
:root {
  --bg: #0f1115;
  --surface: #141824;
  --surface-2: #0d1017;
  --surface-3: #161b27;
  --surface-4: #1d2334;
  --line: #272f3f;
  --text: #e8ecf3;
  --muted: #aab1c2;
  --accent: #1dd87e;
  --accent-800: #145933;

  --headA: 56px;
  --headB: clamp(40px, 5.5vw, 48px);
  --row-height: 64px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: radial-gradient(circle at top, #141a2a 0%, var(--bg) 55%);
  color: var(--text);
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow-x: hidden;
  overflow-y: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(15, 18, 27, 0.92);
  backdrop-filter: blur(20px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(18px, 2vw, 22px);
}

.pill-info {
  background: #1a2233;
  border: 1px solid var(--line);
  padding: 6px 14px;
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.02em;
}

.container {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  max-width: 1440px;
  margin: 0 auto;
  padding: 32px 20px 60px;
  gap: 24px;
  min-height: 0;
}

.matrix-section {
  display: grid;
  gap: 18px;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  min-height: 0;
}

.section-head h2 {
  margin: 0;
  font-size: clamp(20px, 3vw, 26px);
}

.section-head p {
  margin: 6px 0 0;
  color: var(--muted);
}

.matrix-nav {
  display: flex;
  gap: 12px;
  justify-content: flex-start;
  padding: 4px 0 8px;
  flex-wrap: wrap;
}

.nav-dot {
  min-width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: var(--surface-3);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease, background 0.2s ease;
  padding: 0;
}

.nav-dot-1,
.nav-dot-2,
.nav-dot-all,
.nav-dot-sv {
  width: 48px;
}

.nav-dot-4 {
  background: linear-gradient(135deg, #1f2332 0%, #080a11 100%);
  color: var(--text);
  border-radius: 999px;
  padding: 0 20px;
  font-size: 16px;
  gap: 8px;
}

.nav-dot:hover {
  transform: translateY(-2px);
  border-color: rgba(29, 216, 126, 0.4);
}

.nav-dot[aria-current="true"] {
  box-shadow: 0 0 0 3px rgba(29, 216, 126, 0.35);
  border-color: rgba(29, 216, 126, 0.75);
  background: var(--surface-4);
  color: var(--accent);
}

.nav-icon {
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.nav-icon svg {
  width: 100%;
  height: 100%;
  fill: currentColor;
}

.nav-label {
  pointer-events: none;
}

.nav-dot-sv {
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.matrix-wrap {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: 0 24px 40px rgba(0, 0, 0, 0.4);
  padding: 20px 22px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow: hidden;
}

.matrix-canvas {
  display: flex;
  gap: 18px;
  align-items: stretch;
  width: 100%;
  height: clamp(420px, 65vh, calc(var(--headA) + var(--headB) + var(--row-height) * 5));
  overflow-x: auto;
  overflow-y: auto;
  scroll-snap-type: x proximity;
  padding-bottom: 16px;
  scrollbar-gutter: stable both-edges;
}

.matrix-canvas::-webkit-scrollbar {
  height: 10px;
}

.matrix-canvas::-webkit-scrollbar-thumb {
  background: #1d2536;
  border-radius: 999px;
}

/* NUEVO: estructura de grid para headers (headB) + body */
.pane,
.matrix-col,
.matrix-panel {
  display: grid;
  grid-template-rows: var(--headB) 1fr;
  min-height: 0;
}

.pane {
  flex: 0 0 auto;
  position: relative;
  background: var(--surface-3);
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  box-shadow: 0 18px 32px rgba(0, 0, 0, 0.42);
  scroll-snap-align: start;
  min-width: 260px;
}

.pane-accounts {
  min-width: 280px; 
  position: sticky;
  left: 0;
  z-index: 3;
}

.pane-config,
.pane-todos {
  min-width: 260px;
}

.pane-services {
  min-width: 720px;
}

.pane-headA {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-3);
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.pane-headA-inner {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.pane-headA h3 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.pane-accounts .pane-headB {
  gap: 16px;
}

.pane-accounts .pane-headB .search {
  flex: 1 1 auto;
}

.pane-accounts .pane-pin {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 10px solid rgba(29, 216, 126, 0.45);
  background: radial-gradient(circle at 35% 30%, rgba(29, 216, 126, 0.35), rgba(22, 32, 45, 0.6));
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  color: var(--accent);
  cursor: default;
  box-shadow: 0 6px 16px rgba(29, 216, 126, 0.18), inset 0 0 0 1px rgba(3, 17, 11, 0.65);
  border: none;
  appearance: none;
  overflow: visible;
}

.pane-accounts .pane-pin::before {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  border: 10px solid rgba(29, 216, 126, 0.45);
  pointer-events: none;
}

.pane-accounts .pane-pin svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.pane-headB {
  position: sticky;
  top: var(--headA);
  z-index: 1;
  background: var(--surface-4);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  height: var(--headB);
  min-height: var(--headB);
  max-height: var(--headB);
  width: 100%;
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  box-sizing: border-box;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.pane-headB * {
  margin: 0;
}

.pane-headB.pane-headB-config,
.pane-headB.pane-headB-todos,
.pane-headB.pane-headB-services {
  height: var(--headB);
  min-height: var(--headB);
  max-height: var(--headB);
}

.pane-headB span {
  white-space: nowrap;
}

.pane-headB-config {
  display: grid;
  grid-template-columns: 1fr repeat(2, minmax(0, auto));
  gap: 16px;
  align-items: center;
}

.pane-headB-config span:first-child {
  text-align: left;
}

.pane-headB-config span:nth-child(n + 2) {
  text-align: center;
}

.pane-headB-todos {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  justify-items: center;
  align-items: center;
}

/* PATCH: el head de servicios ahora envuelve un grid interno */
.pane-headB-services {
  display: flex;
  align-items: center;
}

.pane-headB-services span:first-child {
  color: var(--text);
}

.pane-body,
.matrix-body,
.panel-body {
  overflow: auto;
  min-height: 0;
  scrollbar-gutter: stable both-edges;
}

.pane-body {
  padding: 12px 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: linear-gradient(180deg, rgba(29, 35, 54, 0.65) 0%, rgba(18, 22, 34, 0.9) 100%);
}

.pane-headB input,
.pane-headB .search,
.pane-headB .input,
.pane-headB .textbox {
  height: calc(var(--headB) - 14px);
  line-height: calc(var(--headB) - 14px);
  padding: 0 12px;
  box-sizing: border-box;
}

.pane-body::-webkit-scrollbar {
  width: 10px;
}

.pane-body::-webkit-scrollbar-thumb {
  background: #1d2536;
  border-radius: 999px;
}

.pane-rows {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pane-row {
  min-height: var(--row-height);
  border-radius: 18px;
  border: 1px solid var(--line);
  background: var(--surface-4);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.account-row {
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.account-number {
  font-weight: 600;
  font-size: 16px;
}

.account-alias {
  font-size: 13px;
  color: var(--muted);
}

.pane-config .pane-row {
  display: grid;
  grid-template-columns: 1fr repeat(2, auto);
  align-items: center;
  gap: 16px;
}

.pane-config .config-cell {
  display: flex;
  align-items: center;
  justify-content: center;
}

.pane-config .config-cell:first-child {
  justify-content: flex-start;
  width: 100%;
}

.pane-todos .pane-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  justify-items: center;
}

/* Servicios individuales por panel */
.pane-services {
  --sv-columns: 1;
  min-width: calc(160px * var(--sv-columns));
}

.pane-services[data-sv-count="2"] {
  --sv-columns: 2;
}

.pane-services[data-sv-count="3"] {
  --sv-columns: 3;
}

.pane-services[data-sv-count="4"] {
  --sv-columns: 4;
}

.pane-services[data-sv-count="7"] {
  --sv-columns: 7;
}

.pane-services .pane-row {
  width: 100%;
}

.pane-services .pane-body {
  min-height: 0;
  overflow: auto;
  scrollbar-gutter: stable both-edges;
}

.pane-services .sv-grid {
  display: grid;
  grid-template-columns: repeat(var(--sv-columns, 1), minmax(0, 1fr));
  gap: 16px;
  align-items: center;
  width: 100%;
  flex: 1 1 auto;
}

.pane-services .sv-grid .cell-toggle {
  justify-content: center;
}

.pane-headB-services .sv-grid {
  display: grid;
  grid-template-columns: repeat(var(--sv-columns, 1), minmax(0, 1fr));
  gap: 16px;
  align-items: center;
  width: 100%;
  flex: 1 1 auto;
}

.pane-headB-services .sv-grid span {
  display: block;
  text-align: center;
}

.input.search,
.input.select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #1b2334;
  color: var(--text);
  font-size: 14px;
}

.input.search::placeholder {
  color: #76819b;
}

.input.select {
  padding-right: 38px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='8' viewBox='0 0 14 8'%3E%3Cpath fill='%23aab1c2' d='M7 8a.99.99 0 0 1-.7-.29l-6-6A1 1 0 0 1 1.41.3L7 5.59 12.59.3A1 1 0 1 1 13.7 1.7l-6 6A1 1 0 0 1 7 8'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 14px;
}

.cell-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell-toggle input {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  border: 1px solid #2c3447;
  background: #1a2232;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  appearance: none;
  color: transparent;
  font-weight: 700;
  transition: all 0.25s ease;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.cell-toggle input::after {
  content: "";
  font-size: 18px;
  line-height: 1;
}

.cell-toggle input:checked {
  background: linear-gradient(135deg, var(--accent) 0%, #0f8f4f 100%);
  border-color: transparent;
  box-shadow: 0 0 0 2px rgba(29, 216, 126, 0.25);
}

.cell-toggle input:checked::after {
  content: "✓";
  color: #eafff3;
}

.cell-toggle input:indeterminate {
  background: #374057;
  box-shadow: 0 0 0 2px rgba(170, 177, 194, 0.25);
}

.cell-toggle input:indeterminate::after {
  content: "–";
  color: #e8ecf3;
  font-size: 18px;
  line-height: 1;
}

.cell-toggle input:focus-visible {
  outline: 3px solid rgba(29, 216, 126, 0.6);
  outline-offset: 2px;
}

.btn-secondary {
  background: #1a2233;
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  color: var(--text);
  border-color: #3a4763;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.25);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent) 0%, #0fb86a 100%);
  color: #042312;
  border: none;
  padding: 12px 20px;
  border-radius: 14px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 22px rgba(0, 0, 0, 0.35);
}

.matrix-controls {
  display: flex;
  gap: 12px;
  justify-content: flex-start;
  padding: 8px 22px 0;
}

.load-more {
  background: #1a2233;
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.load-more:hover {
  color: var(--text);
  border-color: #3a4763;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.25);
}

.footer {
  padding: 24px 20px 40px;
  text-align: center;
  color: var(--muted);
}

.clone-dialog {
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface-3);
  color: var(--text);
  padding: 0;
  width: min(520px, 90vw);
}

.clone-dialog::backdrop {
  background: rgba(8, 10, 16, 0.65);
  backdrop-filter: blur(3px);
}

.clone-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 26px 28px 24px;
}

.clone-header {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.clone-header h2 {
  margin: 0;
  font-size: 20px;
}

.clone-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.clone-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.clone-field label,
.clone-fieldset legend {
  font-size: 13px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.clone-fieldset {
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.clone-targets {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 220px;
  overflow-y: auto;
  padding-right: 4px;
}

.clone-target {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface-4);
  border: 1px solid transparent;
}

.clone-target input {
  width: 18px;
  height: 18px;
}

.clone-target input:disabled + span {
  color: var(--muted);
  opacity: 0.6;
}

.clone-targets::-webkit-scrollbar {
  width: 8px;
}

.clone-targets::-webkit-scrollbar-thumb {
  background: #1d2536;
  border-radius: 999px;
}

.clone-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* Vistas */
.matrix-wrap.view-config .matrix-canvas > .pane,
.matrix-wrap.view-todos .matrix-canvas > .pane,
.matrix-wrap.view-sv-consultas .matrix-canvas > .pane,
.matrix-wrap.view-sv-pagos .matrix-canvas > .pane,
.matrix-wrap.view-sv-cobros .matrix-canvas > .pane,
.matrix-wrap.view-sv-otros .matrix-canvas > .pane {
  display: none;
}

.matrix-wrap.view-config .matrix-canvas > .pane-accounts,
.matrix-wrap.view-config .matrix-canvas > .pane-config,
.matrix-wrap.view-todos .matrix-canvas > .pane-accounts,
.matrix-wrap.view-todos .matrix-canvas > .pane-todos,
.matrix-wrap.view-sv-consultas .matrix-canvas > .pane-accounts,
.matrix-wrap.view-sv-consultas .matrix-canvas > .pane-services--consultas,
.matrix-wrap.view-sv-pagos .matrix-canvas > .pane-accounts,
.matrix-wrap.view-sv-pagos .matrix-canvas > .pane-services--pagos,
.matrix-wrap.view-sv-cobros .matrix-canvas > .pane-accounts,
.matrix-wrap.view-sv-cobros .matrix-canvas > .pane-services--cobros,
.matrix-wrap.view-sv-otros .matrix-canvas > .pane-accounts,
.matrix-wrap.view-sv-otros .matrix-canvas > .pane-services--otros {
  display: grid;
}

@media (max-width: 1100px) {
  .container {
    padding: 24px 12px 48px;
  }

  .pane-services {
    min-width: clamp(320px, calc(140px * var(--sv-columns)), 640px);
  }
}

@media (max-width: 900px) {
  .matrix-nav {
    justify-content: center;
  }

  .pane-headA h3 {
    font-size: 15px;
  }
}

@media (max-width: 768px) {
  .matrix-wrap {
    padding: 16px;
    border-radius: 18px;
  }

  .matrix-canvas {
    height: min(520px, 70vh);
  }

  .pane {
    min-width: 220px;
    border-radius: 20px;
  }

  .pane-accounts {
    min-width: 240px;
  }

  .pane-config,
  .pane-todos {
    min-width: 220px;
  }

  .pane-services {
    min-width: clamp(300px, calc(130px * var(--sv-columns)), 520px);
  }

  .pane-headB-services {
    grid-auto-columns: minmax(140px, 1fr);
  }
}

@media (max-width: 640px) {
  .matrix-nav {
    gap: 10px;
    justify-content: center;
  }

  .nav-dot-4 {
    min-width: 0;
    padding: 0 16px;
  }

  .pane-services {
    min-width: clamp(280px, calc(120px * var(--sv-columns)), 440px);
  }

  .pane-headB-services {
    grid-auto-columns: minmax(120px, 1fr);
  }
}

@media (max-width: 520px) {
  .container {
    padding: 20px 12px 40px;
  }

  .matrix-wrap {
    padding: 14px;
  }

  .matrix-canvas {
    height: min(460px, 72vh);
  }

  .pane {
    min-width: 200px;
    border-radius: 16px;
  }

  .pane-accounts {
    min-width: 210px;
  }

  .pane-config,
  .pane-todos {
    min-width: 200px;
  }

  .pane-services {
    min-width: clamp(260px, calc(110px * var(--sv-columns)), 360px);
  }

  .pane-headB,
  .pane-headA {
    padding-left: 16px;
    padding-right: 16px;
  }
}
