:root {
    --primary: #2C3E50;
    --secondary: #34495E;
    --accent: #3498DB;
    --accent-light: #EBF5FB;
    --bg-color: #F8F9F9;
    --text-color: #2C3E50;
    --text-light: #7F8C8D;
    --border-color: #E5E8E8;
    --white: #FFFFFF;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
    --radius: 12px;
    --font-main: 'Inter', sans-serif;
}

/* Calendar Event Selection */
.event-card.selected,
.month-event.selected {
    box-shadow: 0 0 0 3px #E74C3C !important;
    border: 2px solid #E74C3C !important;
    transform: scale(1.05);
    z-index: 10;
}

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

body {
    font-family: var(--font-main);
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    overflow: hidden;
}

/* Layout */
.app-container {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 260px;
    background: var(--white);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 2rem 1rem;
    z-index: 10;
}

.main-content {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    background: var(--bg-color);
}

/* Sidebar Components */
.logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: 2px;
    margin-bottom: 3rem;
    color: var(--primary);
    line-height: 1.2;
}

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

.nav-btn {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.8rem 1rem;
    border: none;
    background: transparent;
    color: var(--text-light);
    font-family: var(--font-main);
    font-weight: 500;
    font-size: 0.95rem;
    cursor: pointer;
    border-radius: var(--radius);
    transition: all 0.2s ease;
    text-align: left;
}

.nav-btn:hover,
.nav-btn.active {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 600;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.avatar {
    width: 40px;
    height: 40px;
    background: var(--primary);
    color: var(--white);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.9rem;
}

.info .name {
    font-weight: 600;
    font-size: 0.9rem;
}

.info .role {
    font-size: 0.8rem;
    color: var(--text-light);
}

/* Views */
.view {
    display: none;
    animation: fadeIn 0.3s ease;
}

.view.active {
    display: block;
    width: 100%;
    height: 100%;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

/* Modern heading styles with gradient underline */
h1,
h2,
h3 {
    font-weight: 700;
    letter-spacing: -0.5px;
    position: relative;
    padding-bottom: 0.75rem;
    margin-bottom: 1.5rem;
}

h1::after,
h2::after,
h3::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 60px;
    height: 4px;
    background: linear-gradient(90deg, var(--primary), var(--accent));
    border-radius: 2px;
}

h1 {
    font-size: 2.25rem;
}

h2 {
    font-size: 1.8rem;
}

h3 {
    font-size: 1.5rem;
}

.view-header h2 {
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* Buttons */
.btn-primary {
    background: var(--primary);
    color: var(--white);
    border: none;
    padding: 0.8rem 1.5rem;
    border-radius: var(--radius);
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s;
    font-family: var(--font-main);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(44, 62, 80, 0.2);
}

.btn-secondary {
    background: transparent;
    color: var(--text-light);
    border: 1px solid var(--border-color);
    padding: 0.8rem 1.5rem;
    border-radius: var(--radius);
    font-weight: 500;
    cursor: pointer;
    font-family: var(--font-main);
}

/* Calendar Styles */
.calendar-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    background: var(--white);
    padding: 1rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.view-toggle {
    display: flex;
    background: var(--bg-color);
    padding: 4px;
    border-radius: 8px;
}

.toggle-btn {
    border: none;
    background: transparent;
    padding: 6px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    color: var(--text-light);
    font-family: var(--font-main);
}

.toggle-btn.active {
    background: var(--white);
    color: var(--primary);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.date-nav {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-weight: 600;
    font-size: 1.1rem;
}

.date-nav span {
    min-width: 180px;
    text-align: center;
}

.date-nav button {
    background: transparent;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--text-light);
}

.calendar-grid-wrapper {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    min-height: 600px;
}

.calendar-view {
    display: none;
}

.calendar-view.active {
    display: block;
}

/* Week View */
.week-header {
    display: grid;
    grid-template-columns: 60px repeat(5, 1fr) 0.6fr 0.6fr;
    border-bottom: 1px solid var(--border-color);
    background: #FAFAFA;
}

.week-header>div {
    padding: 1rem;
    text-align: center;
    font-weight: 600;
    border-right: 1px solid var(--border-color);
}

.week-body {
    display: grid;
    grid-template-columns: 60px repeat(5, 1fr) 0.6fr 0.6fr;
    /* 15 hours (8-23) */
    grid-template-rows: repeat(15, 45px);
    overflow-y: auto;
    max-height: 70vh;
}

.time-slot {
    border-bottom: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    font-size: 0.8rem;
    color: var(--text-light);
    display: flex;
    align-items: center;
    justify-content: center;
}

.day-column {
    border-right: 1px solid var(--border-color);
    position: relative;
}

.day-cell {
    height: 45px;
    /* Matches grid row height */
    border-bottom: 1px solid #e0e0e0;
    position: relative;
}

.day-cell-alternate {
    background-color: #fcfcfc;
}

.event-card {
    position: absolute;
    left: 4px;
    right: 4px;
    top: 2px;
    bottom: 2px;
    background: var(--accent-light);
    border-left: 3px solid var(--accent);
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    cursor: pointer;
    overflow: hidden;
    transition: transform 0.1s;
    z-index: 5;
}

.event-card .patient-name {
    display: block;
    font-weight: bold;
}

.event-card .visit-location {
    display: block;
}

.event-card:hover {
    transform: scale(1.02);
    z-index: 10;
}

/* Month View */
.month-header {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    background: #FAFAFA;
    border-bottom: 1px solid var(--border-color);
}

.month-header>div {
    padding: 1rem;
    text-align: center;
    font-weight: 600;
}

.month-body {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    grid-auto-rows: minmax(120px, auto);
}

.month-day {
    border-right: 1px solid var(--border-color);
    border-bottom: 1px solid var(--border-color);
    padding: 0.5rem;
    min-height: 120px;
}

.month-day.empty {
    background: #FAFAFA;
}

.day-number {
    font-weight: 600;
    margin-bottom: 0.5rem;
    display: block;
    text-align: right;
    color: var(--text-light);
}

.month-event {
    background: var(--accent-light);
    color: var(--accent);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.75rem;
    margin-bottom: 2px;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Tables */
.table-container {
    background: white;
    border-radius: 12px;
    padding: 0;
    /* Remove padding to fix sticky header overflow issue */
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    overflow: auto;
    /* Enable scrolling */
    max-height: calc(100vh - 250px);
    /* Force vertical scroll */
    margin-bottom: 2rem;
}

.data-table {
    width: 100%;
    border-collapse: separate;
    /* Required for sticky header border */
    border-spacing: 0;
}

.data-table th {
    /* Sticky positioning */
    position: sticky;
    top: 0;
    z-index: 100;
    /* High z-index to stay on top */

    /* Visual styles */
    background-color: #f8f9fa;
    /* Solid background */
    padding: 16px 24px;
    text-align: left;
    border-bottom: 2px solid #eee;

    /* Typography */
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
}

.data-table td {
    padding: 16px 24px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.data-table tr:hover {
    background: #F8F9F9;
}

/* Modals */
.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: 100;
    backdrop-filter: blur(4px);
}

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

.modal {
    background: var(--white);
    width: 500px;
    max-width: 90%;
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    animation: slideUp 0.3s ease;
}

.modal.hidden {
    display: none;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

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

.close-modal {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-light);
}

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

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

label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.9rem;
}

input,
select {
    width: 100%;
    padding: 0.8rem;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-family: var(--font-main);
    font-size: 0.95rem;
}

input:focus,
select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

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

/* Drawer (Side Panel) */
.drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 500px;
    background: var(--white);
    box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
    z-index: 2000;
    display: none;
    /* Hidden by default */
    flex-direction: column;
}

.drawer.open {
    display: flex !important;
    /* Force visible */
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
    }

    to {
        transform: translateX(0);
    }
}

.drawer-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #FAFAFA;
}

.drawer-content {
    padding: 1.5rem;
    overflow-y: auto;
    flex: 1;
}

.drawer-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 190;
    display: none;
    backdrop-filter: blur(2px);
}

.drawer-overlay.open {
    display: block;
}

/* Event Popover */
.event-popover {
    position: absolute;
    background: var(--white);
    border-radius: 8px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
    padding: 1rem;
    width: 250px;
    z-index: 100;
    display: none;
    border: 1px solid var(--border-color);
}

.event-popover.visible {
    display: block;
}

.popover-header {
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--primary);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
}

.popover-body {
    font-size: 0.9rem;
    color: var(--text-color);
    margin-bottom: 1rem;
}

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

.btn-danger {
    background: #E74C3C;
    color: white;
    border: none;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
}

/* Tooltip */
.custom-tooltip {
    position: absolute;
    background: rgba(44, 62, 80, 0.9);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    font-size: 0.85rem;
    pointer-events: none;
    z-index: 1000;
    white-space: pre-line;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    display: none;
}

.custom-tooltip.visible {
    display: block;
}

/* Accordion Row */
.patient-row {
    cursor: pointer;
    transition: background-color 0.2s;
}

.patient-row:hover {
    background-color: #f8f9fa;
}

.patient-row.active {
    background-color: #e8f6f3;
}

.detail-row {
    display: none;
    background-color: #fcfcfc;
}

.detail-row.visible {
    display: table-row;
}

.detail-container {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.02);
}

/* Patient rows should be visible */
tr.patient-row {
    background-color: white;
    cursor: pointer;
    transition: background-color 0.2s;
}

tr.patient-row:hover {
    background-color: #f8f9fa;
}

tr.patient-row.active {
    background-color: #e8f6f3;
}

tr.patient-row td {
    padding: 12px;
    border-bottom: 1px solid #eee;
}

/* =========================================
   MEDIUM SCREEN STYLES (Compact Tables)
   ========================================= */
@media (max-width: 1200px) {

    #patients .data-table th,
    #patients .data-table td {
        padding: 4px 2px !important;
        /* Minimal padding */
        font-size: 0.75rem;
        /* Smaller font */
    }

    #patients .data-table th {
        white-space: normal !important;
        /* Allow text wrap */
        vertical-align: bottom;
        line-height: 1.1;
        height: auto;
    }

    /* Force specific columns to wrap if needed */
    #patients .data-table th:nth-child(3) {
        /* Observación General */
        max-width: 100px;
    }
}

/* =========================================
   MOBILE RESPONSIVE STYLES
   ========================================= */
@media (max-width: 768px) {

    /* --- PATIENTS: Only table scrolls --- */
    #patients .table-container {
        max-height: calc(100vh - 200px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* --- EXPENSE DRAWER: Compact form --- */
    #expense-drawer .drawer-content {
        padding: 1rem !important;
    }

    #expense-drawer .form-group {
        margin-bottom: 0.75rem !important;
    }

    #expense-drawer .modal-actions {
        margin-top: 1rem !important;
        padding-top: 0 !important;
    }

    /* Layout */
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        /* Hide sidebar */
        height: 100vh;
        width: 260px;
        transition: left 0.3s ease;
        box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
        z-index: 999;
    }

    .sidebar.active {
        left: 0;
    }

    .main-content {
        padding: 1rem;
        width: 100%;
        margin-top: 3rem;
        /* Space for mobile menu btn */
    }

    /* Mobile Menu Button */
    #mobile-menu-btn {
        display: block !important;
    }

    /* Tables */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 8px;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    }

    .data-table {
        min-width: 600px;
        /* Force scroll on small screens */
    }

    .data-table th,
    .data-table td {
        padding: 0.75rem 0.5rem;
        font-size: 0.85rem;
    }

    /* Headers */
    .view-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .view-header h2 {
        font-size: 1.5rem;
        margin-bottom: 0.5rem;
    }

    /* Modals */
    #new-expense-modal,
    #patient-modal,
    #invoice-modal,
    #categories-modal {
        width: 95% !important;
        max-width: 95% !important;
        padding: 1.5rem !important;
        max-height: 90vh;
        overflow-y: auto;
    }

    /* Finances Controls */
    .finances-controls {
        flex-direction: column;
        align-items: stretch !important;
    }

    .finances-controls button {
        width: 100%;
        justify-content: center;
    }

    #quarter-total {
        margin-right: 0 !important;
        margin-bottom: 1rem;
        text-align: center;
    }

    /* Dashboard Cards */
    .kpi-grid {
        grid-template-columns: 1fr !important;
    }

    /* Calendar */
    .calendar-header {
        flex-direction: column;
        gap: 1rem;
    }

    .calendar-grid {
        font-size: 0.8rem;
    }

    .calendar-day {
        min-height: 80px;
    }
}

/* Medium screens (tablet/half desktop) */
@media (max-width: 1200px) and (min-width: 1025px) {

    /* Week View Adjustments */
    .week-header {
        grid-template-columns: 50px repeat(5, 1fr) 0.5fr 0.5fr;
    }

    .week-header>div {
        padding: 0.5rem 0.25rem;
        font-size: 0.85rem;
    }

    .week-body {
        grid-template-columns: 50px repeat(5, 1fr) 0.5fr 0.5fr;
        grid-template-rows: repeat(15, 40px);
    }

    .time-slot {
        font-size: 0.7rem;
    }

    .day-cell {
        height: 40px;
    }

    .event-card {
        font-size: 0.7rem;
        padding: 2px 4px;
    }

    /* Month View Adjustments */
    .month-header>div {
        padding: 0.5rem 0.25rem;
        font-size: 0.85rem;
    }

    .month-day {
        min-height: 90px;
        padding: 0.25rem;
    }

    .day-number {
        font-size: 0.85rem;
    }

    .month-event {
        font-size: 0.7rem;
        padding: 2px 4px;
    }
}

/* Note: Screens <= 1024px use mobile calendar interface */

/* Hide top date navigation in Month View on medium screens (redundant with mobile calendar) */
@media (max-width: 1024px) {
    body.calendar-month-view .date-nav {
        display: none !important;
    }
}

/* Mobile Calendar Fixes - Fallback styles */
@media (max-width: 768px) {

    /* Force horizontal scroll for calendar to preserve layout */
    .calendar-grid-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border: 1px solid var(--border-color);
        border-radius: 8px;
    }

    .month-header,
    .month-body,
    .week-header,
    .week-body {
        min-width: 700px;
        /* Ensure days have enough width */
    }

    .month-day {
        min-height: 100px;
        /* Slightly smaller height */
        padding: 0.25rem;
    }

    .day-number {
        font-size: 0.9rem;
        margin-bottom: 0.25rem;
    }

    .month-event {
        font-size: 0.7rem;
        padding: 1px 4px;
        white-space: nowrap;
    }

    /* Adjust header text for mobile */
    .month-header>div {
        padding: 0.5rem;
        font-size: 0.9rem;
    }

    /* Hide view toggle (Week/Month) on mobile - force month view only */
    /* Hide desktop calendar controls on mobile (use mobile interface instead) */
    .calendar-controls {
        display: none !important;
    }
}

/* =========================================
   TABLET/MEDIUM SCREEN CALENDAR STYLES
   Hybrid: Week=Desktop scroll, Month=Mobile
   ========================================= */
@media (max-width: 1024px) {

    /* Show Calendar Controls for view switching */
    .calendar-controls {
        display: flex !important;
        flex-wrap: wrap;
        gap: 0.5rem;
        justify-content: center;
    }

    .calendar-controls button {
        padding: 0.5rem 1rem;
        font-size: 0.85rem;
    }

    /* Week view - compact desktop style */
    .calendar-grid-wrapper {
        display: block !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        min-height: auto;
    }

    /* Week header - compact all 8 columns (time + 7 days) */
    .week-header {
        display: grid !important;
        grid-template-columns: 45px repeat(5, 1fr) 0.5fr 0.5fr !important;
        min-width: 100%;
    }

    .week-header>div {
        padding: 0.4rem 0.1rem !important;
        font-size: 0.7rem !important;
        text-align: center;
        border-right: 1px solid var(--border-color) !important;
        border-bottom: 1px solid var(--border-color) !important;
    }

    /* Week body - taller rows for better visibility */
    .week-body {
        display: grid !important;
        grid-template-columns: 45px repeat(5, 1fr) 0.5fr 0.5fr !important;
        grid-template-rows: repeat(15, 50px) !important;
        max-height: 65vh;
        overflow-y: auto;
        min-width: 100%;
    }

    /* Time slots */
    .time-slot {
        font-size: 0.7rem !important;
        border-right: 1px solid var(--border-color) !important;
        border-bottom: 1px solid var(--border-color) !important;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Day columns with borders */
    .day-column {
        border-right: 1px solid var(--border-color) !important;
    }

    .day-cell {
        height: 50px !important;
        border-bottom: 1px solid #e0e0e0 !important;
    }

    /* Event cards - allow more content */
    /* Event cards - compact text to fit content */
    .event-card {
        font-size: 0.6rem !important;
        padding: 1px 2px !important;
        white-space: normal !important;
        /* Allow text wrap */
        line-height: 1.0 !important;
        display: flex;
        flex-direction: column;
        justify-content: center;
        overflow: hidden;
    }

    .event-card .patient-name {
        font-weight: bold;
        margin-bottom: 0 !important;
        display: block;
    }

    .event-card .visit-location {
        display: none !important;
    }

    /* Month view - hide desktop version, use mobile calendar for month */
    #month-view {
        display: none !important;
    }

    /* Show Mobile Calendar Interface for month view on medium screens */
    #mobile-calendar-interface {
        display: block !important;
    }

    /* Week view should be visible on medium screens */
    body.calendar-week-view .calendar-grid-wrapper {
        display: block !important;
    }

    body.calendar-week-view #mobile-calendar-interface {
        display: none !important;
    }

    /* Month view uses mobile calendar */
    body.calendar-month-view .calendar-grid-wrapper {
        display: none !important;
    }

    body.calendar-month-view #mobile-calendar-interface {
        display: block !important;
    }
}

/* Mobile Day Styles */
.mob-day {
    aspect-ratio: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    position: relative;
    transition: all 0.2s;
}

.mob-day.empty {
    pointer-events: none;
}

.mob-day.has-events::after {
    content: '';
    width: 4px;
    height: 4px;
    background-color: var(--accent);
    border-radius: 50%;
    position: absolute;
    bottom: 4px;
}

.mob-day.selected {
    background-color: var(--primary);
    color: white;
    font-weight: bold;
}

.mob-day.selected::after {
    background-color: white;
}

.mob-day.today {
    border: 1px solid var(--primary);
    color: var(--primary);
}

.mob-day.today.selected {
    color: white;
}

/* Mobile Event Card */
.mob-event-card {
    background: #f8f9fa;
    border-left: 3px solid var(--accent);
    padding: 0.8rem;
    margin-bottom: 0.8rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
}

.mob-event-card.selected {
    background: #e3f2fd;
    border: 1px solid #3498db;
    border-left: 4px solid #3498db;
}

.mob-event-time {
    font-weight: bold;
    font-size: 0.9rem;
    color: var(--primary);
    margin-bottom: 0.2rem;
}

.mob-event-title {
    font-size: 0.95rem;
}
}

/* =========================================
   IMPROVED MOBILE STYLES (Step-by-Step Fixes)
   ========================================= */
@media (max-width: 768px) {

    /* 1. General Layout & Headers */
    .main-content {
        padding: 1rem;
        margin-top: 60px;
        /* Ensure space for fixed header/menu button */
    }

    .view-header {
        flex-direction: column;
        align-items: stretch;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .view-header h2,
    .view-header h3 {
        text-align: center;
        font-size: 1.5rem;
        margin-bottom: 0.5rem;
    }

    .view-header button,
    .view-header .btn-primary,
    .view-header .btn-secondary {
        width: 100%;
        justify-content: center;
    }

    /* Fix for button groups in headers */
    .view-header>div {
        display: flex;
        flex-direction: column;
        width: 100%;
        gap: 0.5rem;
    }

    /* 2. Finances & Expenses Controls */
    .finances-controls {
        flex-direction: column;
        align-items: stretch !important;
        gap: 1rem;
    }

    .finances-controls select,
    .finances-controls input {
        width: 100%;
    }

    /* The container that pushes buttons to the right in desktop */
    .finances-controls>div[style*="margin-left: auto"] {
        margin-left: 0 !important;
        flex-direction: column;
        width: 100%;
        align-items: stretch !important;
    }

    .finances-controls button {
        width: 100%;
        margin-bottom: 0.5rem;
    }

    #expenses-total,
    #quarter-total {
        text-align: center;
        margin: 0.5rem 0;
        font-size: 1.1rem;
    }

    /* 3. Tables */
    .table-container {
        margin-left: -1rem;
        margin-right: -1rem;
        width: calc(100% + 2rem);
        border-radius: 0;
        box-shadow: none;
        border-top: 1px solid #eee;
        border-bottom: 1px solid #eee;
    }

    .data-table {
        font-size: 0.85rem;
    }

    .data-table th,
    .data-table td {
        padding: 0.75rem 1rem;
        white-space: nowrap;
        /* Force horizontal scroll */
    }

    /* 4. Modals (Full Screen on Mobile) */
    .modal,
    #new-tax-modal,
    #new-visit-modal,
    #new-income-modal {
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        top: 0 !important;
        left: 0 !important;
        transform: none !important;
        border-radius: 0 !important;
        display: flex;
        flex-direction: column;
        padding: 1rem !important;
    }

    /* Ensure modal content scrolls */
    .modal form,
    #form-visit-drawer,
    #form-new-tax {
        flex: 1;
        overflow-y: auto;
        padding-bottom: 80px;
        /* Space for actions */
    }

    .modal-actions {
        margin-top: auto;
        flex-direction: column-reverse;
        /* Cancel at bottom */
        gap: 0.8rem;
        padding-top: 1rem;
        background: white;
        /* Sticky bottom actions */
        position: sticky;
        bottom: 0;
        z-index: 10;
    }

    .modal-actions button {
        width: 100%;
        margin: 0 !important;
        padding: 1rem;
    }

    /* 5. Calendar Specifics */
    .calendar-controls {
        flex-direction: column;
        gap: 1rem;
    }

    .date-nav {
        justify-content: center;
        width: 100%;
    }

    .view-toggle {
        width: 100%;
        justify-content: center;
    }

    .toggle-btn {
        flex: 1;
        text-align: center;
    }

    /* 6. Dashboard / Summary */
    .kpi-grid {
        grid-template-columns: 1fr !important;
        /* Single column cards */
        gap: 1rem;
    }

    .chart-container {
        height: 300px;
        /* Fixed height for chart */
    }
}

/* FIX MOBILE MODAL SCROLLING */
@media (max-width: 768px) {

    #new-visit-modal.active,
    #new-expense-modal.active,
    #new-tax-modal.active,
    #new-income-modal.active,
    #patient-modal.active {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        transform: none !important;
        border-radius: 0 !important;
        padding: 0 !important;
        display: flex !important;
        /* Override inline display: block when visible */
        flex-direction: column !important;
        overflow: hidden !important;
        /* Prevent double scrollbar */
    }

    /* Header fixed at top */
    #new-visit-modal>div:first-child,
    #new-expense-modal>div:first-child,
    #new-tax-modal>div:first-child {
        padding: 1rem;
        background: white;
        z-index: 20;
        border-bottom: 1px solid #eee;
        flex-shrink: 0;
    }

    /* Scrollable Content */
    #form-visit-drawer,
    #form-simple-expense,
    #form-new-tax,
    #form-new-income {
        flex: 1;
        overflow-y: auto !important;
        padding: 1rem;
        padding-bottom: 100px;
        /* Extra space for bottom actions */
        -webkit-overflow-scrolling: touch;
    }

    /* Actions fixed at bottom */
    .modal-actions,
    #form-visit-drawer>div:last-child,
    /* Actions container in visit form */
    #form-simple-expense>div:last-child {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        padding: 1rem;
        border-top: 1px solid #eee;
        z-index: 20;
        display: flex;
        gap: 1rem;
    }

    .modal-actions button,
    #form-visit-drawer>div:last-child button,
    #form-simple-expense>div:last-child button {
        flex: 1;
        margin: 0 !important;
    }
}

/* DESKTOP BUTTON ICONS (Global - Icon Only) */
#btn-edit-patient,
#btn-delete-patients,
#btn-email-patient {
    /* Removed display: inline-flex !important so JS can control visibility */
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 0 !important;
    /* Hide text */
    padding: 0 !important;
    width: 38px !important;
    height: 38px !important;
    border-radius: 6px !important;
    margin-right: 0.25rem;
    /* Spacing between buttons */
}

/* When JS sets display: block/inline-block, we force flex to center icon */
#btn-edit-patient[style*="display: block"],
#btn-edit-patient[style*="display: inline-block"],
#btn-delete-patients[style*="display: block"],
#btn-delete-patients[style*="display: inline-block"],
#btn-email-patient[style*="display: block"],
#btn-email-patient[style*="display: inline-block"] {
    display: inline-flex !important;
}

/* Common Icon Style */
#btn-edit-patient::after,
#btn-delete-patients::after,
#btn-email-patient::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
}

/* Modificar Icon */
#btn-edit-patient::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z' /%3E%3C/svg%3E");
}

/* Eliminar Icon */
#btn-delete-patients::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16' /%3E%3C/svg%3E");
}

/* Email Icon */
#btn-email-patient::after {
    /* Dark grey for contrast on light blue button */
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23333' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z' /%3E%3C/svg%3E");
}

/* =========================================
   GLOBAL EDIT/DELETE BUTTON ICON STYLES
   All sections: Patients, Finances, Expenses, Summary
   ========================================= */

/* All Edit Buttons - Icon Only */
#btn-edit-invoice,
#btn-modify-expense,
#btn-modify-tax,
#btn-edit-selected-visit {
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 0 !important;
    padding: 0 !important;
    width: 38px !important;
    height: 38px !important;
    border-radius: 6px !important;
    margin-right: 0.25rem;
    background: #3498db !important;
    border: none !important;
    cursor: pointer;
}

#btn-edit-invoice[style*="display: block"],
#btn-edit-invoice[style*="display: inline-block"],
#btn-modify-expense[style*="display: block"],
#btn-modify-expense[style*="display: inline-block"],
#btn-modify-tax[style*="display: block"],
#btn-modify-tax[style*="display: inline-block"],
#btn-edit-selected-visit[style*="display: block"],
#btn-edit-selected-visit[style*="display: inline-block"] {
    display: inline-flex !important;
}

#btn-edit-invoice::after,
#btn-modify-expense::after,
#btn-modify-tax::after,
#btn-edit-selected-visit::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z' /%3E%3C/svg%3E");
}

/* All Delete Buttons - Icon Only */
#btn-delete-invoices,
#btn-delete-expenses,
#btn-delete-tax,
#btn-delete-selected-visit {
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 0 !important;
    padding: 0 !important;
    width: 38px !important;
    height: 38px !important;
    border-radius: 6px !important;
    margin-right: 0.25rem;
    background: #e74c3c !important;
    border: none !important;
    cursor: pointer;
}

#btn-delete-invoices[style*="display: block"],
#btn-delete-invoices[style*="display: inline-block"],
#btn-delete-expenses[style*="display: block"],
#btn-delete-expenses[style*="display: inline-block"],
#btn-delete-tax[style*="display: block"],
#btn-delete-tax[style*="display: inline-block"],
#btn-delete-selected-visit[style*="display: block"],
#btn-delete-selected-visit[style*="display: inline-block"] {
    display: inline-flex !important;
}

#btn-delete-invoices::after,
#btn-delete-expenses::after,
#btn-delete-tax::after,
#btn-delete-selected-visit::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16' /%3E%3C/svg%3E");
}


/* MOBILE PATIENTS LIST OPTIMIZATION */
@media (max-width: 768px) {

    /* Hide Table Headers */
    #patients .data-table thead {
        display: none;
    }

    /* Patient Row as Grid Card */
    #patients .data-table tr.patient-row {
        display: grid;
        grid-template-columns: 40px 1fr auto;
        /* Checkbox | Info | Badge */
        grid-template-areas:
            "check name badge"
            "check obs obs"
            "check date date";
        padding: 1rem 0.5rem;
        border-bottom: 1px solid #eee;
        background: white;
        align-items: center;
        gap: 0.5rem;
        width: 100%;
    }

    /* 1. Checkbox */
    #patients .data-table tr.patient-row td:nth-child(1) {
        grid-area: check;
        padding: 0;
        border: none;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        height: 100%;
        padding-top: 4px;
    }

    /* 2. Name */
    #patients .data-table tr.patient-row td:nth-child(2) {
        grid-area: name;
        padding: 0;
        border: none;
        font-size: 1.1rem;
        font-weight: 700;
        color: #2C3E50;
        white-space: normal;
        /* Allow wrap */
    }

    /* 3. Observation */
    #patients .data-table tr.patient-row td:nth-child(3) {
        grid-area: obs;
        padding: 0.25rem 0;
        border: none;
        font-size: 0.9rem;
        color: #555;
        white-space: normal !important;
        /* Allow wrap */
        max-width: none !important;
        text-align: left !important;
        /* Keep background color but make it subtle or transparent if needed */
        background-color: transparent !important;
    }

    /* Add label for empty observation */
    #patients .data-table tr.patient-row td:nth-child(3):empty::before {
        content: 'Sin observaciones';
        color: #ccc;
        font-style: italic;
    }

    /* 4. Last Visit */
    #patients .data-table tr.patient-row td:nth-child(4) {
        grid-area: date;
        padding: 0;
        border: none;
        font-size: 0.85rem;
        color: #999;
        display: flex;
        align-items: center;
    }

    #patients .data-table tr.patient-row td:nth-child(4)::before {
        content: '📅 Última visita: ';
        margin-right: 4px;
        font-weight: 500;
    }

    /* 5. Consent Badge */
    #patients .data-table tr.patient-row td:nth-child(5) {
        grid-area: badge;
        padding: 0;
        border: none;
        text-align: right;
    }

    /* Detail Row */
    #patients .data-table tr.detail-row {
        display: none;
    }

    #patients .data-table tr.detail-row.visible {
        display: block;
        width: 100%;
        border-bottom: 1px solid #eee;
    }

    #patients .data-table tr.detail-row td {
        display: block;
        width: 100%;
        padding: 0;
        border: none;
    }

    /* Detail Container */
    .detail-container {
        padding: 1rem;
        background: #f8f9fa;
    }

    /* Detail Grid to Single Column */
    .detail-container>div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
        gap: 0.5rem !important;
    }

    .detail-container>div[style*="grid-template-columns"]>div {
        font-size: 0.95rem;
        padding: 0.25rem 0;
        border-bottom: 1px solid #eee;
    }

    /* New Visit Button */
    .detail-container button.btn-primary {
        width: 100% !important;
        padding: 1rem !important;
        font-size: 1rem !important;
        margin-top: 1rem;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }

    /* History List */
    #visits-list {
        max-height: none !important;
        /* Let it expand */
    }
}

/* MOBILE PATIENTS COMPACT & FIXES */
@media (max-width: 768px) {

    /* 1. More Compact Patient Row */
    #patients .data-table tr.patient-row {
        padding: 0.5rem;
        /* Reduced padding */
        gap: 0.25rem;
        grid-template-columns: 30px 1fr auto;
        /* Smaller checkbox column */
    }

    /* Checkbox */
    #patients .data-table tr.patient-row td:nth-child(1) {
        padding-top: 2px;
    }

    /* Name */
    #patients .data-table tr.patient-row td:nth-child(2) {
        font-size: 0.95rem;
        /* Smaller font */
        line-height: 1.2;
    }

    /* Observation */
    #patients .data-table tr.patient-row td:nth-child(3) {
        font-size: 0.8rem;
        line-height: 1.2;
        color: #666;
        margin-top: 2px;
    }

    /* Last Visit */
    #patients .data-table tr.patient-row td:nth-child(4) {
        font-size: 0.75rem;
        margin-top: 2px;
    }

    /* 2. Fix Detail View Overflow */
    .detail-container {
        padding: 0.5rem !important;
        width: 100vw;
        /* Force width to viewport width */
        max-width: 100%;
        box-sizing: border-box;
        margin-left: -0.5rem;
        /* Counteract parent padding if any */
    }

    /* Fix New Visit Button */
    .detail-container button.btn-primary {
        width: 100% !important;
        box-sizing: border-box;
        margin: 0.5rem 0 !important;
    }

    /* 3. Fix Visits History List (Override inline styles) */
    .detail-container div[id^="visits-"]>div {
        flex-direction: column !important;
        align-items: stretch !important;
        padding: 0.75rem !important;
    }

    /* Visit Info */
    .detail-container div[id^="visits-"]>div>div:first-child {
        margin-bottom: 0.5rem;
    }

    /* Visit Actions Buttons */
    .detail-container div[id^="visits-"]>div>div:last-child {
        display: flex !important;
        width: 100%;
        gap: 0.5rem;
        justify-content: space-between;
    }

    .detail-container div[id^="visits-"]>div>div:last-child button {
        flex: 1;
        text-align: center;
        padding: 0.5rem !important;
        height: auto !important;
    }
}

/* MOBILE PATIENTS V3 - FINAL REFINEMENTS */
@media (max-width: 768px) {

    /* 1. Ultra Compact Patient Row */
    #patients .data-table tr.patient-row {
        padding: 0.5rem;
        gap: 0;
        grid-template-columns: 30px 1fr;
        /* Checkbox | Content */
        grid-template-areas:
            "check name"
            "check obs"
            "check date";
        align-items: flex-start;
        /* Align top */
    }

    /* Checkbox */
    #patients .data-table tr.patient-row td:nth-child(1) {
        grid-area: check;
        padding-top: 2px;
        display: flex;
        justify-content: center;
    }

    /* Hide Badge in List (Now in Detail) */
    #patients .data-table tr.patient-row td:nth-child(5) {
        display: none;
    }

    /* Name */
    #patients .data-table tr.patient-row td:nth-child(2) {
        grid-area: name;
        font-size: 0.95rem;
        font-weight: 700;
        margin-bottom: 2px;
        line-height: 1.2;
    }

    /* Observation (Max 2 lines) */
    #patients .data-table tr.patient-row td:nth-child(3) {
        grid-area: obs;
        font-size: 0.8rem;
        line-height: 1.2;
        color: #666;
        margin-bottom: 2px;

        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        white-space: normal !important;
    }

    /* Last Visit */
    #patients .data-table tr.patient-row td:nth-child(4) {
        grid-area: date;
        font-size: 0.75rem;
        color: #999;
        margin-top: 0;
    }

    /* 2. Smaller New Visit Button */
    .detail-container button.btn-primary {
        width: auto !important;
        /* Not full width */
        display: block;
        margin: 0.75rem auto !important;
        /* Centered */
        padding: 0.6rem 1.5rem !important;
        font-size: 0.9rem !important;
        border-radius: 20px !important;
        /* Pill shape */
    }

    /* 3. Detail Container Fixes */
    .detail-container {
        padding: 0.75rem !important;
    }

    /* Ensure Consent Badge in Detail looks good */
    .detail-container div>span[style*="background"] {
        display: inline-block;
        margin-top: 2px;
    }
}

/* MOBILE PATIENTS V4 - ULTRA COMPACT & 2-LINE OBS */
@media (max-width: 768px) {

    /* Row Layout */
    #patients .data-table tr.patient-row {
        padding: 0.4rem 0.25rem;
        /* Minimal padding */
        gap: 0;
        align-items: flex-start;
    }

    /* Name */
    #patients .data-table tr.patient-row td:nth-child(2) {
        margin-bottom: 0 !important;
        padding-bottom: 0 !important;
        line-height: 1.1 !important;
        /* Tighter text */
        font-size: 0.95rem;
    }

    /* Observation (Strict 2 lines) */
    #patients .data-table tr.patient-row td:nth-child(3) {
        margin-top: 2px !important;
        margin-bottom: 2px !important;
        line-height: 1.15 !important;
        font-size: 0.8rem;
        color: #666;

        /* Force wrapping and clamping */
        white-space: normal !important;
        display: -webkit-box !important;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        max-height: 2.4em;
        /* Fallback height limit */
    }

    /* Last Visit */
    #patients .data-table tr.patient-row td:nth-child(4) {
        margin-top: 0 !important;
        padding-top: 0 !important;
        font-size: 0.75rem;
        line-height: 1 !important;
        color: #999;
    }
}

/* MOBILE PATIENTS V5 - FORCE WRAPPING FIX */
@media (max-width: 768px) {

    /* Target the Observation Cell (3rd child) */
    #patients .data-table tr.patient-row td:nth-child(3) {
        /* CRITICAL: Override inline styles */
        white-space: normal !important;
        /* Allow wrapping */
        max-width: none !important;
        /* Remove width limit */
        width: 100% !important;
        /* Take full available width */

        /* Ensure text breaks if needed */
        word-wrap: break-word !important;
        word-break: break-word !important;
        overflow-wrap: break-word !important;

        /* Line Clamping (2 lines max) */
        display: -webkit-box !important;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden !important;
        text-overflow: ellipsis !important;

        /* Reset height constraints */
        height: auto !important;
        min-height: 0 !important;

        /* Visuals */
        color: #555 !important;
        margin-top: 4px !important;
        margin-bottom: 4px !important;
        line-height: 1.3 !important;
        /* Slightly more breathing room for lines */
    }
}

/* === OBSERVATION CELL BASE STYLES === */
.observation-cell {
    max-width: 250px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* MOBILE PATIENTS V12 - RESTORED (ALIGNMENT FIX) */
@media (max-width: 768px) {

    /* === 1. EMOJI ACTION BUTTONS === */
    #patients .search-bar {
        flex-wrap: wrap;
        gap: 0.5rem !important;
    }

    #patients .search-bar input {
        width: 100% !important;
        order: 1;
        margin-bottom: 0.25rem;
    }

    /* Common button styles */
    #patients .search-bar button {
        order: 2;
        flex: 1;
        /* font-size: 0 inherited from global */
        padding: 0 !important;
        height: 40px !important;
        /* Taller for touch */
        min-width: 40px;
        width: auto !important;
        /* Override fixed width */
        border-radius: 8px !important;
        text-align: center;
        margin-right: 0 !important;
    }

    /* Modificar -> Pencil */
    #btn-edit-patient {
        background-color: #3498db !important;
    }

    /* Eliminar -> Trash */
    #btn-delete-patients {
        background-color: #e74c3c !important;
    }

    /* Email -> Envelope */
    #btn-email-patient {
        background-color: #A8C4E6 !important;
    }

    /* === 2. OBSERVATION BOX FIX === */
    /* Ensure table layout doesn't constrain us */
    #patients .data-table {
        display: block !important;
        /* Break table structure */
        white-space: normal !important;
        width: 100% !important;
        overflow-x: hidden !important;
    }

    #patients .data-table tbody {
        display: block !important;
        width: 100% !important;
    }

    #patients .data-table tr.patient-row {
        display: grid !important;
        grid-template-columns: 30px 1fr !important;
        width: 100% !important;
        box-sizing: border-box;
        height: auto !important;
        padding: 0.5rem !important;
        border-bottom: 1px solid #eee;
        align-items: center !important;
        /* Ensure vertical alignment */
    }

    /* Observation Cell - Targeted via Class */
    #patients .data-table tr.patient-row td.observation-cell {
        grid-column: 2 / -1;

        /* FIXED DIMENSIONS REQUESTED BY USER */
        width: 265px !important;
        min-width: 265px !important;
        max-width: 265px !important;

        height: 45px !important;
        min-height: 45px !important;
        max-height: 45px !important;

        box-sizing: border-box !important;

        /* Box Style */
        background-color: #f8f9fa !important;
        border: 1px solid #e9ecef !important;
        border-radius: 6px !important;
        padding: 0.25rem 0.5rem !important;
        /* Reduced padding for small height */
        margin-top: 0.25rem !important;
        margin-bottom: 0.5rem !important;
        margin-right: 0 !important;

        /* Content Handling */
        white-space: normal !important;
        overflow-y: auto !important;
        /* Scroll if text is too long */
        overflow-x: hidden !important;
        word-wrap: break-word !important;

        /* Typography */
        font-size: 0.8rem !important;
        line-height: 1.2 !important;
        /* Tighter line height */
        color: #444 !important;
        text-align: left !important;

        /* Centering in Grid if needed, though grid-column handles pos */
        justify-self: start;
    }
}


/* MOBILE FINANCES - QUARTERLY INVOICES CARD LAYOUT (V3 - FULL WIDTH) */
@media (max-width: 768px) {

    /* Reduce container padding to maximize space */
    #finances .table-container {
        padding: 0.5rem !important;
        /* Minimal padding */
        background: transparent !important;
        /* Remove white bg of container */
        box-shadow: none !important;
        /* Remove container shadow */
    }

    /* Hide Table Headers in Finances */
    #finances .data-table thead {
        display: none;
    }

    /* Table Structure Reset */
    #finances .data-table {
        display: block;
        width: 346px !important;
        /* Fixed Width Requested */
        min-width: 0 !important;
        /* OVERRIDE GLOBAL MIN-WIDTH (600px) */
        margin: 0 auto;
        /* Center table */
        box-sizing: border-box;
        border: none;
    }

    #finances .data-table tr,
    #finances .data-table td {
        display: block;
        width: 100%;
        box-sizing: border-box;
        border: none;
        /* Reset borders */
    }

    /* TBODY: Fixed Width & Scroll (Sticky Header Effect) */
    #finances .data-table tbody {
        display: block;
        width: 346px !important;
        /* Requested Width */
        max-width: 100%;
        /* Safety for small screens */
        margin: 0 auto;
        /* Center the list */

        /* Internal Scroll */
        height: calc(100vh - 280px) !important;
        /* Adjust height to leave header visible */
        overflow-y: auto !important;
        overflow-x: hidden;
        padding-bottom: 2rem;
        /* Space for bottom scroll */
    }

    /* Row as Card - FIXED DIMENSIONS */
    #finances .data-table tr {
        background: #fff;
        margin-bottom: 1rem;
        border: 1px solid #e0e0e0;
        border-radius: 12px;
        padding: 1.5rem;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        position: relative;

        /* Fixed Dimensions Requested */
        width: 314px !important;
        height: 300px !important;
        margin-left: auto !important;
        margin-right: auto !important;
        /* Center card inside tbody */

        overflow: hidden;
    }

    /* 1. Checkbox - Absolute Top Right */
    #finances .data-table td:nth-child(1) {
        position: absolute;
        top: 1.5rem;
        right: 1.5rem;
        width: auto;
        padding: 0;
        background: transparent;
        z-index: 10;
    }

    #finances .data-table td:nth-child(1) input {
        transform: scale(1.5);
        /* Larger checkbox */
    }

    /* 2. Invoice Number (Top) */
    #finances .data-table td.invoice-number-cell {
        font-size: 1.3rem;
        font-weight: 800;
        color: #2C3E50;
        padding: 0;
        margin-bottom: 0.5rem;
        border: none;
    }

    #finances .data-table td.invoice-number-cell::before {
        content: 'Nº Factura: ';
        font-weight: normal;
        color: #95a5a6;
        font-size: 1rem;
    }

    /* 4. Patient Name (Below Invoice Number) */
    #finances .data-table td.patient-name-cell {
        font-size: 1.1rem;
        font-weight: 700;
        color: #34495e;
        margin-bottom: 2rem;
        /* Spacing */
        text-transform: uppercase;
        padding: 0;
        line-height: 1.3;
    }

    #finances .data-table td.patient-name-cell::before {
        content: '';
        /* No icon */
    }

    /* 3. Date (No Icon) */
    #finances .data-table td.date-cell {
        font-size: 1rem;
        color: #7f8c8d;
        margin-bottom: 0.5rem;
        padding: 0;
    }

    #finances .data-table td.date-cell::before {
        content: '';
        /* No icon */
    }

    /* 5. Initials - HIDE */
    #finances .data-table td.initials-cell {
        display: none;
    }

    /* 7. Quarter - HIDE */
    #finances .data-table td.quarter-cell {
        display: none;
    }

    /* 9. Payment Method */
    #finances .data-table td.payment-method-cell {
        font-size: 1rem;
        color: #34495e;
        margin-bottom: 0.5rem;
        padding: 0;
    }

    #finances .data-table td.payment-method-cell::before {
        content: 'Forma Pago: ';
        color: #95a5a6;
    }

    /* 8. Status (Text: Pagado / No Pagado) */
    #finances .data-table td.status-cell {
        font-size: 1rem;
        margin-bottom: 0.5rem;
        padding: 0;
    }

    #finances .data-table td.status-cell .status-icon {
        display: none;
        /* Hide emoji */
    }

    #finances .data-table td.status-cell::before {
        content: 'Estado: ';
        color: #95a5a6;
    }

    /* Show text based on data attribute */
    #finances .data-table td.status-cell[data-status="paid"]::after {
        content: 'Pagado';
        color: #27ae60;
        font-weight: bold;
    }

    #finances .data-table td.status-cell[data-status="unpaid"]::after {
        content: 'No pagado';
        color: #e74c3c;
        font-weight: bold;
    }

    /* 6. Amount (Bottom Right Corner, No Box) */
    #finances .data-table td.amount-cell {
        position: absolute;
        bottom: 1.5rem;
        right: 1.5rem;
        width: auto;
        font-size: 2rem;
        /* Large */
        font-weight: 800;
        color: #2C3E50;
        background: transparent;
        /* No box */
        padding: 0;
        margin: 0;
        text-align: right;
        border: none;
    }

    #finances .data-table td.amount-cell::before {
        content: 'Importe';
        display: block;
        font-size: 0.8rem;
        font-weight: normal;
        color: #95a5a6;
        text-align: right;
        margin-bottom: -2px;
    }
}

/* =========================================
   MOBILE STYLES FOR EXPENSES & TAXES (V4)
   ========================================= */
@media (max-width: 768px) {

    /* --- COMMON RESET FOR EXPENSES VIEW --- */
    #expenses .table-container {
        padding: 0;
        background: transparent;
        box-shadow: none;
        overflow: visible;
        /* Allow content to dictate width */
    }

    /* --- 1. GASTOS (Compact Table 320px) --- */
    /* Target the first table (Expenses) */
    #expenses>.table-container .data-table {
        width: 320px !important;
        min-width: 0 !important;
        /* Override global min-width */
        margin: 0 auto;
        table-layout: fixed;
        /* Force column widths if needed */
    }

    #expenses>.table-container .data-table th,
    #expenses>.table-container .data-table td {
        padding: 0.5rem 0.25rem !important;
        font-size: 0.8rem;
        white-space: normal;
        /* Allow text wrap */
        overflow: hidden;
    }

    /* Adjust specific columns for Expenses */
    #expenses>.table-container .data-table th:first-child,
    #expenses>.table-container .data-table td:first-child {
        width: 30px;
        /* Checkbox */
    }

    #expenses>.table-container .data-table th:nth-child(2),
    #expenses>.table-container .data-table td:nth-child(2) {
        width: 80px;
        /* Date */
    }

    #expenses>.table-container .data-table th:nth-child(4),
    #expenses>.table-container .data-table td:nth-child(4) {
        width: 80px;
        /* Amount */
        text-align: right;
    }

    /* Category takes remaining space */

    /* --- 2. HACIENDA (Card Format 320px) --- */
    /* Target the nested table container (Taxes) */
    #expenses div[style*="margin-top: 3rem"] .table-container .data-table {
        display: block;
        width: 320px !important;
        min-width: 0 !important;
        margin: 0 auto;
        border: none;
    }

    #expenses div[style*="margin-top: 3rem"] .table-container thead {
        display: none;
        /* Hide headers */
    }

    #expenses div[style*="margin-top: 3rem"] .table-container tbody,
    #expenses div[style*="margin-top: 3rem"] .table-container tr,
    #expenses div[style*="margin-top: 3rem"] .table-container td {
        display: block;
        width: 100%;
        box-sizing: border-box;
        border: none;
    }

    /* Card Style */
    #expenses div[style*="margin-top: 3rem"] .table-container tr {
        background: #fff;
        margin-bottom: 1rem;
        border: 1px solid #e0e0e0;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        position: relative;
        height: auto;
        /* Let content define height */
    }

    /* Checkbox (Top Right) */
    #expenses div[style*="margin-top: 3rem"] .table-container td:first-child {
        position: absolute;
        top: 1rem;
        right: 1rem;
        width: auto;
        padding: 0;
        text-align: right;
    }

    #expenses div[style*="margin-top: 3rem"] .table-container td:first-child input {
        transform: scale(1.3);
    }

    /* Year & Quarter (Inline Top Left) */
    #expenses div[style*="margin-top: 3rem"] .table-container td:nth-child(2),
    /* Year */
    #expenses div[style*="margin-top: 3rem"] .table-container td:nth-child(3) {
        /* Quarter */
        display: inline-block;
        width: auto;
        font-weight: bold;
        color: #7f8c8d;
        font-size: 0.9rem;
        padding: 0 0.5rem 0.25rem 0;
        text-align: left;
    }

    /* Description (Main Content) */
    #expenses div[style*="margin-top: 3rem"] .table-container td:nth-child(4) {
        font-size: 1rem;
        font-weight: 600;
        color: #2C3E50;
        padding: 0.5rem 0;
        text-align: left;
    }

    /* Amount (Bottom Right) */
    #expenses div[style*="margin-top: 3rem"] .table-container td:nth-child(5) {
        font-size: 1.4rem;
        font-weight: 800;
        color: #E74C3C;
        text-align: right;
        padding-top: 0.5rem;
    }

    /* --- TAXES BUTTONS MOBILE LAYOUT --- */
    #expenses div[style*="margin-top: 3rem"] .view-header div {
        width: 100%;
        justify-content: space-between;
        /* Distribute space */
    }

    #btn-new-tax {
        width: 100%;
        margin-bottom: 0.5rem;
        order: 1;
        /* Ensure first */
    }

    #btn-modify-tax {
        flex: 1;
        margin-right: 0.25rem;
        order: 2;
        justify-content: center;
    }

    #btn-delete-tax {
        flex: 1;
        margin-left: 0.25rem;
        order: 3;
        justify-content: center;
    }

    /* --- RESUMEN FINANCIERO MOBILE --- */
    /* Header: Title + Year Selector stacked */
    #summary>div:first-child {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem;
    }

    #summary>div:first-child h2 {
        font-size: 1.3rem;
    }

    #summary>div:first-child select {
        width: 100%;
    }

    /* KPI Cards: Single Column, Compact */
    #summary .summary-cards {
        grid-template-columns: 1fr !important;
        gap: 0.75rem !important;
        margin-bottom: 1.5rem !important;
    }

    #summary .summary-cards .card {
        padding: 1rem !important;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
    }

    #summary .summary-cards .card h3 {
        font-size: 0.75rem !important;
        margin-bottom: 0 !important;
    }

    #summary .summary-cards .card p {
        font-size: 1.3rem !important;
    }

    /* Monthly Evolution Chart: Compact */
    #summary>div:nth-child(3) {
        /* Chart container */
        padding: 1rem !important;
        margin-bottom: 1rem !important;
    }

    #summary>div:nth-child(3) h3 {
        font-size: 1rem !important;
        margin-bottom: 0.75rem !important;
    }

    #summary>div:nth-child(3)>div {
        /* Canvas container */
        height: 250px !important;
    }

    #summary-chart {
        max-width: 100% !important;
    }

    /* Monthly Breakdown Table: Card Format - 3 Rows per Month */
    #summary .monthly-table {
        width: 100% !important;
        min-width: 0 !important;
        max-width: 100%;
        border-collapse: separate;
        border-spacing: 0;
        margin-top: 1rem;
        /* Space after title */
    }

    #summary .monthly-table thead {
        display: none;
    }

    #summary .monthly-table tbody tr {
        display: block;
        background: #f8f9fa;
        border-radius: 8px;
        padding: 0.75rem 1rem;
        margin-bottom: 0.5rem;
        text-align: left;
    }

    #summary .monthly-table td {
        display: block;
        padding: 0.1rem 0 !important;
        border: none !important;
        text-align: left;
    }

    /* Row 1: Month name */
    #summary .monthly-table td:first-child {
        font-weight: 700;
        font-size: 0.95rem !important;
        color: #2C3E50;
        margin-bottom: 0.3rem;
    }

    /* Row 2: Income & Expenses inline */
    #summary .monthly-table td:nth-child(2),
    #summary .monthly-table td:nth-child(3) {
        display: inline-block;
        width: 49%;
        font-size: 0.8rem !important;
    }

    #summary .monthly-table td:nth-child(2) {
        color: #27AE60;
    }

    #summary .monthly-table td:nth-child(3) {
        color: #E74C3C;
        text-align: right;
    }

    /* Row 3: Beneficio Neto */
    #summary .monthly-table td:nth-child(4) {
        display: block;
        font-weight: 700;
        font-size: 0.85rem !important;
        margin-top: 0.3rem;
    }

    /* Gastos */
    #summary .monthly-table td:nth-child(4) {
        font-weight: 600;
    }

    /* B.Neto */

    /* Container adjustments */
    #summary>div:nth-child(4) {
        /* Table container */
        padding: 0.75rem !important;
        max-width: 314px;
        margin: 0 auto;
    }

    #summary>div:nth-child(4) h3 {
        font-size: 1rem !important;
        margin-bottom: 0.5rem !important;
    }

    #summary>div:nth-child(4) .table-container {
        overflow-x: hidden;
        margin: 0 auto !important;
        padding: 0 !important;
        max-width: 284px;
    }

    /* Helper classes for responsive text */
    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: inline !important;
    }
}

/* Desktop: show desktop-only, hide mobile-only */
@media (min-width: 769px) {
    .desktop-only {
        display: inline !important;
    }

    .mobile-only {
        display: none !important;
    }

    /* Fix for Monthly Breakdown Table Alignment */
    #summary .monthly-table {
        table-layout: fixed !important;
        width: 100% !important;
        border-collapse: collapse !important;
        display: table !important;
    }

    #summary .monthly-table thead {
        display: table-header-group !important;
    }

    #summary .monthly-table tbody {
        display: table-row-group !important;
    }

    #summary .monthly-table tr {
        display: table-row !important;
    }

    #summary .monthly-table th,
    #summary .monthly-table td {
        display: table-cell !important;
        padding: 16px 24px !important;
    }

    #summary .monthly-table th:nth-child(1),
    #summary .monthly-table td:nth-child(1) {
        width: 20% !important;
        text-align: left !important;
    }

    #summary .monthly-table th:nth-child(2),
    #summary .monthly-table td:nth-child(2) {
        width: 25% !important;
        text-align: right !important;
    }

    #summary .monthly-table th:nth-child(3),
    #summary .monthly-table td:nth-child(3) {
        width: 25% !important;
        text-align: right !important;
    }

    #summary .monthly-table th:nth-child(4),
    #summary .monthly-table td:nth-child(4) {
        width: 30% !important;
        text-align: right !important;
    }
}

/* Force hide calendar controls on mobile, overriding any tablet styles */
@media (max-width: 768px) {
    .calendar-controls {
        display: none !important;
    }
}

/* Responsive layout for finances sidebar */
@media (max-width: 1024px) {
    .finances-layout {
        flex-direction: column;
    }

    #finances-sidebar {
        width: 100% !important;
        position: static !important;
        margin-top: 2rem;
        max-height: none !important;
    }
}

/* Mobile Expenses Section Layout */
@media (max-width: 768px) {
    .expenses-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 1rem !important;
    }

    .expenses-header .expenses-buttons-container {
        width: 100%;
    }

    .expenses-header #btn-new-expense-action {
        width: 100%;
        padding: 0.75rem 1rem;
        font-size: 13.33px;
    }

    .expenses-header .expenses-action-buttons {
        width: 100%;
    }

    .expenses-header #btn-modify-expense,
    .expenses-header #btn-delete-expenses {
        flex: 1;
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
    }
}

/* Mobile Patients Search Bar - Buttons to the right */
@media (max-width: 768px) {
    #patients .search-bar {
        flex-wrap: nowrap !important;
    }

    #patients .search-bar>input {
        order: 1;
        flex: 1;
        min-width: 0;
    }

    #patients .search-bar>div {
        order: 2;
        flex-shrink: 0;
    }
}

/* Mobile: Hide desktop calendar controls (use mobile calendar only) */
@media (max-width: 768px) {
    #calendar .calendar-controls {
        display: none !important;
    }
}

/* Mobile: Hide ALL desktop calendar views, show only mobile calendar */
@media (max-width: 768px) {
    #calendar .calendar-grid-wrapper {
        display: none !important;
    }

    #calendar #week-view {
        display: none !important;
    }

    #calendar #month-view {
        display: none !important;
    }
}

/* Mobile: Show mobile calendar interface */
@media (max-width: 768px) {
    #mobile-calendar-interface {
        display: block !important;
    }

    /* Override any body class rules for mobile */
    body.calendar-week-view #mobile-calendar-interface,
    body.calendar-month-view #mobile-calendar-interface {
        display: block !important;
    }

    body.calendar-week-view .calendar-grid-wrapper,
    body.calendar-month-view .calendar-grid-wrapper {
        display: none !important;
    }
}

/* Desktop/Tablet: Hide mobile calendar interface (default) */
@media (min-width: 769px) {
    #mobile-calendar-interface {
        display: none;
    }
}

/* Mobile: Fix expenses table container to prevent overlap with Hacienda section */
@media (max-width: 768px) {
    #expenses .table-container {
        max-height: none;
        overflow: visible;
        margin-bottom: 2rem;
        position: relative;
        z-index: 1;
    }

    /* Ensure Hacienda section has proper spacing and positioning */
    #hacienda-section {
        position: relative;
        z-index: 2;
        margin-top: 2rem !important;
    }
}

/* Medium screens: Summary cards in 2x2 grid */
@media (max-width: 1200px) and (min-width: 769px) {
    #summary .summary-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 1rem !important;
    }

    #summary .summary-cards .card {
        padding: 1rem !important;
    }

    #summary .summary-cards .card h3 {
        font-size: 0.85rem !important;
    }

    #summary .summary-cards .card p {
        font-size: 1.5rem !important;
    }
}

/* Add bottom padding to sections for smoother scrolling */
.view {
    padding-bottom: 5rem !important;
    margin-bottom: 2rem !important;
}

/* Create space at the end of scrollable content */
section.view::after {
    content: '';
    display: block;
    height: 3rem;
}

/* Additional padding for main content area */
.main-content {
    padding-bottom: 5rem !important;
}

/* Ensure table containers don't cut off the space */
.table-container {
    margin-bottom: 2rem;
}
/* Mobile: Export buttons same size */
@media (max-width: 768px) {
    #btn-export-excel,
    #btn-export-pdf {
        flex: 1;
        min-width: 45%;
        padding: 0.75rem 1rem !important;
        font-size: 0.9rem !important;
        justify-content: center;
        text-align: center;
    }
    
    /* Container for export buttons */
    #finances .finances-controls > div:last-child {
        width: 100%;
        display: flex;
        gap: 0.5rem;
    }
}

/* Mobile event card selected state */
.mob-event-card.selected {
    background: #e8f4fd !important;
    border-left: 4px solid #2E7D6F !important;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* Mobile visit action buttons container */
#mob-visit-actions {
    animation: fadeIn 0.2s ease-in-out;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}
