:root{
  --purple: #4F3CC9;
  --purple-dark: #3E2FA0;
  --purple-light: #EFECFF;
  --bg: #F4F5F9;
  --surface: #FFFFFF;
  --text: #1B1B23;
  --text-dim: #6B6B7A;
  --border: #ECEDF2;
  --green: #1FAA59;
  --green-bg: #E9F9EF;
  --red: #E4483C;
  --red-bg: #FDEAE9;
  --radius: 18px;
  --radius-sm: 12px;
  --nav-h: 68px;
  --shadow: 0 10px 30px -12px rgba(30,20,90,.15);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:#121218; --surface:#1C1C25; --text:#F2F2F5; --text-dim:#9A9AA8;
    --border:#2A2A35; --purple-light:#2A2560; --green-bg:#12331F; --red-bg:#3A1917;
    color-scheme: dark;
  }
}
:root[data-theme="dark"]{
  --bg:#121218; --surface:#1C1C25; --text:#F2F2F5; --text-dim:#9A9AA8;
  --border:#2A2A35; --purple-light:#2A2560; --green-bg:#12331F; --red-bg:#3A1917;
  color-scheme: dark;
}

*{ box-sizing:border-box; }
html,body{ margin:0; padding:0; }
body{
  background:var(--bg);
  color:var(--text);
  font-family: "Segoe UI", Tahoma, "Cairo", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
svg{ width:22px; height:22px; fill:none; stroke:currentColor; stroke-width:1.8; stroke-linecap:round; stroke-linejoin:round; display:block; }
button{ font-family:inherit; cursor:pointer; }
input,select{ font-family:inherit; }

.app{
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background:var(--bg);
  position:relative;
  display:flex;
  flex-direction:column;
}

.screen{
  display:none;
  flex-direction:column;
  min-height:100vh;
  min-height:100dvh;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
.screen.active{ display:flex; }

.topbar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding: calc(16px + env(safe-area-inset-top)) 20px 12px;
  position:sticky;
  top:0;
  background:var(--bg);
  z-index:5;
}
.topbar-plain{ justify-content:space-between; }
.topbar-title{ font-size:20px; font-weight:700; margin:0; }
.topbar-sub{ margin:0 0 2px; color:var(--text-dim); font-size:13px; }
.icon-btn{
  width:40px; height:40px; border-radius:50%;
  background:var(--surface); border:1px solid var(--border);
  display:flex; align-items:center; justify-content:center;
  color:var(--text); flex-shrink:0;
}
.icon-btn:active{ transform:scale(0.94); }
.icon-btn-spacer{ width:40px; height:40px; }

.scroll-area{
  flex:1;
  padding: 4px 20px 24px;
  overflow-y:auto;
}

.card{
  background:var(--surface);
  border-radius: var(--radius);
  padding:20px;
  margin-bottom:16px;
  box-shadow: var(--shadow);
  border:1px solid var(--border);
}

.hero-card{
  background: linear-gradient(150deg, var(--purple) 0%, var(--purple-dark) 100%);
  color:#fff;
  border:none;
}
.hero-top{ display:flex; justify-content:space-between; align-items:center; margin-bottom:6px; }
.badge{
  background:rgba(255,255,255,.18);
  padding:5px 12px;
  border-radius:20px;
  font-size:12px;
}
.hero-label{ margin:12px 0 2px; font-size:13px; opacity:.85; }
.hero-amount{ margin:0 0 14px; font-size:34px; font-weight:800; letter-spacing:-0.5px; }
.progress-track{
  height:8px; border-radius:6px; background:rgba(255,255,255,.22); overflow:hidden; margin-bottom:14px;
}
.progress-fill{
  height:100%; background:#fff; border-radius:6px; width:0%; transition:width .4s ease;
}
.progress-fill.warn{ background:#FFD166; }
.progress-fill.over{ background:#FF6B6B; }
.hero-stats{
  display:flex; justify-content:space-between; font-size:13px; opacity:.95;
}
.dot{ width:8px; height:8px; border-radius:50%; display:inline-block; margin-inline-end:5px; }
.dot-green{ background:#7CF2A6; }
.dot-red{ background:#FF8A80; }

.daily-badge{
  position:fixed;
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  left:50%;
  transform: translateX(-50%);
  display:flex;
  align-items:center;
  gap:8px;
  background:var(--surface);
  border:1px solid var(--border);
  padding:9px 18px;
  border-radius:30px;
  box-shadow: var(--shadow);
  z-index:20;
  transition: background .25s, border-color .25s, box-shadow .25s;
}
.daily-badge-label{ font-size:12px; color:var(--text-dim); font-weight:600; }
.daily-badge-amount{ font-size:14.5px; font-weight:800; color:var(--text); }
.daily-badge.over-limit{
  background: var(--red);
  border-color: var(--red);
  animation: badge-pulse 1.4s ease-in-out infinite;
}
.daily-badge.over-limit .daily-badge-label,
.daily-badge.over-limit .daily-badge-amount{ color:#fff; }
@keyframes badge-pulse{
  0%, 100% { box-shadow: 0 0 0 0 rgba(228,72,60,.55); }
  50% { box-shadow: 0 0 0 9px rgba(228,72,60,0); }
}

.inline-amount{ display:flex; align-items:center; gap:6px; }
.inline-amount input{
  width:70px; text-align:center; padding:8px 6px; border-radius:10px;
  border:1px solid var(--border); background:var(--bg); color:var(--text); font-size:14px; font-weight:700;
}
.currency-sign-inline{ font-size:13px; color:var(--text-dim); }

.quick-actions{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap:12px;
  margin-bottom: 22px;
}
.quick-btn{
  display:flex; align-items:center; justify-content:center; gap:8px;
  padding:14px 10px;
  border-radius: var(--radius-sm);
  border:1px solid var(--border);
  background:var(--surface);
  font-weight:600; font-size:14px;
  color:var(--text);
}
.quick-btn svg{ width:18px; height:18px; }
.quick-btn.expense{ color:var(--red); }
.quick-btn.expense svg{ transform: rotate(0deg); }
.quick-btn.income{ color:var(--green); }
.quick-btn:active{ transform:scale(0.97); }

.section-head{
  display:flex; justify-content:space-between; align-items:center; margin: 4px 0 10px;
}
.section-head h3{ margin:0; font-size:16px; }
.link-sm{ color:var(--purple); font-size:13px; text-decoration:none; font-weight:600; }

.tx-list{ display:flex; flex-direction:column; gap:10px; }
.tx-item{
  display:flex; align-items:center; gap:12px;
  background:var(--surface);
  border:1px solid var(--border);
  border-radius: var(--radius-sm);
  padding:12px 14px;
}
.tx-icon{
  width:40px; height:40px; border-radius:12px;
  display:flex; align-items:center; justify-content:center;
  flex-shrink:0;
}
.tx-icon.expense{ background:var(--red-bg); color:var(--red); }
.tx-icon.income{ background:var(--green-bg); color:var(--green); }
.tx-icon svg{ width:18px; height:18px; }
.tx-mid{ flex:1; min-width:0; }
.tx-title{ font-weight:600; font-size:14.5px; margin:0 0 2px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.tx-sub{ margin:0; font-size:12.5px; color:var(--text-dim); }
.tx-amount{ font-weight:700; font-size:14.5px; flex-shrink:0; }
.tx-amount.expense{ color:var(--red); }
.tx-amount.income{ color:var(--green); }
.tx-del{
  width:30px; height:30px; border-radius:50%; border:none; background:transparent; color:var(--text-dim);
  display:flex; align-items:center; justify-content:center; flex-shrink:0;
}
.tx-del svg{ width:16px; height:16px; }

.empty-state{
  text-align:center; padding: 30px 10px; color:var(--text-dim); font-size:14px;
}

.filter-row{ display:flex; gap:8px; margin-bottom:14px; }
.chip{
  padding:8px 16px; border-radius:20px; border:1px solid var(--border);
  background:var(--surface); color:var(--text-dim); font-size:13px; font-weight:600;
}
.chip.active{ background:var(--purple); border-color:var(--purple); color:#fff; }

/* Add Transaction */
.segmented{
  display:flex; background:var(--surface); border:1px solid var(--border);
  border-radius: 14px; padding:4px; margin: 4px 20px 20px;
}
.seg-btn{
  flex:1; border:none; background:transparent; padding:11px; border-radius:11px;
  font-weight:700; font-size:14px; color:var(--text-dim);
}
.seg-btn.active{ background:var(--purple); color:#fff; }

#tx-form{ padding:0 20px 30px; }
.field{ display:block; margin-bottom:16px; }
.field-label{ display:block; font-size:13px; color:var(--text-dim); margin-bottom:7px; font-weight:600; }
.field input, .field select{
  width:100%; padding:13px 14px; border-radius:12px; border:1px solid var(--border);
  background:var(--surface); color:var(--text); font-size:15px; outline:none;
}
.field input:focus, .field select:focus{ border-color:var(--purple); }
.field select{
  appearance:none; -webkit-appearance:none; -moz-appearance:none;
  padding-inline-end:40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236B6B7A' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 14px center;
  background-size: 18px;
}
.amount-input{ position:relative; }
.amount-input input{ padding-inline-start:34px; font-weight:700; }
.currency-sign{
  position:absolute; inset-inline-start:14px; top:50%; transform:translateY(-50%);
  color:var(--text-dim); font-size:15px;
}
.chip-wrap{ display:flex; flex-wrap:wrap; gap:8px; margin:-8px 0 16px; }
.chip-wrap .chip{ padding:7px 14px; font-size:12.5px; }
.chip-wrap .chip.add-new{ color:var(--purple); border-color:var(--purple); }

.btn-primary{
  width:100%; padding:15px; border:none; border-radius:14px;
  background:var(--purple); color:#fff; font-size:16px; font-weight:700;
  box-shadow: 0 8px 20px -6px rgba(79,60,201,.55);
}
.btn-primary:active{ transform:scale(0.98); }
.btn-secondary{
  width:100%; padding:14px; border-radius:14px; border:1px solid var(--border);
  background:var(--surface); color:var(--text); font-weight:700; font-size:14.5px; margin-bottom:10px;
}
.btn-danger{
  padding:12px 20px; border:none; border-radius:12px; background:var(--red); color:#fff; font-weight:700;
}

/* Reports */
.card-label{ margin:0 0 6px; font-size:13px; color:var(--text-dim); }
.card-amount{ margin:0 0 4px; font-size:28px; font-weight:800; }
.trend{ margin:0; font-size:13px; color:var(--green); font-weight:600; }
.trend.down{ color:var(--red); }
.legend{ display:flex; gap:16px; font-size:12.5px; color:var(--text-dim); margin-bottom:14px; }
.legend .sw{ width:9px; height:9px; border-radius:3px; display:inline-block; margin-inline-end:5px; }
.sw-income{ background:var(--green); }
.sw-expense{ background:var(--purple); }
.bars{
  display:flex; align-items:flex-end; gap:10px; height:130px; padding-top:10px;
}
.bar-group{ flex:1; display:flex; align-items:flex-end; justify-content:center; gap:3px; height:100%; }
.bar{ width:9px; border-radius:4px 4px 0 0; min-height:3px; }
.bar.income{ background:var(--green); }
.bar.expense{ background:var(--purple); }
.bar-labels{ display:flex; margin-top:8px; }
.bar-labels span{ flex:1; text-align:center; font-size:11px; color:var(--text-dim); }

.cat-list{ display:flex; flex-direction:column; gap:14px; }
.cat-row{ display:flex; flex-direction:column; gap:6px; }
.cat-row-top{ display:flex; justify-content:space-between; font-size:13.5px; font-weight:600; }
.cat-row-top span:last-child{ color:var(--text-dim); font-weight:600; }
.cat-track{ height:7px; background:var(--purple-light); border-radius:6px; overflow:hidden; }
.cat-fill{ height:100%; background:var(--purple); border-radius:6px; }

/* Settings */
.group-title{ font-size:12.5px; color:var(--text-dim); font-weight:700; margin:18px 4px 8px; text-transform:uppercase; letter-spacing:.03em; }
.list-card{
  background:var(--surface); border:1px solid var(--border); border-radius:var(--radius); overflow:hidden;
}
.list-row{
  width:100%; display:flex; align-items:center; justify-content:space-between;
  padding:15px 16px; background:transparent; border:none; border-bottom:1px solid var(--border);
  color:var(--text); font-size:14.5px; font-weight:600; text-align:start;
}
.list-row svg{ width:16px; height:16px; color:var(--text-dim); transform:scaleX(-1); }
.list-row:last-child{ border-bottom:none; }
.list-row.static{ }
.list-row.static b{ font-weight:700; }
.list-row.danger{ color:var(--red); }
.list-row.danger svg{ color:var(--red); }

.switch{ position:relative; display:inline-block; width:44px; height:26px; }
.switch input{ opacity:0; width:0; height:0; }
.slider{
  position:absolute; cursor:pointer; inset:0; background:var(--border); border-radius:26px; transition:.2s;
}
.slider:before{
  content:""; position:absolute; height:20px; width:20px; left:3px; bottom:3px;
  background:#fff; border-radius:50%; transition:.2s;
}
input:checked + .slider{ background:var(--purple); }
input:checked + .slider:before{ transform:translateX(-18px); }

/* Bottom nav */
.bottom-nav{
  position:fixed; bottom:0; left:50%; transform:translateX(-50%);
  width:100%; max-width:480px;
  display:flex;
  background:var(--surface);
  border-top:1px solid var(--border);
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
  z-index:10;
}
.nav-btn{
  flex:1; display:flex; flex-direction:column; align-items:center; gap:3px;
  background:transparent; border:none; color:var(--text-dim); padding:6px 2px; border-radius:12px;
}
.nav-btn span{ font-size:11px; font-weight:600; }
.nav-btn.active{ color:var(--purple); }
.nav-btn svg{ width:22px; height:22px; }

/* Modals */
.modal-overlay{
  position:fixed; inset:0; background:rgba(10,10,20,.5);
  display:flex; align-items:flex-end; justify-content:center;
  opacity:0; pointer-events:none; transition:opacity .2s; z-index:50;
}
.modal-overlay.open{ opacity:1; pointer-events:auto; }
.modal{
  display:none;
  width:100%; max-width:480px;
  background:var(--surface);
  border-radius: 22px 22px 0 0;
  padding:22px 20px calc(22px + env(safe-area-inset-bottom));
  transform:translateY(20px);
  transition:transform .2s;
}
.modal-overlay.open .modal.open{ display:block; transform:translateY(0); }
.modal h3{ margin:0 0 6px; font-size:18px; }
.modal-sub{ margin:0 0 16px; font-size:13px; color:var(--text-dim); line-height:1.5; }
.modal-actions{ display:flex; gap:10px; margin-top:6px; }
.modal-actions .btn-secondary, .modal-actions .btn-primary{ margin-bottom:0; }

.toast{
  position:fixed; bottom: calc(var(--nav-h) + 14px + env(safe-area-inset-bottom)); left:50%;
  transform:translateX(-50%) translateY(20px);
  background:#222; color:#fff; padding:11px 20px; border-radius:30px; font-size:13.5px;
  opacity:0; pointer-events:none; transition:all .25s; z-index:60; white-space:nowrap;
  max-width: 90%; overflow:hidden; text-overflow:ellipsis;
}
.toast.show{ opacity:1; transform:translateX(-50%) translateY(0); }

@media (min-width:480px){
  body{ background:#e7e8ee; }
  .app{ box-shadow: 0 0 40px rgba(0,0,0,.08); min-height:100vh; }
}
