:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--gray-50);
    color: var(--gray-900);
    min-height: 100vh;
}

/* ==================== Onboarding ==================== */

.onboarding {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.progress-bar {
    display: flex;
    align-items: center;
    margin-bottom: 3rem;
    gap: 0.5rem;
}

.progress-step {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--gray-200);
    color: var(--gray-500);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.875rem;
    transition: all 0.3s ease;
}

.progress-step.active {
    background: var(--primary);
    color: white;
}

.progress-step.completed {
    background: var(--success);
    color: white;
}

.progress-line {
    width: 40px;
    height: 2px;
    background: var(--gray-200);
}

.onboarding-screen {
    display: none;
    max-width: 500px;
    width: 100%;
}

.onboarding-screen.active {
    display: block;
}

.screen-content {
    background: white;
    border-radius: 1rem;
    padding: 2.5rem;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.screen-content h1 {
    font-size: 2rem;
    margin-bottom: 0.5rem;
    color: var(--gray-900);
}

.screen-content h2 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--gray-900);
}

.screen-content .subtitle {
    color: var(--gray-500);
    margin-bottom: 2rem;
}

/* ==================== Forms ==================== */

.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    font-weight: 500;
    margin-bottom: 0.5rem;
    color: var(--gray-700);
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group input[type="tel"],
.form-group input[type="url"],
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid var(--gray-300);
    border-radius: 0.5rem;
    font-size: 1rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-group .help-text {
    font-size: 0.875rem;
    color: var(--gray-500);
    margin-top: 0.5rem;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

/* ==================== Radio Options ==================== */

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.radio-option {
    display: flex;
    align-items: center;
    padding: 1rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s;
}

.radio-option:hover {
    border-color: var(--primary);
    background: var(--gray-50);
}

.radio-option input[type="radio"] {
    margin-right: 1rem;
}

.radio-label {
    font-weight: 500;
    margin-right: auto;
}

.radio-desc {
    color: var(--gray-500);
    font-size: 0.875rem;
}

/* ==================== Letterhead Options ==================== */

.letterhead-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.letterhead-option {
    cursor: pointer;
}

.letterhead-option input[type="radio"] {
    display: none;
}

.letterhead-preview {
    border: 2px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 1rem;
    transition: all 0.2s;
    height: 150px;
}

.letterhead-option input:checked + .letterhead-preview {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.letterhead-preview .lh-header {
    font-weight: 600;
    font-size: 0.875rem;
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--gray-200);
}

.letterhead-preview .lh-body {
    height: 60px;
    background: var(--gray-100);
    border-radius: 0.25rem;
    margin-bottom: 0.5rem;
}

.letterhead-preview .lh-footer {
    height: 20px;
    background: var(--gray-200);
    border-radius: 0.25rem;
}

.letterhead-preview.classic .lh-header {
    text-align: center;
    font-family: serif;
}

.letterhead-preview.modern .lh-header {
    border-bottom: 2px solid var(--primary);
}

.letterhead-preview.minimal .lh-header {
    border-bottom: none;
    color: var(--gray-400);
}

/* ==================== Security Options ==================== */

.security-options {
    display: grid;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.security-option {
    cursor: pointer;
}

.security-option input[type="radio"] {
    display: none;
}

.security-card {
    border: 2px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 1.25rem;
    text-align: center;
    transition: all 0.2s;
}

.security-option input:checked + .security-card {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.security-icon {
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.security-card h3 {
    font-size: 1rem;
    margin-bottom: 0.25rem;
}

.security-card p {
    font-size: 0.875rem;
    color: var(--gray-500);
    margin-bottom: 0.5rem;
}

.security-level {
    font-size: 0.75rem;
    color: var(--success);
    font-weight: 600;
}

.security-warning {
    font-size: 0.75rem;
    color: var(--warning);
    font-weight: 600;
}

/* ==================== Buttons ==================== */

.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
}

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

.btn-secondary {
    background: white;
    color: var(--gray-700);
    border: 1px solid var(--gray-300);
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary:hover {
    background: var(--gray-50);
}

.button-group {
    display: flex;
    justify-content: space-between;
    margin-top: 1.5rem;
}

/* ==================== Info & Summary Boxes ==================== */

.info-box {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 1rem;
    margin-bottom: 1.5rem;
}

.info-box p {
    color: var(--gray-600);
    margin-bottom: 0.5rem;
}

.info-box p:last-child {
    margin-bottom: 0;
}

.summary-card {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 1.5rem;
    margin: 1.5rem 0;
}

.summary-card h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--gray-700);
}

.summary-item {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--gray-200);
}

.summary-item:last-child {
    border-bottom: none;
}

.summary-label {
    color: var(--gray-500);
}

.summary-value {
    font-weight: 500;
}

/* ==================== Success Animation ==================== */

.success-animation {
    text-align: center;
    margin: 2rem 0;
}

.checkmark {
    width: 80px;
    height: 80px;
    background: var(--success);
    color: white;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
    animation: pop 0.3s ease;
}

@keyframes pop {
    0% { transform: scale(0); }
    80% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* ==================== Dashboard ==================== */

.dashboard-container {
    padding: 2rem;
}

.hidden {
    display: none !important;
}

.dashboard-container.hidden {
    display: none;
}

header {
    margin-bottom: 2rem;
}

header h1 {
    font-size: 1.75rem;
    color: var(--gray-900);
}

header p {
    color: var(--gray-500);
}

nav {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
    border-bottom: 1px solid var(--gray-200);
    padding-bottom: 0.5rem;
}

.nav-btn {
    background: none;
    border: none;
    padding: 0.5rem 1rem;
    font-size: 1rem;
    color: var(--gray-500);
    cursor: pointer;
    border-radius: 0.375rem;
    transition: all 0.2s;
}

.nav-btn:hover {
    color: var(--gray-700);
    background: var(--gray-100);
}

.nav-btn.active {
    color: var(--primary);
    font-weight: 500;
}

/* ==================== KIMS Workspace ==================== */

.kims-tabs {
    display: flex;
    gap: 0;
    border-bottom: 1px solid var(--gray-200);
    margin-bottom: 1.5rem;
}

.kims-tab {
    padding: 0.75rem 1.25rem;
    background: none;
    border: none;
    color: var(--gray-500);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}

.kims-tab:hover {
    color: var(--gray-700);
}

.kims-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.kims-content {
    position: relative;
}

.kims-panel {
    display: none;
}

.kims-panel.active {
    display: block;
}

main {
    max-width: 1200px;
}

.view {
    display: none;
}

.view.active {
    display: block;
}

.view h2 {
    font-size: 1.25rem;
    margin-bottom: 1.5rem;
    color: var(--gray-900);
}

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
}

.stat-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1.5rem;
}

.stat-card h3 {
    font-size: 0.875rem;
    color: var(--gray-500);
    font-weight: 500;
    margin-bottom: 0.5rem;
}

.stat-card p {
    font-size: 2rem;
    font-weight: 600;
    color: var(--gray-900);
}

/* ==================== Forms Module ==================== */

.template-list-view,
.create-template-view,
.form-builder,
.form-spreadsheet {
    max-width: 900px;
    margin: 0 auto;
}

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

.view-header h2 {
    margin-bottom: 0;
}

.template-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1rem 1.25rem;
    margin-bottom: 0.75rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.template-card h3 {
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--gray-900);
}

.template-actions {
    display: flex;
    gap: 0.5rem;
}

.form-builder .builder-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.builder-section {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.builder-section h3 {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.75rem;
}

.input-large {
    width: 100%;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    font-size: 0.875rem;
    margin-bottom: 0.75rem;
}

.field-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.field-card {
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    background: var(--gray-50);
}

.field-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.field-name-input {
    flex: 1;
    padding: 0.375rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.375rem;
    font-size: 0.8125rem;
    background: white;
}

.field-type-badge {
    font-size: 0.6875rem;
    font-weight: 500;
    color: var(--gray-500);
    background: var(--gray-100);
    padding: 0.125rem 0.5rem;
    border-radius: 0.25rem;
    text-transform: uppercase;
}

.field-required {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.75rem;
    color: var(--gray-500);
    cursor: pointer;
}

.field-options-input {
    width: 100%;
    padding: 0.375rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.375rem;
    font-size: 0.8125rem;
    margin-top: 0.5rem;
}

.formula-input-group {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.formula-input {
    flex: 1;
    padding: 0.375rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.375rem;
    font-size: 0.8125rem;
    font-family: 'SF Mono', Monaco, monospace;
}

.field-meta-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.375rem;
}

.field-label-sm {
    font-size: 0.75rem;
    color: var(--gray-500);
    white-space: nowrap;
}

.field-sm {
    max-width: 200px;
}

.ref-table-card {
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 0.75rem;
    margin-bottom: 0.75rem;
    background: var(--gray-50);
}

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

.ref-table-editor {
    overflow-x: auto;
}

.spreadsheet-mini {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8125rem;
    margin-bottom: 0.5rem;
}

.spreadsheet-mini th,
.spreadsheet-mini td {
    border: 1px solid var(--gray-200);
    padding: 0;
}

.spreadsheet-mini th {
    background: var(--gray-100);
    font-weight: 500;
    padding: 0.375rem 0.5rem;
    text-align: left;
}

.spreadsheet-mini input {
    width: 100%;
    border: none;
    padding: 0.375rem 0.5rem;
    font-size: 0.8125rem;
}

.spreadsheet-mini .header-input {
    background: var(--gray-100);
    font-weight: 600;
    font-size: 0.8125rem;
    padding: 0.375rem 0.5rem;
    min-width: 100px;
}

.spreadsheet-mini .header-input:focus {
    background: white;
    outline: 2px solid var(--primary);
    outline-offset: -2px;
}

/* Reference Table Header Row */
.section-header-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
}

.section-header-row h3 {
    margin: 0;
}

.section-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
}

.ref-table-meta {
    font-size: 0.75rem;
    color: var(--gray-400);
    margin-left: 0.5rem;
}

.ref-table-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

/* Preview Grid */
.ref-table-preview {
    margin-bottom: 0.5rem;
}

.spreadsheet-preview {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8125rem;
}

.spreadsheet-preview th,
.spreadsheet-preview td {
    border: 1px solid var(--gray-200);
    padding: 0.375rem 0.625rem;
    text-align: left;
}

.spreadsheet-preview th {
    background: var(--gray-100);
    font-weight: 600;
    color: var(--gray-700);
    position: sticky;
    top: 0;
}

.spreadsheet-preview .key-col {
    background: #eff6ff;
    color: #1e40af;
    font-weight: 600;
}

.spreadsheet-preview th.key-col {
    background: #dbeafe;
}

.spreadsheet-preview tbody tr:hover {
    background: var(--gray-50);
}

.preview-more {
    text-align: center;
    color: var(--gray-400);
    font-style: italic;
    padding: 0.5rem;
}

/* Import Modal */
.import-modal {
    max-width: 600px;
    width: 90vw;
}

.import-desc {
    color: var(--gray-600);
    font-size: 0.875rem;
    margin-bottom: 1rem;
}

.import-dropzone {
    border: 2px dashed var(--gray-300);
    border-radius: 0.75rem;
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
    margin-bottom: 1rem;
}

.import-dropzone:hover,
.import-dropzone.dragover {
    border-color: var(--primary);
    background: #eff6ff;
}

.dropzone-icon {
    font-size: 2rem;
    display: block;
    margin-bottom: 0.5rem;
}

.dropzone-hint {
    font-size: 0.75rem;
    color: var(--gray-400);
    margin-top: 0.25rem;
}

.import-preview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
}

.import-checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    color: var(--gray-700);
}

#import-file-info {
    color: var(--gray-500);
    font-size: 0.8125rem;
}

#import-preview-table {
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

#import-preview-table .spreadsheet-preview {
    margin: 0;
}

#import-preview-table .spreadsheet-preview th {
    position: sticky;
    top: 0;
    z-index: 1;
}

.formula-card {
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    background: var(--gray-50);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.formula-card select {
    padding: 0.375rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.375rem;
    font-size: 0.8125rem;
}

.help-text {
    font-size: 0.75rem;
    color: var(--gray-500);
    margin-bottom: 0.75rem;
}

.help-text code {
    background: var(--gray-100);
    padding: 0.125rem 0.25rem;
    border-radius: 0.25rem;
    font-family: 'SF Mono', Monaco, monospace;
}

.builder-actions {
    margin-top: 1.5rem;
}

/* ==================== Live Spreadsheet Preview ==================== */

.live-preview-section {
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
}

.live-preview-section h3 {
    color: #166534;
}

.live-preview-container {
    margin-top: 0.5rem;
}

.live-preview-scroll {
    overflow-x: auto;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    background: white;
}

.live-preview-scroll .spreadsheet-preview {
    margin: 0;
}

.live-preview-scroll .spreadsheet-preview th {
    background: #f0fdf4;
    color: #166534;
    font-size: 0.8125rem;
    white-space: nowrap;
}

.live-preview-scroll .spreadsheet-preview .calc-col {
    background: #fefce8;
    color: #854d0e;
}

.live-preview-scroll .spreadsheet-preview .calc-col th {
    background: #fefce8;
    color: #854d0e;
}

.col-unit {
    font-weight: 400;
    color: var(--gray-400);
    font-size: 0.75rem;
    margin-left: 0.25rem;
}

.col-required {
    color: var(--danger);
    font-weight: 700;
    margin-left: 0.125rem;
}

.sample-value {
    color: var(--gray-400);
    font-style: italic;
}

.sample-value.dim {
    opacity: 0.5;
}

.preview-ellipsis td {
    border-top: 1px dashed var(--gray-300);
}

.live-preview-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
    font-size: 0.75rem;
    color: var(--gray-500);
}

.preview-sep {
    color: var(--gray-300);
}

/* ==================== Spreadsheet ==================== */

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

.spreadsheet-toolbar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.spreadsheet-container {
    overflow-x: auto;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    background: white;
}

.spreadsheet {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8125rem;
}

.spreadsheet th,
.spreadsheet td {
    border: 1px solid var(--gray-200);
    padding: 0;
}

.spreadsheet th {
    background: var(--gray-100);
    font-weight: 500;
    padding: 0.5rem 0.75rem;
    text-align: left;
    position: sticky;
    top: 0;
}

.spreadsheet .row-number {
    width: 40px;
    text-align: center;
    color: var(--gray-400);
    background: var(--gray-50);
}

.spreadsheet .actions-col {
    width: 60px;
    text-align: center;
}

.spreadsheet input {
    width: 100%;
    border: none;
    padding: 0.5rem 0.75rem;
    font-size: 0.8125rem;
}

.spreadsheet input:focus {
    outline: 2px solid var(--primary);
    outline-offset: -2px;
}

.spreadsheet-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 1rem;
}

/* ==================== Buttons ==================== */

.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
}

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

.btn-secondary {
    background: white;
    color: var(--gray-700);
    border: 1px solid var(--gray-300);
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
}

.btn-secondary:hover {
    background: var(--gray-50);
}

.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
    border-radius: 0.375rem;
    border: 1px solid var(--gray-300);
    background: white;
    cursor: pointer;
}

.btn-small:hover {
    background: var(--gray-50);
}

.btn-small.btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.btn-icon {
    width: 24px;
    height: 24px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.25rem;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 1rem;
}

.btn-icon.btn-danger {
    color: var(--danger);
}

.btn-icon.btn-danger:hover {
    background: #fef2f2;
}

.empty-state {
    text-align: center;
    color: var(--gray-400);
    padding: 2rem;
    font-size: 0.875rem;
}

/* ==================== Modal ==================== */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: white;
    border-radius: 0.75rem;
    padding: 1.5rem;
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content h3 {
    margin-bottom: 1rem;
}

.field-picker-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.field-picker-item {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.375rem;
    background: white;
    text-align: left;
    cursor: pointer;
    font-size: 0.875rem;
}

.field-picker-item:hover {
    background: var(--gray-50);
    border-color: var(--primary);
}

/* ==================== Projects Module ==================== */

.project-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

.project-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1.25rem;
}

.project-header {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 0.75rem;
}

.project-status {
    font-size: 0.6875rem;
    font-weight: 500;
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
}

.status-active { background: #d1fae5; color: #065f46; }
.status-on-hold { background: #fef3c7; color: #92400e; }
.status-completed { background: #dbeafe; color: #1e40af; }
.status-archived { background: var(--gray-100); color: var(--gray-600); }

.project-card h3 {
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--gray-900);
    margin-bottom: 0.5rem;
}

.project-date {
    font-size: 0.75rem;
    color: var(--gray-500);
    margin-bottom: 0.75rem;
}

.project-actions {
    display: flex;
    gap: 0.5rem;
}

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

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

.empty-text {
    color: var(--gray-400);
    font-size: 0.875rem;
    font-style: italic;
}

.sample-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 0.75rem;
}

.portal-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.portal-card {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 0.75rem;
}

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

.portal-permissions {
    display: flex;
    gap: 0.375rem;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
}

.perm-badge {
    font-size: 0.6875rem;
    padding: 0.125rem 0.375rem;
    background: var(--primary-light, #e0e7ff);
    color: var(--primary, #4f46e5);
    border-radius: 0.25rem;
    font-weight: 500;
}

.portal-actions {
    display: flex;
    gap: 0.375rem;
}

.sample-card {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    padding: 0.75rem;
}

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

.sample-status {
    font-size: 0.625rem;
    font-weight: 500;
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
}

.sample-barcode {
    font-size: 0.625rem;
    color: var(--gray-500);
    font-family: monospace;
}

.sample-card h4 {
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--gray-900);
}

.sample-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.sample-actions .btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.75rem;
}

.status-options {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.status-option {
    padding: 0.75rem 1rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    background: white;
    cursor: pointer;
    text-align: left;
    font-size: 0.875rem;
    transition: all 0.2s;
}

.status-option:hover {
    border-color: var(--primary);
    background: var(--gray-50);
}

.status-option.active {
    border-color: var(--primary);
    background: #eff6ff;
    font-weight: 500;
}

.status-option.past {
    opacity: 0.5;
}

/* ==================== Documents Module ==================== */

.project-selector {
    margin-bottom: 1.5rem;
}

.project-selector label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 0.5rem;
}

.project-selector select {
    width: 100%;
    max-width: 400px;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    font-size: 0.875rem;
    background: white;
}

.document-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1rem;
}

.document-card {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1.25rem;
}

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

.doc-type {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--gray-500);
    text-transform: uppercase;
    background: var(--gray-100);
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
}

.doc-status {
    font-size: 0.6875rem;
    font-weight: 500;
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
}

.status-draft { background: var(--gray-100); color: var(--gray-600); }
.status-in-review { background: #fef3c7; color: #92400e; }
.status-approved { background: #d1fae5; color: #065f46; }
.status-final { background: #dbeafe; color: #1e40af; }

.document-card h3 {
    font-size: 0.9375rem;
    font-weight: 500;
    color: var(--gray-900);
    margin-bottom: 0.5rem;
}

.doc-date {
    font-size: 0.75rem;
    color: var(--gray-500);
    margin-bottom: 0.75rem;
}

.doc-actions {
    display: flex;
    gap: 0.5rem;
}

.document-detail {
    max-width: 900px;
    margin: 0 auto;
}

.detail-section {
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.75rem;
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.detail-section h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--gray-900);
    margin-bottom: 0.75rem;
}

.detail-section p {
    font-size: 0.875rem;
    color: var(--gray-600);
    margin-bottom: 0.5rem;
}

.detail-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

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

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--gray-700);
    margin-bottom: 0.375rem;
}

/* ==================== Security Module ==================== */

.detail-tabs {
    display: flex;
    gap: 0;
    border-bottom: 1px solid var(--gray-200);
    margin-bottom: 1.5rem;
}

.detail-tab {
    padding: 0.75rem 1.25rem;
    background: none;
    border: none;
    color: var(--gray-500);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
}

.detail-tab:hover {
    color: var(--gray-700);
}

.detail-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-panel {
    display: none;
}

.tab-panel.active {
    display: block;
}

.security-status-card {
    padding: 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}

.security-status-card.valid {
    background: #d1fae5;
    border: 1px solid #10b981;
}

.security-status-card.invalid {
    background: #fee2e2;
    border: 1px solid #ef4444;
}

.security-status-card h4 {
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
}

.security-status-card p {
    font-size: 0.8125rem;
    margin-bottom: 0.25rem;
}

.security-issues {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.security-issues h5 {
    font-size: 0.8125rem;
    margin-bottom: 0.5rem;
    color: #dc2626;
}

.security-issues ul {
    margin: 0;
    padding-left: 1.25rem;
    font-size: 0.75rem;
}

.version-list, .signature-list, .audit-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.version-item, .signature-item, .audit-item {
    display: flex;
    gap: 1rem;
    align-items: center;
    padding: 0.75rem;
    background: var(--gray-50);
    border-radius: 0.375rem;
    font-size: 0.8125rem;
}

.version-number {
    font-weight: 600;
    color: var(--primary);
    min-width: 2rem;
}

.version-status, .signature-purpose {
    font-size: 0.6875rem;
    padding: 0.125rem 0.5rem;
    border-radius: 0.25rem;
    background: var(--gray-200);
    color: var(--gray-700);
}

.version-hash, .audit-hash {
    font-family: monospace;
    font-size: 0.75rem;
    color: var(--gray-500);
}

.version-date, .signature-date, .audit-date {
    margin-left: auto;
    color: var(--gray-500);
    font-size: 0.75rem;
}

.signature-user {
    font-weight: 500;
    color: var(--gray-900);
}

.audit-action {
    font-weight: 500;
    color: var(--gray-900);
    min-width: 5rem;
}

.audit-user {
    color: var(--gray-600);
}

/* ==================== Chat Workspace ==================== */

.chat-workspace {
    display: flex;
    height: calc(100vh - 200px);
    min-height: 500px;
    background: white;
    border-radius: 0.75rem;
    border: 1px solid var(--gray-200);
    overflow: hidden;
    position: relative;
}

.chat-sidebar {
    width: 280px;
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    background: var(--gray-50);
}

.chat-sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--gray-200);
}

.chat-sidebar-header h3 {
    margin: 0;
    font-size: 0.875rem;
    font-weight: 600;
}

.chat-conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.conversation-item {
    padding: 0.75rem;
    border-radius: 0.5rem;
    cursor: pointer;
    margin-bottom: 0.25rem;
    transition: background 0.2s;
}

.conversation-item:hover {
    background: var(--gray-200);
}

.conversation-item.active {
    background: white;
    border: 1px solid var(--gray-200);
}

.conv-title {
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--gray-900);
    margin-bottom: 0.25rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-date {
    font-size: 0.6875rem;
    color: var(--gray-500);
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
}

.chat-welcome {
    text-align: center;
    padding: 3rem 2rem;
    color: var(--gray-500);
}

.chat-welcome h2 {
    color: var(--gray-900);
    margin-bottom: 0.5rem;
}

.chat-suggestions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 1.5rem;
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
}

.chat-suggestions button {
    padding: 0.75rem 1rem;
    background: white;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    cursor: pointer;
    text-align: left;
    font-size: 0.8125rem;
    color: var(--gray-700);
    transition: all 0.2s;
}

.chat-suggestions button:hover {
    border-color: var(--primary);
    background: var(--gray-50);
}

.chat-message {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.chat-message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    background: var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6875rem;
    font-weight: 600;
    color: var(--gray-600);
    flex-shrink: 0;
}

.chat-message.user .message-avatar {
    background: var(--primary);
    color: white;
}

.message-content {
    max-width: 70%;
}

.message-text {
    padding: 0.75rem 1rem;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    line-height: 1.5;
}

.chat-message.user .message-text {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 0.25rem;
}

.chat-message.assistant .message-text {
    background: var(--gray-100);
    color: var(--gray-900);
    border-bottom-left-radius: 0.25rem;
}

.message-text code {
    background: rgba(0, 0, 0, 0.1);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-family: monospace;
    font-size: 0.8125rem;
}

.message-text pre {
    background: var(--gray-900);
    color: var(--gray-100);
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0.5rem 0;
}

.message-text pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.message-metadata {
    display: flex;
    gap: 1rem;
    margin-top: 0.5rem;
    font-size: 0.6875rem;
    color: var(--gray-500);
    opacity: 0.8;
}

.message-time {
    font-size: 0.6875rem;
    color: var(--gray-400);
    margin-top: 0.375rem;
    clear: both;
}

.message-time:empty {
    display: none;
}

.thinking-indicator {
    display: flex;
    gap: 0.25rem;
    padding: 0.75rem 1rem;
    background: var(--gray-100);
    border-radius: 0.75rem;
    width: fit-content;
}

.thinking-indicator span {
    width: 0.5rem;
    height: 0.5rem;
    background: var(--gray-400);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.thinking-indicator span:nth-child(1) { animation-delay: -0.32s; }
.thinking-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

.error-message {
    padding: 0.75rem 1rem;
    background: #fee2e2;
    color: #dc2626;
    border-radius: 0.75rem;
    font-size: 0.875rem;
}

.chat-input-area {
    padding: 1rem;
    border-top: 1px solid var(--gray-200);
    background: white;
}

.chat-input-controls {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.chat-controls-top {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.chat-input-controls select {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    font-size: 0.75rem;
    background: white;
    min-width: 120px;
}

.corpus-toggle {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    cursor: pointer;
    user-select: none;
    font-size: 0.75rem;
    color: var(--gray-600);
}

.corpus-toggle input[type="checkbox"] {
    accent-color: var(--primary);
    width: 14px;
    height: 14px;
}

.corpus-toggle-label {
    font-weight: 500;
}

.chat-input-controls textarea {
    flex: 1;
    padding: 0.75rem;
    border: 1px solid var(--gray-200);
    border-radius: 0.5rem;
    resize: none;
    font-size: 0.875rem;
    font-family: inherit;
    line-height: 1.5;
    max-height: 150px;
}

.chat-input-controls textarea:focus {
    outline: none;
    border-color: var(--primary);
}

.chat-input-controls .btn-primary {
    padding: 0.75rem 1.5rem;
}

.chat-input-controls .btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==================== Toggle Popup ==================== */

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

.popup-dialog {
    background: white;
    border-radius: 0.75rem;
    padding: 1.5rem;
    max-width: 480px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.popup-dialog h3 {
    margin: 0 0 0.75rem;
    font-size: 1rem;
    color: var(--gray-900);
}

.popup-dialog p {
    margin: 0 0 1.25rem;
    font-size: 0.875rem;
    color: var(--gray-600);
    line-height: 1.6;
}

.popup-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

.popup-btn {
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid var(--gray-200);
    background: white;
    color: var(--gray-700);
}

.popup-btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.popup-btn-primary:hover {
    opacity: 0.9;
}

.popup-btn-cancel:hover {
    background: var(--gray-50);
}

/* ==================== Citation Chips & Reference Sidebar ==================== */

.citation-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--gray-200);
}

.citation-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.citation-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.15s;
    font-family: inherit;
}

.citation-chip:hover {
    opacity: 0.8;
}

.citation-chip.active {
    background: var(--primary-dark);
    box-shadow: 0 0 0 2px var(--primary);
}

.reference-sidebar {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 420px;
    background: white;
    border-left: 1px solid var(--gray-200);
    z-index: 50;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.reference-sidebar.visible {
    transform: translateX(0);
}

.ref-sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
}

.ref-sidebar-header h3 {
    margin: 0;
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--gray-900);
}

.ref-sidebar-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--gray-400);
    cursor: pointer;
    padding: 0 0.25rem;
    line-height: 1;
}

.ref-sidebar-close:hover {
    color: var(--gray-700);
}

.ref-sidebar-doc {
    padding: 1rem;
    border-bottom: 1px solid var(--gray-200);
    background: var(--gray-50);
}

.ref-sidebar-doc .doc-title {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--gray-900);
}

.ref-sidebar-doc .doc-id {
    font-size: 0.75rem;
    color: var(--gray-400);
    margin-top: 2px;
}

.ref-sidebar-position {
    padding: 0.75rem 1rem;
    font-size: 0.8125rem;
    color: var(--gray-600);
    border-bottom: 1px solid var(--gray-200);
}

.ref-sidebar-position .position-label {
    font-weight: 500;
    color: var(--gray-700);
}

.ref-sidebar-position .tier-badge {
    display: inline-block;
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 1px 6px;
    border-radius: 3px;
    margin-left: 6px;
}

.ref-sidebar-position .tier-badge.ftss {
    background: #dbeafe;
    color: #1e40af;
}

.ref-sidebar-position .tier-badge.semantic {
    background: #fce7f3;
    color: #9d174d;
}

.ref-sidebar-text {
    flex: 1;
    padding: 1rem;
    overflow-y: auto;
    font-size: 0.875rem;
    line-height: 1.7;
    color: var(--gray-800);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.ref-sidebar-text .score-info {
    font-size: 0.75rem;
    color: var(--gray-400);
    margin-bottom: 0.75rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--gray-200);
}

/* ==================== KaTeX in Chat Messages ==================== */

.message-text .katex-display {
    margin: 0.75rem 0;
    overflow-x: auto;
    overflow-y: hidden;
}

.message-text .katex {
    font-size: 1em;
}

.message-text .katex-display > .katex {
    font-size: 1.05em;
}

/* Inline math in user bubbles (white text) */
.chat-message.user .message-text .katex {
    color: white;
}

.chat-message.user .message-text .katex .mord,
.chat-message.user .message-text .katex .mbin,
.chat-message.user .message-text .katex .mrel,
.chat-message.user .message-text .katex .mopen,
.chat-message.user .message-text .katex .mclose,
.chat-message.user .message-text .katex .mpunct,
.chat-message.user .message-text .katex .mop {
    color: white;
}

/* Code blocks inside message text */
.message-text pre {
    background: var(--gray-900);
    color: var(--gray-100);
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0.5rem 0;
    font-size: 0.8125rem;
}

.message-text pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.message-text code {
    background: rgba(0, 0, 0, 0.08);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 0.8125rem;
}

.chat-message.user .message-text code {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

/* Tables in message text */
.message-text table {
    border-collapse: collapse;
    margin: 0.75rem 0;
    font-size: 0.8125rem;
    width: 100%;
}

.message-text th,
.message-text td {
    border: 1px solid var(--gray-200);
    padding: 0.375rem 0.75rem;
    text-align: left;
}

.message-text th {
    background: var(--gray-100);
    font-weight: 600;
}

/* Blockquotes */
.message-text blockquote {
    border-left: 3px solid var(--primary);
    padding-left: 0.75rem;
    margin: 0.5rem 0;
    color: var(--gray-600);
    font-style: italic;
}

/* Lists */
.message-text ul,
.message-text ol {
    padding-left: 1.5rem;
    margin: 0.5rem 0;
}

.message-text li {
    margin: 0.25rem 0;
}

/* Headings in message text */
.message-text h1,
.message-text h2,
.message-text h3,
.message-text h4 {
    margin: 0.75rem 0 0.375rem;
    font-weight: 600;
}

.message-text h1 { font-size: 1.125rem; }
.message-text h2 { font-size: 1rem; }
.message-text h3 { font-size: 0.9375rem; }
