:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e5e7ef;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #6366f1;
  --accent-soft: #eef0ff;
  --ok: #10b981;
  --ok-soft: #e7f8f1;
  --warn: #f59e0b;
  --warn-soft: #fff6e5;
  --danger: #ef4444;
  --danger-soft: #fdecec;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(15 23 42 / .04), 0 4px 16px rgb(15 23 42 / .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0e17; --surface: #131826; --surface-2: #1a2133; --border: #252d42;
    --text: #e8ecf5; --muted: #8b95ad; --accent: #818cf8; --accent-soft: #1f2347;
    --ok-soft: #0f2a22; --warn-soft: #2d2410; --danger-soft: #2e1517;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 8px 24px rgb(0 0 0 / .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0e17; --surface: #131826; --surface-2: #1a2133; --border: #252d42;
  --text: #e8ecf5; --muted: #8b95ad; --accent: #818cf8; --accent-soft: #1f2347;
  --ok-soft: #0f2a22; --warn-soft: #2d2410; --danger-soft: #2e1517;
  --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 8px 24px rgb(0 0 0 / .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -.01em; }
h1 { font-size: 1.75rem; font-weight: 700; }
h2 { font-size: 1.05rem; font-weight: 650; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; padding: 20px 14px;
  border-right: 1px solid var(--border); background: var(--surface);
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 1.05rem; padding: 4px 10px 20px; }
.brand b { color: var(--accent); }
.sidebar a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 550;
}
.sidebar a:hover { background: var(--surface-2); color: var(--text); }
.sidebar a.active { background: var(--accent-soft); color: var(--accent); }
.sidebar svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
main { padding: 32px clamp(16px, 4vw, 48px) 64px; max-width: 1180px; width: 100%; outline: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 24px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: auto 0 0 0; top: auto; height: auto; z-index: 20;
    flex-direction: row; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
  }
  .brand { display: none; }
  .sidebar a { flex-direction: column; gap: 2px; font-size: .7rem; padding: 6px 8px; }
  main { padding: 20px 16px 96px; }
  h1 { font-size: 1.45rem; }
}

/* ---------- surfaces ---------- */
.grid { display: grid; gap: 20px; }
.grid.home { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); align-items: start; }
@media (max-width: 1000px) { .grid.home { grid-template-columns: 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.stack { display: grid; gap: 20px; }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: .8rem; font-weight: 550; }
.stat .value { font-size: 1.35rem; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 560px) { .stats { grid-template-columns: 1fr 1fr; } .stats .stat:last-child { grid-column: span 2; } }

/* ---------- buttons & forms ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 600; font-size: .9rem;
  cursor: pointer; text-decoration: none; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: .8rem; border-radius: 8px; }
.btn.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

label.field { display: grid; gap: 6px; font-size: .85rem; font-weight: 600; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="color"] { padding: 3px; height: 40px; cursor: pointer; }
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 550; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .82rem; font-weight: 600; padding: 5px 10px; border-radius: 7px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- credit card visual ---------- */
.cc {
  --c: #6366f1;
  position: relative; overflow: hidden; color: #fff; border-radius: 18px; padding: 18px 20px;
  aspect-ratio: 1.586; display: flex; flex-direction: column; justify-content: space-between;
  background:
    radial-gradient(120% 90% at 100% 0%, rgb(255 255 255 / .28), transparent 55%),
    linear-gradient(135deg, var(--c), color-mix(in srgb, var(--c) 55%, #0b0e17));
  box-shadow: 0 10px 30px -10px color-mix(in srgb, var(--c) 70%, transparent);
}
.cc::after { content: ""; position: absolute; width: 220px; height: 220px; border-radius: 50%; right: -70px; bottom: -110px; background: rgb(255 255 255 / .08); }
.cc .bank { font-size: .8rem; font-weight: 600; opacity: .85; text-transform: uppercase; letter-spacing: .08em; }
.cc .name { font-size: 1.2rem; font-weight: 700; }
.cc .chip { width: 38px; height: 28px; border-radius: 6px; background: linear-gradient(135deg, #fde68a, #d97706); opacity: .9; }
.cc .digits { font-family: ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .14em; opacity: .9; font-size: .95rem; }
.cc .foot { display: flex; justify-content: space-between; align-items: flex-end; gap: 8px; position: relative; z-index: 1; }
.cc .meta { font-size: .72rem; opacity: .8; text-transform: uppercase; letter-spacing: .06em; }
.cc .meta b { display: block; font-size: .95rem; letter-spacing: 0; text-transform: none; opacity: 1; }

/* ---------- hero (mejor tarjeta) ---------- */
.hero { display: grid; grid-template-columns: 260px 1fr; gap: 24px; align-items: center; }
.hero .cc { max-width: 260px; }
.hero .eyebrow { color: var(--accent); font-weight: 700; font-size: .78rem; text-transform: uppercase; letter-spacing: .08em; }
.hero .big { font-size: 3rem; font-weight: 800; line-height: 1; letter-spacing: -.03em; margin: 8px 0 4px; font-variant-numeric: tabular-nums; }
.hero .big small { font-size: 1rem; font-weight: 600; color: var(--muted); letter-spacing: 0; }
.hero dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 14px 0 0; font-size: .9rem; }
.hero dt { color: var(--muted); }
.hero dd { margin: 0; font-weight: 600; }
@media (max-width: 640px) { .hero { grid-template-columns: 1fr; } .hero .cc { max-width: 100%; } }

.ranking { display: grid; gap: 2px; margin-top: 6px; }
.rank-row { display: grid; grid-template-columns: 22px 1fr auto; align-items: center; gap: 12px; padding: 10px 6px; border-radius: 10px; }
.rank-row:hover { background: var(--surface-2); }
.rank-row .pos { color: var(--muted); font-weight: 700; font-size: .85rem; text-align: center; }
.rank-row .title { display: flex; align-items: center; gap: 8px; font-weight: 600; min-width: 0; }
.rank-row .title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-row .bar { height: 6px; border-radius: 999px; background: var(--surface-2); margin-top: 6px; overflow: hidden; }
.rank-row .bar i { display: block; height: 100%; border-radius: inherit; background: var(--c, var(--accent)); }
.rank-row .days { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.rank-row .days small { display: block; font-weight: 500; color: var(--muted); font-size: .75rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c, var(--accent)); flex: none; }

.tip { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border-radius: 12px; background: var(--warn-soft); color: var(--text); font-size: .87rem; margin-top: 12px; }
.tip svg { width: 18px; height: 18px; flex: none; color: var(--warn); margin-top: 1px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- pagos ---------- */
.pay-list { display: grid; gap: 10px; }
.pay {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 14px; align-items: center;
  padding: 12px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface);
  border-left: 4px solid var(--c);
}
.pay .when { text-align: center; line-height: 1.1; background: var(--surface-2); border-radius: 10px; padding: 6px 0; }
.pay .when b { display: block; font-size: 1.2rem; }
.pay .when span { font-size: .7rem; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.pay .info { min-width: 0; }
.pay .info .t { font-weight: 650; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pay .info .s { color: var(--muted); font-size: .82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pay .right { text-align: right; display: grid; gap: 6px; justify-items: end; }
.pay .amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.pay .amount.none { color: var(--muted); font-weight: 500; font-size: .85rem; }
.pay .actions { display: flex; gap: 6px; }
.pay.is-paid { opacity: .6; }
.pay.is-paid .amount { text-decoration: line-through; }
@media (max-width: 520px) {
  .pay { grid-template-columns: 46px 1fr; }
  .pay .right { grid-column: 1 / -1; grid-template-columns: 1fr auto; align-items: center; justify-items: stretch; }
  .pay .amount { text-align: left; }
}

.pill { display: inline-flex; align-items: center; font-size: .7rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; text-transform: uppercase; letter-spacing: .04em; }
.pill.overdue { background: var(--danger-soft); color: var(--danger); }
.pill.today { background: var(--danger); color: #fff; }
.pill.soon { background: var(--warn-soft); color: var(--warn); }
.pill.upcoming { background: var(--surface-2); color: var(--muted); }
.pill.paid { background: var(--ok-soft); color: var(--ok); }

.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty svg { width: 44px; height: 44px; stroke: currentColor; fill: none; stroke-width: 1.5; opacity: .6; margin-bottom: 8px; }
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty .btn { margin-top: 14px; }

/* ---------- tarjetas ---------- */
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 20px; }
.card-item { display: grid; gap: 12px; }
.card-item .facts { display: flex; justify-content: space-between; gap: 8px; font-size: .85rem; }
.card-item .facts div { display: grid; }
.card-item .facts span { color: var(--muted); font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.card-item .facts b { font-weight: 650; }
.card-item .actions { display: flex; gap: 6px; }

/* ---------- pagos fijos ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; font-weight: 650; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.table td { padding: 12px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table .actions { text-align: right; white-space: nowrap; }
@media (max-width: 640px) {
  .table thead { display: none; }
  .table tr { display: grid; grid-template-columns: 1fr auto; padding: 10px 0; border-bottom: 1px solid var(--border); }
  .table td { border: 0; padding: 2px 6px; }
  .table td.hide-sm { display: none; }
}

/* ---------- calendario ---------- */
.cal-head { display: flex; align-items: center; gap: 8px; }
.cal-head h2 { min-width: 170px; text-align: center; }
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.cal .dow { text-align: center; font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; padding-bottom: 4px; }
.cal .day {
  min-height: 92px; border: 1px solid var(--border); border-radius: 12px; padding: 6px; background: var(--surface);
  display: flex; flex-direction: column; gap: 3px; cursor: pointer; text-align: left; font: inherit; color: inherit; min-width: 0;
}
.cal .day:hover { border-color: var(--accent); }
.cal .day.out { opacity: .35; }
.cal .day.today .n { background: var(--accent); color: #fff; }
.cal .day.sel { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.cal .n { font-size: .8rem; font-weight: 700; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; }
.cal .ev { font-size: .7rem; font-weight: 600; padding: 1px 6px; border-radius: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: color-mix(in srgb, var(--c) 16%, transparent); color: color-mix(in srgb, var(--c) 75%, var(--text)); }
.cal .ev.cutoff { background: transparent; border: 1px dashed color-mix(in srgb, var(--c) 60%, transparent); }
.cal .ev.paid { text-decoration: line-through; opacity: .6; }
.cal .dots { display: none; gap: 3px; flex-wrap: wrap; }
.cal .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--c); }
.cal .dots i.cutoff { background: transparent; box-shadow: inset 0 0 0 1.5px var(--c); }
@media (max-width: 720px) {
  .cal { gap: 3px; }
  .cal .day { min-height: 52px; padding: 4px; align-items: center; }
  .cal .ev { display: none; }
  .cal .dots { display: flex; justify-content: center; }
}
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: .8rem; color: var(--muted); margin-top: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 10px; border-radius: 4px; background: var(--accent-soft); }
.legend i.cut { background: transparent; border: 1px dashed var(--muted); }
.day-detail .ev-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: .9rem; }
.day-detail .ev-row:last-child { border-bottom: 0; }

/* ---------- modal & toast ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 20px; padding: 0; background: var(--surface); color: var(--text);
  width: min(480px, calc(100vw - 32px)); box-shadow: 0 24px 60px rgb(0 0 0 / .25);
}
dialog::backdrop { background: rgb(15 23 42 / .45); backdrop-filter: blur(3px); }
dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
#modal-form { padding: 22px; display: grid; gap: 14px; }
#modal-form .modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
#modal-form .error { color: var(--danger); font-size: .85rem; min-height: 1em; margin: 0; }
.hint { font-size: .78rem; color: var(--muted); font-weight: 500; }
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; font-size: .9rem;
  transition: opacity .2s, transform .2s; z-index: 50; box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (max-width: 820px) { #toast { bottom: 88px; } }

.settings-list { display: grid; gap: 0; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.setting:last-child { border-bottom: 0; }
.setting .txt { max-width: 520px; }
.setting .txt b { display: block; }
.setting input { width: 110px; }
code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: .85em; }
pre.code { background: var(--surface-2); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: .8rem; margin: 8px 0 0; }

/* ---------- acceso ---------- */
body.auth-mode { background: radial-gradient(70% 55% at 50% 0%, var(--accent-soft), var(--bg)); }
body.auth-mode .layout { grid-template-columns: 1fr; }
body.auth-mode .sidebar { display: none; }
body.auth-mode main { max-width: none; min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card {
  width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 22px; box-shadow: var(--shadow); padding: 28px; display: grid; gap: 16px;
}
.auth-card h1 { font-size: 1.4rem; }
.auth-card .logo { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 1.1rem; }
.auth-card .logo b { color: var(--accent); }
.auth-card .error { color: var(--danger); font-size: .85rem; margin: 0; min-height: 1em; }
.btn:disabled { opacity: .6; cursor: progress; }

/* ---------- usuario ---------- */
.avatar {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: .9rem;
}
.side-user { margin-top: auto; display: flex; align-items: center; gap: 10px; padding: 12px 8px 4px; border-top: 1px solid var(--border); }
.side-user .who { display: grid; min-width: 0; flex: 1; line-height: 1.25; }
.side-user .who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }
.side-user .who span { color: var(--muted); font-size: .75rem; }
.side-user svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.user-chip { display: flex; align-items: center; gap: 8px; }
.mobile-only { display: none; }
@media (max-width: 820px) {
  .side-user { display: none; }
  .mobile-only { display: flex; }
}
.pill.open { background: var(--accent-soft); color: var(--accent); }
.card-item .actions { flex-wrap: wrap; }
