*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --header-bg:   #f97316;
  --header-fg:   #ffffff;
  --app-bg:      #fff7ed;
  --surface:     #ffffff;
  --primary:     #f97316;
  --primary-fg:  #ffffff;
  --bot-bg:      #ffffff;
  --bot-fg:      #111827;
  --user-bg:     #f97316;
  --user-fg:     #ffffff;
  --chip-bg:     #ea580c;
  --chip-fg:     #ffffff;
  --footer-bg:   #ffffff;
  --border:      #e6d2b5;
  --muted:       #6b7280;
  --brown-light: #c9a27a;
  --brown-dark:  #7c2d12;
  --radius:      18px;
  --radius-sm:   10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 16px;
  background: var(--app-bg);
  overflow: hidden;
}

/* ── Layout ────────────────────────────────────────────── */

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

.app.hidden { display: none; }

/* ── Header ────────────────────────────────────────────── */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: calc(0.75rem + env(safe-area-inset-top, 0px));
  background: var(--header-bg);
  color: var(--header-fg);
  flex-shrink: 0;
  gap: 0.75rem;
}

.header-identity {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 600;
  font-size: 1rem;
}

.header-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  background: #334155;
  flex-shrink: 0;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--header-fg);
  cursor: pointer;
  padding: 0.25rem;
  opacity: 0.8;
  display: flex;
  align-items: center;
}
.btn-icon:hover { opacity: 1; }

.btn-pending {
  position: relative;
  background: none;
  border: none;
  color: var(--header-fg);
  cursor: pointer;
  padding: 0.25rem;
  opacity: 0.9;
  display: flex;
  align-items: center;
}
.btn-pending:hover { opacity: 1; }

.pending-badge {
  position: absolute;
  top: -2px;
  right: -4px;
  background: #ef4444;
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  padding: 0 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  pointer-events: none;
}

@keyframes signal-flash {
  0%, 100% { box-shadow: none; }
  30%      { box-shadow: 0 0 0 3px #facc15, 0 0 12px 4px rgba(250,204,21,0.5); }
}
.signal-card--flash { animation: signal-flash 0.9s ease; }

/* ── Messages ──────────────────────────────────────────── */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.bubble-row {
  display: flex;
  flex-direction: column;
  max-width: 80%;
}
.bubble-row.bot  { align-self: flex-start; }
.bubble-row.user { align-self: flex-end; }

.bubble {
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
  line-height: 1.45;
  word-break: break-word;
}
.bubble-row.bot  .bubble {
  background: var(--bot-bg);
  color: var(--bot-fg);
  border-bottom-left-radius: var(--radius-sm);
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.bubble-row.user .bubble {
  background: var(--user-bg);
  color: var(--user-fg);
  border-bottom-right-radius: var(--radius-sm);
}

.bubble img {
  max-width: 100%;
  border-radius: 10px;
  display: block;
  margin-bottom: 0.4rem;
}

.bubble-time {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0.2rem;
  padding: 0 0.25rem;
}
.bubble-row.user .bubble-time { text-align: right; color: #93c5fd; }

/* typing indicator */
.typing .bubble {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 0.7rem 1rem;
}
.typing .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing .dot:nth-child(2) { animation-delay: .2s; }
.typing .dot:nth-child(3) { animation-delay: .4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: .3; }
  40% { opacity: 1; }
}

/* ── Footer ────────────────────────────────────────────── */

.app-footer {
  flex-shrink: 0;
  background: var(--footer-bg);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}

/* chips row */
.chips-row {
  display: flex;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem 0.4rem;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips-row::-webkit-scrollbar { display: none; }

.chip-wrapper {
  display: flex;
  align-items: center;
  gap: 0;
  flex-shrink: 0;
  border-radius: 999px;
  overflow: hidden;
  border: 1.5px solid var(--chip-bg);
}

.chip {
  background: var(--chip-bg);
  color: var(--chip-fg);
  border: none;
  padding: 0.45rem 0.8rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  letter-spacing: 0.01em;
}
.chip:active { opacity: 0.8; }

.chip-gear {
  background: var(--chip-bg);
  color: var(--chip-fg);
  border: none;
  border-left: 1px solid rgba(255,255,255,0.15);
  padding: 0.45rem 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  opacity: 0.7;
}
.chip-gear:hover { opacity: 1; }

/* input bar */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem 0.6rem;
}

.btn-attach {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0.4rem;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}
.btn-attach:hover { color: var(--primary); }

#msg-input {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: 22px;
  padding: 0.55rem 0.9rem;
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  outline: none;
  line-height: 1.4;
  max-height: 120px;
  overflow-y: auto;
  background: #f9fafb;
  transition: border-color .2s;
}
#msg-input:focus { border-color: var(--primary); background: #fff; }

.btn-send {
  background: var(--primary);
  border: none;
  color: white;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
.btn-send:hover  { background: #1d4ed8; }
.btn-send:active { transform: scale(0.93); }
.btn-send:disabled { background: var(--border); cursor: default; }

/* ── Bottom sheet panel ────────────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 40;
  animation: fade-in .2s ease;
}
.overlay.hidden { display: none; }

.panel {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 0.5rem 1.25rem 1.5rem;
  padding-bottom: calc(1.5rem + var(--safe-bottom));
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  animation: slide-up .25s ease;
  max-height: 85dvh;
  overflow-y: auto;
}
.panel.hidden { display: none; }

.panel-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 0 auto 0.5rem;
}

.panel-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--bot-fg);
}

/* photo zone */
.photo-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 1.5rem;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  font-size: 0.9rem;
  transition: border-color .15s, color .15s;
}
.photo-zone:hover { border-color: var(--primary); color: var(--primary); }
.photo-zone.has-image { padding: 0; border-style: solid; border-color: var(--primary); overflow: hidden; }

.photo-zone img {
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  display: block;
  border-radius: 8px;
}

.photo-remove {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  background: rgba(0,0,0,.55);
  color: white;
  border: none;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  font-size: 0.8rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.photo-wrapper {
  position: relative;
}

/* Estados del chip de gasto/ingreso */
.chip--analyzing {
  background: rgba(99,102,241,.12);
  color: var(--primary);
  border-color: rgba(99,102,241,.3);
}
.chip--ready {
  background: rgba(22,163,74,.12);
  color: #16a34a;
  border-color: rgba(22,163,74,.3);
}

/* Mensaje de análisis en el panel */
.expense-analyzing-msg {
  display: flex;
  align-items: center;
  gap: .6rem;
  font-size: .82rem;
  color: var(--muted);
  background: var(--surface, #f9fafb);
  border-radius: var(--radius-sm);
  padding: .6rem .75rem;
  border: 1px solid var(--border);
}

.expense-spinner-sm {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

/* Overlay analizando sobre la foto */
.photo-analyzing {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  color: #fff;
  font-size: .82rem;
  border-radius: 8px;
}

.expense-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Campos de transacción (monto + fecha + concepto) */
.expense-fields {
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.expense-row {
  display: flex;
  gap: .5rem;
}

.expense-row input {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: .55rem .75rem;
  font-size: .9rem;
  font-family: inherit;
  outline: none;
  background: #f9fafb;
  color: inherit;
  min-width: 0;
}

.expense-row input:focus { border-color: var(--primary); background: #fff; }

.expense-concept-badge {
  display: inline-block;
  background: rgba(99,102,241,.12);
  color: var(--primary);
  border: 1px solid rgba(99,102,241,.3);
  border-radius: 999px;
  padding: .2rem .85rem;
  font-size: .8rem;
  font-weight: 600;
  align-self: flex-start;
}

.concept-confirm {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .55rem .75rem;
  background: rgba(99,102,241,.07);
  border: 1px solid rgba(99,102,241,.2);
  border-radius: var(--radius-sm);
}

.concept-question-text {
  font-size: .88rem;
  font-weight: 500;
  color: var(--primary);
  flex: 1;
}

.concept-confirm-btns {
  display: flex;
  gap: .35rem;
  flex-shrink: 0;
}

.btn-concept-yes,
.btn-concept-no {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  border: none;
  font-size: .95rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity .15s;
}

.btn-concept-yes { background: #22c55e; color: #fff; }
.btn-concept-no  { background: #ef4444; color: #fff; }
.btn-concept-yes:hover,
.btn-concept-no:hover { opacity: .82; }

.panel textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.9rem;
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  outline: none;
  line-height: 1.45;
  transition: border-color .2s;
  background: #f9fafb;
}
.panel textarea:focus { border-color: var(--primary); background: #fff; }

.btn-primary {
  width: 100%;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.8rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary:hover    { background: #1d4ed8; }
.btn-primary:disabled { background: var(--border); cursor: default; }
.btn-primary[aria-busy="true"]::after {
  content: '';
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: white;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  margin-left: 0.5rem;
  vertical-align: middle;
}

/* ── Gear menu ─────────────────────────────────────────── */

.gear-menu {
  position: fixed;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,.15);
  z-index: 60;
  overflow: hidden;
  min-width: 180px;
  animation: fade-in .15s ease;
}
.gear-menu.hidden { display: none; }

.gear-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--bot-fg);
  text-decoration: none;
  background: none;
  border: none;
  width: 100%;
  text-align: left;
}
.gear-item:hover { background: #f3f4f6; }
.gear-item + .gear-item { border-top: 1px solid var(--border); }

/* ── Gate (login) ──────────────────────────────────────── */

.gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  padding: 2rem 1.5rem;
  gap: 2rem;
  background: linear-gradient(180deg, #f97316 0%, #fb923c 30%, #fed7aa 65%, #fff7ed 100%);
  color: var(--brown-dark);
  text-align: center;
}
.gate.hidden { display: none; }

.gate-logo {
  font-size: 3rem;
  line-height: 1;
}

.gate-title {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #ffffff;
  text-shadow: 0 1px 3px rgba(124,45,18,.25);
}

.gate-subtitle {
  font-size: 0.95rem;
  color: rgba(255,255,255,.85);
  margin-top: -1.5rem;
  text-shadow: 0 1px 2px rgba(124,45,18,.2);
}

.gate-card {
  background: #ffffff;
  border: 1px solid var(--brown-light);
  border-radius: 16px;
  padding: 1.5rem;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  box-shadow: 0 10px 30px rgba(124,45,18,.15);
}

.gate-divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.8rem;
  color: var(--brown-light);
}
.gate-divider::before,
.gate-divider::after {
  content: '';
  flex: 1;
  border-top: 1px solid var(--brown-light);
  opacity: .5;
}

.gate-input-row {
  display: flex;
  gap: 0.5rem;
}

.gate-input-row input {
  flex: 1;
  background: #fff7ed;
  border: 1px solid var(--brown-light);
  border-radius: 10px;
  color: var(--brown-dark);
  padding: 0.6rem 0.8rem;
  font-size: 0.9rem;
  outline: none;
}
.gate-input-row input::placeholder { color: var(--brown-light); opacity: .8; }
.gate-input-row input:focus { border-color: var(--primary); }

.btn-wa {
  background: #25d366;
  color: white;
  border: none;
  border-radius: 10px;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}
.btn-wa:hover    { background: #1ebe5d; }
.btn-wa:disabled { opacity: 0.6; cursor: default; }

.gate-hint {
  font-size: 0.8rem;
  color: var(--brown-light);
  min-height: 1em;
}

.gate-legal {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--brown-dark);
}
.gate-legal a {
  color: var(--brown-dark);
  text-decoration: none;
}
.gate-legal a:hover { text-decoration: underline; }
.gate-legal span { opacity: .4; }

/* ── Animations ────────────────────────────────────────── */

@keyframes slide-up {
  from { transform: translateX(-50%) translateY(100%); }
  to   { transform: translateX(-50%) translateY(0); }
}
@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Processing bubble ─────────────────────────────────── */

.bubble-processing {
  display: flex !important;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.65rem !important;
}

.bubble-processing-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
  margin: 0 !important;
  opacity: 0.85;
}

.bubble-processing-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.82rem;
  color: rgba(255,255,255,0.9);
  white-space: nowrap;
}

.bubble-processing-spinner {
  width: 13px;
  height: 13px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}

/* ── Signal card ───────────────────────────────────────── */

.signal-card {
  padding: 0 !important;
  overflow: hidden;
  width: 260px;
  max-width: 100%;
}

.signal-card-img {
  width: 100%;
  max-height: 190px;
  object-fit: cover;
  border-radius: 0 !important;
  margin: 0 !important;
  display: block;
}

.signal-chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.55rem 0.7rem 0;
}

.signal-chip {
  background: rgba(255,255,255,0.2);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.18rem 0.5rem;
  border-radius: 999px;
}

.signal-question {
  padding: 0.5rem 0.75rem 0.35rem;
  font-size: 0.88rem;
  color: rgba(255,255,255,0.95);
  line-height: 1.35;
}

.signal-input-row {
  display: flex;
  align-items: center;
  padding: 0.35rem 0.6rem 0.6rem;
}

.signal-input {
  flex: 1;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  border-right: none;
  border-radius: 20px 0 0 20px;
  padding: 0.42rem 0.7rem;
  font-size: 0.85rem;
  color: #fff;
  outline: none;
  min-width: 0;
}
.signal-input::placeholder { color: rgba(255,255,255,0.5); }
.signal-input:focus { border-color: rgba(255,255,255,0.65); background: rgba(255,255,255,0.2); }
.signal-input:disabled { opacity: 0.55; }

.signal-btn {
  background: rgba(255,255,255,0.2);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 0 20px 20px 0;
  padding: 0.42rem 0.8rem;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
}
.signal-btn:hover:not(:disabled) { background: rgba(255,255,255,0.32); }
.signal-btn:disabled { opacity: 0.5; cursor: default; }

.signal-confirmed {
  display: block;
  padding: 0.4rem 0.75rem 0.6rem;
  font-size: 0.85rem;
  color: rgba(255,255,255,0.8);
}

/* ── Panel de pendientes ───────────────────────────────── */

.pending-empty {
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 1.5rem 0;
}

.pending-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  transition: opacity 0.2s;
}
.pending-item + .pending-item { margin-top: 0.6rem; }
.pending-item--done { opacity: 0.45; pointer-events: none; }

.pending-chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.pending-chip {
  background: rgba(249,115,22,0.12);
  color: var(--primary);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  border: 1px solid rgba(249,115,22,0.25);
}

.pending-snippet {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.35;
}

.pending-input-row {
  display: flex;
  align-items: center;
  gap: 0;
}

.pending-input {
  flex: 1;
  border: 1.5px solid var(--border);
  border-right: none;
  border-radius: 20px 0 0 20px;
  padding: 0.42rem 0.7rem;
  font-size: 0.85rem;
  font-family: inherit;
  outline: none;
  background: #f9fafb;
  min-width: 0;
}
.pending-input:focus { border-color: var(--primary); background: #fff; }
.pending-input:disabled { opacity: 0.55; }

.pending-btn {
  background: var(--primary);
  border: 1.5px solid var(--primary);
  border-radius: 0 20px 20px 0;
  padding: 0.42rem 0.8rem;
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
  flex-shrink: 0;
}
.pending-btn:hover:not(:disabled) { background: #ea580c; }
.pending-btn:disabled { opacity: 0.5; cursor: default; }

.pending-confirmed {
  font-size: 0.85rem;
  color: #16a34a;
  font-weight: 500;
}

/* ── Utilities ─────────────────────────────────────────── */

.hidden { display: none !important; }
