/* ========================
   MODERN VARIABLES & RESET
   ======================== */
:root {
    --primary: #800000;
    --primary-light: #a31515;
    --primary-glow: rgba(128, 0, 0, 0.35);
    --surface: rgba(255, 255, 255, 0.75);
    --text-main: #2d3436;
    --text-secondary: #636e72;
    --border-radius: 16px;
    --input-bg: rgba(255, 255, 255, 0.6);
    --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    --glass-border: 1px solid rgba(255, 255, 255, 0.4);
    --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15);
    --backdrop: blur(12px);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    background-image: url('assets/fundo.jpg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-color: #f4f4f4;
    color: var(--text-main);
    display: flex;
    justify-content: center;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

/* Efeito Blur Glassmorphism no fundo */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 5; /* Acima das partículas (0) e abaixo do gerador (10) */
    pointer-events: none;
    backdrop-filter: blur(0px);
    -webkit-backdrop-filter: blur(0px);
    background: rgba(0, 0, 0, 0);
    transition: backdrop-filter 0.8s ease, -webkit-backdrop-filter 0.8s ease, background 0.8s ease;
}

body.generator-active::before {
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    background: rgba(0, 0, 0, 0.35); /* Escurecimento e desfoque fortes para o gerador */
}

/* ========================
   GLASS CONTAINER
   ======================== */
.container {
    max-width: 850px;
    width: 100%;
    background: var(--surface);
    backdrop-filter: var(--backdrop);
    -webkit-backdrop-filter: var(--backdrop);
    border: var(--glass-border);
    box-shadow: var(--glass-shadow);
    border-radius: 24px;
    overflow: hidden;
    margin-bottom: 40px;
    animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(30px); }
    to { opacity: 1; transform: translateY(0); }
}

.container h2 {
    margin: 0;
    padding: 30px 40px;
    font-size: 24px;
    background: rgba(255, 255, 255, 0.5);
    color: var(--primary);
    border-bottom: 1px solid rgba(0,0,0,0.05);
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: -0.5px;
}

form {
    padding: 40px;
}

/* ========================
   MODERN INPUTS
   ======================== */
form label {
    display: block;
    margin-top: 25px;
    margin-bottom: 10px;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    transition: var(--transition);
}

form label:first-child { margin-top: 0; }

form input,
form textarea {
    width: 100%;
    padding: 16px 20px;
    box-sizing: border-box;
    border: 2px solid transparent;
    border-radius: 12px;
    background-color: #f0f2f5;
    font-size: 16px;
    font-family: inherit;
    color: var(--text-main);
    transition: var(--transition);
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.02);
}

form input:hover,
form textarea:hover {
    background-color: #eef0f3;
}

form input:focus,
form textarea:focus {
    background-color: #ffffff;
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 5px rgba(128, 0, 0, 0.1), 0 10px 20px rgba(0,0,0,0.05);
    transform: translateY(-2px);
}

form textarea#recipient {
    min-height: 80px;
    resize: vertical;
}

/* ========================
   TOOLBAR & EDITOR MODERNIZADO
   ======================== */
.editor-container {
    background: var(--surface);
    border-radius: 16px;
    box-shadow: var(--glass-shadow);
    border: var(--glass-border);
    overflow: hidden;
    margin-top: 20px;
    transition: var(--transition);
}

.editor-container:focus-within {
    box-shadow: 0 12px 40px rgba(128, 0, 0, 0.15);
    transform: translateY(-2px);
}

.toolbar {
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.9);
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: center;
}

.toolbar-group {
    display: flex;
    gap: 4px;
    padding-right: 12px;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
    align-items: center;
}

.toolbar-group:last-child {
    border-right: none;
    padding-right: 0;
}

.toolbar button {
    width: 36px;
    height: 36px;
    padding: 6px;
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.toolbar button svg {
    width: 18px;
    height: 18px;
}

.toolbar button:hover {
    background: rgba(128, 0, 0, 0.08);
    color: var(--primary);
    transform: translateY(-1px);
}

.toolbar button.active {
    background: var(--primary);
    color: white;
    box-shadow: 0 4px 10px var(--primary-glow);
}

/* Inputs de Cor na Toolbar */
.toolbar input[type="color"] {
    width: 32px;
    height: 32px;
    padding: 0;
    border: 2px solid #fff;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    transition: var(--transition);
}

.toolbar input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

.toolbar input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 50%;
}

.toolbar input[type="color"]:hover {
    transform: scale(1.1);
}

/* Botão Remover Formatação */
.remove-format-group {
    margin-left: auto; /* Empurra pro final da linha */
    border-right: none;
}

.btn-remove-format {
    width: auto !important;
    padding: 0 12px !important;
    background: rgba(255, 71, 87, 0.1) !important;
    color: #ff4757 !important;
    font-size: 13px;
    font-weight: 600;
    gap: 6px;
}

.btn-remove-format:hover {
    background: #ff4757 !important;
    color: white !important;
    box-shadow: 0 4px 10px rgba(255, 71, 87, 0.3) !important;
}

#body {
    border: none;
    min-height: 400px;
    padding: 30px 40px;
    font-size: 16px;
    line-height: 1.8;
    background-color: transparent;
    color: var(--text-main);
    outline: none;
}

/* ========================
   BUTTON "GENERATE"
   ======================== */
form button#generatePdf {
    margin-top: 40px;
    padding: 18px 30px;
    width: 100%;
    background: linear-gradient(135deg, var(--primary) 0%, #500000 100%);
    color: white;
    border: none;
    cursor: pointer;
    border-radius: 50px;
    font-size: 16px;
    font-weight: 700;
    letter-spacing: 1px;
    text-transform: uppercase;
    transition: var(--transition);
    box-shadow: 0 10px 20px var(--primary-glow);
    position: relative;
    overflow: hidden;
}

form button#generatePdf:hover {
    transform: translateY(-4px);
    box-shadow: 0 15px 30px var(--primary-glow);
    background: linear-gradient(135deg, #a00000 0%, #600000 100%);
}

form button#generatePdf:active {
    transform: translateY(-1px);
    box-shadow: 0 5px 10px var(--primary-glow);
}

form button#generatePdf::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: 0.5s;
}

form button#generatePdf:hover::after {
    left: 100%;
}

/* =================================================================
   PDF CONTENT - ÁREA INTOCÁVEL (Lógica do Documento)
   ================================================================= */
#pdf-content {
    display: none;
    position: absolute;
    left: -9999px;
    width: 210mm;
    height: 297mm;
    background: #fff;
    overflow: hidden;
    padding: 20mm;
    box-sizing: border-box;
    color: #000;
    font-family: Arial, sans-serif; 
}

.right-bar {
    position: absolute;
    top: 0;
    right: 0;
    width: 3mm;
    height: 100%;
    background-color: #800000;
}

.logo {
    display: block;
    margin: 0 auto;
    width: 25mm;
    height: auto;
}

.municipio {
    text-align: center;
    font-size: 14pt;
    font-weight: bold;
    margin-top: 2mm;
    margin-bottom: 0;
    color: black;
    text-transform: uppercase;
    font-family: Arial, sans-serif;
}

.title {
    text-align: center;
    font-size: 14pt;
    font-weight: bold;
    margin: 1mm 0;
    color: black;
    text-transform: uppercase;
}

.department {
    text-align: center;
    font-size: 14pt;
    font-weight: bold;
    margin: 1mm;
    margin-bottom: 8mm;
    color: black;
}

/* --- DESTINATÁRIO DINÂMICO --- */
.recipient {
    font-size: 12pt;
    font-weight: bold;
    color: #333;
    margin-bottom: 10mm;
    
    /* Configuração para o bloco dinâmico à direita */
    display: table;       /* Faz o bloco ter apenas a largura do conteúdo (shrink-to-fit) */
    margin-left: auto;    /* Empurra o bloco todo para a direita */
    margin-right: 0;      /* Garante que encosta na margem direita */
    
    text-align: left;     /* O texto DENTRO do bloco continua alinhado à esquerda */
    white-space: pre-wrap;
    
    /* Opcional: define um max-width para não ficar largo demais se o texto for gigante */
    max-width: 60%;       
}
/* ---------------------------------- */

.header-info {
    display: flex;
    justify-content: space-between;
    background-color: #fff8f7;
    padding: 4mm 4mm;
    margin-bottom: 6mm;
    font-size: 10pt;
    font-weight: normal;
    border-radius: 2mm;
    box-sizing: border-box;
}

/* --- BLOCO DE ASSUNTO --- */
.assunto-label {
    float: left;
    font-weight: bold;
    color: #000;
    font-size: 12pt;
    margin: 0;
    padding: 0;
    margin-right: 2mm; 
    line-height: 1.5;
    white-space: nowrap;
}

.subject-box {
    display: block;
    overflow: hidden; 
    margin: 0;
    padding: 0;
    margin-bottom: 5mm; 
    background-color: transparent; 
    border-radius: 0;
}

.subject-box .subject {
    font-weight: bold;
    color: #000;
    font-size: 12pt;
    margin: 0;
    padding: 0;
    display: block;
    text-align: justify;
    line-height: 1.5;
}
/* ----------------------------------------------------- */

.body-text {
    font-size: 12pt;
    line-height: 1.5;
    white-space: pre-wrap;
    margin-bottom: 25mm;
    text-align: justify;
}

.cafe {
    position: absolute;
    bottom: 40mm;
    left: -50mm;
    width: 185mm;
    height: auto;
    opacity: 0.05;
    pointer-events: none;
    user-select: none;
}

.footer {
    position: absolute;
    bottom: 10mm;
    left: 20mm;
    width: calc(100% - 40mm);
    display: flex;
    justify-content: flex-end;
    align-items: center;
}

.address {
    margin-right: 5mm; 
    font-size: 8pt;
    color: #800000;
    text-align: right;
    line-height: 1.3;
}

.qrcode {
    width: 18mm;
    height: 18mm;
}

/* ========================
   SPLASH SCREEN
   ======================== */
.splash-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    transition: opacity 0.8s ease-in-out;
}

.sp.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    transition: opacity 0.3s ease-in-out;
}

.splash-logo {
    width: 280px;
    height: auto;
    opacity: 0;
    animation: dramaticReveal 1.2s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes dramaticReveal {
    0% {
        transform: translateY(40px) scale(0.6) rotate(-5deg);
        opacity: 0;
        filter: drop-shadow(0 5px 10px rgba(0, 0, 0, 0.05));
    }
    100% {
        transform: translateY(0) scale(1) rotate(0deg);
        opacity: 1;
        filter: drop-shadow(0 15px 30px rgba(128, 0, 0, 0.2));
    }
}

.hide-splash {
    opacity: 0;
    pointer-events: none;
}

/* ========================
   LOGIN OVERLAY (GLASSMORPHISM)
   ======================== */
.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4); /* Escurecimento ligeiramente mais suave */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 5000; /* Fica acima de tudo, exceto splash screen */
    pointer-events: none; /* Permite interagir com as partículas atrás */
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.5s ease, visibility 0.5s;
}

.login-overlay.visible {
    opacity: 1;
    visibility: visible;
}

.login-overlay.fade-out {
    opacity: 0;
    visibility: hidden;
}

.login-box {
    position: relative;
    background: rgba(245, 245, 245, 0.95);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    text-align: center;
    max-width: 400px;
    width: 90%;
    transform: translateY(20px);
    transition: transform 0.5s ease;
    will-change: transform; /* Otimização GPU */
    pointer-events: auto; /* Reativa cliques na caixa de login */
    overflow: hidden;
}

.login-box::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 250px;
    height: 250px;
    background-image: url('assets/logo.png');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.08; /* Muito suave, marca d'água */
    z-index: 0;
    pointer-events: none;
}

.login-overlay.visible .login-box {
    transform: translateY(0);
}

.login-box h2, .login-box p, .login-box div, .login-box input, .login-box button {
    position: relative;
    z-index: 1; /* Garante que ficam por cima da marca de água */
}

.login-box h2 {
    color: #333;
    margin-bottom: 10px;
    font-size: 1.5rem;
    font-weight: 800;
}

.login-box p {
    color: #666;
    margin-bottom: 25px;
    font-size: 0.95rem;
    line-height: 1.5;
}

.login-box input {
    width: 100%;
    padding: 15px;
    border: 1px solid #ddd;
    border-radius: 10px;
    font-size: 1rem;
    margin-bottom: 20px;
    outline: none;
    transition: border-color 0.3s;
    font-family: inherit;
    text-align: center; /* Center text for code input looks better */
    background: #fff;
    box-sizing: border-box;
}

.login-box input:focus {
    border-color: #dc1414;
    box-shadow: 0 0 0 4px rgba(220, 20, 20, 0.15); /* Foco vermelho suave */
}

#codeInput {
    letter-spacing: 5px;
    font-size: 1.2rem;
    font-weight: bold;
}

.login-btn {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, rgba(185, 20, 20, 1), rgba(135, 0, 0, 1));
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 8px 15px rgba(185, 20, 20, 0.2);
}

.login-btn:hover {
    background: var(--primary-light);
    transform: translateY(-2px);
    box-shadow: 0 12px 20px var(--primary-glow);
}

.login-btn.loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.login-btn.loading::after {
    content: "";
    position: absolute;
    width: 20px;
    height: 20px;
    top: 50%;
    left: 50%;
    margin-top: -11px;
    margin-left: -11px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spinButton 0.8s linear infinite;
}

@keyframes spinButton {
    to { transform: rotate(360deg); }
}

.back-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    margin-top: 15px;
    font-size: 13px;
    cursor: pointer;
    text-decoration: underline;
}

.back-btn:hover {
    color: var(--primary);
}

.error-message {
    color: #e74c3c !important;
    font-size: 13px !important;
    margin-top: 15px !important;
    margin-bottom: 0 !important;
    font-weight: 600;
    opacity: 0;
    transition: opacity 0.3s;
}

/* MEDIA PRINT */
@media print {
    body {
        background: #fff !important;
        padding: 0;
    }
    .container {
        display: none !important;
    }
    #pdf-content {
        display: block !important;
        position: relative !important;
        left: 0;
        box-shadow: none;
    }
}

.save-load-container button {
    flex: 1;
    padding: 10px;
    border: none;
    border-radius: 8px;
    color: white;
    font-weight: bold;
    cursor: pointer;
    transition: opacity 0.2s;
}

.save-load-container button:hover {
    opacity: 0.8;
}
/* --- Ajuste de Título e Botões Laterais --- */
.header-inline {
    display: flex;
    justify-content: space-between; /* Joga o título pra um lado e botões pro outro */
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 15px;
    border-bottom: 1px solid rgba(128, 0, 0, 0.1); /* Linha fina elegante */
}

.header-inline h2 {
    margin: 0;
    font-size: 1.5rem;
    color: var(--primary);
    font-weight: 600;
}

.action-buttons {
    display: flex;
    gap: 10px;
}

/* Botão com toque de luxo (clean) */
.btn-luxury {
    background: transparent;
    border: 1px solid #d1d1d1;
    color: #666;
    padding: 6px 18px;
    border-radius: 50px; /* Estilo pílula, bem moderno */
    font-size: 13px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn-luxury:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: rgba(128, 0, 0, 0.05);
    transform: translateY(-1px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.05);
}

/* ========================
   PARTICLES BACKGROUND
   ======================== */
#particles-js {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: transparent !important; /* Deixa ver a imagem de fundo do body */
    z-index: 2; /* Fica por cima do fundo e por baixo do login (5000) */
    pointer-events: auto !important; /* Permite interação */
}

/* Garante que o container principal fica por cima das partículas */
#generatorContainer {
    z-index: 10;
    position: relative;
    pointer-events: auto;
}