@font-face {
  font-family: "Brittany Signature";
  src: url("fonts/BrittanySignature.ttf") format("truetype");
  font-display: swap;
}

:root {
  --green-dark: #285039;
  --green: #4e8758;
  --green-light: #dcebdd;
  --ink: #17221b;
  --muted: #66736a;
  --border: #d2ddd4;
  --page: #f1f5f1;
  --paper: #ffffff;
  --danger: #9b2c2c;
  --shadow: 0 18px 55px rgba(34, 63, 43, 0.13);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0, #e0eee1 0, transparent 32rem),
    linear-gradient(180deg, #fafcfa, var(--page));
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
}

/* Acceso básico a la aplicación */
body.login-locked {
  overflow: hidden;
}

body.login-locked > .page-header,
body.login-locked > .app-layout,
body.login-locked > footer {
  visibility: hidden;
  pointer-events: none;
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
  padding: 1rem;
  background:
    radial-gradient(circle at 8% 0, #dcebdd 0, transparent 32rem),
    linear-gradient(180deg, #f8fbf8, #edf3ee);
}

.login-screen[hidden] {
  display: none;
}

.login-card {
  width: min(430px, 100%);
  padding: clamp(1.35rem, 4vw, 2rem);
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.login-card h1 {
  font-size: clamp(2rem, 8vw, 3rem);
}

.login-subtitle {
  margin: 0.75rem 0 0;
  color: var(--muted);
  line-height: 1.5;
}

.login-form {
  margin-top: 1.4rem;
}

.login-message {
  min-height: 1.3rem;
  color: var(--danger);
  font-size: 0.88rem;
  font-weight: 700;
}

.login-submit {
  width: 100%;
}

.logout-button {
  flex: 0 0 auto;
}

button,
input,
select {
  font: inherit;
}

.page-header {
  width: min(1480px, calc(100% - 2rem));
  margin: auto;
  padding: 2.2rem 0 1.2rem;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.eyebrow,
.section-label {
  margin: 0 0 0.35rem;
  color: var(--green);
  font-size: 0.75rem;
  font-weight: 850;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  line-height: 1.08;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.5rem);
  letter-spacing: -0.045em;
}

h2 {
  font-size: 1.25rem;
}

.subtitle {
  max-width: 760px;
  margin: 0.7rem 0 0;
  color: var(--muted);
}

.app-layout {
  width: min(1480px, calc(100% - 2rem));
  margin: 0 auto 3rem;
  display: grid;
  grid-template-columns: minmax(330px, 0.8fr) minmax(580px, 1.2fr);
  gap: 1.2rem;
  align-items: start;
}

.panel {
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.form-panel,
.preview-panel {
  padding: clamp(1rem, 2.5vw, 1.6rem);
}

.preview-panel {
  position: sticky;
  top: 1rem;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.local-badge,
.size-badge {
  padding: 0.4rem 0.7rem;
  color: var(--green-dark);
  background: var(--green-light);
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 800;
  white-space: nowrap;
}

form {
  display: grid;
  gap: 1rem;
  margin-top: 1rem;
}

fieldset {
  min-width: 0;
  margin: 0;
  padding: 1rem;
  display: grid;
  gap: 0.52rem;
  border: 1px solid var(--border);
  border-radius: 14px;
}

legend {
  padding: 0 0.4rem;
  color: var(--green-dark);
  font-weight: 850;
}

label {
  margin-top: 0.3rem;
  font-size: 0.86rem;
  font-weight: 760;
}

input,
select {
  width: 100%;
  padding: 0.73rem 0.8rem;
  color: var(--ink);
  background: #fff;
  border: 1px solid #b9c8bc;
  border-radius: 10px;
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

input:focus,
select:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 4px rgba(78, 135, 88, 0.14);
}

input[readonly] {
  color: #445048;
  background: #edf2ee;
  cursor: not-allowed;
  font-weight: 720;
}

input[type="file"] {
  padding: 0.55rem;
  background: #f9fbf9;
}

small {
  color: var(--muted);
}

.form-message {
  display: none;
  padding: 0.75rem 0.85rem;
  border-radius: 10px;
  font-size: 0.9rem;
}

.form-message.error {
  display: block;
  color: var(--danger);
  background: #fff2f2;
  border: 1px solid #efc0c0;
}

.form-message.success {
  display: block;
  color: var(--green-dark);
  background: var(--green-light);
  border: 1px solid #bed6c1;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}

button {
  padding: 0.72rem 0.95rem;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  font-weight: 800;
}

button:hover {
  transform: translateY(-1px);
}

button.primary {
  color: #fff;
  background: var(--green-dark);
  border-color: var(--green-dark);
}

button.secondary {
  color: var(--muted);
}

.privacy-note {
  margin-top: 1rem;
  padding: 0.9rem;
  color: #4f5b53;
  background: #f7f9f7;
  border: 1px dashed #b6c4b8;
  border-radius: 12px;
  font-size: 0.85rem;
  line-height: 1.5;
}

.cards {
  display: grid;
  justify-items: center;
  gap: 1.25rem;
  padding: 1.4rem 0 0.5rem;
}

.credential {
  position: relative;
  width: min(100%, 720px);
  aspect-ratio: 85.6 / 53.98;
  overflow: hidden;
  isolation: isolate;
  background: var(--paper);
  border: none;
  border-radius: 18px;
  box-shadow: 0 16px 34px rgba(35, 62, 42, 0.16);
}

.credential-front {
  padding: 4.1% 4.8% 3.6%;
  background: #ffffff;
}

.front-image {
  position: absolute;
  inset: -2%;
  z-index: 0;
  width: 104%;
  height: 104%;
  display: block;
  object-fit: fill;
}

/* Anverso: funciona igual que el reverso.
   Solo usa anverso.png como fondo y superpone los datos. */
.credential-front {
  --name-x: 21.7%;
  --name-y: 37%;
  --dpi-x: 21.749131944444446%;
  --dpi-y: 46%;
  --community-x: 22.026909722222225%;
  --community-y: 51.5%;
  --photo-x: 72.5%;
  --photo-y: 22.5%;
  --photo-w: 17.5%;
  --role-x: 81.25%;
  --role-y: 56%;
  --rlevel-x: 81.25%;
  --rlevel-y: 61.5%;
  --qr-x: 53.502604166666664%;
  --qr-y: 66.12292656067176%;
  --qr-width: 16.998697916666668%;
  --signature-x: 13.276909722222218%;
  --signature-y: 63.903228026705214%;
  --signature-w: 17.999131944444443%;
  padding: 0;
  background: #ffffff;
}

.credential-front > :not(.front-image):not(.front-error) {
  position: absolute;
  z-index: 2;
}

.front-error {
  position: absolute;
  z-index: 4;
  inset: 8%;
  display: grid;
  place-items: center;
  padding: 1rem;
  color: #526057;
  background: rgba(248, 250, 248, 0.96);
  border: 2px dashed #9eafa2;
  border-radius: 12px;
  text-align: center;
  font-size: clamp(0.65rem, 1.5vw, 1rem);
}

.front-error[hidden] {
  display: none;
}

.front-overlay {
  width: 55%;
  color: #202520;
  text-align: left;
  white-space: nowrap;
}

.front-overlay span {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: normal;
  text-transform: uppercase;
}

.front-name {
  left: var(--name-x);
  top: var(--name-y);
}

.front-name span {
  font-size: clamp(0.68rem, 1.3vw, 0.98rem);
  font-weight: 820;
}

.front-dpi {
  left: var(--dpi-x);
  top: var(--dpi-y);
}

.front-dpi span,
.front-community span {
  font-size: clamp(0.56rem, 1.08vw, 0.82rem);
  font-weight: 760;
}

.front-community {
  left: var(--community-x);
  top: var(--community-y);
}

.front-photo {
  position: absolute;
  left: var(--photo-x);
  top: var(--photo-y);
  width: var(--photo-w);
  aspect-ratio: 0.82;
  overflow: hidden;
  display: grid;
  place-items: center;
  color: #869087;
  background: rgba(237, 241, 237, 0.94);
  border: 1px solid #c5cfc7;
  border-radius: 6px;
  text-align: center;
  font-size: clamp(0.52rem, 1vw, 0.73rem);
  font-weight: 900;
}

.front-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.front-role {
  left: calc(var(--role-x) - 11%);
  top: var(--role-y);
  width: 22%;
  color: var(--green-dark);
  text-align: center;
  text-transform: uppercase;
  font-size: clamp(0.54rem, 1.15vw, 0.80rem);
  font-weight: 800;
}

.front-rlevel {
  left: calc(var(--rlevel-x) - 11%);
  top: var(--rlevel-y);
  width: 22%;
  color: var(--green-dark);
  text-align: center;
  text-transform: uppercase;
  font-size: clamp(0.48rem, 1.02vw, 0.70rem);
  font-weight: 800;
}

.qr-area {
  position: absolute;
  left: var(--qr-x);
  top: var(--qr-y);
  right: auto;
  bottom: auto;
  width: var(--qr-width);
  text-align: center;
}

.qr-area canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  height: auto;
  background: #ffffff;
  border: 1px solid #d3dbd5;
  image-rendering: pixelated;
}

.qr-area span {
  display: block;
  margin-top: 2px;
  color: #303830;
  font-size: clamp(0.26rem, 0.5vw, 0.38rem);
  font-weight: 800;
  letter-spacing: 0.05em;
}

.front-signature {
  position: absolute;
  left: var(--signature-x);
  top: var(--signature-y);
  width: var(--signature-w);
  display: grid;
  place-items: center;
}

.front-signature img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: contain;
}

.front-signature span {
  display: inline-block;
  color: #46504a;
  font-size: clamp(0.46rem, 0.9vw, 0.68rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* Reverso: cambia estas dos variables para ajustar la posición del cargo. */
.credential-back {
  --texto-y: 51%;
  padding: 0;
  background: #f2f4f2;
}

.reverse-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.back-role-overlay {
  position: absolute;
  z-index: 3;
  top: var(--texto-y);
  left: 50%;
  width: max-content;
  max-width: 90%;
  transform: translate(-50%, -50%);
  color: #46504a;
  font-family: "Brittany Signature", "Segoe Print", "Bradley Hand",
    "Lucida Handwriting", cursive;
  font-size: clamp(0.86rem, 2.75vw, 2.02rem);
  font-weight: 400;
  font-style: normal;
  font-synthesis: none;
  letter-spacing: 0;
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  text-align: center;
  text-transform: none;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.18);
}

.reverse-error {
  position: absolute;
  inset: 8%;
  display: grid;
  place-items: center;
  padding: 1rem;
  color: #526057;
  background: #f8faf8;
  border: 2px dashed #9eafa2;
  border-radius: 12px;
  text-align: center;
}

.reverse-error[hidden] {
  display: none;
}


/* Editor visual de objetos */
.layout-editor {
  margin-top: 1rem;
  padding: 0.8rem;
  background: #f7faf7;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
}

.editor-status {
  flex: 1 1 260px;
  color: var(--muted);
  font-size: 0.8rem;
}

.editor-controls {
  margin-top: 0.8rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--border);
}

.editor-controls[hidden] {
  display: none;
}

.editor-help {
  margin: 0 0 0.7rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.template-manager {
  margin: 0 0 0.9rem;
  padding: 0.8rem;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.template-manager-heading {
  display: grid;
  gap: 0.2rem;
  margin-bottom: 0.65rem;
}

.template-manager-heading strong {
  color: var(--green-dark);
  font-size: 0.9rem;
}

.template-manager-heading small {
  line-height: 1.35;
}

.template-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.55rem;
  align-items: center;
}

.template-row + .template-row {
  margin-top: 0.55rem;
}

.template-row input,
.template-row select {
  min-width: 0;
  padding: 0.55rem 0.65rem;
}

.template-row button {
  white-space: nowrap;
}

.template-file-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  align-items: center;
  margin-top: 0.55rem;
}

.template-file-row small {
  flex: 1 1 220px;
  line-height: 1.35;
}

.editor-grid {
  display: grid;
  grid-template-columns: minmax(130px, 0.42fr) minmax(220px, 1fr);
  gap: 0.55rem 0.8rem;
  align-items: center;
}

.editor-grid label {
  margin: 0;
}

.editor-grid select {
  padding: 0.55rem 0.65rem;
}

.size-control {
  display: grid;
  grid-template-columns: 1fr 58px;
  gap: 0.65rem;
  align-items: center;
}

.size-control input[type="range"] {
  padding: 0;
  border: 0;
  box-shadow: none;
  accent-color: var(--green-dark);
}

.size-control output {
  color: var(--green-dark);
  font-size: 0.84rem;
  font-weight: 850;
  text-align: right;
}

.editable-object[hidden] {
  display: none !important;
}

.preview-panel.editor-enabled .editable-object {
  cursor: move;
  touch-action: none;
  user-select: none;
  outline: 1px dashed rgba(40, 80, 57, 0.46);
  outline-offset: 2px;
}

.preview-panel.editor-enabled .editable-object:hover {
  outline-color: rgba(40, 80, 57, 0.9);
}

.preview-panel.editor-enabled .editable-object.editor-selected {
  z-index: 8 !important;
  outline: 2px solid var(--green-dark);
  outline-offset: 3px;
}

.editable-object > .editor-resize-handle {
  position: absolute;
  z-index: 12;
  right: -7px;
  bottom: -7px;
  width: 15px;
  height: 15px;
  display: none;
  margin: 0;
  padding: 0;
  font-size: 0;
  letter-spacing: 0;
  background: #ffffff;
  border: 3px solid var(--green-dark);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(23, 34, 27, 0.28);
  cursor: nwse-resize;
}

.preview-panel.editor-enabled
  .editable-object.editor-selected
  > .editor-resize-handle {
  display: block;
}

@media (max-width: 590px) {
  .editor-grid,
  .template-row {
    grid-template-columns: 1fr;
  }

  .template-row button,
  .template-file-row button {
    width: 100%;
  }
}

.print-help {
  max-width: 720px;
  margin: 0.6rem auto 0;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: center;
}

footer {
  padding: 1.4rem;
  color: var(--muted);
  text-align: center;
  font-size: 0.84rem;
}

@media (max-width: 1050px) {
  .app-layout {
    grid-template-columns: 1fr;
  }

  .preview-panel {
    position: static;
  }
}

@media (max-width: 590px) {
  .app-layout,
  .page-header {
    width: min(100% - 1rem, 1480px);
  }

  .page-header {
    align-items: stretch;
    flex-direction: column;
  }

  .logout-button {
    align-self: flex-start;
  }

  .form-panel,
  .preview-panel {
    padding: 0.9rem;
    border-radius: 14px;
  }

  .size-badge {
    display: none;
  }

  .credential {
    border-radius: 10px;
  }
}

@media print {
  /* Tarjeta CR-80 / ID-1: 85.60 x 53.98 mm. */
  @page {
    size: 85.6mm 53.98mm;
    margin: 0;
  }

  html,
  body {
    width: 85.6mm;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff;
  }

  .login-screen,
  .page-header,
  .form-panel,
  .panel-title,
  .layout-editor,
  .print-help,
  footer {
    display: none !important;
  }

  .app-layout,
  .preview-panel,
  .cards {
    width: 85.6mm !important;
    display: block;
    margin: 0 !important;
    padding: 0 !important;
    border: 0;
    box-shadow: none;
  }

  /* Cada lado de la credencial ocupa exactamente una página CR-80. */
  .credential {
    width: 85.6mm !important;
    height: 53.98mm !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    overflow: hidden;
    break-inside: avoid;
    page-break-inside: avoid;
    break-after: page;
    page-break-after: always;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .credential:last-child {
    break-after: auto;
    page-break-after: auto;
  }

  .back-role-overlay {
    font-weight: 400 !important;
    font-style: normal !important;
    font-synthesis: none;
    letter-spacing: 0 !important;
    line-height: 1 !important;
    white-space: nowrap !important;
    text-rendering: geometricPrecision;
  }
}



@media print {
  .editable-object {
    outline: none !important;
  }

  .editor-resize-handle {
    display: none !important;
  }
}
