/* === Reset & Base === */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --color-bg: #f8f9fa;
    --color-surface: #ffffff;
    --color-primary: #4a7898;
    --color-primary-hover: #3d6680;
    --color-text: #1f2937;
    --color-text-muted: #6b7280;
    --color-border: #e5e7eb;
    --color-error: #dc2626;
    --color-error-bg: #fef2f2;
    --color-success: #059669;
    --color-table-stripe: #f9fafb;
    --color-table-header: #f3f4f6;
    --radius: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    font-size: 15px;
}

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}

.report-container {
    max-width: 1200px;
}

/* === Header === */
header {
    text-align: center;
    margin-bottom: 2rem;
}

header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.subtitle {
    color: var(--color-text-muted);
    font-size: 1rem;
}

/* === Forms === */
.unified-form {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: 1.5rem;
}

.form-header {
    padding: 1.25rem 1.5rem;
    background: linear-gradient(to bottom, #f8fafc, #ffffff);
    border-bottom: 1px solid var(--color-border);
}

.form-header h2 {
    font-size: 1.15rem;
    font-weight: 600;
    margin-bottom: 0.35rem;
    color: var(--color-text);
}

.form-header p {
    font-size: 0.9rem;
    color: var(--color-text-muted);
    margin: 0;
}

.form-body {
    padding: 1.5rem;
}

.config-section h3,
.upload-section h3 {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
    margin-bottom: 0.75rem;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.upload-section {
    padding-top: 1.25rem;
    border-top: 1px solid var(--color-border);
}

.upload-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.upload-header h3 {
    margin-bottom: 0;
}

.file-group label {
    display: block;
    margin-bottom: 0.35rem;
}

.file-group .hint {
    display: inline;
    margin-right: 0.35rem;
}

.file-group .help-icon {
    vertical-align: middle;
}

.form-footer {
    padding: 1.25rem 1.5rem;
    background: #fafbfc;
    border-top: 1px solid var(--color-border);
    text-align: center;
}

.form-footer .btn-primary {
    display: inline-block;
    width: auto;
    min-width: 200px;
    padding: 0.75rem 2rem;
}

.privacy-notice {
    margin-top: 0.75rem;
    margin-bottom: 0;
    text-align: center;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-style: italic;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-group label {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--color-text);
}

.form-group input {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    transition: border-color 0.15s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(74, 120, 152, 0.1);
}

.hint {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

.btn-primary {
    display: block;
    width: 100%;
    padding: 0.75rem 1.5rem;
    background: var(--color-primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-primary:hover {
    background: var(--color-primary-hover);
}

.btn-primary:disabled {
    background: var(--color-text-muted);
    cursor: not-allowed;
}

.btn-secondary {
    display: inline-block;
    padding: 0.5rem 1rem;
    background: var(--color-surface);
    color: var(--color-primary);
    border: 1px solid var(--color-primary);
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}

.btn-secondary:hover {
    background: rgba(74, 120, 152, 0.05);
}

/* === Error === */
.error-banner {
    background: var(--color-error-bg);
    color: var(--color-error);
    border: 1px solid var(--color-error);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
}

/* === Loading === */
.loading {
    text-align: center;
    padding: 2rem;
}

.loading p {
    color: var(--color-text-muted);
    margin-top: 1rem;
}

.hidden {
    display: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    margin: 0 auto;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* === Report Navigation === */
.report-nav {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 0;
    margin-bottom: 1rem;
    border-bottom: 1px solid var(--color-border);
}

.report-source {
    margin-left: auto;
    color: var(--color-text-muted);
    font-size: 0.875rem;
}

/* === Report Content === */
.report-content {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: var(--shadow);
}

.report-content h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 2rem 0 1rem 0;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--color-primary);
}

.report-content h1:first-child {
    margin-top: 0;
}

.report-content h2 {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 1.75rem 0 0.75rem 0;
    color: var(--color-text);
}

.report-content h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 1.25rem 0 0.5rem 0;
}

.report-content p {
    margin-bottom: 0.75rem;
}

.report-content strong {
    font-weight: 600;
}

.report-content hr {
    border: none;
    border-top: 1px solid var(--color-border);
    margin: 2rem 0;
}

.report-content ul, .report-content ol {
    margin: 0.5rem 0 1rem 1.5rem;
}

.report-content li {
    margin-bottom: 0.25rem;
}

/* === Tables === */
.report-content table {
    border-collapse: collapse;
    margin: 1rem 0 1.5rem 0;
    font-size: 0.82rem;
    overflow-x: auto;
    display: block;
    width: max-content;
    min-width: 100%;
    -webkit-overflow-scrolling: touch;
}

.report-content thead {
    display: table-header-group;
}

.report-content tbody {
    display: table-row-group;
}

.report-content tr {
    display: table-row;
}

.report-content th, .report-content td {
    display: table-cell;
    padding: 0.5rem 0.75rem;
    text-align: left;
    border: 1px solid var(--color-border);
    white-space: nowrap;
    vertical-align: top;
}

.report-content td {
    white-space: normal;
    min-width: 60px;
    max-width: 320px;
    word-break: break-word;
}

.report-content td:not(:first-child) {
    white-space: nowrap;
    max-width: none;
    word-break: normal;
}

.report-content th {
    background: var(--color-table-header);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.report-content tbody tr:nth-child(even) {
    background: var(--color-table-stripe);
}

.report-content tbody tr:hover {
    background: rgba(74, 120, 152, 0.04);
}

/* === Report Footer === */
.report-footer {
    text-align: center;
    padding: 2rem 0;
    color: var(--color-text-muted);
}

.report-footer a {
    color: var(--color-primary);
    text-decoration: none;
    font-weight: 600;
}

.report-footer a:hover {
    text-decoration: underline;
}

/* === Feedback Section === */
.feedback-section {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1.5rem 2rem;
    margin-top: 2rem;
    box-shadow: var(--shadow);
}

.feedback-section h2 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    text-align: center;
}

.rating-options {
    display: flex;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.rating-option {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    cursor: pointer;
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    transition: border-color 0.15s, background 0.15s;
}

.rating-option:hover {
    border-color: var(--color-primary);
    background: rgba(74, 120, 152, 0.04);
}

.rating-option input[type="radio"]:checked + span {
    color: var(--color-primary);
    font-weight: 600;
}

.feedback-section .form-group {
    margin-bottom: 1rem;
}

.feedback-section textarea,
.feedback-section input[type="email"] {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    transition: border-color 0.15s;
}

.feedback-section textarea:focus,
.feedback-section input[type="email"]:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(74, 120, 152, 0.1);
}

.feedback-section .btn-primary {
    margin-top: 0.5rem;
}

/* === Intro Section === */
.intro-section {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-left: 3px solid var(--color-primary);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow);
}

.intro-section p {
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
    line-height: 1.6;
}

.intro-section p:last-child {
    margin-bottom: 0;
}

.intro-sign-off {
    font-weight: 600;
    font-style: italic;
}

/* === Help Icon & Modal === */
.help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--color-primary);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    cursor: pointer;
    margin-left: 0.35rem;
    vertical-align: middle;
    line-height: 1;
}

.help-icon:hover {
    background: var(--color-primary-hover);
}

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-overlay.hidden {
    display: none;
}

.modal-content {
    background: var(--color-surface);
    border-radius: var(--radius);
    padding: 1.5rem;
    max-width: 90vw;
    max-height: 90vh;
    position: relative;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.modal-content img {
    max-width: 100%;
    max-height: 75vh;
    display: block;
    border-radius: var(--radius);
}

.modal-title {
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 1rem;
}

.modal-close {
    position: absolute;
    top: 0.5rem;
    right: 0.75rem;
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--color-text-muted);
    cursor: pointer;
    line-height: 1;
    padding: 0.25rem;
}

.modal-close:hover {
    color: var(--color-text);
}

/* === Exit Intent Modal === */
.exit-modal-content {
    background: var(--color-surface);
    border-radius: 8px;
    padding: 2rem;
    max-width: 420px;
    width: 90%;
    position: relative;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.exit-intro {
    font-size: 1rem;
    font-weight: 600;
    color: var(--color-text);
    margin-bottom: 0.5rem;
}

.exit-subintro {
    font-size: 0.9rem;
    color: var(--color-text-muted);
    margin-bottom: 1.25rem;
}

.exit-options {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.exit-option {
    padding: 0.75rem 1rem;
    background: #f8fafc;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 0.9rem;
    text-align: left;
    cursor: pointer;
    transition: all 0.15s;
    color: var(--color-text);
}

.exit-option:hover {
    background: rgba(74, 120, 152, 0.08);
    border-color: var(--color-primary);
}

.exit-other {
    margin-top: 1rem;
}

.exit-other label {
    display: block;
    font-weight: 600;
    font-size: 0.9rem;
    margin-bottom: 0.5rem;
    color: var(--color-text);
}

.exit-other textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 0.9rem;
    font-family: inherit;
    resize: vertical;
    min-height: 80px;
    transition: border-color 0.15s;
}

.exit-other textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(74, 120, 152, 0.1);
}

.exit-submit {
    margin-top: 0.75rem;
    width: 100%;
}

.exit-thanks {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--color-text);
    padding: 2rem 1rem;
    margin: 0;
}

/* === Print === */
@media print {
    body {
        background: white;
        font-size: 11pt;
    }

    .container {
        max-width: none;
        padding: 0;
    }

    .report-nav, .report-footer, .feedback-section, .modal-overlay, .btn-secondary {
        display: none;
    }

    .report-content {
        border: none;
        box-shadow: none;
        padding: 0;
    }

    .report-content table {
        font-size: 8pt;
    }

    .report-content h1 {
        break-after: avoid;
    }

    .report-content table {
        break-inside: avoid;
    }
}

/* === Responsive === */
@media (max-width: 640px) {
    .container {
        padding: 1rem;
    }

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

    .form-header,
    .form-body,
    .form-footer {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .form-footer .btn-primary {
        width: 100%;
        min-width: auto;
    }

    .report-nav {
        flex-wrap: wrap;
    }

    .report-source {
        margin-left: 0;
        width: 100%;
    }
}

/* === Sample Report Preview === */
.preview-container {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
    overflow: hidden;
}

.preview-header {
    padding: 1rem 1.5rem;
    background: linear-gradient(to bottom, #f0f4f8, #ffffff);
    border-bottom: 1px solid var(--color-border);
}

.preview-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0;
    color: var(--color-text);
}

.report-preview {
    display: flex;
    gap: 1.5rem;
    padding: 1.25rem 1.5rem;
    height: 460px;
}

.preview-label {
    padding: 0.75rem 1.5rem;
    margin: 0;
    text-align: center;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-style: italic;
    background: #fafbfc;
    border-top: 1px solid var(--color-border);
}

.preview-toc {
    flex: 0 0 240px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
    overflow-y: auto;
    height: 100%;
}

.preview-toc .toc-group {
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--color-text);
    margin-top: 0.75rem;
    margin-bottom: 0.25rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.preview-toc .toc-group:first-child {
    margin-top: 0;
}

.preview-toc a {
    display: block;
    padding: 0.3rem 0.5rem;
    color: var(--color-primary);
    text-decoration: none;
    font-size: 0.85rem;
    border-radius: 3px;
    transition: background 0.15s;
}

.preview-toc a:hover {
    background: rgba(74, 120, 152, 0.06);
    text-decoration: underline;
}

.preview-toc a.active {
    font-weight: 600;
    background: rgba(74, 120, 152, 0.1);
}

.preview-content {
    flex: 1;
    min-width: 0;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    overflow-y: auto;
    overflow-x: auto;
    height: 100%;
}

.preview-content table {
    border-collapse: collapse;
    margin: 1rem 0 1.5rem 0;
    font-size: 0.82rem;
    overflow-x: auto;
    display: block;
    width: max-content;
    min-width: 100%;
    -webkit-overflow-scrolling: touch;
}

.preview-content thead {
    display: table-header-group;
}

.preview-content tbody {
    display: table-row-group;
}

.preview-content tr {
    display: table-row;
}

.preview-content th,
.preview-content td {
    display: table-cell;
    padding: 0.5rem 0.75rem;
    text-align: left;
    border: 1px solid var(--color-border);
    white-space: nowrap;
    vertical-align: top;
}

.preview-content td {
    white-space: normal;
    min-width: 60px;
    max-width: 320px;
    word-break: break-word;
}

.preview-content td:not(:first-child) {
    white-space: nowrap;
    max-width: none;
    word-break: normal;
}

.preview-content th {
    background: var(--color-table-header);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.preview-content tbody tr:nth-child(even) {
    background: var(--color-table-stripe);
}

.preview-content h3 {
    font-size: 1rem;
    font-weight: 600;
    margin: 1rem 0 0.5rem;
}

.preview-content h4 {
    font-size: 0.9rem;
    font-weight: 600;
    margin: 0.75rem 0 0.5rem;
}

.preview-content p {
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
}

.preview-content ul, .preview-content ol {
    margin: 0.5rem 0 1rem 1.5rem;
    font-size: 0.9rem;
}

.preview-content li {
    margin-bottom: 0.25rem;
}

.preview-content hr {
    display: none;
}

.preview-section {
    display: none;
}

.preview-section.active {
    display: block;
}

/* === Spend Overview Transposed Table Colors === */
/* Color the Spend row cells in Spend Overview tables */
#preview-spend-overview table tbody tr:first-child td:nth-child(3),
.report-content table tbody tr:first-child td:nth-child(3) {
    background-color: #84b84c;
}

#preview-spend-overview table tbody tr:first-child td:nth-child(4),
.report-content table tbody tr:first-child td:nth-child(4) {
    background-color: #C43C32;
    color: white;
}

#preview-spend-overview table tbody tr:first-child td:nth-child(5),
.report-content table tbody tr:first-child td:nth-child(5) {
    background-color: #d9a54a;
}

@media (max-width: 768px) {
    .report-preview {
        flex-direction: column;
        height: auto;
    }

    .preview-toc {
        flex: none;
        height: auto;
        max-height: 200px;
        overflow-y: auto;
    }

    .preview-content {
        height: auto;
        max-height: 400px;
        overflow-y: auto;
    }
}
