:root {
    /* Spacing & Sizes */
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    --border-radius-sm: 8px;
    --border-radius-md: 12px;
    --border-radius-lg: 16px;

    /* Safe Areas for iPhone */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow-x: hidden;
    /* Prevent pull-to-refresh on mobile if possible */
    overscroll-behavior-y: none; 
}

/* Screens */
.screen {
    width: 100vw;
    min-height: 100vh;
    padding: calc(var(--safe-top) + var(--spacing-md)) calc(var(--safe-right) + var(--spacing-md)) calc(var(--safe-bottom) + var(--spacing-md)) calc(var(--safe-left) + var(--spacing-md));
    display: flex;
    flex-direction: column;
}

.hidden {
    display: none !important;
}

/* Buttons */
.primary-btn, .secondary-btn, .close-btn {
    font-family: inherit;
    border-radius: 8px;
    padding: 10px 16px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.primary-btn {
    background-color: var(--primary-color);
    color: white;
    border: 1px solid transparent;
}

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

.secondary-btn {
    background-color: white;
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.secondary-btn:hover {
    background-color: #f8fafc;
    border-color: #cbd5e1;
}

/* --- MODALS --- */
.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 23, 42, 0.45);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 16px;
}

.modal-content {
    background-color: #ffffff;
    opacity: 1;
    padding: 24px;
    border-radius: 12px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 1001;
}

.modal-content h3 {
    margin-bottom: var(--spacing-md);
    color: var(--text-primary);
    font-size: 20px;
}

.modal-content p {
    margin-bottom: var(--spacing-sm);
    color: var(--text-secondary);
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

/* --- OCR Phase 3A Styles --- */
#verification-layout {
    display: block;
}

#verification-layout.verifying {
    display: flex;
    flex-direction: row;
    gap: 20px;
    align-items: flex-start;
}

#verification-layout.verifying #verification-image-container {
    flex: 1;
    position: sticky;
    top: 20px;
    background: #f8f9fa;
    padding: 15px;
    border: 1px solid #ddd;
    border-radius: 8px;
    max-height: 85vh;
    overflow-y: auto;
}

#verification-layout.verifying #verification-form-container {
    flex: 1;
    background: white;
    padding: 15px;
    border: 1px solid #ddd;
    border-radius: 8px;
}

.ocr-highlight {
    box-shadow: 0 0 5px 2px #ffc107 !important;
    border-color: #ffc107 !important;
    transition: box-shadow 0.3s ease;
}

@media (max-width: 768px) {
    #verification-layout.verifying {
        flex-direction: column;
    }
    
    #verification-layout.verifying #verification-image-container {
        position: relative;
        top: 0;
        max-height: 40vh;
        width: 100%;
        margin-bottom: 20px;
    }
    
    #verification-layout.verifying #verification-form-container {
        width: 100%;
    }
}

/* LOGIN UI */
.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--background);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-card {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 32px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* AUTHENTICATION GATE STATES */
body.auth-checking #login-screen,
body.auth-checking #app-shell {
    display: none !important;
}

body.unauthenticated #login-screen {
    display: flex;
}
body.unauthenticated #app-shell {
    display: none !important;
}

body.authenticated #login-screen {
    display: none !important;
}
body.authenticated #app-shell {
    display: block;
}

/* ========================================================================= */
/* Phase 4K Premium Clinical SaaS Redesign */
/* ========================================================================= */

/* EVENT ACTION TOOLBAR */
.event-action-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin-top: 16px;
    margin-bottom: 24px;
    padding-bottom: 24px;
    border-bottom: 1px solid #e2e8f0;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 12px;
    border-left: 1px solid #e2e8f0;
}

.toolbar-btn-add {
    font-size: 0.95rem;
    padding: 10px 18px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,119,182,0.2);
}

.toolbar-btn-capture, .toolbar-btn-import {
    background-color: #f8fafc;
    border: 1px solid #cbd5e1;
    color: #334155;
    font-size: 0.9rem;
    padding: 8px 14px;
    font-weight: 500;
}

.toolbar-btn-capture:hover, .toolbar-btn-import:hover {
    background-color: #f1f5f9;
    border-color: #94a3b8;
}

.toolbar-btn-capture .nav-icon, .toolbar-btn-import .nav-icon {
    fill: #64748b;
    margin-right: 6px;
}

.utils-group {
    margin-left: auto;
    border-left: none;
    padding-left: 0;
}

.toolbar-btn-util {
    background-color: transparent;
    border: 1px solid transparent;
    color: #64748b;
    font-size: 0.85rem;
    padding: 6px 10px;
}

.toolbar-btn-util:hover {
    background-color: #f1f5f9;
    color: #334155;
}

@media (max-width: 768px) {
    .event-action-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .toolbar-group {
        border-left: none;
        padding-left: 0;
        flex-direction: column;
        align-items: stretch;
    }
    .utils-group {
        margin-left: 0;
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
}

/* WORKSPACE HEADERS */
#record-form-screen {
    background-color: #f8fafc;
    min-height: 100vh;
    padding: 0;
}

/* Dynamic Header Toggling via OCR state */
.capture-record-title { display: none; }
.add-record-title { display: flex; flex-direction: column; }

#record-form-screen.is-capture-mode .capture-record-title { display: flex; flex-direction: column; }
#record-form-screen.is-capture-mode .add-record-title { display: none; }

.workspace-header {
    background-color: #ffffff;
    border-bottom: 1px solid #e2e8f0;
    padding: 16px 24px;
    display: flex;
    align-items: flex-start;
    gap: 24px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.workspace-title-group h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: #0f172a;
    margin: 0 0 4px 0;
}

.workspace-event-context {
    font-size: 0.85rem;
    color: #64748b;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
}

.context-divider {
    color: #94a3b8;
}

.nav-back-btn {
    appearance: none;
    -webkit-appearance: none;
    border: none;
    background: transparent;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: #64748b;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    padding: 8px 12px 8px 8px;
    border-radius: 8px;
    transition: background-color 0.15s, color 0.15s;
    margin-top: -2px; /* optical alignment */
    outline: none;
    box-shadow: none;
}

.nav-back-btn:hover {
    background-color: #f1f5f9;
    color: #0f172a;
}

/* VERIFICATION LAYOUT */
#verification-layout {
    display: flex;
    flex-direction: column;
    max-width: 800px;
    margin: 0 auto;
    padding: 24px 16px 100px 16px; /* 100px bottom for sticky footer */
}

#record-form-screen.is-capture-mode #verification-layout {
    max-width: 1400px;
    flex-direction: row;
    align-items: flex-start;
    gap: 32px;
}

#verification-image-container {
    flex: 1;
    position: sticky;
    top: 100px;
    background: #ffffff;
    padding: 16px;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.image-workspace-header {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    color: #64748b;
    margin-bottom: 12px;
    letter-spacing: 0.5px;
}

#verif-image {
    width: 100%;
    border-radius: 8px;
    display: block;
}

#verification-form-container {
    flex: 1;
    background: #ffffff;
    padding: 32px;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    width: 100%;
}

@media (max-width: 900px) {
    #record-form-screen.is-capture-mode #verification-layout {
        flex-direction: column;
    }
    #verification-image-container {
        position: relative;
        top: 0;
    }
}

/* FORM DESIGN SYSTEM */
.modern-form {
    display: flex;
    flex-direction: column;
}

.form-section {
    padding: 24px 0;
}

.form-section:first-child {
    padding-top: 0;
}

.section-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: #64748b;
    margin-bottom: 24px;
    margin-top: 0;
}

.section-divider {
    border: none;
    border-top: 1px solid #e2e8f0;
    margin: 0;
}

/* Form Groups & Inputs */
.modern-form .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
}

.modern-form .form-group {
    display: flex;
    flex-direction: column;
}

.modern-form .form-group.full-width {
    grid-column: 1 / -1;
}

.modern-form .form-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: #334155;
    margin-bottom: 8px;
}

.modern-form input[type="text"], 
.modern-form input[type="number"], 
.modern-form input[type="date"], 
.modern-form input[type="tel"], 
.modern-form select, 
.modern-form textarea {
    font-family: inherit;
    font-size: 0.95rem;
    border: 1px solid #cbd5e1;
    background-color: #ffffff;
    border-radius: 6px;
    padding: 8px 12px;
    color: #0f172a;
    transition: border-color 0.15s, box-shadow 0.15s;
    height: 38px;
}

.modern-form textarea {
    height: auto;
    resize: vertical;
}

.modern-form input:focus, 
.modern-form select:focus, 
.modern-form textarea:focus {
    outline: none;
    border-color: #0077b6;
    box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.15);
}

.input-with-prefix {
    display: flex;
    align-items: center;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    background-color: #ffffff;
    height: 38px;
}

.input-with-prefix:focus-within {
    border-color: #0077b6;
    box-shadow: 0 0 0 3px rgba(0, 119, 182, 0.15);
}

.prefix-label {
    padding: 0 12px;
    font-weight: 600;
    color: #64748b;
    font-size: 0.95rem;
    border-right: 1px solid #cbd5e1;
    display: flex;
    align-items: center;
    height: 100%;
    background-color: #f8fafc;
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
}

.input-with-prefix input {
    border: none !important;
    box-shadow: none !important;
    height: 100% !important;
    flex: 1;
}

/* Checkboxes and Radios */
.modern-radio-group, .modern-checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
}

.radio-card, .checkbox-card {
    display: inline-flex;
    align-items: center;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    background-color: #ffffff;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    color: #334155;
    transition: all 0.2s;
    height: 38px;
    padding: 0 16px 0 12px;
}

.radio-card:hover, .checkbox-card:hover {
    border-color: #94a3b8;
    background-color: #f8fafc;
}

.radio-card input, .checkbox-card input {
    margin-right: 8px;
    width: 16px;
    height: 16px;
    accent-color: #0077b6;
}

/* Switch */
.switch-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #f8fafc;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    padding: 8px 12px;
    height: 38px;
    cursor: pointer;
    margin-bottom: 0 !important;
}

.switch-text {
    font-weight: 600;
    color: #334155;
}

.switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
}

.switch input { 
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #cbd5e1;
    transition: .2s;
    border-radius: 20px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .2s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #0077b6;
}

input:checked + .slider:before {
    transform: translateX(16px);
}

/* CLINICAL MATRIX (OD/OS) */
.clinical-matrix {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    overflow: hidden;
    background-color: #f8fafc;
}

.matrix-column {
    display: flex;
    flex-direction: column;
}

.matrix-column:first-child {
    border-right: 1px solid #e2e8f0;
}

.matrix-divider {
    display: none; /* Hidden on grid layout */
}

.matrix-header {
    text-align: center;
    font-weight: 700;
    font-size: 0.85rem;
    padding: 12px;
    border-bottom: 1px solid #e2e8f0;
    background-color: #f1f5f9;
    color: #475569;
    letter-spacing: 0.5px;
}

.matrix-group {
    padding: 16px;
}

.ref-group {
    border-top: 1px solid #e2e8f0;
    background-color: #ffffff;
}

.matrix-row {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.matrix-row:last-child {
    margin-bottom: 0;
}

.matrix-row label {
    width: 40px;
    font-size: 0.85rem;
    font-weight: 600;
    color: #64748b;
    margin-bottom: 0 !important;
}

.matrix-row select, .matrix-row input {
    flex: 1;
}

@media (max-width: 600px) {
    .clinical-matrix {
        grid-template-columns: 1fr;
    }
    .matrix-column:first-child {
        border-right: none;
        border-bottom: 1px solid #e2e8f0;
    }
}

/* CONVERSION PANEL */
.conversion-panel {
    background-color: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 24px;
    margin-top: 24px;
}

.conversion-panel .section-title {
    color: #0f172a;
}

/* STICKY ACTION FOOTER */
.sticky-action-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-top: 1px solid #e2e8f0;
    padding: 16px 24px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 12px;
    z-index: 900;
    box-shadow: 0 -4px 6px -1px rgba(0, 0, 0, 0.05);
}

.sticky-action-footer .btn-cancel {
    background: transparent;
    border: 1px solid transparent;
    color: #64748b;
}

.sticky-action-footer .btn-cancel:hover {
    background: #f1f5f9;
    color: #0f172a;
}

.sticky-action-footer .btn-save {
    padding: 12px 24px;
    font-size: 1rem;
}

@media (max-width: 768px) {
    .sticky-action-footer {
        padding: 16px;
    }
}


.btn-sm {
    padding: 6px 12px !important;
    font-size: 0.85rem !important;
}

