:root{
    --bg:#12181D;
    --panel:#1B2329;
    --panel-alt:#222B32;
    --border:#33404A;
    --border-light:#3E4C57;
    --text:#E7EBEE;
    --text-dim:#8FA0AC;
    --accent:#F2B705;
    --accent-ink:#1B2329;
    --danger:#D65A31;
    --ok:#5AA579;
    --stripe: var(--brand-gradient);
    --radius: 2px;
    /* Paleta de gradiente para Login y Chat */
    --grad-violet:#8B5CF6;
    --grad-dark-purple:#4C1D95;
    --grad-cyan:#22D3EE;
    --brand-gradient: linear-gradient(135deg, var(--grad-dark-purple) 0%, var(--grad-violet) 55%, var(--grad-cyan) 100%);
  }
  *{box-sizing:border-box;}
  html,body{margin:0;padding:0; height:100vh; height:100dvh; overflow:hidden;}
  body{
    background:var(--bg);
    color:var(--text);
    font-family:'IBM Plex Sans', system-ui, -apple-system, sans-serif;
    font-size:15px;
    line-height:1.5;
    height:100vh;
    height:100dvh;
    overflow:hidden;
  }
  code, .mono{ font-family:'IBM Plex Mono', 'Courier New', monospace; }

  /* ---------- Layout ---------- */
  .app{ display:flex; height:100vh; height:100dvh; overflow:hidden; }

  .sidebar{
    width:250px;
    flex-shrink:0;
    background:var(--panel);
    border-right:1px solid var(--border);
    display:flex;
    flex-direction:column;
  }
  .brand{
    padding:20px 18px 16px;
    border-bottom:1px solid var(--border);
  }
  .brand .stripe-bar{
    height:4px; width:36px; background:var(--stripe); margin-bottom:12px; border-radius:1px;
  }
  .brand h1{
    font-size:15px; margin:0 0 4px; letter-spacing:0.2px; font-weight:600;
  }
  .brand p{
    margin:0; font-size:12px; color:var(--text-dim);
  }

  nav{ padding:14px 10px; display:flex; flex-direction:column; gap:2px; flex:1; }
  .nav-item{
    display:flex; align-items:center; gap:10px;
    padding:11px 12px;
    border-left:3px solid transparent;
    color:var(--text-dim);
    cursor:pointer;
    font-size:14px;
    border-radius:0 3px 3px 0;
    transition:background .12s ease, color .12s ease, border-color .12s ease;
    user-select:none;
  }
  .nav-item:hover{ background:var(--panel-alt); color:var(--text); }
  .nav-item.active{
    background:var(--panel-alt);
    color:var(--text);
    border-left-color:var(--grad-cyan);
  }
  .nav-item svg{ width:16px; height:16px; flex-shrink:0; opacity:.85; }

  .sidebar-footer{
    padding:14px; border-top:1px solid var(--border);
    font-size:12px; color:var(--text-dim);
  }
  .status-dot{
    display:inline-block; width:7px; height:7px; border-radius:50%;
    background:var(--danger); margin-right:6px; position:relative; top:-1px;
  }
  .status-dot.ok{ background:var(--ok); }

  .main{ flex:1; display:flex; flex-direction:column; min-width:0; min-height:0; }
  .topbar{
    height:52px; flex-shrink:0;
    border-bottom:1px solid var(--border);
    display:flex; align-items:center; justify-content:space-between;
    padding:0 22px;
    background:var(--panel);
  }
  .topbar h2{ font-size:14px; margin:0; font-weight:600; letter-spacing:.2px; }
  .topbar .scope-tag{
    font-size:11px; color:var(--grad-cyan); border:1px solid var(--grad-cyan); border-radius:2px;
    padding:2px 7px; font-family:'IBM Plex Mono',monospace; letter-spacing:.3px;
  }

  .view{ flex:1; overflow-y:auto; padding:22px; min-height:0; }
  .view.hidden{ display:none; }

  /* ---------- Chat ---------- */
  #chat-view{ display:flex; flex-direction:column; height:100%; padding:0; }
  .chat-layout{ display:flex; flex:1; min-height:0; }
  .chat-history-panel{
    width:230px; flex-shrink:0; border-right:1px solid var(--border); background:var(--panel);
    padding:16px 12px; overflow-y:auto;
  }
  .chat-hist-item{
    padding:9px 10px; border-radius:var(--radius); font-size:13px; cursor:pointer; color:var(--text-dim);
    display:flex; justify-content:space-between; align-items:center; gap:6px; margin-bottom:2px;
  }
  .chat-hist-item:hover{ background:var(--panel-alt); color:var(--text); }
  .chat-hist-item.active{ background:var(--panel-alt); color:var(--text); border-left:2px solid var(--grad-cyan); }
  .chat-hist-item .del-x{ opacity:.5; font-size:12px; padding:2px 5px; }
  .chat-hist-item .del-x:hover{ opacity:1; color:var(--danger); }
  .chat-main-col{ flex:1; display:flex; flex-direction:column; min-width:0; min-height:0; }
  .chat-scroll{ flex:1; min-height:0; overflow-y:auto; -webkit-overflow-scrolling:touch; padding:22px; display:flex; flex-direction:column; gap:14px; }
  .msg{ max-width:720px; padding:12px 14px; border-radius:var(--radius); font-size:14.5px; white-space:pre-wrap; }
  .msg.user{ align-self:flex-end; background:var(--panel-alt); border:1px solid var(--border); }
  .msg.assistant{ align-self:flex-start; background:transparent; border-left:2px solid var(--grad-violet); padding-left:14px; }
  .msg.system-note{ align-self:center; color:var(--text-dim); font-size:12.5px; border:none; font-style:italic; }
  .chat-input-bar{
    border-top:1px solid var(--border); padding:14px 22px; display:flex; gap:10px; background:var(--panel);
  }
  .chat-input-bar textarea{
    flex:1; resize:none; height:46px; background:var(--bg); color:var(--text);
    border:1px solid var(--border); border-radius:var(--radius); padding:11px 12px; font-family:inherit; font-size:14px;
  }
  .chat-input-bar textarea:focus{ outline:none; border-color:var(--grad-cyan); box-shadow:0 0 0 3px rgba(34,211,238,0.12); }

  /* ---------- Buttons / inputs ---------- */
  button{
    font-family:inherit; cursor:pointer; border-radius:var(--radius); border:1px solid var(--border-light);
    background:var(--panel-alt); color:var(--text); padding:10px 16px; font-size:13.5px; font-weight:500;
    transition:background .12s ease, border-color .12s ease;
  }
  button:hover{ border-color:var(--grad-cyan); }
  button.primary{ background:var(--brand-gradient); color:#fff; border-color:transparent; font-weight:600; }
  button.primary:hover{ filter:brightness(1.06); }
  button:disabled{ opacity:.45; cursor:not-allowed; }
  input[type=text], input[type=password], textarea, select{
    background:var(--bg); color:var(--text); border:1px solid var(--border); border-radius:var(--radius);
    padding:9px 11px; font-family:inherit; font-size:14px; width:100%;
  }
  input:focus, textarea:focus, select:focus{ outline:none; border-color:var(--grad-cyan); box-shadow:0 0 0 3px rgba(34,211,238,0.12); }
  label{ font-size:12.5px; color:var(--text-dim); display:block; margin-bottom:5px; text-transform:none; }
  .field{ margin-bottom:14px; }
  .row{ display:flex; gap:14px; }
  .row > .field{ flex:1; }

  .panel-box{
    background:var(--panel); border:1px solid var(--border); border-radius:var(--radius); padding:18px;
  }
  .section-title{
    font-size:13px; text-transform:uppercase; letter-spacing:.6px; color:var(--text-dim);
    border-bottom:1px solid var(--border); padding-bottom:8px; margin:0 0 16px;
  }

  /* ---------- Documentos ---------- */
  .doc-grid{ display:grid; grid-template-columns: 340px 1fr; gap:18px; align-items:start; }
  .doc-type-list{ display:flex; flex-direction:column; gap:6px; }
  .doc-type{
    padding:10px 12px; border:1px solid var(--border); border-radius:var(--radius); cursor:pointer; font-size:13.5px;
  }
  .doc-type:hover{ border-color:var(--border-light); }
  .doc-type.selected{ border-color:var(--grad-cyan); background:var(--panel-alt); box-shadow:inset 0 0 0 1px var(--grad-cyan); }
  .doc-output{
    background:var(--bg); border:1px solid var(--border); border-radius:var(--radius); padding:20px;
    min-height:300px; white-space:pre-wrap; font-size:14px;
  }
  .doc-actions{ display:flex; gap:10px; margin-top:12px; }
  .empty-hint{ color:var(--text-dim); font-size:13.5px; }

  /* ---------- Checklists ---------- */
  .cl-templates{ display:flex; flex-wrap:wrap; gap:8px; margin-bottom:18px; }
  .cl-chip{
    padding:8px 13px; border:1px solid var(--border); border-radius:20px; font-size:13px; cursor:pointer; color:var(--text-dim);
  }
  .cl-chip:hover{ border-color:var(--grad-cyan); color:var(--text); }
  .cl-item{
    display:flex; align-items:flex-start; gap:12px; padding:12px 0; border-bottom:1px solid var(--border);
  }
  .cl-item:last-child{ border-bottom:none; }
  .cl-item .cl-text{ flex:1; font-size:14px; }
  .cl-item .cl-code{ color:var(--text-dim); font-size:11.5px; margin-right:6px; }
  .cl-status{ display:flex; gap:6px; }
  .cl-status button{ padding:5px 10px; font-size:12px; }
  .cl-status button.sel-ok.active{ background:var(--ok); border-color:var(--ok); color:#0d1a12; }
  .cl-status button.sel-no.active{ background:var(--danger); border-color:var(--danger); color:#1c0a04; }
  .cl-status button.sel-na.active{ background:var(--border-light); }
  .cl-obs{ width:100%; margin-top:8px; font-size:13px; }
  .cl-header-bar{ display:flex; justify-content:space-between; align-items:center; margin-bottom:12px; }

  ::-webkit-scrollbar{ width:10px; }
  ::-webkit-scrollbar-thumb{ background:var(--border-light); border-radius:5px; }
  ::-webkit-scrollbar-track{ background:transparent; }

  /* ============================================================
     DISEÑO RESPONSIVO — Tablet y Celular
  ============================================================ */

  /* Tablet: apilar el panel de generación de documentos */
  @media (max-width: 1024px) {
    .doc-grid { grid-template-columns: 1fr; gap: 14px; }
    .sidebar { width: 210px; }
  }

  /* Celular: sidebar se convierte en barra de pestañas horizontal arriba */
  @media (max-width: 768px) {
    .app { flex-direction: column; }
    .sidebar {
      width: 100%; height:auto; flex-shrink:0;
      flex-direction: row; align-items: center;
      border-right: none; border-bottom: 1px solid var(--border);
      padding: 6px 8px;
    }
    .brand, .sidebar-footer { display: none; }
    nav {
      flex: 1; flex-direction: row; padding: 0; gap: 4px;
      overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
    }
    nav::-webkit-scrollbar{ display:none; }
    .nav-item {
      flex-shrink: 0; white-space: nowrap;
      padding: 9px 12px; font-size: 13px;
      border-left: none; border-bottom: 2px solid transparent;
    }
    .nav-item.active { border-left: none; border-bottom-color: var(--grad-cyan); }

    .topbar { padding: 0 14px; height: 46px; }
    .topbar h2 { font-size: 13px; }
    .topbar .scope-tag { display: none; }

    .view { padding: 14px; }
    .panel-box { padding: 14px; }
    .row { flex-direction: column; gap: 10px; }
    .doc-actions, .cl-header-bar, .form-card-actions { flex-wrap: wrap; }

    /* Chat: el input queda siempre visible porque .chat-scroll ahora
       tiene min-height:0 y absorbe el scroll internamente */
    .chat-layout { flex-direction: column; }
    .chat-history-panel {
      width: 100%; max-height: 110px;
      border-right: none; border-bottom: 1px solid var(--border);
      padding: 10px 12px;
    }
    .chat-scroll { padding: 14px; }
    .msg { max-width: 88%; }
    .chat-input-bar { padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
    .chat-input-bar textarea { height: 42px; font-size: 13px; }

    .form-cards { grid-template-columns: 1fr; }

    /* Login adaptado para móviles */
    #login-view .panel-box { margin: 16px; max-width: 100% !important; padding: 26px 20px; }
  }

  @media (max-width: 380px) {
    .nav-item { padding: 8px 9px; font-size: 12px; }
    .brand h1{ font-size:14px; }
  }

  /* ============================================================
     GRADIENTE MORADO / VIOLETA / CYAN — LOGIN Y CHAT
  ============================================================ */
  #login-view{
    background:
      radial-gradient(circle at 12% 18%, rgba(139,92,246,0.35), transparent 45%),
      radial-gradient(circle at 88% 82%, rgba(34,211,238,0.22), transparent 50%),
      linear-gradient(160deg, #0c0a16 0%, #1a1030 45%, #0c0a16 100%);
  }
  #login-view .panel-box{
    background: rgba(24,18,42,0.7);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(139,92,246,0.35);
    box-shadow: 0 25px 70px rgba(76,29,149,0.4), 0 0 0 1px rgba(34,211,238,0.06);
    border-radius: 12px;
    padding: 34px 30px;
  }
  #login-view .section-title{
    background: var(--brand-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    color: transparent;
    border: none;
    font-size: 19px;
    font-weight: 700;
    padding-bottom: 0;
    margin-bottom: 6px;
  }
  #login-view input[type=text], #login-view input[type=password]{
    background: rgba(12,10,22,0.6);
    border: 1px solid rgba(139,92,246,0.3);
    border-radius: 8px;
  }
  #login-view input:focus{ border-color: var(--grad-cyan); box-shadow: 0 0 0 3px rgba(34,211,238,0.15); }
  #btn-email-login{
    background: var(--brand-gradient);
    border: none;
    color: #fff;
    border-radius: 8px;
    padding: 12px 16px;
    font-weight: 600;
    letter-spacing:.2px;
    box-shadow: 0 8px 24px rgba(139,92,246,0.35);
  }
  #btn-email-login:hover{ filter: brightness(1.1); }
  #btn-google-login{
    border-radius: 8px;
    padding: 12px 16px;
  }

  /* Chat con toques de gradiente */
  .chat-history-panel{
    background: linear-gradient(180deg, #1b1430 0%, #140e24 100%);
    border-right: 1px solid rgba(139,92,246,0.22);
  }
  .chat-hist-item.active{
    background: rgba(139,92,246,0.14);
    border-left: 2px solid var(--grad-cyan);
  }
  #chat-new{
    background: var(--brand-gradient);
    border: none; color: #fff; border-radius: 8px;
  }
  .msg.user{
    background: linear-gradient(135deg, rgba(139,92,246,0.16), rgba(34,211,238,0.07));
    border: 1px solid rgba(139,92,246,0.32);
    border-radius: 10px;
  }
  .msg.assistant{
    border-left: 3px solid var(--grad-violet);
    border-image: linear-gradient(180deg, var(--grad-cyan), var(--grad-violet)) 1;
    border-radius: 0 8px 8px 0;
  }
  #chat-send{
    background: var(--brand-gradient);
    border: none; color: #fff; font-weight: 600; border-radius: 8px;
  }
  #chat-send:hover{ filter: brightness(1.1); }
  .chat-input-bar{ border-radius: 0; }

  /* Adjuntar archivos en el chat */
  .chat-input-col{ flex:1; display:flex; flex-direction:column; gap:8px; }
  .chat-input-row{ display:flex; gap:10px; align-items:flex-end; }
  #chat-attach-btn{
    background: rgba(139,92,246,0.15); border:1px solid rgba(139,92,246,0.4);
    border-radius:8px; padding:0; width:46px; height:46px; font-size:18px;
    display:flex; align-items:center; justify-content:center; flex-shrink:0;
  }
  #chat-attach-btn:hover{ border-color: var(--grad-cyan); }
  .chat-attachments{ display:flex; flex-wrap:wrap; gap:6px; }
  .chat-attachments:empty{ display:none; }
  .attach-chip{
    display:flex; align-items:center; gap:6px; background: rgba(139,92,246,0.15);
    border:1px solid rgba(139,92,246,0.35); border-radius:14px; padding:5px 10px; font-size:12px;
  }
  .attach-chip .attach-remove{ cursor:pointer; opacity:.7; }
  .attach-chip .attach-remove:hover{ opacity:1; color:var(--danger); }
  .chat-scroll.drag-over{
    outline: 2px dashed var(--grad-cyan); outline-offset:-8px;
    background: rgba(34,211,238,0.04);
  }
  .drop-hint{
    text-align:center; color:var(--text-dim); font-size:13px; padding:10px;
    border:1px dashed rgba(139,92,246,0.3); border-radius:8px; margin-bottom:6px;
  }

  /* ---------- Formularios oficiales ---------- */
  .form-cards{ display:grid; grid-template-columns: repeat(auto-fill, minmax(220px,1fr)); gap:14px; margin-top:14px; }
  .form-card{
    border:1px solid var(--border); border-radius:8px; padding:16px; background:var(--panel-alt);
    display:flex; flex-direction:column; gap:10px;
  }
  .form-card h4{ margin:0; font-size:14px; }
  .form-card p{ margin:0; font-size:12.5px; color:var(--text-dim); flex:1; }
  .form-card .form-card-actions{ display:flex; gap:8px; flex-wrap:wrap; }
  .form-section-title{
    font-size:12.5px; text-transform:uppercase; letter-spacing:.5px; color:var(--grad-cyan);
    margin:18px 0 10px; border-bottom:1px solid rgba(139,92,246,0.25); padding-bottom:6px;
  }
  .form-section-title:first-child{ margin-top:0; }
  .form-field-row{ display:flex; flex-direction:column; gap:4px; margin-bottom:12px; }
  .form-field-row label{ margin-bottom:0; }

/* ---------- Botones de descarga en respuestas del chat ---------- */
.msg-actions{
  display:flex; gap:6px; flex-wrap:wrap;
  margin-top:10px; padding-top:9px;
  border-top:1px solid rgba(139,92,246,0.22);
}
.msg-actions button{
  padding:4px 10px; font-size:11.5px; border-radius:20px;
  background:rgba(139,92,246,0.12); border:1px solid rgba(139,92,246,0.35);
  color:var(--text-dim);
}
.msg-actions button:hover{ border-color:var(--grad-cyan); color:var(--text); }
