:root {
    --brand-blue: #182339;
    --brand-blue-light: #00A3dA;
    --brand-slate: #0f172a;
    --brand-ice: #f2f6fb;
    --text-body: #0f172a;
    --text-muted: #5f6c7b;
    --border: rgba(11, 52, 85, 0.15);
    --success: #2e8540;
    --danger: #b91c1c;
    font-family: 'Titillium Web', sans-serif;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--brand-ice);
    color: var(--text-body);
    font-family: 'Titillium Web', sans-serif;
}

a {
    color: inherit;
    text-decoration: none;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem 1.5rem;
    border-radius: 999px;
    border: none;
    cursor: pointer;
    font-weight: 600;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    font-size: 0.95rem;
}

.btn.primary {
    background: var(--brand-blue-light);
    color: #fff;
    box-shadow: 0 8px 20px rgba(11, 52, 85, 0.25);
}

.btn.primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 24px rgba(11, 52, 85, 0.3);
}

.btn.ghost {
    background: transparent;
    border: 1px solid var(--brand-blue);
    color: var(--brand-blue);
}

.btn.danger {
    background: var(--danger);
    color: #fff;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    box-shadow: none;
}

/* Auth & General Layout */
.auth-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at top, rgba(11, 52, 85, 0.2), transparent 70%), var(--brand-ice);
}

.auth-container {
    width: 100%;
    max-width: 420px;
    padding: 2rem;
}

.auth-card {
    background: var(--brand-blue);
    border-radius: 24px;
    padding: 2.5rem 2rem;
    box-shadow: 0 20px 40px rgba(15, 23, 42, 0.08);
}

.auth-header {
    text-align: center;
    margin-bottom: 2rem;
    max-width: 100%;
    overflow: hidden;
}

.auth-header img {
    max-width: 100%;
    height: auto;
    display: block;
    object-fit: contain;
}

.auth-logo {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.85rem 1.75rem;
    color: #fff;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin-bottom: 1rem;
}

.auth-form label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.4rem;
}

.auth-form input {
    width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    margin-bottom: 1rem;
    font-size: 1rem;
}

.auth-container h1,p {
    color: var(--brand-ice)
}

.form-feedback {
    margin-top: 0.75rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.form-feedback.error {
    color: var(--danger);
}

.form-feedback.success {
    color: var(--success);
}

.app-header {
    background: #182339;
    padding: 1rem 2.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 8px 16px rgba(15, 23, 42, 0.05);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: 1.5rem;
}

.brand-banner {
    width: auto;
    height: 100px;
    overflow: hidden;
    flex: 0 0 auto;
}

.brand-banner img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.brand-meta h1 {
    margin: 0;
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--brand-ice);
}

.brand-meta p {
    margin: 0.25rem 0 0;
    color: white;
}

.nav-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.nav-actions form {
    margin: 0;
}

.app-shell {
    padding: 2rem 2.5rem 3rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.app-shell.narrow {
    max-width: 860px;
    margin: 0 auto;
}

.panel {
    background: #fff;
    border-radius: 24px;
    padding: 2rem;
    box-shadow: 0 16px 32px rgba(15, 23, 42, 0.05);
}

.panel.two-column {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 2rem;
}

.panel h2 {
    margin-top: 0;
    font-size: 1.4rem;
}

.field {
    margin-bottom: 1.5rem;
}

.field label {
    font-weight: 600;
    display: block;
    margin-bottom: 0.5rem;
}

.input,
.field input,
.field select,
textarea {
    width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    font-size: 1rem;
    font-family: inherit;
}

.inline-inputs {
    display: flex;
    gap: 0.75rem;
}

.inline-inputs input {
    flex: 1;
}

.inline-inputs button {
    flex: 0 0 auto;
}

.upload-card {
    background: #f8fbff;
    border: 1px dashed rgba(11, 52, 85, 0.2);
    border-radius: 20px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.upload-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.drop-zone {
    border: 2px dashed rgba(11, 52, 85, 0.4);
    border-radius: 16px;
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
}
.drop-zone p {
    color:  var(--brand-slate);
}

.drop-zone.dragover {
    border-color: var(--brand-blue);
    background: rgba(11, 52, 85, 0.05);
}

.helper {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}

.link {
    color: var(--brand-blue);
    font-weight: 600;
    cursor: pointer;
}

.signature-upload {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.current-signature img {
    max-width: 240px;
    border-radius: 12px;
    border: 1px solid var(--border);
}

.table-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.table-container {
    max-height: 520px;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 16px;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

th, td {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid rgba(15, 23, 42, 0.05);
}

tr:hover {
    background: rgba(11, 52, 85, 0.04);
}

.editable-cell {
    cursor: text;
    transition: background 0.15s ease;
}

.editable-cell:hover {
    background: rgba(0, 163, 218, 0.08);
}

.editable-cell.editing {
    padding: 0;
    background: rgba(0, 163, 218, 0.12);
}

.editable-cell.saving {
    background: rgba(24, 35, 57, 0.08);
}

.table-inline-input {
    width: 100%;
    padding: 0.85rem 1rem;
    border: none;
    background: transparent;
    font: inherit;
    color: inherit;
    outline: none;
}

.table-inline-input::placeholder {
    color: var(--text-muted);
}

.table-inline-input:focus {
    background: rgba(255, 255, 255, 0.9);
    box-shadow: inset 0 0 0 2px rgba(0, 163, 218, 0.35);
}


#projectSelect[data-editing="true"] {
    display: none;
}

.select-inline-input {
    width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    font-size: 1rem;
    font-family: inherit;
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.select-inline-input:focus {
    outline: none;
    border-color: rgba(0, 163, 218, 0.6);
    box-shadow: 0 0 0 3px rgba(0, 163, 218, 0.15);
}

.field.project-rename-active {
    position: relative;
}

.field.project-rename-active .select-inline-input {
    box-shadow: 0 0 0 3px rgba(0, 163, 218, 0.12);
}

.field.project-rename-saving .select-inline-input {
    cursor: progress;
    opacity: 0.85;
}

.project-fields-row {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    align-items: flex-start;
}

.project-field {
    flex: 1 1 320px;
}

.project-select-field {
    min-width: 320px;
}

.job-number-field {
    flex: 0 1 220px;
    max-width: 260px;
}

.project-select-control {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.project-select-control .input {
    flex: 1;
}

.project-delete-note {
    margin-top: 0.5rem;
    font-size: 0.82rem;
}


.settings-shell {
    max-width: 1080px;
    margin: 0 auto;
}

.letter-settings {
    display: flex;
    flex-direction: column;
    gap: 1.75rem;
}

.letter-settings-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1.5rem;
}

.letter-settings-header .helper {
    margin-top: 0.75rem;
    max-width: 640px;
}

.letter-editor {
    display: flex;
    justify-content: center;
}

/* 
   LETTER PREVIEW 
   Calibrated for A4 PDF Scale approx 1.35x 
*/
.letter-page {
    width: 100%;
    max-width: 820px; /* ~595pt * 1.38 */
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 18px 36px rgba(15, 23, 42, 0.08);
    overflow: hidden;
    padding-bottom: 2rem;
}

.letter-header-image {
    display: block;
    width: 100%;
    height: auto;
}

.letter-content {
    /* 
       PDF MarginX=60pt ~ 80px. 
       Padding should match. 
       40px top padding + 80px sides.
    */
    padding: 40px 80px 60px 80px;
    /* PDF font size 10pt ~ 13.5px */
    font-size: 13.5px;
    line-height: 1.6;
    color: #000; /* PDF defaults to blackish */
}

/* Remove default preview elements for LOA */
.letter-date, .letter-address {
    display: none;
}

.doc-title {
    /* PDF Size 14pt ~ 19px */
    font-size: 19px;
    font-weight: 700;
    text-align: center;
    margin: 32px 0 42px 0;
    text-transform: uppercase;
    /* No underline in new PDF build */
    text-decoration: none;
    color: #000;
}

.doc-grid {
    display: grid;
    /* PDF Label gap ~135px (100pt) */
    grid-template-columns: 135px 1fr;
    gap: 12px;
    margin-bottom: 32px;
}

.doc-label {
    font-weight: 700;
}

.doc-value.bold {
    font-weight: 700;
}

.letter-body-editor {
    min-height: 220px;
    margin-bottom: 32px;
    white-space: pre-wrap;
    outline: none;
    cursor: text;
    /* Match PDF body font */
    font-size: 13.5px;
}

.letter-body-editor:focus {
    box-shadow: inset 0 0 0 2px rgba(0, 163, 218, 0.45);
}

.letter-body-editor:empty::before {
    content: attr(data-placeholder);
    color: var(--text-muted);
    opacity: 0.7;
}

.financial-summary {
    margin: 32px 0;
    width: 100%;
}

.fin-row {
    display: grid;
    /* PDF Summary Label Gap 300pt ~ 405px */
    grid-template-columns: 405px 1fr;
    margin-bottom: 12px;
}

.fin-row.bold {
    font-weight: 700;
    margin-top: 24px;
}

.fin-note {
    /* PDF 9pt ~ 12px */
    font-size: 12px;
    font-style: italic;
    margin-top: 12px;
}

.signatures-grid {
    display: grid;
    /* PDF Col 2 starts at ~317pt -> ~430px. Grid 1fr 1fr is close enough for 800px width */
    grid-template-columns: 1fr 1fr;
    gap: 60px; /* PDF gap roughly 40pt */
    margin-top: 50px;
}

.sig-header {
    font-size: 13.5px;
    margin: 0 0 16px;
}

.sig-role {
    font-weight: 700;
    margin: 0 0 32px;
}

.sig-line-group {
    display: flex;
    flex-direction: column;
    gap: 48px; /* PDF rowGap 35pt ~ 47px */
}

.sig-line {
    display: flex;
    align-items: flex-end; /* Align label with bottom of value box */
    gap: 8px;
    min-height: 24px;
}

.sig-line.img-line {
    align-items: center;
    /* PDF Image area reserved ~40pt height */
    height: 50px; 
}

.sig-line.auto-height {
    height: auto;
    align-items: flex-start;
}

/* PDF Label width 60pt ~ 80px */
.sig-label {
    width: 80px;
    font-weight: 600; /* PDF uses normal but bold looks better for label? No PDF uses normal? app.js says doc.text('Signature:', ...) which is normal weight. */
    font-weight: 400; 
    flex-shrink: 0;
}

/* PDF Line width 140pt ~ 190px. */
.sig-val-box {
    width: 190px;
    border-bottom: 1px solid #000; /* PDF draws line */
    min-height: 20px;
    font-size: 13.5px;
}

.sig-val-box.no-border {
    border-bottom: none;
    display: flex;
    align-items: center;
}

.sig-val-box.input-box {
    border-bottom: 1px solid #000;
    padding: 0;
    display: flex;
}

.signature-placeholder {
    color: #999;
    font-style: italic;
    font-size: 12px;
}

.inline-textarea {
    width: 100%;
    border: none;
    font-family: inherit;
    font-size: inherit;
    resize: vertical;
    padding: 0;
    background: transparent;
    outline: none;
    min-height: 48px;
    line-height: 1.4;
}

.inline-input {
    width: 100%;
    border: none;
    font-family: inherit;
    font-size: inherit;
    padding: 0;
    background: transparent;
    outline: none;
}

.inline-textarea:focus,
.inline-input:focus {
    background: rgba(0, 163, 218, 0.05);
}

.email-settings {
    margin-top: 2.5rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.email-settings h3 {
    margin: 0;
    font-size: 1.2rem;
}

.email-settings label {
    font-weight: 600;
}

.email-settings input,
.email-settings textarea {
    width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    font-family: inherit;
    font-size: 1rem;
    resize: vertical;
}

.email-settings textarea {
    min-height: 150px;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 768px) {
    .app-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .nav-actions {
        align-self: stretch;
        justify-content: space-between;
        width: 100%;
    }

    .inline-inputs {
        flex-direction: column;
    }

    .email-settings {
        margin-top: 2rem;
        padding-top: 1.5rem;
    }

    .project-fields-row {
        flex-direction: column;
        align-items: stretch;
    }

    .job-number-field {
        max-width: none;
        flex: 1 1 auto;
    }

    .letter-content {
        padding: 36px;
    }

    .letter-settings-header {
        flex-direction: column;
        align-items: stretch;
    }

    .letter-editor {
        justify-content: flex-start;
    }
    
    .signatures-grid {
        grid-template-columns: 1fr;
        gap: 32px;
    }
    
    .fin-row {
        grid-template-columns: 1fr;
        gap: 4px;
    }
    
    .doc-grid {
        grid-template-columns: 100px 1fr;
    }
}

#lettersTable tbody tr.focused {
    outline: 2px solid rgba(11, 52, 85, 0.35);
    outline-offset: -2px;
    background: rgba(11, 52, 85, 0.08);
}

#lettersTable {
    text-align: left;
}

.status-pill {
    padding: 4px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
    color: #fff;
    display: inline-block;
}

.status-pill.sent {
    background-color: #28a745;
}

.status-pill.not-sent {
    background-color: #dc3545;
}

.status .status-select {
    visibility: hidden;
    width: 0;
    transition: all 0.2s ease-in-out;
    margin-left: 8px;
}

.status:hover .status-select {
    visibility: visible;
    width: auto;
}
