:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --card2: #f7f7fa;
  --text: #111114;
  --text2: #6c6c70;
  --text3: #a1a1a6;
  --sep: rgba(60, 60, 67, .16);
  --fill: rgba(118, 118, 128, .12);
  --accent: #007aff;
  --accent-press: #0062cc;
  --on-accent: #ffffff;
  --accent-bg: #e3efff;
  --accent-text: #0058c7;
  --green: #1f7a37;
  --green-bg: #e2f4e7;
  --red: #d70015;
  --shadow: 0 10px 30px rgba(0, 0, 0, .18);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card2: #2c2c2e;
    --text: #f5f5f7;
    --text2: #98989f;
    --text3: #636366;
    --sep: rgba(84, 84, 88, .55);
    --fill: rgba(118, 118, 128, .24);
    --accent: #0a84ff;
    --accent-press: #409cff;
    --accent-bg: #0b2a4d;
    --accent-text: #79bbff;
    --green: #30d158;
    --green-bg: #0f3319;
    --red: #ff453a;
    --shadow: 0 10px 30px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }
.num { font-variant-numeric: tabular-nums; }

.splash { height: 100vh; display: grid; place-items: center; font-weight: 700; font-size: 22px; color: var(--accent); }

/* ---------- Estructura (teléfono) ---------- */
.shell { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.sidebar { display: none; }
.main {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: calc(var(--safe-top) + 10px) 16px calc(var(--safe-bottom) + 96px);
}
.main-inner { max-width: 640px; margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 44px; margin-bottom: 8px; }
.topbar h1 { font-size: 30px; font-weight: 700; letter-spacing: -.02em; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around; align-items: center;
  padding: 6px 12px calc(var(--safe-bottom) + 6px);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px);
  border-top: .5px solid var(--sep);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--text2); min-width: 64px; padding: 4px; }
.tab.on { color: var(--accent); }
.tab svg, .navitem svg { width: 24px; height: 24px; }
.fab {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center; box-shadow: 0 4px 14px rgba(0, 122, 255, .35);
  margin-top: -18px;
}
.fab svg { width: 28px; height: 28px; }
.fab:active { transform: scale(.95); background: var(--accent-press); }

/* Panel: en teléfono es una hoja que sube desde abajo */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 30; opacity: 0; pointer-events: none; transition: opacity .2s; }
.panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  top: calc(var(--safe-top) + 12px);
  background: var(--card); border-radius: 14px 14px 0 0;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  transform: translateY(105%); transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  box-shadow: var(--shadow);
  padding: 8px 16px calc(var(--safe-bottom) + 24px);
}
.sheet-open .panel { transform: translateY(0); }
.sheet-open .backdrop { opacity: 1; pointer-events: auto; }
.panel-inner { max-width: 520px; margin: 0 auto; }

/* ---------- iPad y Mac ---------- */
@media (min-width: 700px) {
  .shell { flex-direction: row; }
  .tabbar, .backdrop { display: none; }
  .sidebar {
    display: flex; flex-direction: column; gap: 4px; width: 84px; flex: none;
    padding: calc(var(--safe-top) + 16px) 8px 16px; border-right: .5px solid var(--sep); background: var(--card);
  }
  .brand { display: none; font-weight: 700; font-size: 20px; padding: 4px 10px 14px; color: var(--accent); }
  .navitem { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 4px; border-radius: 10px; font-size: 11px; color: var(--text2); }
  .navitem.on { background: var(--accent-bg); color: var(--accent-text); }
  .navitem.new { color: var(--accent); }
  .main { padding: calc(var(--safe-top) + 16px) 24px 32px; }
  .panel {
    position: static; transform: none; transition: none; box-shadow: none; border-radius: 0;
    width: 360px; flex: none; border-left: .5px solid var(--sep);
    padding: calc(var(--safe-top) + 12px) 20px 32px;
  }
  .sheet-head .cancel { visibility: hidden; }
  .panel.editing .sheet-head .cancel { visibility: visible; }
}
@media (min-width: 1024px) {
  .sidebar { width: 210px; padding-left: 12px; padding-right: 12px; }
  .brand { display: block; }
  .navitem { flex-direction: row; gap: 10px; font-size: 16px; padding: 9px 12px; }
  .navitem svg { width: 22px; height: 22px; }
  .panel { width: 400px; }
}

/* ---------- Saldo ---------- */
.balance {
  background: var(--accent); color: var(--on-accent);
  border-radius: 18px; padding: 16px 18px; margin-bottom: 22px;
  display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
}
.balance.even { background: var(--card); color: var(--text); }
.balance .who { font-size: 15px; opacity: .92; }
.balance .amt { font-size: 34px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.balance .btn-settle {
  background: rgba(255, 255, 255, .22); color: inherit; font-weight: 600;
  padding: 8px 16px; border-radius: 20px; flex: none;
}
.balance.even .btn-settle { background: var(--fill); color: var(--accent); }

/* ---------- Lista de movimientos ---------- */
.month-head { display: flex; justify-content: space-between; align-items: baseline; margin: 18px 4px 6px; }
.month-head h2 { font-size: 20px; font-weight: 700; }
.month-head span { font-size: 13px; color: var(--text2); }
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 10px 14px; position: relative; }
.item + .item::before { content: ""; position: absolute; top: 0; left: 62px; right: 0; border-top: .5px solid var(--sep); }
.item:active { background: var(--fill); }
.emoji { width: 36px; height: 36px; border-radius: 50%; background: var(--card2); display: grid; place-items: center; font-size: 19px; flex: none; }
.emoji.settle { background: var(--green-bg); color: var(--green); font-size: 17px; font-weight: 700; }
.item .mid { flex: 1; min-width: 0; }
.item .title, .item .sub, .item .amount, .item .mine { display: block; }
.item .title { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { font-size: 13px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .right { text-align: right; flex: none; }
.item .amount { font-size: 16px; font-weight: 600; }
.item .mine { font-size: 12px; color: var(--text2); }
.item.is-settle .amount { color: var(--green); font-weight: 500; }

.empty { text-align: center; color: var(--text2); padding: 40px 16px; }
.empty b { display: block; color: var(--text); font-size: 18px; margin-bottom: 4px; }
.more { display: block; margin: 16px auto; color: var(--accent); padding: 10px 16px; }

/* ---------- Formularios ---------- */
.sheet-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 44px; margin-bottom: 6px; }
.sheet-head h2 { font-size: 17px; font-weight: 600; text-align: center; }
.sheet-head .cancel { color: var(--accent); justify-self: start; padding: 8px 0; }
.grab { width: 36px; height: 5px; border-radius: 3px; background: var(--fill); margin: 0 auto 6px; }
@media (min-width: 700px) { .grab { display: none; } }

.amount-field { display: flex; align-items: baseline; justify-content: center; gap: 6px; margin: 10px 0 14px; }
.amount-field span { font-size: 26px; color: var(--text2); font-weight: 600; }
.amount-field input {
  width: 100%; max-width: 240px; border: 0; background: transparent; outline: none;
  font-size: 44px; font-weight: 700; letter-spacing: -.02em; text-align: left; padding: 0;
}
.amount-field input::placeholder { color: var(--text3); }

.field {
  width: 100%; border: 0; outline: none; background: var(--card2);
  border-radius: 12px; padding: 12px 14px; font-size: 16px; margin-bottom: 14px;
}
.field:focus { box-shadow: 0 0 0 2px var(--accent); }
.label { font-size: 13px; color: var(--text2); margin: 0 2px 6px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip {
  padding: 8px 12px; border-radius: 18px; background: var(--card2); font-size: 15px;
  border: 1px solid transparent; white-space: nowrap;
}
.chip.on { background: var(--accent); color: var(--on-accent); }
.chip:active { transform: scale(.97); }

.seg { display: flex; background: var(--card2); border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.seg button { flex: 1; padding: 8px; border-radius: 8px; font-size: 15px; }
.seg button.on { background: var(--card); box-shadow: 0 1px 4px rgba(0, 0, 0, .15); font-weight: 600; }
@media (prefers-color-scheme: dark) { .seg button.on { background: #636366; } }

.custom { display: flex; align-items: center; gap: 8px; margin: -4px 0 12px; font-size: 15px; flex-wrap: wrap; }
.custom input { width: 76px; text-align: center; margin: 0; padding: 8px; }
.split-line { font-size: 14px; color: var(--text2); margin: -4px 2px 16px; }

.date-row {
  position: relative; display: flex; justify-content: space-between; align-items: center;
  background: var(--card2); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px;
}
.date-row .v { color: var(--accent); }
.date-row input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; -webkit-appearance: none; appearance: none; }

.primary {
  display: block; width: 100%; background: var(--accent); color: var(--on-accent);
  border-radius: 14px; padding: 15px; font-size: 17px; font-weight: 600;
}
.primary:active { background: var(--accent-press); }
.primary:disabled { opacity: .6; }
.danger-link { display: block; width: 100%; color: var(--red); padding: 14px; margin-top: 8px; font-size: 16px; }
.err { color: var(--red); font-size: 14px; min-height: 20px; margin: -6px 2px 8px; }
.hint { font-size: 13px; color: var(--text2); text-align: center; margin: 10px 0 0; }
.hint.tight { margin: -6px 0 16px; }
.hint.left { text-align: left; margin: 8px 16px 0; }
.settle-dir { text-align: center; color: var(--text2); margin-top: 8px; }

/* ---------- Ajustes ---------- */
.section-title { font-size: 13px; color: var(--text2); text-transform: uppercase; letter-spacing: .03em; margin: 22px 16px 6px; }
.cat-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; position: relative; }
.cat-row + .cat-row::before { content: ""; position: absolute; top: 0; left: 10px; right: 0; border-top: .5px solid var(--sep); }
.cat-row input { border: 0; outline: none; background: transparent; font-size: 16px; padding: 6px 4px; border-radius: 8px; }
.cat-row input:focus { background: var(--card2); }
.cat-row .ic { width: 48px; text-align: center; font-size: 20px; }
.cat-row .nm { flex: 1; min-width: 0; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; color: var(--red); flex: none; }
.icon-btn.add { color: var(--accent); font-weight: 600; width: auto; padding: 0 10px; border-radius: 10px; }
.icon-btn svg { width: 20px; height: 20px; }
.acct { padding: 14px 16px; }
.acct p { color: var(--text2); font-size: 14px; }
.list .row-btn { display: block; width: 100%; text-align: left; padding: 14px 16px; color: var(--accent); border-top: .5px solid var(--sep); }
.list .row-btn.danger { color: var(--red); }
.version { text-align: center; color: var(--text3); font-size: 12px; margin: 24px 0; }

/* ---------- Inicio de sesión ---------- */
.login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 20px calc(var(--safe-bottom) + 24px); overflow-y: auto; }
.login form { width: 100%; max-width: 360px; }
.login .logo { width: 84px; height: 84px; border-radius: 20px; display: block; margin: 0 auto 14px; }
.login h1 { text-align: center; font-size: 28px; font-weight: 700; margin-bottom: 4px; }
.login p.sub { text-align: center; color: var(--text2); margin-bottom: 24px; }
.login .field { background: var(--card); }

/* ---------- Avisos ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 96px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: rgba(30, 30, 32, .94); color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 15px;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 700px) { .toast { bottom: 32px; } }
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--accent-bg); color: var(--accent-text); border-radius: 12px; padding: 10px 14px; margin-bottom: 12px; font-size: 15px;
}
.banner button { color: var(--accent); font-weight: 600; }
.banner.warn { background: var(--card); color: var(--text2); }
