:root {
    --ds-space-1: .25rem;
    --ds-space-2: .5rem;
    --ds-space-3: .75rem;
    --ds-space-4: 1rem;
    --ds-space-5: 1.5rem;
    --ds-space-6: 2rem;
    --ds-radius-sm: 8px;
    --ds-radius-md: 12px;
    --ds-radius-lg: 18px;
    --ds-elevation-1: 0 4px 12px rgba(31, 45, 61, .06);
    --ds-elevation-2: 0 12px 30px rgba(31, 45, 61, .10);
    --ds-elevation-3: 0 24px 60px rgba(31, 45, 61, .16);
    --ds-success: #168052;
    --ds-warning: #a86e10;
    --ds-danger: #b42318;
    --ds-info: #4966aa;
}

.ds-panel,
.dashboard-card,
.settings-section,
.analytics-panel,
.communications-panel,
.api-docs-section {
    border-radius: var(--ds-radius-lg);
    box-shadow: var(--ds-elevation-1);
}

.ds-form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--ds-space-4);
}

.ds-form-actions,
.ds-toolbar,
.ds-pagination {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--ds-space-3);
}

.ds-form-actions,
.ds-pagination {
    justify-content: flex-end;
}

.modal-content,
.ds-modal .modal-content {
    border-radius: var(--ds-radius-lg);
    box-shadow: var(--ds-elevation-3);
}

.ds-modal .modal-header,
.ds-modal .modal-footer,
.ds-modal .modal-body {
    padding: var(--ds-space-5);
}

/* Wide modals (modal-xl) grow with the screen on large viewports.
   Keeps a 1.75rem margin each side and stops at 1400px. Narrower
   viewports keep Bootstrap's own widths. */
@media (min-width: 1200px) {
    .modal-dialog.modal-xl {
        --bs-modal-width: min(1400px, calc(100vw - 3.5rem));
    }
}

.dropdown-menu {
    border-radius: var(--ds-radius-md);
    box-shadow: var(--ds-elevation-2);
}

.alert,
.ds-alert {
    border-radius: var(--ds-radius-md);
}

.ds-empty-state {
    display: grid;
    min-height: 180px;
    place-items: center;
    padding: var(--ds-space-6);
    border: 1px dashed var(--app-border);
    border-radius: var(--ds-radius-lg);
    color: var(--app-muted);
    background: var(--app-panel-bg);
    text-align: center;
}

.ds-empty-state i {
    display: block;
    margin-bottom: var(--ds-space-3);
    color: var(--app-accent);
    font-size: 2rem;
}

.ds-empty-state h2,
.ds-empty-state h3 {
    color: var(--app-ink);
}

.ds-skeleton {
    position: relative;
    overflow: hidden;
    border-radius: var(--ds-radius-sm);
    background: var(--app-hover-bg);
}

.ds-skeleton::after {
    position: absolute;
    inset: 0;
    content: "";
    background: linear-gradient(90deg, transparent, rgba(255,255,255,.42), transparent);
    transform: translateX(-100%);
    animation: ds-skeleton-shimmer 1.35s ease-in-out infinite;
}

.ds-skeleton-line {
    height: .85rem;
    margin-bottom: var(--ds-space-3);
}

.ds-skeleton-card {
    height: 112px;
}

.ds-badge,
.badge {
    border-radius: 999px;
}

.badge-success,
.ds-badge-success {
    color: #fff;
    background: var(--ds-success);
}

.badge-warning,
.ds-badge-warning {
    color: #fff;
    background: var(--ds-warning);
}

.badge-danger,
.ds-badge-danger {
    color: #fff;
    background: var(--ds-danger);
}

.badge-info,
.ds-badge-info {
    color: #fff;
    background: var(--ds-info);
}

.ds-pagination {
    padding: var(--ds-space-4);
}

.ds-toast-region {
    position: fixed;
    right: 1rem;
    top: 1rem;
    z-index: 1080;
    display: grid;
    width: min(360px, calc(100vw - 2rem));
    gap: var(--ds-space-3);
    pointer-events: none;
}

.ds-toast {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: start;
    gap: var(--ds-space-3);
    border: 1px solid var(--app-border);
    border-radius: var(--ds-radius-md);
    padding: .85rem 1rem;
    color: var(--app-ink);
    background: var(--app-surface);
    box-shadow: var(--ds-elevation-3);
    pointer-events: auto;
    animation: ds-toast-enter .18s ease-out;
}

.ds-toast.is-success i {
    color: var(--ds-success);
}

.ds-toast.is-error i {
    color: var(--ds-danger);
}

.ds-toast.is-warning i {
    color: var(--ds-warning);
}

.ds-toast button {
    border: 0;
    color: var(--app-muted);
    background: transparent;
}

:focus-visible {
    outline: 3px solid color-mix(in srgb, var(--app-primary) 58%, transparent);
    outline-offset: 3px;
}

@keyframes ds-skeleton-shimmer {
    to { transform: translateX(100%); }
}

@keyframes ds-toast-enter {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 575.98px) {
    .ds-form-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .ds-form-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .ds-form-actions .btn {
        width: 100%;
    }

    .ds-modal .modal-header,
    .ds-modal .modal-footer,
    .ds-modal .modal-body {
        padding: var(--ds-space-4);
    }

    .ds-toast-region {
        right: .5rem;
        top: .5rem;
        width: calc(100vw - 1rem);
    }
}

@media (prefers-reduced-motion: reduce) {
    .ds-skeleton::after,
    .ds-toast {
        animation: none;
    }

    .ds-loading-overlay {
        transition: none;
    }
}

/* The spinner is not turned off by reduced motion. It shows on every data refresh. */

/* Frosted glass loading. Content stays visible, blurred behind a translucent panel with a spinner in the middle. */
.ds-loading-host {
    position: relative;
}

.ds-loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--app-surface) 55%, transparent);
    -webkit-backdrop-filter: blur(6px) saturate(1.15);
    backdrop-filter: blur(6px) saturate(1.15);
    border-radius: inherit;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition: opacity .18s ease;
}

.ds-loading-overlay.is-visible {
    opacity: 1;
    pointer-events: auto;
}

/* The spinner in the middle of the frosted panel. It always turns, including when reduced motion is set. */
.ds-spinner {
    position: relative;
    z-index: 1;
    flex: 0 0 auto;
    width: 2.6rem;
    height: 2.6rem;
    border: 3px solid var(--app-border);
    border-top-color: var(--app-primary);
    border-radius: 50%;
    animation: ds-spin .8s linear infinite;
}


@supports not ((-webkit-backdrop-filter: blur(1px)) or (backdrop-filter: blur(1px))) {
    .ds-loading-overlay {
        background: color-mix(in srgb, var(--app-surface) 88%, transparent);
    }
}

.ds-loading-card {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    gap: .6rem;
    padding: .55rem 1rem;
    border: 1px solid var(--app-border);
    border-radius: 999px;
    background: var(--app-surface);
    color: var(--app-ink);
    box-shadow: var(--app-shadow);
    font-size: .9rem;
    font-weight: 600;
}

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

body {
    font-family: var(--user-font-family, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif);
    font-size: var(--user-base-size, 13px);
}

.admin-main h1,
.print-profile-header h1 {
    font-family: var(--user-font-family, inherit);
    font-size: var(--user-page-heading-size, inherit) !important;
    font-weight: var(--user-page-heading-weight, inherit) !important;
}

.dashboard-card h2,
.settings-section h2,
.analytics-panel h2,
.api-docs-section h2,
.communications-panel h2,
.users-panel h2,
.card-header h2 {
    font-size: var(--user-card-heading-size, inherit) !important;
    font-weight: var(--user-card-heading-weight, inherit) !important;
}

.admin-main table thead th,
.admin-main table thead th button {
    font-size: var(--user-table-header-size, inherit) !important;
    font-weight: var(--user-table-header-weight, inherit) !important;
}

.admin-main table tbody td,
.admin-main table tbody th {
    font-size: var(--user-table-cell-size, inherit) !important;
    font-weight: var(--user-table-cell-weight, inherit) !important;
}

.admin-main table[data-density="compact"] > :not(caption) > * > * {
    padding-block: .35rem;
    padding-inline: .5rem;
}

.user-table-preference-toolbar {
    display: flex;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: .75rem;
    margin-bottom: .75rem;
}

.user-table-density {
    display: grid;
    min-width: 150px;
    gap: .25rem;
    color: var(--app-muted);
    font-size: .75rem;
}

.user-table-column-panel {
    position: relative;
}

.user-table-column-panel > summary,
.dashboard-personalization > summary {
    cursor: pointer;
    color: var(--app-accent);
    font-size: .85rem;
    font-weight: 650;
}

.user-table-column-panel[open] {
    z-index: 4;
}

.user-table-column-list {
    display: grid;
    max-height: 320px;
    min-width: min(100%, 340px);
    gap: .35rem;
    overflow: auto;
    margin-top: .5rem;
    padding: .6rem;
    border: 1px solid var(--app-border);
    border-radius: 10px;
    background: var(--app-surface);
    box-shadow: var(--app-shadow);
}

.user-table-column-item,
.dashboard-widget-preference {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .75rem;
    color: var(--app-ink);
    font-size: .85rem;
}

.user-table-column-item > label,
.dashboard-widget-preference > label {
    display: flex;
    min-width: 0;
    align-items: center;
    gap: .5rem;
}

.user-table-column-move,
.dashboard-widget-move,
.user-table-preference-actions {
    display: flex;
    flex: 0 0 auto;
    gap: .35rem;
}

.user-table-column-move .btn,
.dashboard-widget-move .btn {
    min-width: 32px;
}

.user-table-preference-actions {
    margin-top: .35rem;
}

.user-table-preference-status,
.dashboard-preference-status {
    min-height: 1.25rem;
    color: var(--app-muted);
    font-size: .75rem;
}

.dashboard-personalization {
    padding: 1rem 1.25rem;
    border: 1px solid var(--app-border);
    border-radius: 14px;
    background: var(--app-surface);
}

.dashboard-widget-preference-list {
    display: grid;
    max-width: 560px;
    gap: .6rem;
}

@media print {
    .admin-sidebar,
    .admin-sidebar-header,
    .admin-sidebar-toggle,
    .maintenance-footer,
    .ds-form-actions,
    .ds-toast-region,
    .modal,
    .btn,
    button {
        display: none !important;
    }

    body,
    .dashboard-page,
    .admin-layout,
    .admin-main {
        height: auto !important;
        min-height: 0 !important;
        overflow: visible !important;
        color: #111 !important;
        background: #fff !important;
    }

    .admin-main {
        width: 100% !important;
        padding: 12.7mm !important;
    }

    .ds-panel,
    .dashboard-card,
    .settings-section,
    .analytics-panel,
    .communications-panel,
    .api-docs-section,
    .table,
    .card,
    .modal-content {
        break-inside: avoid;
        border: 1px solid #bbb !important;
        color: #111 !important;
        background: #fff !important;
        box-shadow: none !important;
    }

    .table {
        width: 100% !important;
        border-collapse: collapse !important;
    }

    .table th,
    .table td {
        color: #111 !important;
        border: 1px solid #bbb !important;
        background: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    @page {
        margin: 12.7mm;
    }
}

/* Close button (modals, alerts): the X takes the theme's text colors instead of Bootstrap's fixed black,
   so it is visible in every light and dark theme. */
.btn-close {
    --bs-btn-close-opacity: 1;
    --bs-btn-close-hover-opacity: 1;
    --bs-btn-close-focus-opacity: 1;
    --bs-btn-close-focus-shadow: 0 0 0 .2rem color-mix(in srgb, var(--app-primary) 35%, transparent);
    width: 1.9rem;
    height: 1.9rem;
    padding: 0;
    border-radius: 8px;
    color: var(--app-muted);
    background: currentColor;
    -webkit-mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-5 -5 26 26'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e") center / 1.1rem no-repeat;
    mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-5 -5 26 26'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e") center / 1.1rem no-repeat;
    filter: none;
}

.btn-close:hover,
.btn-close:focus-visible {
    color: var(--app-ink);
}

/* Date and time pill. Fixed in the top-right corner, never blocks clicks, and follows the theme.
   Markup: <div class="ds-clock" data-clock data-timezone="Asia/Manila" data-server-time="<ms>"> with
   [data-clock-date], [data-clock-time] and .ds-clock-zone inside. clock-widget.js keeps it current. */
.ds-clock {
    position: fixed;
    top: .85rem;
    right: 1.25rem;
    z-index: 1030;
    display: inline-flex;
    align-items: center;
    gap: .5rem;
    padding: .4rem .9rem;
    /* A stronger border and shadow than a card, so the pill stands out on light and dark pages alike. */
    border: 1px solid color-mix(in srgb, var(--app-ink) 22%, var(--app-border));
    border-radius: 999px;
    color: var(--app-ink);
    background: color-mix(in srgb, var(--app-surface) 94%, transparent);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    box-shadow: 0 6px 18px rgba(15, 23, 42, .18), 0 1px 3px rgba(15, 23, 42, .12);
    font-size: .8rem;
    font-variant-numeric: tabular-nums;
    pointer-events: none;
}

.ds-clock > i {
    color: var(--app-primary);
}

/* The minimize button is the only part of the pill that takes clicks. */
.ds-clock-toggle {
    display: inline-grid;
    width: 1.5rem;
    height: 1.5rem;
    margin: -.1rem -.5rem -.1rem .1rem;
    place-items: center;
    border: 0;
    border-radius: 999px;
    color: var(--app-muted);
    background: transparent;
    pointer-events: auto;
}

.ds-clock-toggle:hover,
.ds-clock-toggle:focus-visible {
    color: var(--app-ink);
    background: var(--app-hover-bg);
}

/* Minimized: only a round clock button is left. Pressing it brings the pill back. */
.ds-clock.is-minimized {
    gap: 0;
    padding: .3rem;
}

.ds-clock.is-minimized > :not(.ds-clock-toggle) {
    display: none;
}

.ds-clock.is-minimized .ds-clock-toggle {
    width: 2rem;
    height: 2rem;
    margin: 0;
    color: var(--app-primary);
}

.ds-clock-date {
    color: color-mix(in srgb, var(--app-ink) 72%, transparent);
}

.ds-clock-time {
    min-width: 5.6em;
    font-weight: 700;
    text-align: left;
}

.ds-clock-zone {
    color: color-mix(in srgb, var(--app-ink) 62%, transparent);
    font-size: .7rem;
    font-weight: 700;
    letter-spacing: .06em;
}

.ds-clock.is-static {
    position: static;
}

/* Wide screens: toasts move down so they do not cover the pill, and the page keeps clear room above its content. */
@media (min-width: 992px) {
    body:has(.ds-clock:not(.is-static)) .ds-toast-region {
        top: 3.6rem;
    }

    body:has(.ds-clock:not(.is-static)) .admin-main {
        padding-top: 3.75rem;
    }
}

/* Small screens: the top of the page holds the menu bar, so the pill moves to the bottom-left corner. */
@media (max-width: 991.98px) {
    .ds-clock:not(.is-static) {
        top: auto;
        right: auto;
        bottom: .8rem;
        left: .8rem;
        padding: .3rem .7rem;
    }
}

@media (max-width: 575.98px) {
    .ds-clock:not(.is-static) .ds-clock-date {
        display: none;
    }
}

@media print {
    .ds-clock {
        display: none;
    }
}

/* Stat tiles and meters: a number with a label, and a thin bar for a share of a whole. Used on dashboard cards. */
.ds-stat-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--ds-space-3);
}

.ds-stat {
    padding: .8rem 1rem;
    border: 1px solid var(--app-border);
    border-radius: var(--ds-radius-md);
    background: var(--app-panel-bg);
}

.ds-stat-value {
    color: var(--app-ink);
    font-size: 1.6rem;
    font-weight: 700;
    line-height: 1.1;
}

.ds-stat-label {
    margin-top: .2rem;
    color: var(--app-muted);
    font-size: .78rem;
    font-weight: 600;
}

.ds-meter-row {
    display: grid;
    grid-template-columns: 5.5rem minmax(0, 1fr) 2rem;
    align-items: center;
    gap: var(--ds-space-3);
    margin-bottom: .45rem;
    color: var(--app-ink);
    font-size: .82rem;
}

.ds-meter-row strong {
    text-align: right;
}

.ds-meter {
    height: .5rem;
    overflow: hidden;
    border-radius: 999px;
    background: var(--app-hover-bg);
}

.ds-meter > span {
    display: block;
    width: var(--ds-meter, 0%);
    height: 100%;
    border-radius: inherit;
    background: var(--app-primary);
}

/* Activity feed: icon, what happened and who did it, and when. */
.ds-activity-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

.ds-activity-item {
    display: flex;
    align-items: flex-start;
    gap: var(--ds-space-3);
    padding: .7rem 0;
    border-bottom: 1px solid var(--app-border);
}

.ds-activity-item:last-child {
    border-bottom: 0;
}

.ds-activity-icon {
    display: grid;
    flex: 0 0 2rem;
    height: 2rem;
    place-items: center;
    border-radius: 50%;
    color: var(--app-primary);
    background: var(--app-hover-bg);
}

.ds-activity-text {
    flex: 1 1 auto;
    min-width: 0;
    color: var(--app-ink);
}

.ds-activity-meta,
.ds-activity-time {
    color: var(--app-muted);
    font-size: .75rem;
}

.ds-activity-time {
    flex: 0 0 auto;
    white-space: nowrap;
}

/* Pagination: the one pager used everywhere. Summary on the left, First / five page numbers / Last on the right.
   Markup: <footer class="ds-pager"><span>5599 patients · page 1 of 560</span><ul class="pagination pagination-sm mb-0"></ul></footer>
   Fill the list with AppUI.renderPagination(list, page, lastPage, onSelect). Colors follow the theme. */
.ds-pager {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--ds-space-3);
    padding: .85rem 1rem;
    border-top: 1px solid var(--app-border);
    color: var(--app-muted);
    font-size: .82rem;
}

.pagination {
    --bs-pagination-color: var(--app-primary);
    --bs-pagination-bg: var(--app-surface);
    --bs-pagination-border-color: var(--app-border);
    --bs-pagination-hover-color: var(--app-primary-dark);
    --bs-pagination-hover-bg: var(--app-hover-bg);
    --bs-pagination-hover-border-color: var(--app-border);
    --bs-pagination-focus-color: var(--app-primary-dark);
    --bs-pagination-focus-bg: var(--app-hover-bg);
    --bs-pagination-active-color: #fff;
    --bs-pagination-active-bg: var(--app-primary);
    --bs-pagination-active-border-color: var(--app-primary);
    --bs-pagination-disabled-color: var(--app-muted);
    --bs-pagination-disabled-bg: var(--app-panel-bg);
    --bs-pagination-disabled-border-color: var(--app-border);
}
