:root {
  --bg: #0f1420;
  --bg-elev: #171e2e;
  --bg-elev2: #1f2740;
  --line: #2a3350;
  --text: #e8ecf6;
  --muted: #8a95b0;
  --accent: #4c8dff;
  --good: #2ecc8f;
  --warn: #f0b429;
  --bad: #ff5c6c;
  --radius: 16px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(12px + env(safe-area-inset-top)) 18px 12px;
  background: linear-gradient(180deg, var(--bg) 70%, transparent);
  backdrop-filter: blur(8px);
}
.topbar h1 { font-size: 22px; margin: 0; letter-spacing: -0.02em; }
.badge {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  background: var(--warn); color: #1a1400; padding: 3px 8px; border-radius: 999px; font-weight: 700;
}

main { padding: 0 16px; }

/* Búsqueda */
.searchrow input {
  width: 100%; padding: 13px 16px; font-size: 16px;
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); outline: none;
}
.searchrow input:focus { border-color: var(--accent); }
.search-results {
  margin-top: 8px; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden;
}
.search-results .sr {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.search-results .sr:last-child { border-bottom: none; }
.search-results .sr:active { background: var(--bg-elev2); }
.sr .sym { font-weight: 700; }
.sr .desc { color: var(--muted); font-size: 13px; margin-left: 10px; text-align: right; flex: 1; }

/* Secciones plegables del Radar */
.group {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 4px 14px; margin-bottom: 12px;
}
.group > summary {
  list-style: none; cursor: pointer; padding: 12px 0; position: relative;
  display: flex; flex-direction: column; gap: 2px;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after {
  content: '▸'; position: absolute; right: 2px; top: 14px; color: var(--muted);
  transition: transform .15s ease;
}
.group[open] > summary::after { transform: rotate(90deg); }
.group .g-title { font-size: 16px; font-weight: 700; }
.group .g-count {
  display: inline-block; min-width: 22px; text-align: center; margin-left: 4px;
  background: var(--bg-elev2); color: var(--muted); border-radius: 999px;
  font-size: 13px; padding: 1px 7px; font-weight: 700;
}
.group .g-sub { font-size: 12px; color: var(--muted); }
.group .cards { margin-top: 0; padding-bottom: 8px; }

/* Tarjetas */
.cards { margin-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; display: flex; align-items: center; gap: 12px; cursor: pointer;
  transition: transform .08s ease;
}
.card:active { transform: scale(.985); }
.card .logo {
  width: 38px; height: 38px; border-radius: 10px; background: #fff; object-fit: contain;
  flex-shrink: 0;
}
.card .logo.placeholder {
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-elev2); color: var(--muted); font-weight: 700; font-size: 14px;
}
.card .info { flex: 1; min-width: 0; }
.card .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.card .right { text-align: right; flex-shrink: 0; }
.card .price { font-weight: 700; font-variant-numeric: tabular-nums; }
.chg { font-size: 13px; font-variant-numeric: tabular-nums; }
.chg.up { color: var(--good); }
.chg.down { color: var(--bad); }
.pos-del {
  flex-shrink: 0; margin-left: 6px; width: 28px; height: 28px; border-radius: 8px;
  background: transparent; border: none; color: var(--muted); font-size: 14px; cursor: pointer;
}
.pos-del:active { background: var(--bg-elev2); color: var(--bad); }

/* Nota (score) */
.score-pill {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; height: 30px; padding: 0 10px; border-radius: 999px;
  font-weight: 800; font-size: 15px; font-variant-numeric: tabular-nums;
}
.s-great { background: rgba(46,204,143,.16); color: var(--good); }
.s-good  { background: rgba(76,141,255,.16); color: var(--accent); }
.s-mid   { background: rgba(240,180,41,.16); color: var(--warn); }
.s-bad   { background: rgba(255,92,108,.16); color: var(--bad); }
.s-na    { background: var(--bg-elev2); color: var(--muted); }

.empty { color: var(--muted); text-align: center; margin: 40px 20px; line-height: 1.5; }

/* Acciones de cartera */
.cartera-actions { margin-top: 14px; }
.btn-import {
  width: 100%; padding: 13px; font-size: 15px; font-weight: 700; cursor: pointer;
  background: var(--bg-elev); color: var(--text); border: 1px dashed var(--accent);
  border-radius: 12px;
}
.btn-import:active { background: var(--bg-elev2); }
.backup-row { display: flex; gap: 10px; margin-top: 10px; }
.btn-mini {
  flex: 1; padding: 10px; font-size: 13px; font-weight: 600; cursor: pointer;
  background: var(--bg-elev); color: var(--muted); border: 1px solid var(--line); border-radius: 10px;
}
.btn-mini:active { background: var(--bg-elev2); }

/* Resumen cartera */
.summary {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-top: 14px;
}
.summary .total { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.summary .pl { font-size: 15px; margin-top: 4px; font-variant-numeric: tabular-nums; }

/* Panel / perfil */
.panel {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-top: 14px; display: flex; flex-direction: column; gap: 14px;
}
.panel label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.panel input, .panel select {
  padding: 12px 14px; font-size: 16px; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; outline: none;
}
.panel input:focus, .panel select:focus { border-color: var(--accent); }
.btn-primary {
  padding: 13px; font-size: 16px; font-weight: 700; border: none; border-radius: 12px;
  background: var(--accent); color: #fff; cursor: pointer;
}
.btn-primary:active { filter: brightness(.92); }
.alloc .row {
  display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line);
}
.alloc .row:last-child { border-bottom: none; }
.alloc .bar { height: 8px; border-radius: 4px; background: var(--accent); margin-top: 6px; }

.disclaimer { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 16px 4px 0; }

/* Detalle */
.detail { position: fixed; inset: 0; z-index: 50; background: rgba(6,9,16,.6); backdrop-filter: blur(4px); }
.detail-inner {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92vh; overflow-y: auto;
  background: var(--bg-elev); border-radius: 22px 22px 0 0; border-top: 1px solid var(--line);
  padding: 20px 18px calc(28px + env(safe-area-inset-bottom));
  animation: slideup .22s ease;
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: translateY(0); } }
.detail-close {
  position: absolute; top: 14px; right: 14px; width: 34px; height: 34px; border-radius: 50%;
  background: var(--bg-elev2); border: none; color: var(--text); font-size: 16px; cursor: pointer;
}
.detail h2 { margin: 0 0 2px; font-size: 22px; }
.detail .dsub { color: var(--muted); margin-bottom: 16px; }
.detail .bigscore {
  display: flex; align-items: baseline; gap: 10px; margin: 8px 0 18px;
}
.detail .bigscore .num { font-size: 46px; font-weight: 800; line-height: 1; }
.detail .bigscore .lbl { font-size: 15px; color: var(--muted); }

.subscore { margin: 14px 0; }
.subscore .head { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 6px; }
.subscore .head .v { color: var(--muted); font-variant-numeric: tabular-nums; }
.track { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.track > i { display: block; height: 100%; border-radius: 4px; }

.context-box {
  background: var(--bg-elev2); border-radius: 14px; padding: 16px; margin: 18px 0; line-height: 1.55;
}
.context-box h3 { margin: 0 0 8px; font-size: 15px; }
.links { display: flex; flex-wrap: wrap; gap: 8px; }
.links a {
  display: inline-block; padding: 8px 12px; border-radius: 10px; font-size: 13px; font-weight: 600;
  background: var(--bg); border: 1px solid var(--line); color: var(--accent); text-decoration: none;
}
.links a:active { background: var(--bg-elev2); }
.etf {
  display: block; padding: 11px 13px; border-radius: 12px; text-decoration: none;
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
}
.etf:active { background: var(--bg-elev2); }
.etf b { color: var(--accent); font-size: 14px; }
.etf span { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; line-height: 1.45; }
.ai-out {
  margin-top: 12px; padding: 14px; border-radius: 12px; background: var(--bg);
  border: 1px solid var(--line); font-size: 14px; line-height: 1.6;
}
.ai-out b { color: var(--accent); }

.detail-actions { display: flex; gap: 10px; margin-top: 18px; }
.detail-actions button { flex: 1; padding: 13px; border-radius: 12px; font-size: 15px; font-weight: 700; cursor: pointer; border: 1px solid var(--line); background: var(--bg-elev2); color: var(--text); }
.detail-actions .primary { background: var(--accent); color: #fff; border: none; }

/* Añadir posición */
.pos-form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.pos-form input { padding: 12px 14px; font-size: 16px; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 10px; }
.pos-form .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

code {
  background: var(--bg); border: 1px solid var(--line); border-radius: 5px;
  padding: 1px 5px; font-size: 12px; font-family: ui-monospace, monospace;
}

/* Aviso efímero */
#flash {
  position: fixed; left: 50%; transform: translateX(-50%) translateY(20px);
  bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--bg-elev2); color: var(--text); border: 1px solid var(--line);
  padding: 11px 16px; border-radius: 12px; font-size: 14px; max-width: 88%;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; text-align: center;
}
#flash.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Tabbar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; background: var(--bg-elev); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1; background: none; border: none; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 10px 0 8px; font-size: 20px;
}
.tab span { font-size: 11px; }
.tab.active { color: var(--accent); }
