:root {
  color-scheme: dark;
  --bg: #0e131f;
  --bg-soft: #141b29;
  --card: #1a2231;
  --card-2: #212a3b;
  --text: #e7ecf6;
  --muted: #8b96ab;
  --border: #2a3346;
  --primary: #7c83ff;
  --primary-dark: #6366f1;
  --green: #34d399;
  --green-bg: rgba(52, 211, 153, 0.14);
  --red: #f87171;
  --red-bg: rgba(248, 113, 113, 0.14);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 14px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, -apple-system, BlinkMacSystemFont, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  min-height: 100vh;
  min-height: 100dvh;
}

.btn, .tab, button { touch-action: manipulation; }

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.block { display: block; }
.right { text-align: right; }
.center { text-align: center; }
.mono { font-variant-numeric: tabular-nums; }

/* ============ LOGIN ============ */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(900px 500px at 15% 10%, rgba(99, 102, 241, 0.25), transparent 60%),
    radial-gradient(800px 500px at 85% 90%, rgba(124, 58, 237, 0.22), transparent 60%),
    #0a0e18;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 34px 30px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
}
.brand { text-align: center; margin-bottom: 22px; }
.brand-logo {
  width: 54px; height: 54px; margin: 0 auto 12px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff; border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; font-weight: 800;
  box-shadow: 0 8px 22px rgba(99, 102, 241, 0.45);
}
.brand-logo.sm { width: 40px; height: 40px; font-size: 20px; margin: 0; border-radius: 11px; }
.brand h1 { font-size: 22px; margin: 0; }
.brand h1 span { color: var(--primary); }
.login-card label { display: block; margin-bottom: 14px; font-size: 13px; font-weight: 600; color: #aab4c8; }
.login-card input {
  width: 100%; margin-top: 6px; padding: 11px 13px;
  border: 1px solid var(--border); border-radius: 10px;
  font-size: 15px; background: var(--bg-soft); color: var(--text);
}
.login-card input::placeholder { color: #5f6b82; }
.login-card input:focus { outline: none; border-color: var(--primary); background: #101725; }

/* ============ BOTONES ============ */
.btn {
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--text);
  padding: 9px 15px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: .15s;
  white-space: nowrap;
}
.btn:hover { background: #2a3346; }
.btn-primary { background: var(--primary-dark); border-color: var(--primary-dark); color: #fff; }
.btn-primary:hover { background: var(--primary); border-color: var(--primary); }
.btn-green { background: #16a34a; border-color: #16a34a; color: #fff; }
.btn-green:hover { background: #15803d; }
.btn-red { background: #b91c1c; border-color: #b91c1c; color: #fff; }
.btn-red:hover { background: #991b1b; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: rgba(255, 255, 255, 0.06); color: var(--text); }
.btn-block { width: 100%; padding: 12px; font-size: 15px; }
.btn-sm { padding: 6px 10px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ============ LAYOUT ============ */
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 12px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky; top: 0; z-index: 20;
}
.topbar-brand { display: flex; align-items: center; gap: 12px; }
.topbar-brand strong { font-size: 16px; }
.topbar-user { display: flex; align-items: center; gap: 10px; }

.tabs {
  display: flex; gap: 4px; padding: 10px 22px 0;
  background: var(--card); border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab {
  border: none; background: transparent; padding: 11px 16px;
  font-size: 14px; font-weight: 600; color: var(--muted);
  cursor: pointer; border-bottom: 3px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

main { padding: 22px; max-width: 1200px; margin: 0 auto; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 20px; }

/* ============ CARDS ============ */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px;
}
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.stat .stat-label { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat .stat-value { font-size: 26px; font-weight: 800; margin-top: 6px; }
.stat.entradas .stat-value { color: var(--green); }
.stat.salidas .stat-value { color: var(--red); }
.stat.balance .stat-value { color: var(--primary); }

/* ============ TABLAS ============ */
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; background: transparent; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
th { background: var(--bg-soft); font-size: 12.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tbody tr:hover { background: rgba(255, 255, 255, 0.035); }
tbody tr:last-child td { border-bottom: none; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 700; }
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-red { background: var(--red-bg); color: var(--red); }
.badge-gray { background: #2a3346; color: var(--muted); }
.badge-blue { background: rgba(124, 131, 255, 0.16); color: var(--primary); }
.amount-in { color: var(--green); font-weight: 700; }
.amount-out { color: var(--red); font-weight: 700; }
.row-cancelado { opacity: .5; }
.row-cancelado td { text-decoration: line-through; }

/* ============ FORMULARIOS ============ */
.field { display: block; }
.field > label { display: block; font-size: 13px; font-weight: 600; color: #aab4c8; margin-bottom: 6px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
label.field { display: block; font-size: 13px; font-weight: 600; color: #aab4c8; }
label.field input, label.field select, label.field textarea {
  width: 100%; margin-top: 6px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; font-size: 14.5px;
  background: var(--bg-soft); color: var(--text); font-family: inherit;
}
label.field input::placeholder, label.field textarea::placeholder { color: #5f6b82; }
label.field input:focus, label.field select:focus, label.field textarea:focus {
  outline: none; border-color: var(--primary); background: #101725;
}
label.field select option { background: var(--card); color: var(--text); }
.form-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.checkbox-line { display: flex; align-items: center; gap: 8px; margin-top: 28px; font-size: 14px; }
.checkbox-line input { width: auto; }

.tipo-toggle { display: flex; gap: 10px; }
.tipo-toggle button {
  flex: 1; padding: 12px; border-radius: 10px; border: 2px solid var(--border);
  background: var(--bg-soft); color: var(--muted); font-weight: 700; cursor: pointer; font-size: 14.5px;
  transition: .15s;
}
.tipo-toggle button:hover { color: var(--text); }
.tipo-toggle button.active.in { border-color: var(--green); color: var(--green); background: var(--green-bg); }
.tipo-toggle button.active.out { border-color: var(--red); color: var(--red); background: var(--red-bg); }

.filters { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.filters label.field { min-width: 160px; }
.filters .combo-field { min-width: 240px; }

/* ============ BARRA DE FILTROS ============ */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 16px;
  margin-bottom: 16px;
}
.search-box { position: relative; flex: 1 1 240px; display: flex; align-items: center; }
.search-box .search-icon {
  position: absolute; left: 13px; font-size: 19px; font-weight: 700;
  color: var(--muted); pointer-events: none; line-height: 1;
}
.search-box .input { padding-left: 40px; }
.input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14.5px;
  font-family: inherit;
}
.input::placeholder { color: #5f6b82; }
.input:focus { outline: none; border-color: var(--primary); background: #101725; }
select.input { cursor: pointer; }
select.input option { background: var(--card); color: var(--text); }
.chip { white-space: nowrap; }
.chip.active { background: var(--primary-dark); border-color: var(--primary-dark); color: #fff; }
.chip.active:hover { background: var(--primary); border-color: var(--primary); }

/* ============ COMBOBOX BUSCABLE ============ */
.combobox { position: relative; }
.combo-field { min-width: 240px; }
.combo-search { cursor: text; }
.combo-list {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  max-height: 280px; overflow-y: auto; z-index: 60;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .5);
}
.combo-item { padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border); }
.combo-item:last-child { border-bottom: none; }
.combo-item:hover, .combo-item.active { background: var(--bg-soft); }
.combo-item-main { font-size: 14.5px; color: var(--text); }
.combo-item-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.combo-empty { padding: 14px; text-align: center; color: var(--muted); font-size: 13.5px; }

/* ============ MODAL ============ */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 18px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(4, 7, 14, .72); backdrop-filter: blur(2px); }
.modal-box {
  position: relative; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  width: 100%; max-width: 520px; box-shadow: 0 28px 70px rgba(0, 0, 0, .6);
  max-height: 90vh; display: flex; flex-direction: column;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

/* ============ TOAST ============ */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #0b1220; color: #fff; border: 1px solid var(--border);
  padding: 13px 18px; border-radius: 12px;
  font-size: 14px; font-weight: 600; z-index: 60; box-shadow: 0 14px 34px rgba(0, 0, 0, .55);
  display: flex; align-items: center; gap: 14px; max-width: 92vw;
}
.toast.ok { background: #0f2f22; border-color: rgba(52, 211, 153, .4); }
.toast.err { background: #3a1516; border-color: rgba(248, 113, 113, .4); }
.toast button { background: rgba(255, 255, 255, .14); color: #fff; border: none; padding: 7px 12px; border-radius: 8px; font-weight: 700; cursor: pointer; }
.toast button:hover { background: rgba(255, 255, 255, .24); }

.bar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.bar-label { width: 150px; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; background: var(--bg-soft); border-radius: 8px; height: 22px; overflow: hidden; display: flex; }
.bar-in { background: var(--green); height: 100%; }
.bar-out { background: var(--red); height: 100%; }
.bar-val { width: 130px; text-align: right; font-size: 13px; font-weight: 700; }

.progress { height: 6px; width: 120px; background: var(--bg-soft); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.progress.full > span { background: var(--green); }

.stack-right { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }

.hint {
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; margin-top: 16px;
  font-size: 14px; color: var(--muted);
}
.hint strong { color: var(--text); }
.hint.warn { border-color: rgba(248, 113, 113, .5); }
.hint.warn strong { color: var(--red); }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }.error { color: var(--red); font-size: 13px; font-weight: 600; margin-top: 10px; text-align: center; }
.spinner { text-align: center; padding: 40px; color: var(--muted); }

h3 { color: var(--text); }

/* ==========================================================
   MÓVIL (Android / iPhone)
   ========================================================== */
@media (max-width: 760px) {
  /* Layout general */
  main {
    padding: 14px calc(14px + env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .topbar {
    padding: calc(10px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) 10px calc(14px + env(safe-area-inset-left));
  }
  .tabs {
    padding-left: calc(12px + env(safe-area-inset-left));
    padding-right: calc(12px + env(safe-area-inset-right));
  }
  .page-head { margin-bottom: 14px; }
  .page-head h2 { font-size: 18px; }

  /* Evita el zoom automático de iOS al enfocar campos */
  label.field input, label.field select, label.field textarea,
  .login-card input, .input { font-size: 16px; }
  label.field input, label.field select, label.field textarea { padding: 12px; }

  /* Tabs: scroll horizontal suave, sin barra */
  .tabs { scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { padding: 12px 14px; }

  /* Estadísticas: 2 por fila */
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .stat .stat-value { font-size: 21px; overflow-wrap: anywhere; }

  /* Filtros a lo ancho */
  .filters { flex-direction: column; align-items: stretch; gap: 12px; }
  .filters label.field { min-width: 0; width: 100%; }
  .filters .field, .filters .combo-field { min-width: 0; width: 100%; }
  .filters .btn { width: 100%; }

  /* Barra de filtros de carteras */
  .filter-bar { flex-direction: column; align-items: stretch; gap: 10px; }
  .filter-bar .search-box { flex: 1 1 auto; width: 100%; }
  .filter-bar .input, .filter-bar .btn { width: 100%; }
  #cf-count { text-align: center; }

  /* Botones táctiles cómodos */
  .btn { padding: 11px 16px; }
  .btn-sm { padding: 9px 12px; font-size: 13px; }
  .form-actions { flex-direction: column; }
  .form-actions .btn { width: 100%; }
  .tipo-toggle button { font-size: 13.5px; padding: 12px 8px; }

  /* Tablas -> tarjetas apiladas */
  .table-wrap { padding: 12px; overflow: visible; }
  table, thead, tbody, tr, th, td { display: block; width: 100%; }
  thead { display: none; }
  tbody tr {
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-soft);
    padding: 6px 4px;
    margin-bottom: 12px;
  }
  tbody tr:last-child { margin-bottom: 0; }
  tbody tr:hover { background: var(--bg-soft); }
  tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    text-align: right;
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
    padding: 9px 12px;
  }
  tbody td:last-child { border-bottom: none; }
  tbody td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .03em;
    color: var(--muted);
    text-align: left;
  }
  tbody td:first-child {
    font-size: 16px;
    padding-top: 12px;
    padding-bottom: 12px;
  }
  /* Celdas sin etiqueta (p. ej. fila de "sin datos") */
  tbody td[colspan]::before { content: none; }
  tbody td[colspan] { justify-content: center; }
  tbody tr.empty-row { border: none; background: transparent; padding: 0; margin-bottom: 0; }
  .empty { padding: 26px 12px; }

  /* Celdas con contenido ancho (avance/pago) */
  tbody td .progress { width: 96px; }

  /* Acciones: botones en fila que se acomodan */
  td[data-label="Acciones"] { flex-wrap: wrap; }
  td[data-label="Acciones"] .btn { flex: 1 1 auto; }

  /* Modal tipo "bottom sheet" (más cómodo en el pulgar) */
  .modal { align-items: flex-end; padding: 0; }
  .modal-box {
    max-width: 100%;
    border-radius: 18px 18px 0 0;
    max-height: 92vh;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-foot { flex-direction: column-reverse; }
  .modal-foot .btn { width: 100%; }

  /* Toast a lo ancho */
  .toast {
    left: 12px; right: 12px; transform: none; max-width: none;
    bottom: calc(12px + env(safe-area-inset-bottom));
    flex-wrap: wrap;
  }
  .toast button { flex: 1 1 100%; }

  /* Barras de flujo */
  .bar-label { width: 88px; font-size: 12px; }
  .bar-val { width: auto; text-align: right; font-size: 12px; }
  .bar-row { flex-wrap: wrap; }
  .bar-track { flex: 1 1 100%; order: 3; margin-top: 4px; }
}

/* Teléfonos muy pequeños */
@media (max-width: 360px) {
  .grid-4 { grid-template-columns: 1fr; }
  .stat .stat-value { font-size: 20px; }
  tbody td::before { font-size: 11px; }
}
