:root {
  --bg: #0e1014;
  --panel: #161920;
  --panel-2: #1c2029;
  --line: #262b36;
  --text: #e8eaef;
  --muted: #8a92a3;
  --faint: #5b6273;
  --accent: hsl(205 70% 62%);
  --accent-soft: hsl(205 35% 32%);
  --red: #f0605a;
  --amber: #f2b33d;
  --green: #5fc79a;
  --radius: 14px;
  --pad: 24px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
/* Lo oculto se queda oculto aunque su clase le dé display: grid o flex */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
html, body { background: var(--bg); color: var(--text); }
body {
  margin: 0 auto;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  max-width: 1280px;
  /* Respeta la muesca y la barra de inicio del iPhone cuando se abre como app */
  padding:
    max(var(--pad), env(safe-area-inset-top))
    max(var(--pad), env(safe-area-inset-right))
    max(var(--pad), env(safe-area-inset-bottom))
    max(var(--pad), env(safe-area-inset-left));
}
.num { font-variant-numeric: tabular-nums; }
h2 { font-size: 13px; font-weight: 600; margin: 0 0 16px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* Entrada */
.auth { min-height: 80vh; min-height: 80dvh; display: grid; place-items: center; }
.auth-card { width: 100%; max-width: 380px; }
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.auth-brand img { width: 52px; height: 52px; border-radius: 12px; }
.auth-brand h1 { font-size: 22px; margin: 0; }
.auth-brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.auth-msg { font-size: 13px; min-height: 18px; margin-top: 10px; color: var(--red); }
.auth-msg.ok { color: var(--green); }
.linkbtn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 8px 0 0; font-size: 13px; }

/* Buscador */
.search { position: sticky; top: env(safe-area-inset-top); z-index: 5; margin: 0 0 14px; padding-top: 4px; background: var(--bg); }
.search-box { position: relative; display: flex; align-items: center; }
.search-box svg { position: absolute; left: 13px; width: 17px; height: 17px; color: var(--muted); pointer-events: none; }
.search-box input {
  width: 100%; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 40px 11px 40px; font-size: 16px; -webkit-appearance: none; appearance: none;
}
.search-box input::-webkit-search-cancel-button { display: none; }
.search-box input:focus { outline: none; border-color: var(--accent); }
.search-clear { position: absolute; right: 6px; width: 32px; height: 32px; border: 0; border-radius: 8px; background: none; color: var(--muted); font-size: 22px; line-height: 1; cursor: pointer; }
.search-clear:hover { color: var(--text); background: var(--panel-2); }
.search-results {
  position: absolute; left: 0; right: 0; top: calc(100% + 6px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 16px 40px rgb(0 0 0 / .5); max-height: min(60vh, 520px); overflow-y: auto;
}
.sr-head { display: flex; justify-content: space-between; gap: 10px; padding: 10px 14px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.sr-list { list-style: none; margin: 0; padding: 0; }
.sr-item { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; width: 100%; padding: 11px 14px; border: 0; border-top: 1px solid var(--line); background: none; text-align: left; cursor: pointer; }
.sr-list li:first-child .sr-item { border-top: 0; }
.sr-item:hover, .sr-item:focus-visible { background: var(--panel-2); outline: none; }
.sr-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sr-amount { font-weight: 650; white-space: nowrap; text-align: right; }
.sr-amount.in { color: var(--green); }
.sr-meta { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; font-size: 12px; color: var(--muted); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--accent-soft); color: hsl(205 70% 82%); font-size: 11.5px; }
.sr-item mark { background: hsl(205 70% 62% / .28); color: inherit; border-radius: 3px; padding: 0 1px; }
.sr-empty { padding: 18px 14px; color: var(--muted); font-size: 13px; }
.moves li.flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: hsl(205 70% 62% / .25); } to { background: transparent; } }

/* Barra superior */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-nav .label { min-width: 170px; text-align: center; font-size: 18px; font-weight: 600; }
.tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.sync { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-right: 6px; background: none; border: 0; padding: 4px 0; cursor: default; text-align: left; }
.sync::before { content: ""; flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.sync[data-state="cloud"]::before { background: var(--green); }
.sync[data-state="local"]::before { background: var(--amber); }
.sync[data-state="error"] { cursor: pointer; color: var(--text); }
.sync[data-state="error"]::before { background: var(--red); }

.btn {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  padding: 8px 13px; cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover { background: #242936; border-color: #333a48; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: #07131d; border-color: transparent; font-weight: 600; }
.btn.primary:hover { background: hsl(205 70% 68%); }
.btn.danger { background: var(--red); color: #1a0706; border-color: transparent; font-weight: 600; }
.btn.icon { padding: 6px 11px; font-size: 16px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--panel-2); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.wide { width: 100%; padding: 11px; }

/* Rejilla */
.grid-top { display: grid; grid-template-columns: 360px 1fr; gap: 20px; margin-bottom: 20px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
@media (max-width: 960px) {
  :root { --pad: 14px; }
  .grid-top, .grid-2 { grid-template-columns: 1fr; }
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; min-width: 0; }

/* Formulario */
.type-toggle { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.type-toggle button { border: 0; background: transparent; padding: 8px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.type-toggle button.on { background: var(--panel-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.type-toggle button.on[data-tipo="ingreso"] { color: var(--green); }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field label { font-size: 12px; color: var(--muted); }
.field input, .field select, .field textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; width: 100%; min-width: 0;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 38px; }
/* En iPhone el campo de fecha trae un ancho propio que se sale de la tarjeta */
.field input[type="date"] { -webkit-appearance: none; appearance: none; min-height: 42px; text-align: left; }
.row-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.amount-input { font-size: 20px !important; font-weight: 600; }
.form-actions { display: flex; gap: 8px; margin-top: 4px; }
.form-actions .primary { flex: 1; }
.form-error { color: var(--red); font-size: 12px; min-height: 16px; margin-top: 6px; }
.editing-note { font-size: 12px; color: var(--amber); margin: -6px 0 12px; }

/* Números grandes */
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.kpi { background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 18px 18px 16px; min-width: 0; }
.kpi .k-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.kpi .k-value { font-size: clamp(24px, 3.2vw, 38px); font-weight: 650; letter-spacing: -.02em; margin-top: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi.income .k-value { color: var(--green); }
.kpi.left .k-value.neg { color: var(--red); }
.kpi .k-sub { font-size: 12px; color: var(--faint); margin-top: 4px; }
.compare { margin-top: 16px; padding: 14px 16px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); display: flex; gap: 14px; align-items: center; }
.compare .big { font-size: 26px; font-weight: 650; white-space: nowrap; }
.compare .big.up { color: var(--red); }
.compare .big.down { color: var(--green); }
.compare .txt { color: var(--muted); font-size: 13px; }
.compare .txt b { color: var(--text); font-weight: 600; }
.sample-note { margin-top: 12px; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* Torta */
.donut-wrap { display: grid; grid-template-columns: minmax(180px, 240px) 1fr; gap: 22px; align-items: center; }
@media (max-width: 520px) { .donut-wrap { grid-template-columns: 1fr; justify-items: center; } }
.donut-wrap svg { width: 100%; max-width: 240px; height: auto; display: block; }
.donut-seg { transition: opacity .15s; }
.legend { list-style: none; margin: 0; padding: 0; width: 100%; }
.legend li { display: grid; grid-template-columns: 12px 1fr auto auto; gap: 10px; align-items: center; padding: 6px; border-radius: 7px; font-size: 13px; }
.legend li.hl { background: var(--panel-2); }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; }
.legend .pct { font-weight: 600; text-align: right; min-width: 44px; }
.legend .amt { color: var(--muted); text-align: right; font-size: 12px; min-width: 88px; }
.empty { color: var(--faint); padding: 30px 0; text-align: center; }

/* Barras */
.bars svg { width: 100%; height: auto; display: block; }
.bars-note { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* Lista */
.list-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.list-head h2 { margin: 0; }
.list-head select { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; }
.moves { list-style: none; margin: 0; padding: 0; max-height: 560px; overflow-y: auto; }
.moves li { display: grid; grid-template-columns: 52px 1fr auto auto; gap: 12px; align-items: center; padding: 10px 4px; border-top: 1px solid var(--line); }
.moves li:first-child { border-top: 0; }
.moves .date { color: var(--muted); font-size: 12px; line-height: 1.2; }
.moves .date b { display: block; color: var(--text); font-size: 16px; font-weight: 600; }
.moves .cat { font-weight: 500; }
.moves .note { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.moves .amount { font-weight: 600; text-align: right; white-space: nowrap; }
.moves .amount.in { color: var(--green); }
.moves .acts { display: flex; gap: 2px; opacity: .55; transition: opacity .15s; }
.moves li:hover .acts, .moves li:focus-within .acts { opacity: 1; }
.moves li.editing { background: var(--panel-2); border-radius: 8px; }
.list-total { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; color: var(--muted); font-size: 12px; padding-top: 10px; border-top: 1px solid var(--line); margin-top: 4px; }

/* Presupuestos */
.budget { padding: 10px 0; border-top: 1px solid var(--line); }
.budget:first-of-type { border-top: 0; }
.budget .b-top { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; margin-bottom: 7px; }
.budget .b-name { font-weight: 500; }
.budget .b-cap { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.budget .b-cap input { width: 118px; background: var(--bg); border: 1px solid var(--line); border-radius: 7px; padding: 5px 8px; text-align: right; }
.budget .b-cap input:focus { outline: none; border-color: var(--accent); }
.track { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; }
.fill { height: 100%; border-radius: 99px; background: var(--accent); transition: width .3s; }
.fill.warn { background: var(--amber); }
.fill.over { background: var(--red); }
.budget .b-info { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); margin-top: 5px; }
.budget .b-info .warn { color: var(--amber); }
.budget .b-info .over { color: var(--red); font-weight: 600; }

/* Confirmación y avisos */
.modal { position: fixed; inset: 0; background: rgb(0 0 0 / .6); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; max-width: 380px; width: 100%; }
.modal-card p { margin: 0 0 18px; white-space: pre-line; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: var(--panel-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; max-width: calc(100% - 32px); z-index: 11; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Celular: primero los números, compactos; después el formulario */
@media (max-width: 640px) {
  :root { --pad: 12px; }
  .topbar { margin-bottom: 12px; gap: 10px; }
  .month-nav { width: 100%; justify-content: space-between; }
  .month-nav .label { min-width: 0; flex: 1; }
  .tools { width: 100%; }
  .tools .btn { flex: 1; }
  .tools .btn.ghost { flex: none; }
  .sync { width: 100%; margin: 0; }
  .panel { padding: 16px; }
  .grid-top, .grid-2 { gap: 12px; margin-bottom: 12px; }
  .grid-top > .panel:first-child { order: 2; }
  .kpis { gap: 0; background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; grid-template-columns: 1fr; }
  .kpi { display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 2px 10px; background: none; border: 0; border-top: 1px solid var(--line); border-radius: 0; padding: 12px 14px; }
  .kpi:first-child { border-top: 0; }
  .kpi .k-value { font-size: 26px; margin: 0; text-align: right; grid-row: span 2; align-self: center; }
  .kpi .k-sub { margin: 0; }
  .compare { padding: 12px; gap: 12px; }
  .compare .big { font-size: 22px; }
  /* 16px evita que el iPhone haga zoom al tocar un campo */
  .field input, .field select, .field textarea, .list-head select, .budget .b-cap input { font-size: 16px; }
  .btn.primary { padding: 12px; font-size: 16px; }
  .moves { max-height: none; overflow: visible; }
  .moves li { grid-template-columns: 40px 1fr auto; row-gap: 4px; }
  .moves .acts { opacity: 1; grid-column: 2 / 4; justify-content: flex-end; margin-top: -2px; }
  .legend .amt { min-width: 0; }
  .list-head { flex-wrap: wrap; }
  .list-head select { width: 100%; }
}
