:root {
  --bg-a: #3e4637;
  --bg-b: #222820;
  --panel: #4c5844;
  --panel-dark: #31382d;
  --ink: #d8ded3;
  --muted: #aeb8a5;
  --accent: #c4b550;
  --accent-2: #a7b56d;
  --danger: #e27b64;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  min-height: 100vh;
  margin: 14px;
  background:
    radial-gradient(circle at top left, rgba(196, 181, 80, 0.14), transparent 28%),
    radial-gradient(circle at bottom right, rgba(118, 159, 92, 0.10), transparent 34%),
    linear-gradient(180deg, var(--bg-a), var(--bg-b));
  background-attachment: fixed;
  color: var(--ink);
}
body::after {
  content: "";
  pointer-events: none;
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(to bottom, rgba(255,255,255,0.025), rgba(255,255,255,0.025) 1px, transparent 1px, transparent 4px);
  mix-blend-mode: screen;
  opacity: .28;
}
main { max-width: 1360px; margin: 0 auto; }
.window, .box { box-shadow: 0 10px 22px rgba(0,0,0,.20); }
.window:not(.headless)::before, .titlebar, legend {
  text-transform: uppercase;
  letter-spacing: 2px;
  font-size: 12px;
  color: white;
  font-weight: bold;
}
.app-shell { max-width: none !important; width: 100%; }
.app-shell .box { max-width: none; }
.tab-panel > .box,
.contacts-panel { width: 100%; max-width: none; }
h1 { font-size: clamp(22px, 3vw, 34px); margin: 0 0 8px; }
h2 { color: #f2f5ee; margin-top: 0; }
p, label, td, th, button, input, select, textarea { font-size: 13px; }
button, input, select, textarea { max-width: 100%; }
textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.lead { max-width: 960px; line-height: 1.55; }
.lead.mini { font-size: 12px; color: var(--muted); }
.eyebrow { margin-bottom: 10px; color: #f2f5ee; text-transform: uppercase; letter-spacing: 2px; font-size: 12px; font-weight: bold; }
.intro-block { margin-bottom: 18px; padding-bottom: 12px; border-bottom: 1px solid rgba(137, 146, 129, .35); }
.window-row, .stats-row { display: flex; gap: 12px; flex-wrap: wrap; }
.stat-box { flex: 1; min-height: 78px; margin-bottom: 0; min-width: 160px; }
.stat-box.wide { flex: 1.8; }
.stat-label { display: block; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; font-size: 11px; }
.stat-box strong, .record-name, .pill, code { color: var(--accent); }
.stat-box strong { display: block; font-size: 26px; line-height: 1.1; }
.stat-box.wide strong { font-size: 20px; }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.tab { min-width: 140px; }
.tab.active, .primary-button { color: #111; background: var(--accent); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.layout.two-col { display: grid; grid-template-columns: minmax(340px, 0.9fr) minmax(420px, 1.1fr); gap: 18px; align-items: start; }
.grid { display: grid; gap: 12px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid .span-2 { grid-column: span 2; }
.grid .span-3 { grid-column: span 3; }
.small-gap { gap: 8px; }
.panel-toolbar, .button-row, .list-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.panel-toolbar, .list-head { justify-content: space-between; margin-bottom: 10px; }
.pill { display: inline-flex; align-items: center; min-height: 28px; padding: 5px 9px; border: 1px solid #899281; background: #31382d; box-shadow: inset 1px 1px #1f241d; font-weight: bold; }
fieldset { border: 1px solid #899281; padding: 12px; margin-bottom: 14px; }
label { display: grid; gap: 5px; color: var(--muted); text-transform: uppercase; letter-spacing: .7px; }
.check-line { display: flex; align-items: center; gap: 8px; text-transform: none; letter-spacing: normal; color: var(--ink); }
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 680px; }
th, td { padding: 10px; border-bottom: 1px solid rgba(137,146,129,.25); vertical-align: top; }
th { text-align: left; color: #f2f5ee; text-transform: uppercase; letter-spacing: 1px; font-size: 11px; }
.muted, .mono-small { color: var(--muted); }
.mono-small { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.empty-state { padding: 22px; color: var(--muted); text-align: center; }
.hidden { display: none !important; }
.actions-cell { white-space: nowrap; }
button.danger { color: var(--danger); }
.form-card { max-height: none; }
.meter-wrap { margin: 14px 0; }
.meter { height: 18px; border: 1px solid #899281; background: #242a22; box-shadow: inset 1px 1px #111; overflow: hidden; }
.meter span { display: block; height: 100%; width: 0%; background: linear-gradient(90deg, #70824e, var(--accent)); transition: width .2s ease; }
.meter span.over { background: linear-gradient(90deg, #7b372d, var(--danger)); }
.report { margin-top: 12px; padding: 10px; border: 1px solid rgba(137,146,129,.45); background: rgba(0,0,0,.14); min-height: 42px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; color: var(--muted); }
.json-box { min-height: 260px; max-height: 520px; overflow: auto; padding: 12px; border: 1px solid #899281; background: #1d231c; color: #dbe6d1; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; }
.json-box.small { min-height: 560px; }
#tokenOutput, #decodeInput { min-height: 110px; }
@media (max-width: 980px) {
  .layout.two-col { grid-template-columns: 1fr; }
  .grid.two, .grid.three { grid-template-columns: 1fr; }
  .grid .span-2, .grid .span-3 { grid-column: auto; }
  table { min-width: 620px; }
}

/* Ajustes solicitados: título único, modal de registro y móvil */
.window:not(.headless)::before,
.titlebar {
  white-space: normal;
  line-height: 1.35;
}

.contacts-panel {
  width: 100%;
}

.contacts-actions .search-box {
  flex: 1 1 260px;
}

.contacts-actions .toolbar-actions {
  justify-content: flex-end;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(11, 14, 10, .68);
  backdrop-filter: blur(2px);
}

.modal-backdrop.hidden {
  display: none !important;
}

.modal-window {
  width: min(1320px, calc(100vw - 28px));
  max-width: none !important;
  max-height: calc(100dvh - 28px);
  overflow: hidden;
}

.modal-window .form-card {
  max-height: calc(100dvh - 98px);
  overflow: auto;
  padding-bottom: 16px;
}

.modal-toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 8px 0 10px;
  background: linear-gradient(180deg, var(--panel), rgba(76, 88, 68, .95));
  border-bottom: 1px solid rgba(137, 146, 129, .35);
}

body.modal-open {
  overflow: hidden;
}


/* Aprovecha mejor el ancho disponible en escritorio */
@media (min-width: 981px) {
  body {
    margin-left: 6px;
    margin-right: 6px;
  }

  main {
    width: 100%;
    max-width: none;
  }

  .app-shell {
    max-width: none !important;
  }

  .contacts-actions {
    display: grid;
    grid-template-columns: minmax(360px, 1fr) auto;
    align-items: end;
  }

  .toolbar-actions {
    justify-content: flex-end;
  }

  .contacts-table {
    min-width: 0;
  }

  .contacts-table th:nth-child(1),
  .contacts-table td:nth-child(1) { width: 32%; }
  .contacts-table th:nth-child(2),
  .contacts-table td:nth-child(2) { width: 16%; }
  .contacts-table th:nth-child(3),
  .contacts-table td:nth-child(3) { width: 26%; }
  .contacts-table th:nth-child(4),
  .contacts-table td:nth-child(4) { width: 26%; }

  .actions-cell {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
  }

  .modal-window .grid.two {
    grid-template-columns: repeat(2, minmax(280px, 1fr));
  }

  .modal-window .grid.three {
    grid-template-columns: repeat(3, minmax(220px, 1fr));
  }
}

/* El click fuera del modal no cierra: solo da una pequeña pista visual */
.modal-window.modal-attention {
  animation: modalNudge .14s linear 0s 2;
}

@keyframes modalNudge {
  0% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
  100% { transform: translateX(0); }
}

@media (max-width: 720px) {
  body {
    margin: 6px;
  }

  main,
  .app-shell {
    width: 100%;
    max-width: none !important;
  }

  .app-shell {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  .intro-block {
    margin-bottom: 10px;
    padding-bottom: 8px;
  }

  h1 {
    font-size: 20px;
    line-height: 1.2;
  }

  .lead {
    font-size: 12px;
    line-height: 1.45;
  }

  .stats-row {
    gap: 8px;
  }

  .stat-box,
  .stat-box.wide {
    min-width: 0;
    flex: 1 1 100%;
    min-height: auto;
    padding: 10px !important;
  }

  .stat-label {
    margin-bottom: 4px;
  }

  .stat-box strong,
  .stat-box.wide strong {
    font-size: 18px;
    overflow-wrap: anywhere;
  }

  .tabs {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .tab,
  button {
    width: 100%;
    min-width: 0;
    min-height: 40px;
  }

  .contacts-actions,
  .panel-toolbar,
  .button-row,
  .list-head,
  .primary-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .contacts-actions .search-box,
  .contacts-actions .toolbar-actions {
    width: 100%;
  }

  .contacts-table,
  .contacts-table thead,
  .contacts-table tbody,
  .contacts-table tr,
  .contacts-table td {
    display: block;
    width: 100%;
  }

  .contacts-table {
    min-width: 0;
  }

  .contacts-table thead {
    display: none;
  }

  .contacts-table tr {
    margin-bottom: 10px;
    padding: 10px;
    border: 1px solid rgba(137,146,129,.45);
    background: rgba(0,0,0,.10);
  }

  .contacts-table td {
    padding: 5px 0;
    border: 0;
  }

  .actions-cell {
    display: grid !important;
    grid-template-columns: 1fr;
    gap: 6px;
    white-space: normal;
  }

  fieldset {
    padding: 10px 8px;
  }

  input,
  select,
  textarea {
    width: 100%;
    min-height: 38px;
    font-size: 16px; /* evita zoom automático en iOS */
  }

  textarea {
    min-height: 96px;
  }

  .modal-backdrop {
    align-items: stretch;
    padding: 6px;
  }

  .modal-window {
    width: 100%;
    max-height: calc(100dvh - 12px);
  }

  .modal-window .form-card {
    max-height: calc(100dvh - 78px);
    padding-left: 8px !important;
    padding-right: 8px !important;
  }

  .modal-toolbar {
    gap: 8px;
  }

  .modal-toolbar .toolbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .modal-toolbar .toolbar-actions button {
    width: 100%;
  }

  .json-box.small {
    min-height: 280px;
  }

  .json-box {
    max-height: 420px;
    font-size: 12px;
  }
}

@media (max-width: 390px) {
  .modal-toolbar .toolbar-actions {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .contacts-table td::before {
    display: block;
    margin-bottom: 3px;
    color: var(--muted);
    font-size: 10px;
    letter-spacing: 1px;
    text-transform: uppercase;
  }
  .contacts-table td:nth-child(1)::before { content: "Destinatario"; }
  .contacts-table td:nth-child(2)::before { content: "Documento"; }
  .contacts-table td:nth-child(3)::before { content: "Contacto"; }
  .contacts-table td:nth-child(4)::before { content: "Acciones"; }
}

/* Fix solicitado: no tocar el ancho del bloque madre, solo ajustar los bloques internos */
body {
  margin: 14px;
}

main {
  width: auto;
  max-width: 1360px;
  margin: 0 auto;
}

.app-shell {
  width: 100%;
  max-width: 1360px !important;
}

/* Quitar botones decorativos de ventana (_ y X) del tema original */
.window:not(.headless)::before,
.titlebar {
  background-image: none !important;
  background-position: initial !important;
  background-repeat: no-repeat !important;
}

@media (min-width: 981px) {
  body {
    margin-left: 14px;
    margin-right: 14px;
  }

  main {
    width: auto;
    max-width: 1360px;
  }

  .app-shell {
    width: 100%;
    max-width: 1360px !important;
  }

  .stats-row {
    display: grid;
    grid-template-columns: minmax(160px, .75fr) minmax(160px, .75fr) minmax(260px, 1.5fr);
    width: 100%;
  }

  .stat-box,
  .stat-box.wide {
    min-width: 0;
    width: 100%;
  }

  .contacts-panel {
    width: 100%;
    max-width: none;
  }

  .contacts-actions {
    display: grid;
    grid-template-columns: minmax(280px, 1fr) auto;
    align-items: end;
    width: 100%;
  }

  .contacts-actions .search-box,
  .contacts-actions .search-box input {
    width: 100%;
  }

  .contacts-table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  .contacts-table th:nth-child(1),
  .contacts-table td:nth-child(1) { width: 30%; }
  .contacts-table th:nth-child(2),
  .contacts-table td:nth-child(2) { width: 18%; }
  .contacts-table th:nth-child(3),
  .contacts-table td:nth-child(3) { width: 26%; }
  .contacts-table th:nth-child(4),
  .contacts-table td:nth-child(4) { width: 26%; }
}

@media (max-width: 720px) {
  main,
  .app-shell {
    width: 100%;
    max-width: none !important;
  }

  .window:not(.headless)::before,
  .titlebar {
    height: auto;
    min-height: 18px;
    text-indent: 4px;
  }
}

/* Fix columnas CRUD: distribución más equilibrada sin tocar el ancho madre */
@media (min-width: 981px) {
  .contacts-table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  .contacts-table th,
  .contacts-table td {
    padding-left: 10px;
    padding-right: 10px;
  }

  .contacts-table th:nth-child(1),
  .contacts-table td:nth-child(1) { width: 34%; }
  .contacts-table th:nth-child(2),
  .contacts-table td:nth-child(2) { width: 16%; }
  .contacts-table th:nth-child(3),
  .contacts-table td:nth-child(3) { width: 32%; }
  .contacts-table th:nth-child(4),
  .contacts-table td:nth-child(4) { width: 18%; }

  .record-name,
  .record-doc,
  .record-contact {
    overflow-wrap: anywhere;
    word-break: normal;
  }

  .actions-cell {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    white-space: nowrap;
  }

  .actions-cell button {
    width: auto !important;
    min-width: 74px;
    min-height: 28px;
    padding-left: 9px;
    padding-right: 9px;
  }
}

@media (min-width: 981px) and (max-width: 1120px) {
  .contacts-table th:nth-child(1),
  .contacts-table td:nth-child(1) { width: 30%; }
  .contacts-table th:nth-child(2),
  .contacts-table td:nth-child(2) { width: 16%; }
  .contacts-table th:nth-child(3),
  .contacts-table td:nth-child(3) { width: 28%; }
  .contacts-table th:nth-child(4),
  .contacts-table td:nth-child(4) { width: 26%; }
}


/* Fix línea rota en acciones: el <td> debe seguir siendo table-cell.
   La línea de la fila va en el <tr>, no en cada celda por separado. */
@media (min-width: 721px) {
  .contacts-table {
    border-collapse: collapse;
  }

  .contacts-table tbody tr {
    border-bottom: 1px solid rgba(137,146,129,.25);
  }

  .contacts-table th,
  .contacts-table td {
    border-bottom: 0 !important;
  }

  .contacts-table .actions-cell {
    display: table-cell !important;
    vertical-align: middle;
    white-space: nowrap;
  }

  .contacts-table .row-actions {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
  }

  .contacts-table .row-actions button {
    width: auto !important;
    min-width: 74px;
    min-height: 28px;
    padding-left: 9px;
    padding-right: 9px;
  }
}

@media (max-width: 720px) {
  .contacts-table .row-actions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
  }
}

/* Restaurar separador superior de la fila de datos */
@media (min-width: 721px) {
  .contacts-table thead tr {
    border-bottom: 1px solid rgba(137,146,129,.25);
  }
}

/* Ajuste solicitado: texto de botones en negrita */
button,
.button-row button,
.tab,
.primary-button,
.row-actions button {
  font-weight: 700 !important;
}

/* Modal PIN + estado de lectura NFC */
.pin-window {
  width: min(560px, calc(100vw - 28px));
}

.pin-card {
  max-height: none !important;
}

.mini-report {
  min-height: auto;
  margin: 10px 0;
  font-size: 12px;
}

#pinModalInput {
  font-size: 18px;
}

@media (max-width: 720px) {
  .pin-window {
    width: 100%;
  }
}

/* Modal de operación NFC con feedback claro */
.nfc-window {
  width: min(520px, calc(100vw - 28px));
}

.nfc-card {
  max-height: none !important;
  text-align: center;
  overflow: hidden !important;
}

.nfc-message {
  margin: 12px 0 8px;
  font-size: 16px;
}

.nfc-visual {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 150px;
  margin: 8px auto 12px;
}

.nfc-phone {
  position: relative;
  width: 86px;
  height: 132px;
  border: 2px solid rgba(217, 255, 181, .78);
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(14, 20, 12, .95), rgba(40, 58, 32, .75));
  box-shadow: 0 0 18px rgba(185, 255, 129, .20), inset 0 0 12px rgba(0,0,0,.55);
}

.nfc-chip {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 24px;
  height: 24px;
  transform: translate(-50%, -50%);
  border: 2px solid rgba(217, 255, 181, .85);
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(185, 255, 129, .28);
}

.nfc-chip::before,
.nfc-chip::after {
  content: "";
  position: absolute;
  background: rgba(217, 255, 181, .85);
}

.nfc-chip::before {
  left: -8px;
  right: -8px;
  top: 50%;
  height: 2px;
  transform: translateY(-50%);
}

.nfc-chip::after {
  top: -8px;
  bottom: -8px;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
}

.nfc-wave {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 66px;
  height: 66px;
  transform: translate(-50%, -50%) scale(.4);
  border: 2px solid rgba(217, 255, 181, .55);
  border-radius: 50%;
  opacity: 0;
}

.nfc-visual.waiting .nfc-wave,
.nfc-visual.working .nfc-wave {
  animation: nfcPulse 1.45s ease-out infinite;
}

.nfc-visual.waiting .wave-2,
.nfc-visual.working .wave-2 {
  animation-delay: .28s;
}

.nfc-visual.waiting .wave-3,
.nfc-visual.working .wave-3 {
  animation-delay: .56s;
}

.nfc-visual.working .nfc-phone {
  animation: nfcShake .5s linear infinite;
}

.nfc-visual.success .nfc-phone {
  border-color: rgba(213, 255, 160, .95);
  box-shadow: 0 0 26px rgba(185, 255, 129, .48), inset 0 0 12px rgba(0,0,0,.55);
}

.nfc-visual.success::after {
  content: "OK";
  position: absolute;
  right: calc(50% - 78px);
  top: 20px;
  padding: 4px 8px;
  border: 1px solid rgba(217, 255, 181, .85);
  background: rgba(23, 46, 18, .9);
  color: rgba(229, 255, 207, .98);
  font-weight: 900;
  letter-spacing: 1px;
  box-shadow: 0 0 14px rgba(185,255,129,.35);
}

.nfc-visual.error .nfc-phone {
  border-color: rgba(255, 170, 170, .9);
  box-shadow: 0 0 22px rgba(255, 120, 120, .28), inset 0 0 12px rgba(0,0,0,.55);
}

.nfc-visual.error::after {
  content: "ERROR";
  position: absolute;
  right: calc(50% - 98px);
  top: 20px;
  padding: 4px 8px;
  border: 1px solid rgba(255, 185, 185, .85);
  background: rgba(70, 18, 18, .9);
  color: rgba(255, 222, 222, .98);
  font-weight: 900;
  letter-spacing: 1px;
}

.nfc-progress {
  height: 8px;
  margin: 12px 0;
  overflow: hidden;
  border: 1px solid rgba(137,146,129,.48);
  background: rgba(0,0,0,.25);
}

.nfc-progress span {
  display: block;
  height: 100%;
  width: 35%;
  background: rgba(217, 255, 181, .72);
  box-shadow: 0 0 12px rgba(185,255,129,.4);
}

.nfc-progress span.waiting,
.nfc-progress span.working {
  animation: nfcProgress 1.1s linear infinite;
}

.nfc-progress span.success {
  width: 100%;
}

.nfc-progress span.error {
  width: 100%;
  background: rgba(255, 170, 170, .75);
}

@keyframes nfcPulse {
  0% { transform: translate(-50%, -50%) scale(.35); opacity: .9; }
  100% { transform: translate(-50%, -50%) scale(1.55); opacity: 0; }
}

@keyframes nfcShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-1px); }
  75% { transform: translateX(1px); }
}

@keyframes nfcProgress {
  0% { transform: translateX(-110%); }
  100% { transform: translateX(330%); }
}

@media (max-width: 720px) {
  .nfc-window {
    width: 100%;
  }
  .nfc-visual {
    min-height: 132px;
  }
}
