:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --text: #17201b;
  --muted: #66736c;
  --border: #dde4e0;
  --accent: #0f7a4a;
  --accent-ink: #ffffff;
  --pos: #11804d;
  --neg: #c2362b;
  --warn: #a8740a;
  --pos-soft: rgba(17, 128, 77, .14);
  --neg-soft: rgba(194, 54, 43, .14);
  --warn-soft: rgba(217, 162, 27, .16);
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --surface: #171e1b;
    --surface-2: #1f2824;
    --text: #e6ece9;
    --muted: #8d9a93;
    --border: #2a3530;
    --accent: #2fbf7a;
    --accent-ink: #06130c;
    --pos: #46c98a;
    --neg: #ff7468;
    --warn: #f0c04a;
    --pos-soft: rgba(70, 201, 138, .16);
    --neg-soft: rgba(255, 116, 104, .16);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .9rem; }
small { color: var(--muted); }

/* ---------- Barra superior / navegación ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 1.5rem;
  padding: .55rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: .5rem; white-space: nowrap; }
.brand img { border-radius: 7px; }
.brand span { color: var(--accent); }
.tabs { display: flex; gap: .25rem; flex: 1; }
.tabs button {
  display: flex; align-items: center; gap: .4rem;
  border: 0; background: none; color: var(--muted);
  padding: .5rem .75rem; border-radius: 9px; font: inherit; cursor: pointer; white-space: nowrap;
}
.tabs svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs button:hover { color: var(--text); background: var(--surface-2); }
.tabs button.active { color: var(--accent); background: var(--surface-2); font-weight: 600; }
.logout { margin-left: auto; }

main { max-width: 1180px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }
.tab { display: none; }
.tab.active { display: block; }

/* ---------- Tarjetas y formularios ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1rem;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .8rem 1rem; flex-wrap: wrap; margin-bottom: .6rem; }
.card-head h2 { margin: 0; }
.inline { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }

details.form-card { padding: 0; }
details.form-card > summary, details.more > summary {
  list-style: none; cursor: pointer; user-select: none;
  font-weight: 600; color: var(--accent);
}
details.form-card > summary { padding: 1rem 1.2rem; }
details.form-card > summary::-webkit-details-marker, details.more > summary::-webkit-details-marker { display: none; }
details.form-card[open] > summary { color: var(--text); padding-bottom: .2rem; }
details.form-card > form { padding: 0 1.2rem 1.2rem; }
details.more { margin-top: 1rem; border-top: 1px solid var(--border); padding-top: .9rem; }
details.more > summary::before { content: "▸ "; }
details.more[open] > summary::before { content: "▾ "; }

label { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; color: var(--muted); }
label.compact { flex-direction: row; align-items: center; gap: .45rem; }
label.compact input { width: 4.5rem; }
label.check { flex-direction: row; align-items: center; gap: .5rem; color: var(--text); font-size: .92rem; min-height: 44px; }

input, select {
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: .55rem .7rem;
  min-width: 0; min-height: 42px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type="checkbox"] { width: 1.25rem; height: 1.25rem; min-height: 0; accent-color: var(--accent); flex: none; }

button {
  font: inherit; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 9px; padding: .55rem 1rem; min-height: 42px;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: .35; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.block { width: 100%; }
button.ghost { background: none; padding: .3rem .6rem; font-size: .88rem; min-height: 36px; }
button.danger { color: var(--neg); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .8rem; align-items: end; margin-top: .8rem; }
.form-grid .wide { grid-column: span 2; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .9rem; }
.form-grid .actions { margin-top: 0; }
fieldset { border: 1px solid var(--border); border-radius: 9px; padding: .4rem .8rem; margin: 0; }
legend { font-size: .82rem; color: var(--muted); padding: 0 .3rem; }
.checks { display: flex; flex-wrap: wrap; gap: 0 1.2rem; }
.hint { color: var(--muted); font-size: .85rem; margin: .4rem 0 .8rem; }
.empty { text-align: center; color: var(--muted); padding: 1.4rem .5rem; margin: 0; }

/* ---------- Calculadora rápida ---------- */
.calc-quick .calc-row { display: flex; align-items: flex-end; gap: .7rem; }
.calc-quick label { flex: 1; }
.calc-quick label:nth-child(3) { flex: 0 0 6.5rem; }
.calc-quick input { font-size: 1.7rem; width: 100%; padding: .4rem .7rem; font-variant-numeric: tabular-nums; }
.op { font-size: 1.6rem; color: var(--muted); padding-bottom: .55rem; }
.result {
  display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; flex-wrap: wrap;
  margin-top: 1rem; padding: .8rem 1rem; border-radius: 12px; background: var(--pos-soft);
}
.result small { font-size: .9rem; color: var(--text); }
.result strong {
  font-size: 2.3rem; line-height: 1.1; color: var(--accent);
  font-variant-numeric: tabular-nums; letter-spacing: -.02em;
}
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .9rem; }
.chips button { padding: .3rem .85rem; border-radius: 999px; font-size: .95rem; min-height: 38px; }
.chips button.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* ---------- Stake por cuenta ---------- */
.select-all { min-height: 36px; font-size: .85rem; color: var(--muted); }
.stake-list { border-top: 1px solid var(--border); }
.stake-row {
  display: grid; grid-template-columns: auto 1fr 7.5rem auto; gap: .7rem; align-items: center;
  padding: .55rem 0; border-bottom: 1px solid var(--border);
}
.stake-name { display: flex; flex-direction: column; min-width: 0; }
.stake-name b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stake-name .tag { align-self: flex-start; }
.stake-row .m-stake { width: 100%; font-size: 1.1rem; font-weight: 600; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; margin: 0 -1.2rem; padding: 0 1.2rem; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
tr.muted td, .muted { opacity: .55; }
tr.pending td:first-child { box-shadow: inset 3px 0 0 #d9a21b; }
td.empty { text-align: center; color: var(--muted); padding: 1.6rem; }
.row-actions { white-space: nowrap; text-align: right; }
.num-input { width: 6.5rem; text-align: right; padding: .35rem .5rem; font-variant-numeric: tabular-nums; }
.resolve { display: inline-flex; gap: .2rem; align-items: center; }
.resolve button { min-width: 38px; font-size: 1rem; }
.shares { font-size: .85rem; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.tag {
  display: inline-block; font-size: .72rem; padding: .05rem .45rem; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); vertical-align: middle;
}
.tag.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; font-weight: 600; }
.tag.ok { background: var(--pos-soft); color: var(--pos); border-color: transparent; }

.totals { display: flex; gap: .3rem 1.2rem; flex-wrap: wrap; font-size: .88rem; color: var(--muted); margin-bottom: .6rem; }
.totals b { color: var(--text); font-variant-numeric: tabular-nums; }
.totals b.pos { color: var(--pos); } .totals b.neg { color: var(--neg); }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; background: none; color: var(--muted); }
.segmented button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .8rem; margin: .4rem 0 1rem; }
.kpi { background: var(--surface-2); border-radius: 12px; padding: .8rem 1rem; display: flex; flex-direction: column; gap: .15rem; }
.kpi strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi span { font-size: .8rem; color: var(--muted); }

.net-cell { position: relative; min-width: 9rem; }
.net-cell b { position: relative; }
.net-cell .bar { position: absolute; right: .3rem; top: 22%; bottom: 22%; border-radius: 4px; }
.bar.pos { background: var(--pos-soft); }
.bar.neg { background: var(--neg-soft); }

/* ---------- Cuentas ---------- */
.acc-totals { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; margin-bottom: .8rem; }
.acc-totals > div { background: var(--surface-2); border-radius: 12px; padding: .6rem .8rem; display: flex; flex-direction: column; }
.acc-totals strong { font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.acc-totals:empty { display: none; }
.acc-list { border-top: 1px solid var(--border); }
.acc-item {
  display: grid; grid-template-columns: auto 1fr auto auto; gap: .4rem 1rem; align-items: center;
  padding: .7rem 0; border-bottom: 1px solid var(--border);
}
.acc-order { display: flex; flex-direction: column; }
.acc-order button { min-height: 30px; padding: 0 .5rem; font-size: 1rem; }
.acc-main { min-width: 0; display: flex; flex-direction: column; gap: .15rem; }
.acc-name { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; font-size: 1.02rem; }
.acc-figures { text-align: right; display: flex; flex-direction: column; }
.acc-figures strong { font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.acc-actions { display: flex; gap: .2rem; }

/* ---------- Login ---------- */
.login-page { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 1rem; }
.login-card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 1.8rem 1.5rem; display: flex; flex-direction: column; gap: 1rem;
}
.login-card img { align-self: center; border-radius: 14px; }
.login-card h1 { text-align: center; margin-bottom: .3rem; }
.login-card h1 span { color: var(--accent); }
.login-card input { font-size: 1rem; }
.login-card button { min-height: 48px; font-size: 1rem; }
.login-error { color: var(--neg); margin: 0; min-height: 1.2em; font-size: .9rem; }

.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translate(-50%, 1rem); z-index: 20;
  background: var(--text); color: var(--bg); padding: .7rem 1.1rem; border-radius: 12px;
  opacity: 0; pointer-events: none; transition: .2s; font-size: .92rem; max-width: calc(100% - 2rem);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--neg); color: #fff; }

/* =========================================================
   MÓVIL
   ========================================================= */
@media (max-width: 760px) {
  body { font-size: 16px; }
  .topbar { padding: .55rem 1rem; padding-top: calc(.55rem + env(safe-area-inset-top)); }

  /* Navegación abajo, al alcance del pulgar */
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding: 0 .2rem env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--border);
    gap: 0; justify-content: space-around;
  }
  .tabs button {
    flex: 1; flex-direction: column; justify-content: center; gap: .15rem;
    padding: .3rem 0; border-radius: 0; font-size: .68rem; min-height: 0;
  }
  .tabs button.active { background: none; }
  .tabs svg { width: 22px; height: 22px; }

  main { padding: .8rem .75rem calc(var(--nav-h) + 1.5rem + env(safe-area-inset-bottom)); }
  .toast { bottom: calc(var(--nav-h) + 1rem + env(safe-area-inset-bottom)); }
  .card { padding: 1rem; border-radius: 16px; }
  details.form-card > summary { padding: 1rem; }
  details.form-card > form { padding: 0 1rem 1rem; }
  .table-wrap { margin: 0; padding: 0; overflow: visible; }

  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-grid .wide, .form-grid fieldset, .form-grid .actions { grid-column: 1 / -1; }
  .form-grid .actions button { flex: 1; }
  .inline.filters { width: 100%; display: grid; grid-template-columns: 1fr 1fr; }
  .inline.filters > * { width: 100%; }

  .calc-quick input { font-size: 1.5rem; }
  .result strong { font-size: 2.1rem; }
  .chips { display: grid; grid-template-columns: repeat(4, 1fr); }

  .stake-row { grid-template-columns: auto 1fr 6.5rem; }
  .stake-row .m-copy { display: none; }

  /* Tablas → tarjetas */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody { display: block; }
  table.responsive tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: .1rem .8rem;
    padding: .7rem 0; border-bottom: 1px solid var(--border);
  }
  table.responsive tbody tr:hover { background: none; }
  table.responsive td {
    display: flex; flex-direction: column; align-items: flex-start; gap: 0;
    border: 0; padding: .2rem 0; white-space: normal; text-align: left;
  }
  table.responsive td::before {
    content: attr(data-label); font-size: .68rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
  }
  table.responsive td:not([data-label])::before, table.responsive td[data-label=""]::before { content: none; }
  table.responsive td:first-child { grid-column: 1 / -1; font-weight: 600; }
  table.responsive td:first-child::before { content: none; }
  table.responsive td.row-actions { grid-column: 1 / -1; flex-direction: row; justify-content: flex-end; gap: .3rem; margin-top: .2rem; }
  table.responsive td:empty { display: none; }
  table.responsive td.empty { grid-column: 1 / -1; align-items: center; }
  table.responsive td.empty::before { content: none; }
  table.responsive td.shares { grid-column: 1 / -1; }
  table.responsive tr.pending td:first-child { box-shadow: none; }
  table.responsive tr.pending { box-shadow: inset 3px 0 0 #d9a21b; padding-left: .7rem; }
  table.responsive .resolve { flex-wrap: wrap; }
  table.responsive .resolve .num-input { width: 6rem; }
  .net-cell { min-width: 0; }
  .net-cell .bar { display: none; }

  .acc-totals { grid-template-columns: repeat(3, 1fr); gap: .4rem; }
  .acc-totals > div { padding: .5rem .6rem; }
  .acc-totals strong { font-size: 1rem; }
  .acc-item { grid-template-columns: auto 1fr auto; }
  .acc-actions { grid-column: 2 / -1; justify-content: flex-end; }
}

@media (max-width: 380px) {
  .brand { font-size: .95rem; }
  .acc-totals { grid-template-columns: 1fr 1fr; }
}

/* El formulario de cuenta solo aparece al pulsar «+ Nueva cuenta» o «Editar» */
#acc-card:not([open]) { display: none; }

/* En pantalla grande, la calculadora rápida no se estira de lado a lado */
.calc-quick .calc-row, .calc-quick .chips, .calc-quick .result, .calc-quick .hint { max-width: 560px; }
.acc-order button { display: grid; place-items: center; width: 34px; }
.acc-order svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Campos: nunca más anchos que su hueco ---------- */
.form-grid { grid-template-columns: repeat(auto-fill, minmax(min(180px, 100%), 1fr)); }
.form-grid > *, .inline.filters > * { min-width: 0; }
.form-grid input:not([type="checkbox"]), .form-grid select { width: 100%; }
select { max-width: 100%; text-overflow: ellipsis; }
input[type="date"] { -webkit-appearance: none; appearance: none; min-width: 0; }
input[type="date"]::-webkit-date-and-time-value { text-align: left; }
.form-grid .check { align-self: center; }

@media (max-width: 760px) {
  /* 16 px evita que el iPhone haga zoom al tocar un campo */
  input, select { font-size: 16px; min-height: 44px; padding: .5rem .65rem; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7rem .6rem; }
  .inline.filters { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
  .calc-quick input { font-size: 1.4rem; }
  .card-head .inline:not(.filters) { width: 100%; }
}
@media (max-width: 340px) {
  .form-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  table.responsive td.resolve-cell { grid-column: 1 / -1; }
  table.responsive .resolve { flex-wrap: nowrap; width: 100%; }
  table.responsive .resolve .num-input { flex: 1; width: auto; }
  table.responsive .resolve button { flex: 0 0 48px; min-height: 44px; }
}
