:root {
  --bg: #0b0f14;
  --surface: #141b24;
  --surface2: #1c2633;
  --border: #2a3647;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 12px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

.hidden { display: none !important; }

.screen { min-height: 100vh; }

.login-card {
  max-width: 400px;
  width: calc(100% - 2rem);
  margin: 8vh auto;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.brand { text-align: center; margin-bottom: 2rem; }
.brand.compact { display: flex; align-items: center; gap: .75rem; text-align: left; margin-bottom: 2rem; font-weight: 600; }
.brand-icon {
  width: 48px; height: 48px; border-radius: 12px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .9rem;
}
.brand h1 { margin: .5rem 0 .25rem; font-size: 1.5rem; }
.brand p { color: var(--muted); margin: 0; font-size: .9rem; }

form label { display: block; margin: .75rem 0 .25rem; font-size: .875rem; color: var(--muted); }
input, select, textarea, button {
  font: inherit;
}
input, select, textarea {
  width: 100%;
  padding: .65rem .75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}
button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: .65rem 1rem;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
}
button:hover { background: var(--primary-hover); }

#login-form button { width: 100%; margin-top: 1rem; }
.erro { color: var(--danger); font-size: .875rem; margin-top: .5rem; }

.login-footer { text-align: center; margin-top: 1.5rem; }
.login-footer .powered { font-size: .75rem; color: var(--muted); display: block; }
.app-footer {
  text-align: center; padding: 1rem 0; margin-top: 2rem;
  border-top: 1px solid var(--border); font-size: .75rem; color: var(--muted);
}
.footer-logo {
  height: 30px; width: auto; object-fit: contain; border-radius: 4px;
  vertical-align: middle; margin-right: .5rem;
}
.login-footer-logo { display: block; height: 28px; margin: .5rem auto 0; }
.app-footer a { color: var(--primary); text-decoration: none; }
.app-footer a:hover { text-decoration: underline; }

#app-screen { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }

.sidebar-overlay { display: none; }

.mobile-topbar { display: none; }

.btn-icon {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  flex-shrink: 0;
}
.btn-icon:hover { background: var(--border); }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.25rem;
  padding-top: calc(1.25rem + env(safe-area-inset-top));
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}

.nav-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  color: var(--muted);
  margin-bottom: .25rem;
  font-weight: 500;
  min-height: 44px;
  padding: .65rem .75rem;
}
.nav-item:hover, .nav-item.active {
  background: var(--surface2);
  color: var(--text);
}
.nav-item.active { border-left: 3px solid var(--primary); }

.nav-divider { height: 1px; background: var(--border); margin: .75rem 0; }

.vinculos-banner {
  display: flex; flex-wrap: wrap; gap: .5rem;
  margin-bottom: 1rem; padding: .75rem 1rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.vinculo-chip {
  background: var(--surface2); padding: .35rem .75rem; border-radius: 999px; font-size: .85rem;
}
.vinculo-chip small { color: var(--muted); margin-left: .35rem; }

.vinculos-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.vinculo-card {
  display: flex; gap: 1rem; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem;
}
.vinculo-card-icon {
  width: 44px; height: 44px; border-radius: 10px; background: linear-gradient(135deg, #3b82f6, #6366f1);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: .8rem; flex-shrink: 0;
}

.avisos-list { display: grid; gap: .75rem; }
.aviso-card {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: var(--radius); padding: 1rem;
}
.aviso-card.aviso-urgente { border-left-color: var(--danger); }
.aviso-card.aviso-aviso { border-left-color: var(--warning); }
.aviso-card.lido { opacity: .65; }
.aviso-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .35rem; }

.cal-nav { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.calendario-grid { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem; }
.cal-dow { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; margin-bottom: 4px; }
.cal-dow span { text-align: center; font-size: .75rem; color: var(--muted); padding: .25rem; }
.cal-days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-day { min-height: 64px; background: var(--bg); border-radius: 6px; padding: 4px; font-size: .75rem; }
.cal-day.empty { background: transparent; }
.cal-day.has-events { background: var(--surface2); border: 1px solid var(--border); }
.cal-day[data-date] { cursor: pointer; }
.cal-day[data-date]:hover { outline: 1px solid var(--accent, #3b82f6); }
.cal-num { font-weight: 600; display: block; margin-bottom: 2px; }
.cal-ev { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--primary); font-size: .65rem; }

.btn-ghost {
  margin-top: auto;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}

.content {
  padding: 1.5rem 2rem;
  padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
  overflow-x: hidden;
  min-width: 0;
}
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem; gap: .75rem; flex-wrap: wrap; }
.page-header h2 { margin: 0; font-size: 1.5rem; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.stat-card .label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; }
.stat-card .value { font-size: 1.75rem; font-weight: 700; margin-top: .25rem; }
.stat-card .value.money { color: var(--success); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
}
.card h3 { margin: 0 0 1rem; font-size: 1rem; }

.btn-primary, .btn-secondary {
  padding: .5rem 1rem;
  font-size: .875rem;
  min-height: 44px;
}
.btn-secondary {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-secondary:hover { background: var(--border); }

.actions-row { display: flex; gap: .75rem; flex-wrap: wrap; }

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
.table-wrap .data-table { min-width: 560px; }
table { width: 100%; border-collapse: collapse; font-size: .875rem; }
th, td { padding: .65rem .75rem; text-align: left; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: .75rem; text-transform: uppercase; }

.badge {
  display: inline-block;
  padding: .2rem .5rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
}
.badge.pendente { background: #422006; color: #fbbf24; }
.badge.pago { background: #14532d; color: #4ade80; }
.badge.vencido { background: #450a0a; color: #f87171; }

.chat-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 1rem;
  height: calc(100vh - 120px);
}
.chat-sidebar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
  padding: .5rem;
}
.chat-item {
  padding: .5rem .75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: .85rem;
  margin-bottom: .25rem;
}
.chat-item:hover, .chat-item.active { background: var(--surface2); }
.chat-main {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}
.msg { margin-bottom: 1rem; max-width: 85%; }
.msg.user { margin-left: auto; }
.msg .bubble {
  padding: .75rem 1rem;
  border-radius: 12px;
  background: var(--surface2);
  white-space: pre-wrap;
  font-size: .9rem;
  line-height: 1.5;
}
.msg.user .bubble { background: var(--primary); }
.chat-input {
  display: flex;
  gap: .5rem;
  padding: 1rem;
  border-top: 1px solid var(--border);
}
.chat-input input { flex: 1; }

.config-form fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}
.config-form legend { padding: 0 .5rem; color: var(--muted); font-size: .875rem; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; }
.checkbox { display: flex; align-items: center; gap: .5rem; margin: .5rem 0; }
.checkbox input { width: auto; }
.hint { color: var(--muted); font-size: .8rem; }

.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: .75rem 1.25rem;
  border-radius: 8px;
  z-index: 100;
}

.agenda-status-bar {
  padding: .75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: .9rem;
  color: var(--muted);
}

.agenda-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: .75rem;
  margin-bottom: 1.5rem;
}
.agenda-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .75rem 1rem;
  text-align: center;
}
.agenda-stat strong { display: block; font-size: 1.5rem; color: var(--primary); }
.agenda-stat span { font-size: .75rem; color: var(--muted); text-transform: uppercase; }

.agenda-tabs { display: flex; gap: .5rem; margin-bottom: 1rem; }
.agenda-tab {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: .4rem .75rem;
  font-size: .85rem;
}
.agenda-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.agenda-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
}

.agenda-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: .65rem 0;
  border-bottom: 1px solid var(--border);
  gap: .5rem;
}
.agenda-item:last-child { border-bottom: none; }
.agenda-item.done { opacity: .55; text-decoration: line-through; }
.agenda-item-meta { font-size: .8rem; color: var(--muted); margin-top: .15rem; }
.agenda-rec { opacity: .85; }
.hint-inline { font-size: .85rem; color: var(--muted); display: block; margin: .25rem 0 .5rem; }
.agenda-item-actions { display: flex; gap: .25rem; flex-shrink: 0; }
.btn-sm { padding: .35rem .65rem; font-size: .75rem; min-height: 36px; }
.btn-danger {
  background: #7f1d1d;
  border: 1px solid #991b1b;
  color: #fecaca;
}
.btn-danger:hover { background: #450a0a; border-color: #7f1d1d; }
.row-muted { opacity: .55; }
.actions-cell { white-space: nowrap; }

.nota-item {
  padding: .75rem 0;
  border-bottom: 1px solid var(--border);
}
.nota-item:last-child { border-bottom: none; }
.nota-item strong { display: block; margin-bottom: .25rem; }
.nota-item p { margin: 0; font-size: .9rem; color: var(--muted); white-space: pre-wrap; }

.stat-card .value.danger { color: var(--danger); }
.stat-card .value.warning { color: var(--warning); }

.tipo-recebimento { color: var(--success); }
.tipo-despesa { color: var(--danger); }

.setup-steps {
  margin: .5rem 0 1rem 1.1rem;
  padding: 0;
  color: var(--muted);
  font-size: .9rem;
  line-height: 1.55;
}
.setup-steps li { margin-bottom: .45rem; }
.setup-steps strong { color: var(--text); }
.calendar-setup legend { font-weight: 600; }
.calendar-setup code {
  font-size: .8rem;
  word-break: break-all;
  background: var(--bg-elevated, rgba(0,0,0,.2));
  padding: .15rem .35rem;
  border-radius: 4px;
}

@media (max-width: 1024px) {
  .content { padding: 1.25rem 1.5rem; }
  .chat-layout { grid-template-columns: 180px 1fr; }
}

@media (max-width: 768px) {
  #app-screen {
    display: block;
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 88vw);
    z-index: 200;
    transform: translateX(-100%);
    transition: transform .3s ease;
    overflow-y: auto;
    box-shadow: 4px 0 24px rgba(0, 0, 0, .35);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    z-index: 190;
    opacity: 0;
    pointer-events: none;
    transition: opacity .3s;
  }
  .sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .75rem 0;
    padding-top: calc(.5rem + env(safe-area-inset-top));
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--bg);
  }
  #mobile-topbar-title {
    font-weight: 600;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .content {
    padding: 1rem;
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }

  .page-header {
    flex-direction: column;
    align-items: stretch;
  }
  .page-header h2 { font-size: 1.25rem; }
  .page-header .actions-row,
  .page-header > div { width: 100%; }
  .page-header .actions-row > *,
  .page-header > div > * { flex: 1 1 auto; }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .75rem;
  }
  .agenda-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .field-row { grid-template-columns: 1fr; gap: .75rem; }

  .actions-row { gap: .5rem; }
  .actions-row > select,
  .actions-row > input,
  .actions-row > button { min-width: 0; }
  .actions-row input[style*="max-width"] { max-width: none !important; width: 100%; }

  .agenda-tabs { flex-wrap: wrap; }
  .agenda-tab {
    flex: 1 1 auto;
    min-height: 44px;
    text-align: center;
  }

  .chat-layout {
    grid-template-columns: 1fr;
    height: calc(100dvh - 10rem);
    min-height: 360px;
  }
  .chat-sidebar {
    display: block;
    max-height: 140px;
    flex-shrink: 0;
  }

  .agenda-grid,
  .vinculos-grid { grid-template-columns: 1fr; }

  .cal-day { min-height: 52px; font-size: .62rem; }
  .cal-ev { font-size: .58rem; }

  .aviso-header { flex-direction: column; align-items: flex-start; gap: .35rem; }

  .toast {
    left: 1rem;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom));
  }

  .card .page-header { margin-bottom: .75rem; }
  .card .page-header .actions-row {
    flex-direction: column;
    align-items: stretch;
  }
  .card .page-header .actions-row > * { width: 100%; }

  input, select, textarea {
    min-height: 44px;
    font-size: 16px;
  }
  textarea { min-height: 88px; }

  .config-form button[type="submit"] { width: 100%; }

  .jarvis-screen { min-height: calc(100dvh - 8rem); padding: 1.25rem; }
  .jarvis-controls { flex-wrap: wrap; justify-content: center; }
  .jarvis-btn { min-height: 44px; }
}

@media (max-width: 480px) {
  .stats-grid,
  .agenda-stats { grid-template-columns: 1fr; }
  .stat-card .value { font-size: 1.45rem; }
  .stat-card { padding: 1rem; }

  .login-card { margin: 4vh auto; padding: 1.25rem; }

  .table-wrap.cards-mobile {
    overflow-x: visible;
  }
  .table-wrap.cards-mobile .data-table {
    min-width: 0;
    border: none;
  }
  .table-wrap.cards-mobile .data-table thead { display: none; }
  .table-wrap.cards-mobile .data-table tbody { display: block; }
  .table-wrap.cards-mobile .data-table tr {
    display: block;
    margin-bottom: .75rem;
    padding: .85rem 1rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }
  .table-wrap.cards-mobile .data-table td {
    display: block;
    border: none;
    padding: .3rem 0;
    text-align: left;
  }
  .table-wrap.cards-mobile .data-table td:last-child {
    padding-top: .5rem;
    margin-top: .35rem;
    border-top: 1px solid var(--border);
  }
  .table-wrap.cards-mobile .data-table td .btn-sm,
  .table-wrap.cards-mobile .data-table td .btn-secondary,
  .table-wrap.cards-mobile .data-table td .btn-danger {
    margin: .2rem .35rem .2rem 0;
  }

  .vinculo-card { flex-direction: column; }
  .chat-layout { height: calc(100dvh - 9rem); }
}

@media (max-width: 320px) {
  .content { padding: .75rem; }
  .brand h1 { font-size: 1.25rem; }
  .page-header h2 { font-size: 1.1rem; }
}
