:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary: #64748b;
    --bg-main: #f8fafc;
    --bg-card: #ffffff;
    --border: #e2e8f0;
    --text-main: #1e293b;
    --text-muted: #64748b;
    /* Patient Portal Variables (Oquendo Brand Colors) */
    --portal-primary: #e65c00;
    --portal-primary-hover: #bf4d00;
    --portal-accent: #1565c0;
    --portal-dark: #1a1a2e;
    --portal-bg-light: #ffffff;
    --portal-text-on-dark: #ffffff;
    --portal-font: 'Inter', sans-serif;
    --portal-gold: #e65c00;
    --portal-gold-hover: #bf4d00;
    --radius: 12px;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --z-modal: 10000;
    --z-toast: 11000;
}

.custom-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(59, 130, 246, 0.1);
    border-top: 3px solid #3b82f6;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Global Theme Overrides for Text Visibility */
body:not(.dark) .toast {
    background: white !important;
    border: 1px solid var(--border) !important;
}
body:not(.dark) .toast-content h4 { color: #1e293b !important; }
body:not(.dark) .toast-content p { color: #64748b !important; }

body.dark {
    --bg-main: #020617;
    --bg-card: #0f172a;
    --border: #1e293b;
    --text-main: #ffffff;
    --text-muted: #cbd5e1; /* Increased brightness for muted text in dark mode */
    --input-bg: #1e293b;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.8);
}

/* Force high contrast in Dark Mode */
body.dark h1, body.dark h2, body.dark h3, body.dark h4, 
body.dark p, body.dark span, body.dark label, body.dark b, body.dark strong,
body.dark .nav-text, body.dark .nav-item span,
body.dark .logo span {
    color: #ffffff !important;
}

body.dark .text-muted, body.dark small, body.dark .nav-section h3 {
    color: #cbd5e1 !important;
}

/* Results Section Labels Fix */
.results-table-cell {
    color: #1e293b; /* Light mode default */
}
body.dark .results-table-cell {
    color: #ffffff !important; /* Dark mode force */
}

/* Exam Selector Portal Fixes */
#exam-selector-portal div {
    color: #1e293b; /* Light mode */
}
body.dark #exam-selector-portal div,
body.dark #exam-selector-portal span,
body.dark #exam-selector-portal h3 {
    color: #ffffff !important;
}

body.dark #exam-selector-portal input {
    background: #1e293b !important;
    color: #ffffff !important;
    border-color: #334155 !important;
}

body.dark .card, body.dark .sidebar, body.dark .header, body.dark .order-panel {
    background-color: var(--bg-card) !important;
    border-color: var(--border) !important;
}

body.dark .order-item {
    background-color: #1e293b !important;
    border-color: #334155 !important;
}

body.dark .order-item-name {
    color: #ffffff !important;
}

/* Input Placeholders */
::placeholder {
    color: #94a3b8;
    opacity: 1;
}
body.dark ::placeholder {
    color: #64748b;
}

/* Toast Dark Mode */
body.dark .toast {
    background-color: #1e293b !important;
    border: 1px solid #334155 !important;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5) !important;
}

body.dark .toast-content h4 {
    color: #ffffff !important;
}

body.dark .card, 
body.dark .sidebar, 
body.dark .header,
body.dark .order-panel,
body.dark aside[style*="background: #f8fafc"],
body.dark aside[style*="background: rgb(248, 250, 252)"] {
    background-color: var(--bg-card) !important;
    border-color: var(--border) !important;
}

/* Fix Exam Selector (Portal) in Dark Mode */
body.dark #exam-selector-portal div {
    background-color: transparent;
    color: #f8fafc !important;
}

body.dark #exam-selector-portal > div {
    background-color: var(--bg-card) !important;
}

/* Categories in Portal */
body.dark #exam-selector-portal div[style*="background: #f1f5f9"],
body.dark #exam-selector-portal div[style*="background: rgb(241, 245, 249)"] {
    background-color: #1e293b !important;
    border-color: var(--border) !important;
}

/* Active/Hover states in Portal */
body.dark #exam-selector-portal div[style*="background: #eff6ff"],
body.dark #exam-selector-portal div[style*="background: rgb(239, 246, 255)"] {
    background-color: rgba(37, 99, 235, 0.2) !important; /* Light blue tint on dark bg */
    border-color: var(--primary) !important;
}

body.dark .form-control,
body.dark input,
body.dark select,
body.dark textarea {
    background-color: #1e293b !important;
    border-color: #334155 !important;
    color: #f8fafc !important;
}

.error-input {
    border-color: #ef4444 !important;
    background-color: #fef2f2 !important;
    box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.1) !important;
}

body.dark .error-input {
    background-color: rgba(239, 68, 68, 0.1) !important;
    border-color: #ef4444 !important;
}

body.dark .input-group label {
    color: #f8fafc !important;
    font-weight: 600;
    opacity: 0.9;
}

body.dark .section-header {
    border-bottom: 2px solid #1e293b !important;
}

body.dark .section-header h2 {
    color: #3b82f6 !important; /* Bright blue for headers */
}

body.dark .nav-item:hover {
    background-color: #1e293b !important;
}

body.dark .nav-item.active {
    background-color: #1e293b !important;
    border: 1px solid var(--primary) !important;
}

/* Results Table visibility in Dark Mode */
body.dark .results-table-header {
    background-color: #1e293b !important;
    color: #94a3b8 !important;
}

body.dark .results-row:hover {
    background-color: rgba(255, 255, 255, 0.05) !important;
}

/* Checkbox and toggle items */
body.dark .selector-item {
    background-color: #1e293b !important;
}

body.dark .selector-item span {
    color: #f8fafc !important;
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-main);
    color: var(--text-main);
    line-height: 1.5;
}

#root {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.app-container {
    display: flex;
    min-height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 250px;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    padding: 1.5rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    flex-shrink: 0;
}

.sidebar.collapsed {
    width: 80px;
    padding: 1.5rem 0.75rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-weight: 700;
    font-size: 1.25rem;
    color: var(--primary);
}

.sidebar.collapsed .logo {
    justify-content: center;
    gap: 0;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background: var(--primary);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    flex-shrink: 0;
}

.sidebar-toggle {
    position: absolute;
    top: 1.25rem;
    right: -14px;
    width: 28px;
    height: 28px;
    background: white;
    border: 1px solid var(--border);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 100;
    box-shadow: var(--shadow);
    transition: all 0.2s;
    font-size: 0.9rem;
    color: var(--primary);
}

/* Dark Mode Toggle */
.dark-mode-btn {
    width: 42px; /* Increased 15% */
    height: 42px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-main);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1.45rem; /* Increased font size as well */
}

body.dark .sidebar-toggle {
    background: var(--bg-card);
}

.sidebar-toggle:hover {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.nav-section {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.nav-section h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    padding-left: 0.5rem;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    border-radius: var(--radius);
    text-decoration: none;
    color: var(--text-main);
    font-weight: 500;
    transition: all 0.2s;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
}

.sidebar.collapsed .nav-item {
    justify-content: center;
    padding: 0.75rem 0;
    gap: 0;
}

.sidebar.collapsed .nav-item span {
    font-size: 1.25rem;
}

.sidebar.collapsed .nav-item .nav-text,
.sidebar.collapsed h3,
.sidebar.collapsed .logo span,
.sidebar.collapsed .user-info-text,
.sidebar.collapsed .btn-logout-text {
    display: none;
}

/* Main Content */
.main-content {
    flex: 1;
    width: 100%;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 1.5rem 2rem;
    background: white;
    border-radius: 12px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.header-title h1 {
    font-size: 1.875rem;
    font-weight: 700;
    color: var(--text-main);
}

.header-actions {
    display: flex;
    gap: 1rem;
}

/* Card Styles */
.form-card, .card {
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: 2rem;
}

.form-card {
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
}

.section-group {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.section-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-main);
}

.grid-cols-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1.5rem;
}

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

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

.input-group label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-muted);
}

input, select {
    padding: 0.625rem 0.875rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    font-family: inherit;
    font-size: 0.9375rem;
    transition: all 0.2s;
    width: 100%;
    background-color: var(--bg-card);
    color: var(--text-main);
}

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

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.9375rem;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
}

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

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background: #f1f5f9;
    color: #475569;
}

.btn-secondary:hover {
    background: #e2e8f0;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-main);
}

.btn-outline:hover {
    background: var(--bg-main);
}

/* Section Selector */
.section-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    padding: 1.5rem;
    background: white;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    margin-bottom: 2rem;
}

.selector-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.25rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
    flex: 1;
    min-width: 180px;
}

.selector-item:hover {
    border-color: var(--primary);
    background: #f8fafc;
}

.selector-item.active {
    border-color: var(--primary);
    background: #eff6ff;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.selector-item input {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: var(--primary);
    cursor: pointer;
}

.selector-item span {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-main);
}

/* Visibility Toggle Button */
.visibility-toggle {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    padding: 2px 6px;
    border-radius: 4px;
    opacity: 0.5;
    transition: all 0.2s;
}
.visibility-toggle:hover {
    opacity: 1;
    background: #e2e8f0;
}
.visibility-toggle.hidden {
    opacity: 1;
    color: #ef4444;
}
.results-grid.hidden-row {
    opacity: 0.4;
    background: #f8fafc;
}


.selector-item .icon {
    font-size: 1.25rem;
}

/* Dropzone */
.dropzone {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 2.5rem;
    text-align: center;
    background: #f8fafc;
    transition: all 0.2s;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

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

.dropzone-icon {
    font-size: 2.5rem;
    color: var(--primary);
}

.dropzone p {
    font-size: 0.9375rem;
    color: var(--text-muted);
}

.dropzone .highlight {
    color: var(--primary);
    font-weight: 600;
}

/* Results Table UI */
.results-grid {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr 1.5fr auto;
    gap: 1rem;
    align-items: center;
}

.results-header {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.editable-field-container:hover .edit-pencil {
    opacity: 1 !important;
    color: var(--primary);
}

.edit-pencil {
    transition: all 0.2s;
}

.edit-pencil:hover {
    transform: scale(1.2);
}

body.dark .results-header {
    color: #3b82f6 !important; /* Bright blue for column headers in dark mode */
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column-reverse; /* Newest on top */
    gap: 0.75rem;
    z-index: 9999;
    pointer-events: none;
    align-items: center;
}

.toast {
    pointer-events: auto;
    min-width: 320px;
    background: white;
    padding: 1rem 1.25rem;
    border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    display: flex;
    align-items: center;
    gap: 1rem;
    border-left: 4px solid var(--primary);
    animation: toast-slide-up 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    transition: all 0.3s;
}

.toast.removing {
    transform: translateY(20px);
    opacity: 0;
}

.toast-icon {
    font-size: 1.5rem;
    width: 2.5rem;
    height: 2.5rem;
    background: #eff6ff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.toast-content {
    flex: 1;
}

.toast-content h4 {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 0.125rem;
}

.toast-content p {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.toast-success { border-left-color: #10b981; }
.toast-success .toast-icon { background: #ecfdf5; color: #10b981; }

.toast-error { border-left-color: #ef4444; }
.toast-error .toast-icon { background: #fef2f2; color: #ef4444; }

.toast-info { border-left-color: var(--primary); }
.toast-info .toast-icon { background: #eff6ff; color: var(--primary); }

@keyframes toast-slide-up {
    from { transform: translateY(100%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

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

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

/* History/User Table Styling - More Dynamic */
.history-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
}

.history-table th {
    text-align: left;
    padding: 1.25rem 1.5rem;
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    border-bottom: 2px solid #f1f5f9;
    background: #f8fafc;
}

.history-table td {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: middle;
    font-size: 0.9rem;
    transition: background 0.2s;
}

.history-table tr:hover td {
    background-color: #f1f5f9;
}

.history-table tr:last-child td {
    border-bottom: none;
}

.history-table .badge {
    padding: 0.25rem 0.75rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
}

/* Unify Toast Styling */
.toast {
    pointer-events: auto;
    min-width: 350px;
    background: white;
    padding: 1.25rem;
    border-radius: 16px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    border: 1px solid var(--border);
    animation: toast-slide-up 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    transition: all 0.3s;
}

.toast-icon {
    font-size: 1.25rem;
    padding: 0.5rem;
    border-radius: 12px;
    flex-shrink: 0;
}

@keyframes modal-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-overlay {
    animation: modal-fade-in 0.3s ease-out;
}

/* Order Panel - Re-compact but slightly larger (+10%) */
.order-panel {
    width: 160px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.5rem;
    box-shadow: var(--shadow);
    position: sticky;
    top: 2rem;
    transition: all 0.2s;
}

.order-panel-title {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    font-weight: 800;
}

.order-list {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.order-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.4rem;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    cursor: default;
    transition: all 0.2s;
    min-height: 26px;
}

.order-item:hover {
    border-color: var(--primary);
    background: #eff6ff;
}

.drag-handle {
    cursor: grab;
    color: #94a3b8;
    display: flex;
    align-items: center;
    padding: 1px;
    flex-shrink: 0;
}

.drag-handle:active {
    cursor: grabbing;
}

.order-item-icon {
    font-size: 0.85rem;
    flex-shrink: 0;
}

.order-item-name {
    font-size: 0.65rem;
    font-weight: 600;
    white-space: normal;
    line-height: 1.15;
    flex: 1;
    color: #334155;
    padding: 1px 0;
}

.order-item-status {
    color: #10b981;
    display: flex;
    align-items: center;
    flex-shrink: 0;
    opacity: 0.7;
}

.sortable-ghost {
    opacity: 0.4;
    background: var(--primary) !important;
}

.sortable-chosen {
    box-shadow: var(--shadow-lg);
}

/* --- Responsive Design --- */
@media (max-width: 1024px) {
    .order-panel {
        display: none; /* Hide on small screens to save space as per user's "clear interface" goal, or we can move it. Let's move it below. */
    }
}

@media (max-width: 768px) {
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        width: 100% !important;
        height: auto !important;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 1rem;
        flex-direction: row !important;
        align-items: center;
        justify-content: space-between;
        position: sticky;
        top: 0;
        z-index: 1000;
    }

    .sidebar.collapsed {
        width: 100% !important;
    }

    .sidebar-toggle {
        display: none !important;
    }

    .nav-section {
        flex-direction: row;
        gap: 0.5rem;
    }

    .nav-section h3 {
        display: none;
    }

    .nav-item {
        padding: 0.5rem;
    }

    .nav-item .nav-text {
        display: none; /* Icons only on very small screens */
    }

    .logo span {
        display: none;
    }

    .user-info, .sidebar button[style*="dc2626"] {
        display: none !important; /* Move logout elsewhere or keep it simple */
    }

    /* Editor Layout Stacking */
    .editor-layout {
        flex-direction: column !important;
    }

    .exam-selector-aside {
        width: 100% !important;
        height: auto !important;
        max-height: 300px;
        border-right: none !important;
        border-bottom: 1px solid var(--border);
    }

    .editor-content-area {
        padding: 1rem !important;
    }

    .header {
        flex-direction: column;
        align-items: flex-start;
        padding: 1rem;
        gap: 1rem;
    }

    .header-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .grid-cols-2, .grid-cols-3 {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

    .form-card, .card {
        padding: 1rem;
    }

    /* Tables Mobile Fix - card layout on very small screens */
    .history-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 12px;
    }

    /* On very narrow screens: stack rows as cards */
    .history-table, .history-table thead, .history-table tbody, .history-table th, .history-table td, .history-table tr {
        display: block;
    }

    .history-table thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    .history-table tr {
        border-bottom: 1px solid var(--border);
        margin-bottom: 1rem;
        padding-bottom: 1rem;
    }

    .history-table td {
        border: none;
        position: relative;
        padding-left: 50%;
        text-align: right;
    }

    .history-table td:before {
        position: absolute;
        left: 0.5rem;
        width: 45%;
        padding-right: 10px;
        white-space: nowrap;
        text-align: left;
        font-weight: bold;
        color: var(--text-muted);
        content: attr(data-label);
    }

    .results-order-container {
        flex-direction: column !important;
    }

    .order-panel {
        display: block !important;
        width: 100% !important;
        max-height: 300px;
        overflow-y: auto;
        margin-top: 1rem;
        border-radius: 8px;
        border: 1px solid var(--border);
    }
    
    .results-grid {
        grid-template-columns: 1fr;
        gap: 0.5rem;
        border: 1px solid var(--border);
        padding: 1rem;
        border-radius: 8px;
        margin-bottom: 1rem;
    }

    .results-header {
        display: none;
    }

    .results-grid > div:nth-child(2):before { content: "Resultado: "; font-weight: bold; }
    .results-grid > div:nth-child(3):before { content: "U.M.: "; font-weight: bold; }
    .results-grid > div:nth-child(4):before { content: "Val. Ref.: "; font-weight: bold; }

    /* History view mobile: stack layout in header */
    .history-view header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 1rem;
    }

    .history-view header input,
    .history-view header div {
        width: 100% !important;
    }

    /* Preview & modal fit on small screens */
    .modal-overlay > div {
        border-radius: 0 !important;
        height: 100svh !important;
        height: 100% !important;
    }

    /* Helper modifier to disable block stacking and allow horizontal table scrolling */
    .force-scroll-mobile table,
    .force-scroll-mobile thead,
    .force-scroll-mobile tbody,
    .force-scroll-mobile th,
    .force-scroll-mobile td,
    .force-scroll-mobile tr {
        display: revert !important;
    }

    .force-scroll-mobile thead tr {
        position: static !important;
        top: auto !important;
        left: auto !important;
    }

    .force-scroll-mobile td {
        padding-left: 1rem !important;
        text-align: left !important;
        border-bottom: 1px solid #f1f5f9 !important;
    }

    .force-scroll-mobile tr {
        border-bottom: none !important;
        margin-bottom: 0 !important;
        padding-bottom: 0 !important;
    }

    .force-scroll-mobile td:before {
        display: none !important;
    }

    /* Toasts: move to bottom-center on mobile */
    .toasts-container {
        bottom: 1rem;
        left: 0.75rem;
        right: 0.75rem;
        align-items: center;
    }

    .toast {
        min-width: unset;
        width: 100%;
    }
}

/* Landscape / wider phones: make tables scroll horizontally */
@media (max-width: 768px) and (orientation: landscape),
       (min-width: 480px) and (max-width: 768px) {
    .history-table,
    .history-table thead,
    .history-table tbody,
    .history-table th,
    .history-table td,
    .history-table tr {
        display: revert !important;
    }

    .history-table thead tr {
        position: static !important;
        top: auto !important;
        left: auto !important;
    }

    .history-table td {
        padding-left: 1rem !important;
        text-align: left !important;
    }

    .history-table td:before {
        display: none !important;
    }

    .history-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* Order Panel - Remove button */
.order-item-remove {
    background: none;
    border: none;
    cursor: pointer;
    color: #94a3b8;
    font-size: 1.1rem;
    font-weight: 700;
    padding: 0 2px;
    line-height: 1;
    border-radius: 4px;
    transition: all 0.15s;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
}

.order-item-remove:hover {
    background: #fee2e2;
    color: #ef4444;
}

/* Custom Confirm Modal */
.custom-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    animation: fadeIn 0.2s ease-out;
}

.custom-modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2rem;
    max-width: 400px;
    width: 90%;
    box-shadow: var(--shadow-lg);
    text-align: center;
    transform: scale(1);
    animation: scaleIn 0.2s ease-out;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes scaleIn {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.custom-modal h3 {
    margin-bottom: 1rem;
    font-size: 1.25rem;
    font-weight: 700;
}

.custom-modal p {
    color: var(--text-muted);
    margin-bottom: 2rem;
    font-size: 0.95rem;
    line-height: 1.5;
}

.custom-modal-actions {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

.modal-btn {
    padding: 0.75rem 1.5rem;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    border: 1px solid transparent;
    font-size: 0.9rem;
}

.modal-btn-secondary {
    background: transparent;
    border-color: var(--border);
    color: var(--text-main);
}

.modal-btn-danger {
    background: #ef4444;
    color: white;
}

.modal-btn-danger:hover {
    background: #dc2626;
}

.modal-btn-secondary:hover {
    background: var(--bg-main);
}

/* --- Added for Dynamism and Notifications --- */

.dynamic-table {
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
    border-radius: 12px;
    overflow: hidden;
}

.dynamic-table tr {
    transition: all 0.2s ease;
}

.dynamic-table tr:hover {
    background-color: #f8fafc;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.toasts-container {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    z-index: var(--z-toast);
    pointer-events: none;
}

.toast {
    background: white;
    padding: 1rem 1.5rem;
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    gap: 1rem;
    border-left: 4px solid var(--primary);
    animation: toast-slide-in 0.3s ease-out;
    pointer-events: auto;
    min-width: 300px;
}

.toast-success { border-left-color: #10b981; }
.toast-error { border-left-color: #ef4444; }
.toast-info { border-left-color: #3b82f6; }

.toast-content h4 {
    margin: 0;
    font-size: 0.9rem;
    font-weight: 700;
}

.toast-content p {
    margin: 4px 0 0;
    font-size: 0.8rem;
    color: var(--text-muted);
}

@keyframes toast-slide-in {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* History Grouping Styles */
.history-date-header:hover {
    border-color: var(--primary) !important;
    background: #f8fafc !important;
}

body.dark .history-date-header:hover {
    background: #1e293b !important;
}

body.dark .history-date-header h3 {
    color: #ffffff !important;
}

/* Loading Overlays */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /* Lighter overlay — content stays partially visible */
    background-color: rgba(248, 250, 252, 0.55);
    backdrop-filter: blur(3px) saturate(0.85);
    -webkit-backdrop-filter: blur(3px) saturate(0.85);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: overlayIn 0.2s ease-out;
    border-radius: inherit;
}

body.dark .loading-overlay {
    background-color: rgba(2, 6, 23, 0.55);
    backdrop-filter: blur(3px) saturate(0.7);
    -webkit-backdrop-filter: blur(3px) saturate(0.7);
}

.loading-overlay.full-screen {
    position: fixed;
    border-radius: 0;
}

/* Floating pill badge with spinner */
.loading-badge {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
    background: rgba(255, 255, 255, 0.92);
    border: 1px solid rgba(37, 99, 235, 0.2);
    border-radius: 999px;
    box-shadow: 0 8px 32px rgba(37, 99, 235, 0.15), 0 2px 8px rgba(0,0,0,0.08);
    backdrop-filter: blur(8px);
    animation: badgeIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

body.dark .loading-badge {
    background: rgba(15, 23, 42, 0.92);
    border-color: rgba(37, 99, 235, 0.35);
    box-shadow: 0 8px 32px rgba(37, 99, 235, 0.2), 0 2px 8px rgba(0,0,0,0.4);
}

.loading-spinner {
    width: 20px;
    height: 20px;
    border: 2.5px solid rgba(37, 99, 235, 0.2);
    border-top: 2.5px solid var(--primary);
    border-radius: 50%;
    animation: spin 0.75s linear infinite;
    flex-shrink: 0;
}

body.dark .loading-spinner {
    border-color: rgba(37, 99, 235, 0.25);
    border-top-color: #60a5fa;
}

.loading-label {
    color: var(--primary);
    font-weight: 700;
    font-size: 0.8rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    white-space: nowrap;
}

body.dark .loading-label {
    color: #60a5fa;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes overlayIn {
    from { opacity: 0; backdrop-filter: blur(0px); }
    to { opacity: 1; backdrop-filter: blur(8px); }
}

/* --- Estilos de Impresión (Dashboard y Reportes) --- */
@media print {
    .sidebar, .nav-section, .sidebar-footer, .header, .toast-container, button, .btn {
        display: none !important;
    }

    /* Reset de contenedores principales para permitir múltiples páginas */
    html, body, #root, .app-container, .main-content {
        height: auto !important;
        min-height: auto !important;
        overflow: visible !important;
        display: block !important;
        position: static !important;
    }

    /* Eliminar scrolls internos de React */
    .main-content > div, 
    .history-groups-container, 
    .dashboard-container {
        overflow: visible !important;
        height: auto !important;
        position: static !important;
    }

    body {
        background-color: white !important;
        padding: 0 !important;
        margin: 0 !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .main-content {
        margin: 0 !important;
        padding: 1.5cm !important;
        width: 100% !important;
    }

    .dashboard-container {
        padding: 0 !important;
        animation: none !important;
    }

    .card {
        box-shadow: none !important;
        border: 1px solid #eee !important;
        break-inside: avoid;
        margin-bottom: 2rem !important;
        background-color: white !important;
        page-break-inside: avoid;
    }

    .dashboard-grid {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 1rem !important;
    }

    canvas {
        max-width: 100% !important;
        height: auto !important;
        display: block !important;
    }

    /* Forzar modo claro en impresión si el usuario está en modo oscuro */
    body.dark { background: white !important; color: black !important; }
    body.dark .card { background: white !important; border-color: #ddd !important; }
    body.dark h1, body.dark h2, body.dark h3, body.dark p, body.dark span, body.dark td, body.dark th {
        color: black !important;
    }
}

@keyframes badgeIn {
    from { opacity: 0; transform: scale(0.85) translateY(8px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* --- PATIENT PORTAL STYLES --- */

.portal-page-wrapper {
    min-height: 100vh;
    font-family: var(--portal-font);
    background-color: var(--portal-bg-light);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Login - Split Screen Style */
.portal-login-container {
    display: flex;
    flex: 1;
    min-height: 100vh;
    background-color: var(--portal-dark);
}

/* ---- PORTAL SLIDER / HERO ---- */
.portal-hero-section {
    flex: 1.2;
    position: relative;
    display: flex;
    align-items: flex-end;
    justify-content: flex-start;
    overflow: hidden;
    min-height: 100vh;
}

/* Each slide sits stacked, absolute */
.portal-slide {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    opacity: 0;
    transition: opacity 1.2s ease-in-out;
    will-change: transform, opacity;
}

.portal-slide.active {
    opacity: 1;
    animation: kenBurns 8s ease-in-out forwards;
}

.portal-slide.prev {
    opacity: 0;
    animation: none;
}

/* Ken Burns: slow zoom in */
@keyframes kenBurns {
    0%   { transform: scale(1);    }
    100% { transform: scale(1.12); }
}

/* Dark gradient overlay at bottom */
.portal-hero-section::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
        to right,
        rgba(26, 26, 46, 0.65) 0%,
        rgba(26, 26, 46, 0.3)  60%,
        rgba(26, 26, 46, 0.15) 100%
    );
    z-index: 2;
    pointer-events: none;
}

/* Slide indicator dots */
.portal-slide-dots {
    position: absolute;
    bottom: 24px;
    left: 40px;
    display: flex;
    gap: 8px;
    z-index: 10;
}

.portal-slide-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: rgba(255,255,255,0.4);
    transition: all 0.3s;
    cursor: pointer;
    border: none;
    padding: 0;
}

.portal-slide-dot.active {
    width: 24px;
    border-radius: 4px;
    background: #e65c00;
}

.portal-hero-section::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0);
}

/* Portal Tabs Menu on Hero */
.portal-nav-tabs-overlay {
    position: absolute;
    top: 25%;
    left: 40px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    z-index: 5;
}

.portal-nav-tab {
    padding: 12px 25px;
    border: 1px solid var(--portal-gold);
    background: rgba(0, 0, 0, 0.3);
    color: var(--portal-gold);
    text-transform: uppercase;
    font-weight: 700;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.3s;
    text-align: center;
    min-width: 180px;
    letter-spacing: 1px;
}

.portal-nav-tab.active {
    background: var(--portal-bg-light);
    color: var(--portal-dark);
    border-color: var(--portal-bg-light);
}

.portal-nav-tab:hover:not(.active) {
    background: rgba(230, 92, 0, 0.15);
}

.portal-login-card {
    flex: 1;
    background-color: var(--portal-bg-light);
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 4rem;
    max-width: 550px;
    box-shadow: -10px 0 30px rgba(0,0,0,0.1);
}

.portal-login-header {
    text-align: center;
    margin-bottom: 3rem;
}

.portal-login-header h1 {
    color: var(--portal-dark);
    font-size: 1.8rem;
    font-weight: 800;
    margin-bottom: 1rem;
    text-transform: uppercase;
}

.portal-login-header p {
    color: #666;
    font-size: 0.9rem;
}

.portal-form .form-group {
    margin-bottom: 1.5rem;
}

.portal-form label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--portal-dark);
    text-transform: uppercase;
}

.portal-form input {
    width: 100%;
    padding: 12px 15px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 1rem;
    transition: border-color 0.2s;
}

.portal-form input:focus {
    border-color: var(--portal-gold);
    outline: none;
}

.password-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.password-toggle-btn {
    position: absolute;
    right: 12px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.2rem;
    color: #666;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    z-index: 5;
}

.portal-btn-primary {
    width: 100%;
    padding: 14px;
    background-color: var(--portal-gold);
    color: var(--portal-dark);
    border: none;
    font-weight: 700;
    font-size: 1rem;
    text-transform: uppercase;
    cursor: pointer;
    margin-top: 1rem;
    transition: background 0.2s;
}

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

.portal-error {
    background: #ffeeee;
    color: #cc0000;
    padding: 10px;
    border-left: 4px solid #cc0000;
    margin-bottom: 1.5rem;
    font-size: 0.85rem;
}

/* Dashboard Styles */
.portal-dashboard {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}

.portal-header {
    background-color: var(--portal-dark);
    color: var(--portal-text-on-dark);
    padding: 1rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 4px solid var(--portal-primary);
}

.portal-header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.clinical-logo-portal {
    color: var(--portal-gold);
    font-size: 1.5rem;
    font-weight: 900;
    margin-right: 10px;
}

.patient-name {
    margin-right: 1.5rem;
    color: #64748b;
}

.btn-logout-portal {
    background: #f1f5f9;
    color: #64748b;
    border: none;
    padding: 8px 16px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-logout-portal:hover {
    background: #fee2e2;
    color: #dc2626;
}

.portal-main {
    padding: 2rem;
    background-color: #f8f9fa;
    flex: 1;
}

.portal-content-card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.05);
    padding: 2rem;
}

.portal-filters {
    margin-bottom: 2.5rem;
}

.portal-filters h3 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 1.5rem;
}

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

.filter-item label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 600;
    color: #64748b;
    margin-bottom: 0.5rem;
}

.filter-item input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    font-size: 0.9375rem;
}

.btn-filter {
    background: #2563eb;
    color: white;
    border: none;
    padding: 10px 24px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}

.portal-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 2rem;
}

.portal-table th {
    background-color: #1565c0;
    color: var(--portal-text-on-dark);
    text-align: left;
    padding: 1rem;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.portal-table td {
    padding: 1rem;
    border-bottom: 1px solid #eee;
    font-size: 0.9rem;
}

.portal-table tr td:first-child {
    border-radius: 12px 0 0 12px;
}

.portal-table tr td:last-child {
    border-radius: 0 12px 12px 0;
}

.actions-cell {
    display: flex;
    gap: 0.75rem;
}

.btn-view-portal, .btn-download-portal {
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-view-portal {
    background: #eff6ff;
    color: #2563eb;
    border: 1px solid #dbeafe;
}

.btn-view-portal:hover {
    background: #dbeafe;
}

.btn-download-portal {
    background: #f0fdf4;
    color: #16a34a;
    border: 1px solid #dcfce7;
}

.btn-download-portal:hover {
    background: #dcfce7;
}

.portal-footer {
    text-align: center;
    padding: 3rem;
    color: #94a3b8;
    font-size: 0.875rem;
}

@media (max-width: 768px) {
    .portal-login-container {
        flex-direction: column;
    }
    .portal-login-card {
        padding: 2rem;
        max-width: 100%;
        order: 2;
    }
    .portal-hero-section {
        min-height: 250px;
        order: 1;
    }
    .hero-content h2 {
        font-size: 2rem;
    }
}

/* --- Action Buttons (Historial) --- */
.btn-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    border: 1px solid transparent;
}

.btn-action:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    filter: brightness(0.95);
}

.btn-action:active {
    transform: translateY(0);
}

/* Tooltip standard behavior is handled by browser 'title', 
   but we can enhance button feel with these specific hovers */
.btn-view:hover { background-color: #dbeafe !important; border-color: #2563eb !important; color: #1d4ed8 !important; }
.btn-download:hover { background-color: #f1f5f9 !important; border-color: #475569 !important; color: #334155 !important; }
.btn-whatsapp:hover { background-color: #dcfce7 !important; border-color: #16a34a !important; color: #15803d !important; }
.btn-delete:hover { background-color: #fee2e2 !important; border-color: #dc2626 !important; color: #b91c1c !important; }

/* Spinner for actions */
.loading-spinner-tiny {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(0, 0, 0, 0.1);
    border-left-color: currentColor;
    border-radius: 50%;
    animation: spin-tiny 0.8s linear infinite;
}

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

/* Dark mode adjustments for action buttons */
body.dark .btn-action {
    background-color: #1e293b !important;
    border-color: #334155 !important;
}

body.dark .btn-view { color: #60a5fa !important; }
body.dark .btn-download { color: #94a3b8 !important; }
body.dark .btn-whatsapp { color: #4ade80 !important; }
body.dark .btn-delete { color: #f87171 !important; }

body.dark .btn-view:hover { background-color: #1e3a8a !important; border-color: #3b82f6 !important; }
body.dark .btn-download:hover { background-color: #334155 !important; border-color: #64748b !important; }
body.dark .btn-whatsapp:hover { background-color: #064e3b !important; border-color: #22c55e !important; }
body.dark .btn-delete:hover { background-color: #7f1d1d !important; border-color: #ef4444 !important; }
