:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fbfbfc;
  --ink: #16181d;
  --ink-2: #5b616e;
  --ink-3: #8b909c;
  --line: #e3e5ea;
  --line-strong: #c9ccd4;
  --accent: #1f5cd8;
  --accent-soft: #eaf0fd;
  --ok: #1a7f4b;
  --ok-soft: #e6f4ec;
  --warn: #9a6300;
  --warn-soft: #fdf1dc;
  --danger: #b3261e;
  --danger-soft: #fceceb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 18, 23, 0.06), 0 4px 12px rgba(16, 18, 23, 0.04);
}

/* El tema lo fija un script en el <head> (data-theme="light" | "dark") a partir de lo que
   eligió la persona o, si nunca eligió, de la preferencia del sistema. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1116;
  --surface: #171a21;
  --surface-2: #1c2029;
  --ink: #e8eaee;
  --ink-2: #a2a8b5;
  --ink-3: #767d8c;
  --line: #272b35;
  --line-strong: #3a3f4c;
  --accent: #6f9bff;
  --accent-soft: #1b2437;
  --ok: #56d394;
  --ok-soft: #14271e;
  --warn: #e2b062;
  --warn-soft: #2a2113;
  --danger: #f28b82;
  --danger-soft: #2d1a19;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

h1 {
  font-size: 22px;
  margin: 0;
  letter-spacing: -0.01em;
}
h2 {
  font-size: 15px;
  margin: 0 0 12px;
  letter-spacing: -0.005em;
}
h3 {
  font-size: 13px;
  margin: 0 0 8px;
}

/* ---------------------------------------------------------------- estructura */

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 22px;
  height: 54px;
}

.brand {
  font-weight: 650;
  color: var(--ink);
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand:hover {
  text-decoration: none;
}
.brand span {
  color: var(--ink-3);
  font-weight: 400;
}

.nav {
  display: flex;
  gap: 2px;
  flex: 1;
  overflow-x: auto;
}

.nav a {
  color: var(--ink-2);
  padding: 6px 10px;
  border-radius: 7px;
  white-space: nowrap;
}
.nav a:hover {
  background: var(--surface-2);
  color: var(--ink);
  text-decoration: none;
}
.nav a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 550;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink-3);
  font-size: 13px;
  white-space: nowrap;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink-2);
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.theme-toggle:hover {
  color: var(--ink);
  border-color: var(--line-strong);
}
.theme-toggle svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* En claro se ofrece la luna (pasar a oscuro); en oscuro, el sol. */
.theme-toggle .icon-sun {
  display: none;
}
:root[data-theme="dark"] .theme-toggle .icon-sun {
  display: block;
}
:root[data-theme="dark"] .theme-toggle .icon-moon {
  display: none;
}
.theme-toggle.floating {
  position: fixed;
  top: 14px;
  right: 14px;
}

.page {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 20px 72px;
}

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.page-head p {
  margin: 4px 0 0;
  color: var(--ink-2);
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

/* -------------------------------------------------------------------- cards */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 18px;
  overflow: hidden;
}

.card-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.card-head h2 {
  margin: 0;
}
.card-body {
  padding: 16px;
}
.card-body > :last-child {
  margin-bottom: 0;
}

.grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  margin-bottom: 18px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-3);
  font-weight: 600;
}
.stat-value {
  font-size: 22px;
  font-weight: 650;
  margin-top: 6px;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat-value.small {
  font-size: 17px;
}
.stat-note {
  color: var(--ink-3);
  font-size: 12px;
  margin-top: 4px;
}

/* ------------------------------------------------------------------- tablas */

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-3);
  font-weight: 600;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

tbody tr:last-child td {
  border-bottom: none;
}
tbody tr:hover {
  background: var(--surface-2);
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.muted {
  color: var(--ink-3);
}
.strong {
  font-weight: 600;
}
.nowrap {
  white-space: nowrap;
}

/* ------------------------------------------------------------------ badges */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid transparent;
}
.badge.neutral {
  background: var(--surface-2);
  color: var(--ink-2);
  border-color: var(--line);
}
.badge.info {
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.ok {
  background: var(--ok-soft);
  color: var(--ok);
}
.badge.warn {
  background: var(--warn-soft);
  color: var(--warn);
}
.badge.danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.pill-usd {
  font-size: 11px;
  color: var(--ink-3);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}

/* ------------------------------------------------------------------ botones */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  font-size: 13.5px;
  font-weight: 550;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover {
  filter: brightness(1.08);
}
.btn.danger {
  color: var(--danger);
  border-color: var(--line-strong);
}
.btn.danger:hover {
  background: var(--danger-soft);
}
.btn.small {
  padding: 4px 9px;
  font-size: 12.5px;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  padding: 0;
}
.btn-link.danger {
  color: var(--danger);
}

.inline-form {
  display: inline;
}

/* ---------------------------------------------------------------- formularios */

.field {
  margin-bottom: 14px;
}

label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 5px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="file"],
select,
textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

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

.hint {
  font-size: 12px;
  color: var(--ink-3);
  margin-top: 4px;
}

.row {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
}

.check {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
}
.check input {
  margin-top: 3px;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}
.check label {
  margin: 0;
  font-weight: 500;
  color: var(--ink);
}

/* -------------------------------------------------------------------- flash */

.flash {
  padding: 11px 14px;
  border-radius: var(--radius);
  margin-bottom: 14px;
  border: 1px solid transparent;
  font-weight: 500;
}
.flash.success {
  background: var(--ok-soft);
  color: var(--ok);
  border-color: currentColor;
}
.flash.error {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: currentColor;
}
.flash.info {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: currentColor;
}

/* -------------------------------------------------------------------- login */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-box {
  width: 100%;
  max-width: 360px;
}
.login-box .card-body {
  padding: 24px;
}
.login-title {
  font-size: 19px;
  font-weight: 650;
  margin-bottom: 4px;
}
.login-sub {
  color: var(--ink-3);
  margin-bottom: 20px;
  font-size: 13px;
}

/* ---------------------------------------------------------- adjuntos de cobros */
.attachments-cell {
  max-width: 220px;
}
.attachment-line {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}
.attachment-line a {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attachment-line .btn-link {
  font-size: 15px;
  line-height: 1;
}
.attachment-upload {
  font-size: 12px;
}
.attachment-upload label {
  cursor: pointer;
}

/* ------------------------------------------------- acciones sobre períodos */
.period-actions {
  font-size: 12.5px;
}
.period-actions .inline-form + .inline-form,
.period-actions .btn-link + .inline-form {
  margin-left: 8px;
}
.period-sub,
.discount-note {
  font-size: 11.5px;
  white-space: nowrap;
}

/* ------------------------------------------------------- mail a mano (Gmail) */
.manual-mail {
  border-top: 1px solid var(--line);
  background: var(--surface-2);
}
.copy-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.copy-row input {
  flex: 1;
}
.mail-body {
  min-height: 260px;
  font-family: inherit;
  white-space: pre-wrap;
}
.manual-mail .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* ------------------------------------------------------------------ diálogos */
.dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  text-align: left;
  white-space: normal;
  font-size: 14px;
}
.dialog::backdrop {
  background: rgba(10, 12, 18, 0.45);
}
.dialog h3 {
  font-size: 15px;
  margin-bottom: 4px;
}
.dialog .section-note {
  margin-bottom: 14px;
}
.dialog .actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.dialog-foot {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

/* -------------------------------------------------------------------- varios */

.empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--ink-3);
}

.section-note {
  color: var(--ink-2);
  margin: 0 0 14px;
}

.alert-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.alert-line:last-child {
  border-bottom: none;
}
.alert-line .who {
  font-weight: 600;
}
.alert-line .what {
  color: var(--ink-2);
  font-size: 13px;
}

.split {
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  align-items: start;
}

@media (max-width: 860px) {
  .split {
    grid-template-columns: 1fr;
  }
}

.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  font-size: 13.5px;
}
.kv dt {
  color: var(--ink-3);
  white-space: nowrap;
}
.kv dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.total-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding-top: 10px;
  margin-top: 10px;
  border-top: 1px solid var(--line);
  font-size: 18px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.filters {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

pre.log {
  white-space: pre-wrap;
  font-size: 12.5px;
  color: var(--ink-2);
  background: var(--surface-2);
  padding: 10px 12px;
  border-radius: 8px;
  margin: 0;
  max-height: 220px;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
