:root {
  --bg: #0f0f13;
  --surface: #1a1a24;
  --surface2: #242433;
  --surface3: #2e2e3e;
  --primary: #6c5ce7;
  --primary-h: #5a4bd1;
  --primary-g: #a29bfe;
  --income: #00b894;
  --expense: #ff7675;
  --text: #f5f5f7;
  --muted: #a0a0b0;
  --border: #2e2e3e;
  --radius: 14px;
  --warn: #fdcb6e;
  --ok: #00b894;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  line-height: 1.45;
}

#authScreen, #appScreen {
  width: 100%;
  max-width: 520px;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
  position: relative;
}

#authScreen { display: none; flex-direction: column; justify-content: center; align-items: center; padding: 24px; }
#authScreen.active { display: flex; }

#appScreen { display: none; flex-direction: column; }
#appScreen.active { display: flex; }

@media (min-width: 768px) {
  body { background: radial-gradient(circle at 50% 0, #181829 0%, #0f0f13 80%); }
  #authScreen, #appScreen { height: 100%; max-width: none; border-radius: 0; box-shadow: none; }
  .auth-box { margin: 0 auto; }
  #panel-chat .messages, #panel-chat .input-area { width: 100%; max-width: 1000px; align-self: center; }
  .panel .scroll { align-items: center; padding: 14px 24px; }
  .card { width: 100%; max-width: 1000px; }
  .msg { max-width: 70%; }
}

/* Auth */
.auth-box { width: 100%; max-width: 360px; }

.auth-logo { text-align: center; margin-bottom: 28px; }
.auth-logo .icon {
  width: 64px; height: 64px;
  background: linear-gradient(135deg, var(--primary), var(--primary-g));
  border-radius: 18px;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff;
  margin-bottom: 12px;
}
.auth-logo h1 { font-size: 1.45rem; font-weight: 700; }
.auth-logo p { color: var(--muted); font-size: 0.85rem; margin-top: 4px; }

.auth-tabs {
  display: flex;
  margin-bottom: 20px;
  background: var(--surface2);
  border-radius: 12px;
  padding: 4px;
}

.auth-tab {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border: none;
  background: transparent;
  transition: all .2s ease;
}
.auth-tab.active { background: var(--primary); color: #fff; }

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 5px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.field input,
.field select,
textarea {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color .2s;
}
.field input:focus,
.field select:focus,
textarea:focus { border-color: var(--primary); }

.btn-full {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  margin-top: 6px;
  transition: background .15s, transform .05s;
}
.btn-full:active { background: var(--primary-h); transform: translateY(1px); }

.auth-err { color: var(--expense); font-size: 0.85rem; margin-top: 12px; text-align: center; min-height: 20px; }

/* App header */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top));
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1rem; }
.logo-icon {
  width: 30px; height: 30px;
  background: linear-gradient(135deg, var(--primary), var(--primary-g));
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
}

.header-right { display: flex; align-items: center; gap: 8px; }
.badge {
  font-size: .62rem;
  background: var(--surface2);
  color: var(--muted);
  padding: 3px 8px;
  border-radius: 10px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.btn-icon {
  background: var(--surface2);
  border: none;
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 10px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.btn-icon:active { background: var(--border); }

/* Tabs */
.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
}

.tab {
  flex: 1;
  min-width: 68px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
  padding: 10px 4px;
  font-size: .68rem;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  background: none;
  border-top: none; border-left: none; border-right: none;
  white-space: nowrap;
  transition: all .2s ease;
}
.tab svg { opacity: .7; }
.tab.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }
.tab.active svg { opacity: 1; }

/* Panels */
.panel { display: none; flex: 1; overflow: hidden; flex-direction: column; }
.panel.active { display: flex; }

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: 0 4px 12px rgba(0,0,0,.12);
}
.card h3 {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin-bottom: 10px;
}

.stat-row { display: flex; justify-content: space-between; margin-bottom: 5px; font-size: .9rem; }
.stat-row .v { font-weight: 600; }
.income { color: var(--income); }
.expense { color: var(--expense); }
.bal { font-size: 1.35rem; font-weight: 700; margin-top: 6px; }

.btn {
  padding: 10px 12px;
  border-radius: 10px;
  border: none;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  transition: background .15s;
}
.btn:active { background: var(--border); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active { background: var(--primary-h); }
.btn.danger { background: #3d1f1f; color: var(--expense); }
.btn.danger:active { background: #4f2828; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.btn-row .btn { flex: 1; min-width: 90px; }

/* Chat */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: .9rem;
  animation: fadeIn .2s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

.msg.user { align-self: flex-end; background: var(--primary); border-bottom-right-radius: 4px; color: #fff; }
.msg.ai { align-self: flex-start; background: var(--surface2); border-bottom-left-radius: 4px; }
.msg.ai .lbl {
  font-size: .6rem;
  color: var(--muted);
  margin-bottom: 3px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.msg.ai .src {
  font-size: .55rem;
  background: var(--surface);
  padding: 1px 6px;
  border-radius: 8px;
  color: var(--warn);
  text-transform: uppercase;
}

.input-area {
  padding: 8px 12px 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chips { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 8px; }
.chip {
  flex-shrink: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 11px;
  border-radius: 18px;
  font-size: .72rem;
  cursor: pointer;
  white-space: nowrap;
  transition: all .15s;
}
.chip:active { background: var(--border); color: var(--text); }

.input-box {
  display: flex;
  gap: 6px;
  background: var(--surface2);
  border-radius: 22px;
  padding: 5px 5px 5px 12px;
  align-items: center;
}
.input-box input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: .92rem;
  outline: none;
  min-width: 0;
}

.btn-send, .btn-cam {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: transform .05s;
}
.btn-send { background: var(--primary); }
.btn-send:active { background: var(--primary-h); transform: scale(.96); }
.btn-cam { background: var(--surface); color: var(--text); }
.btn-cam:active { background: var(--border); }

/* Goals */
.goal-bar { height: 8px; background: var(--surface); border-radius: 4px; overflow: hidden; margin: 8px 0; }
.goal-fill { height: 100%; border-radius: 4px; background: var(--primary); transition: width .3s ease; }
.goal-fill.ok { background: var(--ok); }
.goal-fill.warn { background: var(--warn); }
.goal-fill.bad { background: var(--expense); }
.goal-meta { display: flex; justify-content: space-between; font-size: .78rem; color: var(--muted); margin-top: 4px; }

/* Rules */
.rule-item { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: .88rem; }
.rule-item:last-child { border-bottom: none; }
.tag { font-size: .7rem; background: var(--surface); padding: 2px 8px; border-radius: 8px; color: var(--muted); }

/* Modal */
.modal-bg {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.75);
  z-index: 100;
  display: none;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}
.modal-bg.show { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 20px;
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 -10px 40px rgba(0,0,0,.35);
}

@media (min-width: 560px) {
  .modal-bg { align-items: center; padding: 20px; }
  .modal { border-radius: 18px; max-width: 460px; }
}

.modal h2 { font-size: 1.1rem; margin-bottom: 12px; }
.modal input[type="file"] {
  width: 100%;
  margin-bottom: 12px;
  color: var(--text);
  font-size: .85rem;
}

.preview-img {
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border-radius: 10px;
  background: #000;
  margin-bottom: 12px;
}

.ocr-status { font-size: .85rem; color: var(--muted); margin-bottom: 10px; min-height: 20px; }

.typing { display: flex; gap: 4px; padding: 4px 0; }
.typing span { width: 6px; height: 6px; background: var(--muted); border-radius: 50%; animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }

@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(-5px); } }

/* Recent list */
.recent-item { display: flex; justify-content: space-between; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: .85rem; }
.recent-item:last-child { border-bottom: none; }
.recent-left { display: flex; flex-direction: column; gap: 2px; }
.recent-desc { font-weight: 500; }
.recent-meta { font-size: .7rem; color: var(--muted); }

.empty { color: var(--muted); font-size: .88rem; text-align: center; padding: 20px 0; }

/* Utility */
.loading { color: var(--muted); font-size: .85rem; text-align: center; padding: 12px 0; }
