/* Gestión Patrimonial — estilos base.
   Filosofía (spec 7.1/7.2): consola clara, poco ruido, lectura rápida.
   Sin alarmas rojas automáticas (7.7): los estados usan tonos neutros. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #5f6b7a;
  --border: #e3e7ec;
  --accent: #2f6f5e;
  --accent-soft: #e4f1ec;
  --track: #e9edf1;
  --notice-bg: #fff8e6;
  --notice-border: #f0dca8;
  --warn: #8a5a00;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151a;
    --surface: #181d24;
    --text: #e6e9ee;
    --muted: #9aa5b1;
    --border: #2a313b;
    --accent: #6cc2a6;
    --accent-soft: #1d3a31;
    --track: #262d36;
    --notice-bg: #2a2416;
    --notice-border: #4a3d1c;
    --warn: #e0b35a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.page { max-width: 980px; margin: 0 auto; padding: 16px 16px 48px; }

h1, h2, h3 { line-height: 1.25; margin: 0; }
h2 { font-size: 1.2rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---------- barra superior ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 980px; margin: 0 auto; padding: 12px 16px;
  display: flex; align-items: center; gap: 16px;
}
.brand { font-weight: 650; color: var(--text); text-decoration: none; }
.nav { display: flex; flex-wrap: wrap; gap: 6px 14px; flex: 1; min-width: 0; }
.nav a { color: var(--muted); text-decoration: none; }
.nav a:hover { color: var(--text); }
.link-button {
  background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; padding: 4px;
}
.link-button:hover { color: var(--text); }

/* ---------- tarjetas ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; margin-top: 16px;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.note { color: var(--muted); font-size: 0.8rem; margin: 12px 0 0; }
.notice {
  background: var(--notice-bg); border: 1px solid var(--notice-border); border-radius: 8px;
  padding: 10px 12px; font-size: 0.9rem; margin: 0 0 12px;
}

/* ---------- cajones ---------- */
.buckets { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
@media (min-width: 720px) { .buckets { grid-template-columns: 1fr 1fr; } }

.bucket { border: 1px solid var(--border); border-radius: 10px; padding: 14px; }
.bucket-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.bucket-name { font-weight: 600; }
.prio {
  display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 8px;
  border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 0.8rem;
}
.status { font-size: 0.8rem; padding: 2px 8px; border-radius: 999px; background: var(--track); color: var(--muted); white-space: nowrap; }
.status-completed, .status-on_track, .status-ahead { background: var(--accent-soft); color: var(--accent); }

progress {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 10px; margin: 10px 0 6px; border: 0; border-radius: 999px;
  background: var(--track); overflow: hidden;
}
progress::-webkit-progress-bar { background: var(--track); border-radius: 999px; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 999px; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 999px; }

.bucket-figures { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
.bucket-meta { margin: 10px 0 0; display: grid; gap: 4px; font-size: 0.88rem; }
.bucket-meta div { display: flex; justify-content: space-between; gap: 12px; }
.bucket-meta dt { color: var(--muted); }
.bucket-meta dd { margin: 0; text-align: right; }

/* ---------- cartera ---------- */
.group-title { font-size: 1rem; margin: 18px 0 8px; }
.table-wrap { overflow-x: auto; }
.positions { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; table-layout: fixed; }
.col-asset { width: 43%; }
.col-qty { width: 15%; }
.col-avg { width: 20%; }
.col-total { width: 22%; }
.m-label { display: none; }
.positions th, .positions td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.positions thead th { color: var(--muted); font-weight: 500; font-size: 0.85rem; }
.positions tbody th { font-weight: 400; }
.positions tfoot th, .positions tfoot td { font-weight: 600; border-bottom: 0; }
.positions .num { text-align: right; white-space: nowrap; }
.ticker { font-weight: 600; margin-right: 6px; }
.asset-name { color: var(--muted); font-size: 0.88rem; }
.flag { display: block; color: var(--warn); font-size: 0.8rem; }

.status-pending { background: var(--notice-bg); color: var(--warn); }

/* Móvil: cada posición pasa a ser una ficha de dos líneas
   (activo · coste total / cantidad · coste medio), sin scroll lateral. */
@media (max-width: 600px) {
  .topbar-inner { gap: 10px; }
  .brand { font-size: 0.95rem; }
  .positions, .positions tbody, .positions tfoot { display: block; }
  .positions colgroup, .positions thead { display: none; }
  .positions tr {
    display: grid; grid-template-columns: 1fr auto; column-gap: 12px; row-gap: 2px;
    padding: 10px 0; border-bottom: 1px solid var(--border);
  }
  .positions th, .positions td { padding: 0; border: 0; font-size: 0.92rem; }
  .cell-asset { grid-column: 1; grid-row: 1; }
  .cell-total { grid-column: 2; grid-row: 1; font-weight: 600; }
  .cell-qty { grid-column: 1; grid-row: 2; text-align: left !important; color: var(--muted); font-size: 0.82rem !important; }
  .cell-avg { grid-column: 2; grid-row: 2; color: var(--muted); font-size: 0.82rem !important; }
  .cell-empty { display: none; }
  .m-label { display: inline; margin-right: 4px; }
  .asset-name { display: block; }
  .positions tfoot tr { border-bottom: 0; padding: 6px 0; }
}

/* ---------- login ---------- */
.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login-form { display: grid; gap: 8px; margin-top: 24px; text-align: left; }
.login-form input {
  font: inherit; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
}
.login-form button {
  font: inherit; padding: 10px 12px; border: 0; border-radius: 8px; margin-top: 8px;
  background: var(--accent); color: #fff; cursor: pointer;
}
.form-error { color: var(--warn); margin: 0; font-size: 0.9rem; }

/* ---------- registrar ingreso ---------- */
.home-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.button, button.primary, .stack-form button, .inline-form button, .actions button {
  display: inline-block; font: inherit; padding: 9px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); cursor: pointer; text-decoration: none;
}
.button.primary, button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button[disabled] { opacity: 0.45; cursor: not-allowed; }
.card.narrow { max-width: 560px; margin-left: auto; margin-right: auto; }
.stack-form { display: grid; gap: 6px; margin-top: 16px; }
.stack-form label { margin-top: 8px; font-size: 0.9rem; }
.stack-form input, .inline-form input, .stack-form select, .inline-form select {
  font: inherit; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); width: 100%;
}
.stack-form button { margin-top: 16px; }
.input-suffix { display: flex; align-items: center; gap: 8px; }
.input-suffix span { color: var(--muted); }
.allocation { list-style: none; padding: 0; margin: 16px 0; font-variant-numeric: tabular-nums; }
.allocation li { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.inline-form { display: grid; gap: 6px; margin-top: 10px; }
.inline-form label { font-size: 0.88rem; }
.actions { display: flex; align-items: center; gap: 16px; margin-top: 18px; }
.success { background: var(--accent-soft); color: var(--accent); border-radius: 8px; padding: 10px 12px; margin: 0 0 8px; }
.form-error.box { background: var(--notice-bg); border: 1px solid var(--notice-border); border-radius: 8px; padding: 10px 12px; }

/* Ajustes de móvil estrecho */
.bucket-figures { gap: 12px; }
.bucket-figures span:last-child { white-space: nowrap; }
@media (max-width: 480px) {
  .brand { display: none; }
  .nav { gap: 6px 14px; }
}

/* ---------- reglas ---------- */
.rule { padding: 14px 0; border-top: 1px solid var(--border); }
.card h2 + .rule { border-top: 0; }
.rule-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.rule-head h3 { font-size: 1rem; }
.rule p { margin: 8px 0; }
.rule-headline { font-size: 1.05rem; font-weight: 600; color: var(--accent); }
.simple { width: 100%; border-collapse: collapse; margin: 10px 0; font-variant-numeric: tabular-nums; }
.simple th, .simple td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.simple thead th { color: var(--muted); font-weight: 500; font-size: 0.85rem; }
.simple .num { text-align: right; }
.origins { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { font-size: 0.78rem; padding: 2px 8px; border-radius: 999px; background: var(--track); color: var(--muted); }

/* ---------- historial de ingresos ---------- */
.big-figure { font-size: 1.6rem; font-weight: 650; margin: 4px 0 8px; font-variant-numeric: tabular-nums; }
.allocation.compact { margin: 6px 0; }
.allocation.compact li { padding: 6px 0; font-size: 0.92rem; }
.income-list { list-style: none; padding: 0; margin: 8px 0 0; }
.income-list > li { padding: 12px 0; border-top: 1px solid var(--border); }
.income-list > li:first-child { border-top: 0; }
.income-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* ---------- ficha de empresa ---------- */
.company-title { font-size: 1.5rem; margin-top: 4px; }
.kv { display: grid; gap: 6px; margin: 10px 0 0; }
.kv div { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--border); padding: 6px 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
a.ticker { color: inherit; text-decoration: underline; text-decoration-color: var(--border); text-underline-offset: 3px; }
a { color: var(--accent); }

/* ---------- editar tesis ---------- */
.stack-form textarea, .stack-form select {
  font: inherit; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); width: 100%;
}
.weights { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 12px; margin: 10px 0 0; display: grid; gap: 6px; }
.weights legend { font-size: 0.9rem; padding: 0 4px; }

.simple .num { white-space: nowrap; }
@media (max-width: 480px) {
  .simple th, .simple td { padding: 8px 4px; font-size: 0.88rem; }
}
.allocation li > span:last-child { white-space: nowrap; padding-left: 8px; }

/* ---------- lista de comprobación (¿se puede comprar/ampliar?) ---------- */
.checklist { list-style: none; padding: 0; margin: 8px 0; }
.checklist li { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--border); }
.check-mark { flex: 0 0 22px; height: 22px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; }
.check-ok .check-mark { background: var(--accent-soft); color: var(--accent); }
.check-fail .check-mark { background: var(--notice-bg); color: var(--warn); }
.check-unknown .check-mark { background: var(--border); color: var(--muted); }

.pre { white-space: pre-wrap; }
.stack-form textarea { font: inherit; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); width: 100%; box-sizing: border-box; resize: vertical; }
.row-set { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin: 0 0 8px; display: grid; gap: 6px; }
.row-set input, .row-set select { width: 100%; box-sizing: border-box; }
.inline-form input[type=checkbox], .stack-form input[type=checkbox] { width: auto; margin-right: 6px; }
.inline-form { display: grid; gap: 6px; margin-top: 12px; }
.stack-form fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin: 4px 0; display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; }
.stack-form fieldset legend { font-size: 0.9rem; padding: 0 4px; font-weight: 600; }
.stack-form fieldset input:not([type=radio]), .stack-form fieldset textarea { flex: 1 1 100%; }
.stack-form input[type=radio] { width: auto; margin-right: 4px; }
.stack-form details summary { cursor: pointer; font-weight: 600; margin: 8px 0; }
td.ok { color: var(--accent); }
