/* ════════════════════════════════════════════════════════════════
   NORMAIA · App Console (Post-Login)
   Identidad editorial — sidebar light (paper-2) + chat con
   bubbles serif y composer NormaIA.
   Mantiene todas las clases legacy para no romper handlers JS.
   ══════════════════════════════════════════════════════════════ */

#app-console {
  display: none;
  height: 100vh;             /* fallback */
  height: 100dvh;            /* viewport dinámico — se ajusta a la URL bar móvil */
  width: 100vw;
  background: var(--paper);
  color: var(--ink);
  overflow: hidden;
  flex-direction: row;
}
/* Cuando JS lo activa con style="display:flex", debe verse en row */
#app-console[style*="display: flex"] { flex-direction: row; }

/* ════════════════════════════════════════════════════════════════
   SIDEBAR (light editorial)
   ══════════════════════════════════════════════════════════════ */
.app-sidebar {
  width: 280px;
  background: var(--paper-2);
  color: var(--ink);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 20;
  min-height: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 22px 22px 6px;
  margin-bottom: 14px;
}

.sidebar-logo {
  font-family: var(--serif);
  font-weight: 400;
  font-size: 24px;
  line-height: 1;
  letter-spacing: -.02em;
  color: var(--ink);
}
.sidebar-logo span {
  font-family: var(--sans);
  font-style: normal;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: .04em;
  color: var(--accent);
  margin-left: 4px;
  vertical-align: 4px;
}

/* SVG brand-mark inyectado vía HTML — el ::before queda como respaldo */
.sidebar-logo::before {
  content: "N";
  display: inline-grid;
  place-items: center;
  width: 32px; height: 32px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--serif);
  font-style: italic;
  font-size: 22px;
  letter-spacing: -.02em;
  margin-right: 10px;
  vertical-align: -8px;
  position: relative;
}

.sidebar-close-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--ink-3);
  font-size: 1.4rem;
  cursor: pointer;
}
.sidebar-close-btn:hover { color: var(--ink); }

.sidebar-scrollable {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 14px;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  gap: 4px;
}

/* Historial — toma el espacio restante con scroll propio */
.history-section {
  flex: 1;
  flex-shrink: 0;
  min-height: 200px;
  display: flex;
  flex-direction: column;
  padding-top: 6px;
  border-top: 1px solid var(--line-soft);
  margin-top: 6px;
  overflow: hidden;
}

/* Mis declaraciones — sección compacta, no se come el historial.
   Altura tope ~240px; lista interna scrollea sola. */
.decl-section {
  flex: 0 0 auto;
  max-height: 240px;
}
.decl-section .history-list {
  max-height: 180px;
}

/* Clientes — espacio cómodo para 5-6 visibles, scroll suave para más.
   Items compactos con ellipsis estricto en título y subtítulo. */
.clientes-section {
  flex: 0 0 auto;
  max-height: 320px;
}
.clientes-section .history-list {
  max-height: 260px;
  overflow-y: auto;
  padding-right: 4px;
}
.clientes-section .history-item {
  margin-bottom: 2px;
}
.clientes-section .history-item-main {
  padding: 6px 10px;
  min-height: 0;
}
.clientes-section .history-item-title {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.25;
}
.clientes-section .history-item-date {
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  margin-top: 1px;
  color: var(--ink-4);
}
.clientes-section .history-list::-webkit-scrollbar { width: 6px; }
.clientes-section .history-list::-webkit-scrollbar-thumb {
  background: oklch(from var(--ink) l c h / .12);
  border-radius: 4px;
}

/* Botón "+ Nuevo cliente" — visualmente prominente, bloqueado si el plan está al tope. */
#btn-new-cliente {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
}
#btn-new-cliente.locked {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ════════════════════════════════════════════════════════════════
   FASE 3 — Dashboard por cliente
   ══════════════════════════════════════════════════════════════ */
.dash-list {
  list-style: none;
  padding: 0;
  margin: 14px 0 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.dash-row {
  display: grid;
  grid-template-columns: 36px 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  transition: border-color 0.15s, background 0.15s;
}
.dash-row:hover { border-color: var(--ink-4); background: var(--paper-2); }
.dash-row-icon {
  font-size: 20px;
  text-align: center;
}
.dash-row-main { min-width: 0; }
.dash-row-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}
.dash-row-sub {
  font-weight: normal;
  color: var(--ink-3);
  font-size: 12px;
}
.dash-row-period {
  font-size: 11px;
  color: var(--ink-4);
  font-family: var(--mono);
  margin-top: 2px;
}
.dash-row-action {
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  border-radius: 6px;
}
.dash-row-action:disabled { opacity: 0.4; cursor: not-allowed; }

/* Badges de estado */
.dash-badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.dash-badge-ok       { background: oklch(from var(--success, #10b981) l c h / 0.15); color: var(--success, #10b981); }
.dash-badge-warn     { background: oklch(from var(--warn, #f59e0b) l c h / 0.18); color: oklch(from var(--warn, #f59e0b) calc(l - 0.15) c h); }
.dash-badge-pending  { background: oklch(from var(--ink) l c h / 0.06); color: var(--ink-3); }
.dash-badge-overdue  { background: oklch(from var(--danger, #c43c3c) l c h / 0.13); color: var(--danger, #c43c3c); }

.dash-empty {
  padding: 30px 20px;
  text-align: center;
  color: var(--ink-3);
  font-size: 13px;
  background: var(--paper-2);
  border-radius: 8px;
  margin-top: 14px;
}
.dash-footer {
  margin-top: 16px;
  font-size: 11px;
  color: var(--ink-4);
  text-align: center;
  padding: 0 12px;
}

/* Grid de checkboxes de obligaciones en el modal de cliente (Fase 3). */
.cliente-obligaciones-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  padding: 8px 10px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 6px;
}
.cliente-obligaciones-grid label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: normal;
  cursor: pointer;
  padding: 2px 0;
}
.cliente-obligaciones-grid label:hover {
  color: var(--accent);
}
.cliente-obligaciones-grid input[type="checkbox"] {
  margin: 0;
  flex: 0 0 auto;
}

/* Cliente activo — destacado en la lista. */
#clientes-list .history-item.active {
  background: oklch(from var(--accent) l c h / .08);
  border-left: 2px solid var(--accent);
}
#clientes-list .history-item.active .history-item-title {
  font-weight: 600;
  color: var(--accent);
}

/* ════════════════════════════════════════════════════════════════
   FASE 2.5 — Banner de pre-llenado IT-1 desde 606/607
   ══════════════════════════════════════════════════════════════ */
.ff-prefill-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  margin: 0 0 14px;
  background: oklch(from var(--success, #10b981) l c h / 0.08);
  border-left: 3px solid var(--success, #10b981);
  border-radius: 6px;
  font-size: 13px;
  color: var(--ink);
  animation: prefillIn 0.25s ease-out;
}
.ff-prefill-icon {
  color: var(--success, #10b981);
  font-weight: 700;
  font-size: 15px;
  flex: 0 0 auto;
}
.ff-prefill-text {
  flex: 1;
  line-height: 1.4;
}
.ff-prefill-revert {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  color: var(--ink-3);
  white-space: nowrap;
  flex: 0 0 auto;
  transition: all 0.15s;
}
.ff-prefill-revert:hover {
  background: var(--paper-2);
  color: var(--ink);
  border-color: var(--ink-4);
}
@keyframes prefillIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.nav-section-title {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-4);
  margin: 14px 12px 8px;
  padding: 0;
}

/* Nav items (Asistente + Smart Tools) */
.nav-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  margin: 0 0 2px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
}
.nav-item:hover {
  background: oklch(from var(--ink) l c h / .04);
  color: var(--ink);
}
.nav-item.active {
  background: var(--ink);
  color: var(--paper);
  font-weight: 500;
  position: relative;
}
.nav-item.active::before {
  content: "";
  position: absolute;
  left: -8px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 22px;
  border-radius: 2px;
  background: var(--accent);
}

.nav-item-label { display: inline-flex; align-items: center; gap: 8px; }

.lock-badge {
  font-family: var(--mono);
  font-size: 9.5px;
  color: var(--ink-4);
  border: 1px solid var(--line);
  background: var(--card);
  padding: 1px 5px;
  border-radius: 4px;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.nav-item.active .lock-badge {
  color: oklch(from var(--paper) l c h / .85);
  border-color: oklch(from var(--paper) l c h / .25);
  background: oklch(from var(--paper) l c h / .1);
}

/* Sidebar bottom — compacto */
.sidebar-bottom {
  padding: 10px 14px 14px;
  border-top: 1px solid var(--line);
  background: oklch(from var(--ink) l c h / .015);
}

/* Tarjeta de uso compacta — uso + barra + botón mejorar en un solo bloque */
.usage-card {
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  margin-bottom: 8px;
}
.usage-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.usage-label {
  font-size: 11px;
  color: var(--ink-3);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: .06em;
}
.usage-mono {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink);
  font-weight: 500;
}
.usage-bar-bg {
  height: 4px;
  background: oklch(from var(--ink) l c h / .08);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 8px;
}
.usage-bar-fill {
  height: 100%;
  background: linear-gradient(to right, var(--pos), var(--warn));
  border-radius: 999px;
  transition: width 0.3s;
}

.btn-upgrade-sidebar {
  width: 100%;
  background: linear-gradient(135deg, var(--accent) 0%, oklch(from var(--accent) calc(l - .08) c calc(h + 20)) 100%);
  color: var(--paper);
  border: 0;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  font: 600 12px/1 var(--sans);
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-upgrade-sidebar:hover {
  filter: brightness(1.08);
  transform: translateY(-1px);
}

/* Usuario + logout en una sola línea */
.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
}

.avatar-mini {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 12px;
  flex: 0 0 auto;
}

.user-info-text {
  min-width: 0;
  flex: 1;
  line-height: 1.2;
}
.user-info-text #user-name-display {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-info-text #user-plan-display {
  font-size: 11px;
  color: var(--accent);
  font-weight: 500;
}

.btn-logout-icon {
  width: 30px;
  height: 30px;
  background: transparent;
  color: var(--ink-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
  flex: 0 0 auto;
}
.btn-logout-icon:hover {
  color: var(--danger-norma);
  border-color: oklch(from var(--danger-norma) l c h / .35);
  background: oklch(from var(--danger-norma) l c h / .06);
}

/* ════════════════════════════════════════════════════════════════
   TOOLBAR DE SMART TOOLS (arriba del chat)
   Chips horizontales clickeables — sustituyen la lista del sidebar.
   ══════════════════════════════════════════════════════════════ */
.tools-bar {
  position: relative; z-index: 2;
  background: oklch(from var(--paper) l c h / .85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line-soft);
  padding: 10px 36px;
}
.tools-bar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 1100px;
  margin: 0 auto;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.tools-bar-inner::-webkit-scrollbar { display: none; }

.tools-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink-4);
  flex: 0 0 auto;
  margin-right: 4px;
}

.tool-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px 7px 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--ink-2);
  font: 500 12.5px/1 var(--sans);
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
  transition: all 0.15s;
}
.tool-chip:hover {
  border-color: oklch(from var(--accent) l c h / .45);
  background: oklch(from var(--accent-soft) l c h / .35);
  color: var(--ink);
  transform: translateY(-1px);
}
.tool-chip-icon {
  font-size: 14px;
  line-height: 1;
  display: inline-flex;
}
.tool-chip-label { font-weight: 500; }
.tool-chip-lock {
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--ink-4);
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 2px;
}
/* Chips de Smart Tools bloqueadas (plan Libre intentando Pro) */
.tool-chip.locked {
  opacity: 0.65;
}
.tool-chip.locked:hover {
  opacity: 1;
  border-color: oklch(from var(--accent) l c h / .35);
}

/* ════════════════════════════════════════════════════════════════
   DROPDOWN — Formatos DGII (606/607/608/609 agrupados)
   ══════════════════════════════════════════════════════════════ */
.tool-chip-dropdown {
  position: relative;
  display: inline-block;
  flex: 0 0 auto;
}
.tool-chip-dgii {
  background: oklch(from var(--accent-soft) l c h / .25);
}
.tool-chip-dgii[aria-expanded="true"] {
  background: oklch(from var(--accent) l c h / .15);
  border-color: oklch(from var(--accent) l c h / .55);
}
.dgii-menu {
  /* position: fixed (no absolute) para escapar del overflow:auto del tools-bar.
     Las coordenadas se setean por JS al abrir.
     width fijo (no min-width) para que el JS pueda calcular la posición
     correctamente sin que el contenido empuje el menú fuera de pantalla. */
  position: fixed;
  top: 0;
  left: 0;
  z-index: 200;
  width: 320px;
  max-width: calc(100vw - 16px);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-float);
  padding: 6px;
  display: none;
  flex-direction: column;
  gap: 2px;
  animation: dgiiMenuIn 0.15s ease-out;
}
.dgii-menu.open { display: flex; }
@keyframes dgiiMenuIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.dgii-menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: all 0.15s;
}
.dgii-menu-item:hover {
  background: var(--paper-2);
  border-color: var(--line);
}
.dgii-menu-num {
  font-family: var(--mono);
  font-weight: 600;
  font-size: 13px;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 5px 9px;
  border-radius: 6px;
  flex: 0 0 auto;
  letter-spacing: -0.02em;
}
.dgii-menu-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  min-width: 0;
}
.dgii-menu-text strong {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}
.dgii-menu-text small {
  font-size: 11px;
  color: var(--ink-3);
  font-weight: 400;
  margin-top: 2px;
  /* Permitir que envuelva si la descripción es larga, en lugar de cortarse. */
  white-space: normal;
  overflow-wrap: anywhere;
}

@media (max-width: 880px) {
  .dgii-menu {
    width: 280px;
    max-width: calc(100vw - 16px);
  }
}

/* ════════════════════════════════════════════════════════════════
   HISTORIAL — Items
   ══════════════════════════════════════════════════════════════ */
.history-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.history-refresh-btn {
  background: transparent;
  border: none;
  color: var(--ink-4);
  cursor: pointer;
  padding: 0.2rem;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, background 0.2s;
}
.history-refresh-btn:hover {
  color: var(--ink);
  background: oklch(from var(--ink) l c h / .06);
}

.btn-new-conv {
  width: calc(100% - 8px);
  margin: 0 4px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 12px;
  background: transparent;
  color: var(--ink);
  border: 1px dashed var(--line);
  border-radius: var(--r-md);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-new-conv:hover {
  background: oklch(from var(--ink) l c h / .04);
  border-style: solid;
  border-color: oklch(from var(--accent) l c h / .35);
  color: var(--accent);
}

.history-list {
  list-style: none;
  padding: 0 2px 8px 0;
  margin: 0 0 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.history-list::-webkit-scrollbar { width: 8px; }
.history-list::-webkit-scrollbar-thumb {
  background: oklch(from var(--ink) l c h / .12);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: content-box;
}

.history-empty {
  flex-shrink: 0;
  font-size: 12px;
  color: var(--ink-3);
  padding: 14px;
  font-style: italic;
  border: 1px dashed var(--line);
  border-radius: var(--r-md);
  text-align: center;
  margin: 4px;
}

.history-item {
  flex-shrink: 0;
  position: relative;        /* para las acciones absolutas */
  display: flex;
  align-items: stretch;
  border-radius: 8px;
  overflow: hidden;
  transition: background 0.15s;
}
.history-item:hover {
  background: oklch(from var(--ink) l c h / .04);
}
.history-item.active {
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
}
.history-item.loading { opacity: 0.5; }

.history-item-main {
  flex: 1;
  min-width: 0;        /* CLAVE: permite que el hijo con ellipsis se trunque */
  display: block;
  padding: 8px 10px;
  background: transparent;
  border: none;
  color: var(--ink-2);
  text-align: left;
  cursor: pointer;
  font: inherit;
  overflow: hidden;
}
.history-item-main:hover { color: var(--ink); }
.history-item.active .history-item-main { color: var(--accent-ink); }

.history-item-title {
  display: block;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  max-width: 100%;
  line-height: 1.25;
  margin-bottom: 2px;
}
.history-item-date {
  display: block;
  font-size: 10.5px;
  color: var(--ink-4);
  font-variant-numeric: tabular-nums;
}

/* Las acciones flotan a la derecha (absolute) y solo aparecen al hover/active.
   No comen ancho del título — el item completo está disponible para el texto. */
.history-item-actions {
  position: absolute;
  right: 4px; top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 2px;
  padding: 2px 4px;
  border-radius: 6px;
  background: linear-gradient(
    to right,
    transparent 0%,
    oklch(from var(--ink) l c h / .02) 8%,
    oklch(from var(--paper-2) l c h / 0.95) 25%
  );
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.history-item:hover .history-item-actions,
.history-item.active .history-item-actions {
  opacity: 1;
  pointer-events: auto;
}
.history-item.active .history-item-actions {
  background: linear-gradient(
    to right,
    transparent 0%,
    oklch(from var(--accent-soft) l c h / .4) 8%,
    var(--accent-soft) 25%
  );
}
.history-action-btn {
  background: transparent;
  border: none;
  color: var(--ink-3);
  cursor: pointer;
  font-size: 13px;
  padding: 3px 6px;
  border-radius: 4px;
  line-height: 1;
  transition: color 0.15s, background 0.15s;
}
.history-action-btn:hover {
  color: var(--ink);
  background: oklch(from var(--ink) l c h / .08);
}
.history-action-btn.danger:hover {
  color: var(--danger-norma);
  background: oklch(from var(--danger-norma) l c h / .1);
}

/* ════════════════════════════════════════════════════════════════
   MAIN — CHAT AREA
   ══════════════════════════════════════════════════════════════ */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--paper);
  min-height: 0;
  overflow: hidden;
}

/* Decorative gradient en la esquina superior derecha */
.app-main::before {
  content: "";
  position: absolute;
  inset: -200px -200px auto auto;
  width: 600px; height: 600px;
  background: radial-gradient(circle at 30% 30%, var(--accent-soft), transparent 70%);
  opacity: .45;
  pointer-events: none;
  z-index: 0;
}

.mobile-header {
  position: relative; z-index: 1;
  display: none;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line-soft);
  background: oklch(from var(--paper) l c h / .9);
  backdrop-filter: blur(10px);
  align-items: center;
  justify-content: space-between;
  font-family: var(--serif);
  font-size: 20px;
  color: var(--ink);
}
.mobile-menu-btn {
  background: var(--card);
  border: 1px solid var(--line);
  width: 36px; height: 36px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
  color: var(--ink-2);
}

/* Chat container (mensajes) */
.chat-container {
  position: relative; z-index: 1;
  flex: 1;
  overflow-y: auto;
  padding: 38px 36px 12px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  scroll-behavior: smooth;
}
.chat-container::-webkit-scrollbar { width: 10px; }
.chat-container::-webkit-scrollbar-thumb {
  background: oklch(from var(--ink) l c h / .12);
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: content-box;
}

.chat-message {
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 16px;
  align-items: flex-start;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  animation: fadeInNorma 0.3s ease-out forwards;
}
@keyframes fadeInNorma {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.chat-message.msg-user {
  grid-template-columns: 1fr 44px;
}
.chat-message.msg-user .msg-avatar { grid-column: 2; }
.chat-message.msg-user .msg-content { grid-column: 1; justify-self: end; }

.msg-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--serif);
  font-style: italic;
  font-size: 20px;
  letter-spacing: -.02em;
  flex-shrink: 0;
  position: relative;
}
.msg-bot .msg-avatar {
  background: var(--ink);
  color: var(--paper);
}
.msg-bot .msg-avatar::after {
  content: "";
  position: absolute;
  right: -2px; bottom: -2px;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: var(--pos);
  box-shadow: 0 0 0 2px var(--paper);
}
.msg-user .msg-avatar {
  background: var(--paper-2);
  border: 1px solid var(--line);
  color: var(--ink-2);
  font-family: var(--sans);
  font-style: normal;
  font-weight: 500;
  font-size: 12px;
}

.msg-content {
  padding: 18px 22px;
  border-radius: var(--r-xl);
  line-height: 1.6;
  font-size: 14.5px;
  max-width: 90%;
  color: var(--ink);
}
.msg-bot .msg-content {
  background: var(--card);
  border: 1px solid var(--line);
  border-bottom-left-radius: 8px;
  box-shadow: var(--shadow-card-norma);
}
.msg-user .msg-content {
  background: var(--ink);
  color: var(--paper);
  border-bottom-right-radius: 8px;
}
.msg-content strong { font-weight: 600; }
.msg-content code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
}
.msg-user .msg-content code {
  background: oklch(from var(--paper) l c h / .12);
  border-color: oklch(from var(--paper) l c h / .2);
  color: var(--paper);
}

/* Imagen adjunta dentro del mensaje */
.msg-image {
  max-width: 220px;
  border-radius: 12px;
  margin-bottom: 8px;
  display: block;
}

/* Indicador "escribiendo..." */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 4px;
}
.typing-indicator span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--ink-3);
  animation: typingNorma 1.4s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingNorma {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.cursor-blink {
  animation: blink 1s step-end infinite;
  color: var(--accent);
  font-weight: 700;
}
@keyframes blink { 50% { opacity: 0; } }

/* ════════════════════════════════════════════════════════════════
   INPUT AREA (Composer)
   ══════════════════════════════════════════════════════════════ */
.input-area-wrapper {
  position: relative; z-index: 1;
  padding: 14px 36px 28px;
  background: linear-gradient(to top, var(--paper) 60%, oklch(from var(--paper) l c h / 0));
}

.chat-attachment-preview {
  max-width: 760px;
  margin: 0 auto 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.chat-attachment-preview img {
  width: 52px; height: 52px;
  border-radius: 8px;
  object-fit: cover;
}
.chat-attachment-preview .att-info { flex: 1; min-width: 0; }
.chat-attachment-preview .att-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-attachment-preview .att-status {
  font-size: 11.5px;
  color: var(--ink-3);
}
.chat-attachment-preview.uploading .att-status { color: var(--accent); }
.chat-attachment-preview.error { border-color: oklch(from var(--danger-norma) l c h / .35); }
.chat-attachment-preview.error .att-status { color: var(--danger-norma); }
.att-remove {
  background: transparent;
  border: none;
  color: var(--ink-3);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}
.att-remove:hover { background: var(--paper-2); color: var(--ink); }

.chat-input-box {
  max-width: 760px;
  margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  padding: 12px 14px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  box-shadow: 0 1px 0 oklch(100% 0 0 / .9) inset,
              0 18px 50px -18px oklch(20% 0.018 255 / .25);
  transition: border-color 0.15s;
}
.chat-input-box:focus-within { border-color: oklch(from var(--accent) l c h / .55); }

.btn-attach {
  appearance: none;
  background: transparent;
  border: 1px solid transparent;
  width: 36px; height: 36px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--ink-3);
  cursor: pointer;
  flex: 0 0 auto;
}
.btn-attach:hover { background: var(--paper-2); color: var(--ink); }
.btn-attach svg { width: 16px; height: 16px; }

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--ink);
  padding: 8px 4px;
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  max-height: 150px;
  outline: none;
}
#chat-input::placeholder { color: var(--ink-4); }
#chat-input:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-send {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 16px;
  flex: 0 0 auto;
  transition: background 0.15s, transform 0.08s;
}
.btn-send:hover { background: var(--accent); transform: translateY(-1px); }
.btn-send.sending {
  background: var(--ink-4);
  cursor: not-allowed;
}

/* ════════════════════════════════════════════════════════════════
   SMART CARD PANEL (Formularios)
   ══════════════════════════════════════════════════════════════ */
.smart-card-panel {
  position: fixed;
  top: 0; right: 0;
  width: 540px;
  max-width: 92vw;
  height: 100vh;
  height: 100dvh;
  background: var(--card);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow-float);
  z-index: 50;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
/* Módulos DGII (606-609) usan tabla ancha — panel a 92vw */
.smart-card-panel:has(.f606-table) { width: 720px; max-width: 96vw; }
.smart-card-panel.open { transform: translateX(0); }
.smart-card-panel::-webkit-scrollbar { width: 8px; }
.smart-card-panel::-webkit-scrollbar-thumb {
  background: oklch(from var(--ink) l c h / .15);
  border-radius: 4px;
}

/* ════════════════════════════════════════════════════════════════
   RESPONSIVE — Mobile
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 880px) {
  /* En móvil: sidebar overlay (oculto por default), main toma todo el ancho */
  #app-console { flex-direction: row; }

  .app-sidebar {
    position: fixed;
    top: 0; left: -100%;
    width: 86%; max-width: 320px;
    height: 100vh;
    height: 100dvh;
    transition: left 0.3s ease;
    box-shadow: 0 0 40px oklch(20% 0.018 255 / .25);
    z-index: 30;
  }
  .app-sidebar.open { left: 0; }

  .mobile-header { display: flex; }

  /* Main ocupa 100% del viewport en móvil y se organiza vertical:
     mobile-header (auto) + chat-container (flex:1) + input-area (auto). */
  .app-main {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  /* El chat scrollea por dentro, no la página. */
  .chat-container {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px 16px 8px;
    -webkit-overflow-scrolling: touch;
  }

  /* Composer fijo abajo, siempre visible. Padding-bottom: safe-area-inset
     para no quedar debajo del notch en iPhone. */
  .input-area-wrapper {
    flex-shrink: 0;
    padding: 10px 16px calc(14px + env(safe-area-inset-bottom, 0px));
    background: var(--paper);
    border-top: 1px solid var(--line-soft);
  }

  /* El mensaje del bubble en móvil se ajusta mejor */
  .chat-message { max-width: 100%; gap: 10px; grid-template-columns: 36px 1fr; }
  .chat-message.msg-user { grid-template-columns: 1fr 36px; }
  .msg-avatar { width: 32px; height: 32px; font-size: 16px; }
  .msg-content { padding: 12px 14px; font-size: 14px; }

  /* Overlay del sidebar al abrirse en móvil — backdrop oscuro */
  .app-sidebar.open ~ .app-main::after {
    content: "";
    position: fixed;
    inset: 0;
    background: oklch(from var(--ink) l c h / .35);
    z-index: 25;
    backdrop-filter: blur(2px);
  }

  /* Smart Card panel a pantalla casi completa en móvil */
  .smart-card-panel { width: 96vw; }

  /* Tools bar más compacta en móvil + padding lateral menor */
  .tools-bar { padding: 8px 16px; }
  .tools-bar-inner { gap: 6px; }
  .tools-label { display: none; }  /* en móvil quitamos "Herramientas:" para ganar espacio */
  .tool-chip { padding: 6px 11px 6px 9px; font-size: 12px; }
  .tool-chip-icon { font-size: 13px; }
}

/* Móviles muy pequeños */
@media (max-width: 480px) {
  .input-area-wrapper { padding: 8px 12px calc(12px + env(safe-area-inset-bottom, 0px)); }
  .chat-container { padding: 12px 12px 6px; }
  .chat-input-box { padding: 10px 12px; }
  .mobile-header { padding: 10px 14px; font-size: 18px; }
}

/* Toast container & toasts */
.toast-container {
  position: fixed;
  top: 20px; right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-float);
  font-size: 13.5px;
  color: var(--ink);
  min-width: 220px;
  max-width: 380px;
  animation: toastIn 0.2s ease-out;
}
.toast .icon {
  width: 22px; height: 22px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 13px; font-weight: 600;
  flex: 0 0 auto;
}
.toast.success .icon { background: oklch(from var(--pos) l c h / .15); color: var(--pos); }
.toast.error .icon   { background: oklch(from var(--danger-norma) l c h / .15); color: var(--danger-norma); }
.toast.info .icon    { background: var(--accent-soft); color: var(--accent); }
.toast.fadeout { opacity: 0; transform: translateX(20px); transition: all 0.3s; }
@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}
