:root {
  --navy: #1d3d6a;
  --navy-deep: #163056;
  --sky: #e8f0fe;
  --sky-mid: #d4e4f7;
  --border: #c5d5ea;
  --border-soft: #dbe6f4;
  --text: #243447;
  --text-muted: #6b7c90;
  --white: #ffffff;
  --disabled-bg: #eceff3;
  --disabled-text: #a8b0ba;
  --danger: #c45c5c;
  --success: #4cd964;
  --radius: 8px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --shadow: 0 1px 2px rgba(29, 61, 106, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: #f5f8fc;
  min-height: 100vh;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.error { color: var(--danger); font-size: 0.9rem; margin-top: 0.75rem; }

.login-docs {
  margin: 1rem 0 0;
  text-align: center;
  font-size: 0.88rem;
}

.login-docs a {
  color: var(--navy, #0b3d5c);
  font-weight: 600;
  text-decoration: none;
}

.login-docs a:hover {
  text-decoration: underline;
}

/* —— Buttons (design system) —— */
button,
.btn {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  padding: 0.65rem 1rem;
  background: var(--sky);
  color: var(--navy);
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
  width: auto;
}

button:hover,
.btn:hover {
  background: var(--sky-mid);
}

button:active,
.btn:active,
button.is-active {
  background: var(--navy);
  color: var(--white);
}

button:disabled,
.btn:disabled {
  background: var(--disabled-bg);
  color: var(--disabled-text);
  cursor: not-allowed;
}

.btn-primary {
  background: var(--navy);
  color: var(--white);
}
.btn-primary:hover { background: var(--navy-deep); color: var(--white); }
.btn-primary:active { background: #102844; color: var(--white); }

.btn-block { width: 100%; }
.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
}

/* —— Inputs —— */
label.field {
  display: block;
  margin: 0.85rem 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--navy);
}

label.field > span { display: block; margin-bottom: 0.35rem; }

input,
textarea,
select {
  font: inherit;
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--white);
  color: var(--text);
}

input:focus,
textarea:focus,
select:focus {
  outline: 2px solid var(--sky-mid);
  border-color: var(--navy);
}

input:disabled,
textarea:disabled {
  background: var(--disabled-bg);
  color: var(--disabled-text);
}

textarea { min-height: 96px; resize: vertical; }

.file-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--sky);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border-soft);
}
.file-row input[type="file"] { flex: 1; border: none; background: transparent; padding: 0; }

/* —— Login —— */
#login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background:
    linear-gradient(180deg, var(--sky) 0%, #f5f8fc 55%);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  padding: 1.75rem 1.5rem 1.5rem;
  box-shadow: var(--shadow);
}

.login-card .brand {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--navy);
  margin: 0 0 0.25rem;
}

.login-card .lead {
  margin: 0 0 1.25rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* —— App shell / header —— */
#app-view { min-height: 100vh; display: flex; flex-direction: column; }

.app-header {
  background: var(--white);
  border-bottom: 1px solid var(--border-soft);
  padding: 0.85rem 1.25rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  align-items: center;
  justify-content: space-between;
}

.app-header .brand-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.app-header .brand {
  font-weight: 700;
  color: var(--navy);
  font-size: 1.1rem;
}

.app-header .subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.app-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.app-nav button {
  padding: 0.5rem 0.85rem;
  font-size: 0.9rem;
}

.app-nav button.is-active {
  background: var(--navy);
  color: var(--white);
}

.app-nav .nav-docs {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  margin-left: 0.35rem;
  padding-left: 0.65rem;
  border-left: 1px solid var(--border, #d0d5dd);
  font-size: 0.82rem;
}

.app-nav .nav-docs a {
  color: var(--muted, #667085);
  text-decoration: none;
  font-weight: 600;
}

.app-nav .nav-docs a:hover {
  color: var(--navy, #0b3d5c);
  text-decoration: underline;
}

main {
  flex: 1;
  padding: 1.25rem;
  width: 90%;
  max-width: none;
  margin: 0 auto;
}

.screen-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.screen-title h2 {
  margin: 0;
  font-size: 1.25rem;
  color: var(--navy);
}

.screen-title p {
  margin: 0.15rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.filter-bar {
  background: var(--sky);
  border-radius: var(--radius);
  padding: 0.65rem 0.9rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.85rem;
  color: var(--navy);
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid var(--border-soft);
}

/* —— Cards / list items —— */
.card {
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  margin-bottom: 0.65rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.card:hover {
  border-color: var(--border);
  background: #fafcff;
}

.card:active {
  background: var(--sky);
}

.card-row {
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  background: var(--sky);
  color: var(--navy);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-weight: 700;
}

.card-body { flex: 1; min-width: 0; }
.card-title {
  font-weight: 700;
  color: var(--navy);
  margin: 0 0 0.2rem;
}
.card-desc {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text);
}
.card-meta {
  margin: 0.35rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: var(--sky);
  color: var(--navy);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.badge-dismissed { background: #fde8e8; color: var(--danger); }
.badge-pending { background: var(--sky); color: var(--navy); }
.badge-paid { background: #e5f8ea; color: #1f7a3a; }

.form-card {
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 1.1rem 1.15rem;
  box-shadow: var(--shadow);
}

.form-card h3 {
  margin: 1.25rem 0 0.5rem;
  font-size: 1rem;
  color: var(--navy);
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
}

.detail-panel {
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 1rem;
  margin-top: 0.75rem;
}

.detail-panel pre {
  margin: 0;
  white-space: pre-wrap;
  background: var(--sky);
  color: var(--text);
  padding: 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border-soft);
  overflow: auto;
  font-size: 0.8rem;
}

#detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.85rem;
}

.empty {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
  background: var(--white);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.filter-panel { margin-bottom: 1rem; }
.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem 1rem;
}

.table-wrap {
  overflow: auto;
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.data-table th {
  background: var(--sky);
  color: var(--navy);
  text-align: left;
  padding: 0.7rem 0.75rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}

.data-table tbody tr:nth-child(even) { background: #f7fafc; }
.data-table tbody tr:hover { background: var(--sky); }
.data-table tbody tr.row-inactive td { color: var(--text-muted); opacity: 0.85; }

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

.actions button {
  padding: 0.35rem 0.55rem;
  font-size: 0.8rem;
}

.attach-list {
  list-style: none;
  padding: 0;
  margin: 0.5rem 0 0;
}

.attach-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  background: var(--sky);
  border-radius: var(--radius);
  margin-bottom: 0.35rem;
}

.tab-bar {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.tab-btn {
  background: var(--sky);
  color: var(--navy);
  padding: 0.45rem 0.9rem;
}

.tab-btn.is-active {
  background: var(--navy);
  color: var(--white);
}

.import-options {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 0.75rem 0.9rem;
  background: var(--sky);
  border-radius: var(--radius);
}

.import-options .check {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: var(--navy);
  font-weight: 600;
  cursor: pointer;
}

.import-options .check input {
  width: auto;
}

.import-preview,
.import-result {
  margin-top: 0.75rem;
  padding: 0.75rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: #f7fafc;
  max-height: 220px;
  overflow: auto;
  font-size: 0.85rem;
}

.import-preview table,
.import-result table {
  width: 100%;
  border-collapse: collapse;
}

.import-preview th,
.import-preview td,
.import-result th,
.import-result td {
  border-bottom: 1px solid var(--border-soft);
  padding: 0.35rem 0.4rem;
  text-align: left;
}

a.btn {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.level-list {
  margin: 0.5rem 0 0;
  padding-left: 1.1rem;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

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

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(29, 61, 106, 0.45);
}

.modal-panel {
  position: relative;
  width: min(640px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--white);
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  box-shadow: 0 12px 40px rgba(29, 61, 106, 0.25);
  z-index: 1;
}

.modal-panel-lg {
  width: min(720px, 100%);
}

.modal-header,
.modal-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--border-soft);
}

.modal-footer {
  border-bottom: none;
  border-top: 1px solid var(--border-soft);
  justify-content: flex-end;
}

.modal-header h3 {
  margin: 0;
  color: var(--navy);
  font-size: 1.15rem;
}

.modal-sub {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 400;
}

.modal-body { padding: 1rem 1.1rem 1.2rem; }

.form-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 1rem;
}

.levels-editor {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.level-row {
  display: grid;
  grid-template-columns: 4.5rem 1fr auto;
  gap: 0.5rem;
  align-items: end;
  padding: 0.65rem 0.75rem;
  background: var(--surface-2, #f4f7fb);
  border: 1px solid var(--border, #d5dee8);
  border-radius: 6px;
}

.level-row .field { margin: 0; }

.levels-summary {
  font-size: 0.85rem;
  color: var(--text-muted, #5a6b7d);
  line-height: 1.45;
}

@media (max-width: 640px) {
  .app-header { align-items: stretch; }
  .app-nav { width: 100%; }
  .app-nav button { flex: 1 1 auto; }
  .form-grid-2 { grid-template-columns: 1fr; }
  .level-row { grid-template-columns: 1fr; }
}

.ai-config-intro {
  margin-bottom: 1rem;
}
.ai-config-intro p {
  margin: 0;
  line-height: 1.45;
  color: var(--text-muted, #5a6b7d);
}
.ai-config-intro code {
  font-size: 0.85em;
}

.ai-form-banner {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.75rem 0.9rem;
  margin-bottom: 1rem;
  border-radius: 6px;
  border: 1px solid #c5d5ea;
  background: #e8f0fe;
  color: var(--navy);
  font-size: 0.9rem;
  line-height: 1.4;
}
.ai-form-banner strong {
  font-size: 0.95rem;
}

.capture-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
  margin: 0.5rem 0 0.75rem;
}
.capture-btn {
  min-height: 3rem;
  font-size: 1rem;
  padding: 0.75rem 0.85rem;
  border-radius: 8px;
  touch-action: manipulation;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 640px) {
  .capture-actions {
    grid-template-columns: 1fr;
  }
  .modal-panel-lg {
    max-height: 92vh;
  }
}

/* —— APSV wizard / campana / reportes —— */
.apsv-stage-title {
  margin: 0.85rem 0 0.75rem;
  font-weight: 700;
  color: var(--navy);
  font-size: 0.95rem;
}

.apsv-tabs {
  display: flex;
  gap: 0;
  margin: 0 0 1rem;
  border-bottom: 2px solid var(--border-soft);
}

.apsv-tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 0.55rem 0.45rem;
  font-weight: 700;
  color: var(--muted, #64748b);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  font-size: 0.78rem;
  line-height: 1.25;
}

.sig-pad-wrap {
  border: 1px dashed #94a3b8;
  border-radius: 6px;
  background: #fff;
  overflow: hidden;
  touch-action: none;
  margin-bottom: 0.4rem;
}

.sig-canvas {
  display: block;
  width: 100%;
  height: 120px;
  cursor: crosshair;
  touch-action: none;
}

.sig-pad-actions {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}

.sig-clear {
  font-size: 0.78rem;
  padding: 0.3rem 0.55rem;
}

.flatpickr-alt-input {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  font: inherit;
  background: #fff;
}

.flatpickr-calendar {
  z-index: 10050 !important;
}

/* SweetAlert2 por encima de modales (2000) y flatpickr (10050) */
.swal2-container {
  z-index: 12000 !important;
}

.lines-table .flatpickr-alt-input {
  min-width: 9.5rem;
  font-size: 0.8rem;
  padding: 0.35rem 0.4rem;
}

.apsv-tab.active {
  color: var(--navy);
  border-bottom-color: var(--navy);
  background: #f0f5fa;
}

.apsv-tab-panel {
  max-height: 58vh;
  overflow-y: auto;
  padding-right: 0.25rem;
}

.apsv-sign-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin: 0.75rem 0;
}

.apsv-sign-box {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.75rem;
  background: #f7fafc;
  min-height: 8rem;
}

.apsv-sign-label {
  margin: 0 0 0.65rem;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--navy);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.apsv-total-line {
  margin: 0.25rem 0 0.85rem;
  font-size: 0.9rem;
}

.apsv-grand-total {
  margin: 0.75rem 0 1rem;
  padding: 0.75rem 0.85rem;
  background: #eef4fa;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
}

.apsv-grand-total p {
  margin: 0.25rem 0;
}

.apsv-son-pesos {
  font-size: 0.85rem;
  text-transform: uppercase;
}

.apsv-receipt-stmt {
  font-style: italic;
  margin: 0.5rem 0 0.75rem;
}

@media (max-width: 640px) {
  .apsv-sign-grid {
    grid-template-columns: 1fr;
  }
}

.radio-fieldset {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 0.65rem 0.85rem;
  margin: 0.85rem 0;
  background: #f7fafc;
}

.radio-fieldset legend {
  padding: 0 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--navy);
}

.radio-fieldset .radio {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-right: 1rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
}

.radio-fieldset .radio input {
  width: auto;
}

.form-footer-apsv {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.lines-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  margin: 0.5rem 0;
}

.lines-table th,
.lines-table td {
  border: 1px solid var(--border-soft);
  padding: 0.35rem;
  vertical-align: middle;
}

.lines-table th {
  background: var(--sky);
  color: var(--navy);
  font-size: 0.72rem;
}

.lines-table input {
  padding: 0.35rem 0.4rem;
  font-size: 0.8rem;
}

.lines-toolbar {
  display: flex;
  gap: 0.4rem;
  margin: 0.35rem 0 0.75rem;
}

.settlement-block {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border-soft);
}

.events-list {
  list-style: none;
  padding: 0;
  margin: 0.5rem 0 0;
}

.events-list li {
  padding: 0.45rem 0.6rem;
  background: #f7fafc;
  border-radius: var(--radius);
  margin-bottom: 0.35rem;
  font-size: 0.85rem;
  border: 1px solid var(--border-soft);
}

.badge-draft { background: #eef1f5; color: #4a5568; }
.badge-settlement { background: #fff4e0; color: #9a6700; }
.badge-observed { background: #fde8e8; color: var(--danger); }
.badge-settled { background: #e5f0ff; color: #1d3d6a; }
.badge-liquidated { background: #e5f8ea; color: #1f7a3a; }

.notif-wrap {
  position: relative;
}

.btn-notif {
  position: relative;
  padding: 0.45rem 0.65rem;
  min-width: 2.5rem;
}

.notif-bell { display: block; }

.notif-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 1.1rem;
  height: 1.1rem;
  padding: 0 0.25rem;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.notif-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 0.35rem);
  width: min(340px, 92vw);
  max-height: 360px;
  overflow: auto;
  background: var(--white);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(29, 61, 106, 0.18);
  z-index: 50;
}

.notif-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border-soft);
  color: var(--navy);
}

.notif-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.notif-list li {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
}

.notif-list li:hover { background: var(--sky); }
.notif-list li.is-unread { background: #f0f6ff; }
.notif-list .notif-title { font-weight: 700; color: var(--navy); font-size: 0.85rem; }
.notif-list .notif-body { margin: 0.2rem 0 0; font-size: 0.8rem; color: var(--text-muted); }
.notif-empty { padding: 1rem; text-align: center; color: var(--text-muted); font-size: 0.85rem; }
