/* =========================================================
   TechDoctor — Design System
   Loaded AFTER style.css. Adds what was missing rather than
   restyling what works: focus states, reduced motion,
   skeletons, a real drawer, touch-target floors, and a
   card fallback for wide tables on small screens.
   ========================================================= */

/* =========================================================
   1. FOCUS — keyboard visibility
   style.css contained a single :focus rule across 2,835
   lines, so tabbing through the app was effectively
   invisible. :focus-visible keeps mouse clicks clean.
   ========================================================= */

:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
    border-radius: var(--radius-sm);
}

/* Inputs already carry a border — inset the ring so it
   doesn't collide with the field edge. */
.form-control:focus-visible {
    outline-offset: 0;
    border-color: var(--primary);
}

/* Suppress the default ring only where we've replaced it. */
:where(a, button):focus:not(:focus-visible) {
    outline: none;
}

/* Skip link — first tab stop on every page. */
.skip-link {
    position: absolute;
    top: 0;
    left: var(--space-4);
    transform: translateY(-120%);
    z-index: var(--z-toast);
    padding: var(--space-3) var(--space-5);
    background: var(--primary);
    color: #fff;
    font-weight: var(--weight-semibold);
    font-size: var(--text-body);
    border-radius: 0 0 var(--radius) var(--radius);
    transition: transform var(--duration-base) var(--ease-out);
}
.skip-link:focus {
    transform: translateY(0);
    color: #fff;
}

/* Screen-reader-only text. */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* =========================================================
   2. REDUCED MOTION
   The app animates on scroll, hover, drawer, modal and
   toast. Vestibular disorders make that actively painful.
   ========================================================= */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    html { scroll-behavior: auto; }
}

/* =========================================================
   3. TOUCH TARGETS
   Applied on coarse pointers only, so desktop density is
   untouched. Uses min-height rather than padding to avoid
   disturbing existing layouts.
   ========================================================= */

@media (pointer: coarse) {
    .btn,
    .nav-links > li > a,
    .sidebar-nav a,
    .nav-dropdown-menu a,
    .filter-tabs a,
    .modal-close,
    .pagination a,
    .table td a {
        min-height: var(--touch-target);
        display: inline-flex;
        align-items: center;
    }

    .btn-sm {
        min-height: 38px;
    }

    /* Icon-only controls need width as well as height. */
    .modal-close,
    .nav-toggle,
    .sidebar-close,
    .sidebar-mobile-toggle {
        min-width: var(--touch-target);
        min-height: var(--touch-target);
        justify-content: center;
    }

    /* Checkbox and radio hit areas. */
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 20px;
        min-height: 20px;
    }
}

/* =========================================================
   4. SKELETON LOADERS
   Shapes mirror the components they stand in for, so the
   layout doesn't jump when real content arrives.
   ========================================================= */

.skeleton {
    position: relative;
    overflow: hidden;
    background: var(--neutral-100);
    border-radius: var(--radius-sm);
}

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

@keyframes skeleton-sweep {
    100% { transform: translateX(100%); }
}

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

.skeleton-text {
    height: 0.75em;
    margin-bottom: var(--space-2);
    border-radius: var(--radius-full);
}
.skeleton-text:last-child { margin-bottom: 0; }
.skeleton-text.w-40 { width: 40%; }
.skeleton-text.w-60 { width: 60%; }
.skeleton-text.w-80 { width: 80%; }

.skeleton-title {
    height: 1.25em;
    width: 55%;
    margin-bottom: var(--space-3);
    border-radius: var(--radius-full);
}

.skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-full);
    flex-shrink: 0;
}

.skeleton-stat {
    height: 92px;
    border-radius: var(--radius);
}

.skeleton-card {
    padding: var(--space-5);
    background: var(--card-solid);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

.skeleton-row {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    padding: var(--space-4) 0;
    border-bottom: 1px solid var(--border);
}
.skeleton-row:last-child { border-bottom: 0; }

/* =========================================================
   5. NAVIGATION DRAWER (≤768px)
   Replaces the absolutely-positioned dropdown panel with a
   slide-in drawer: backdrop, scroll lock, ESC and focus
   trap are wired in app.js. Desktop nav is untouched.
   ========================================================= */

.drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(10, 22, 40, 0.45);
    opacity: 0;
    visibility: hidden;
    z-index: var(--z-backdrop);
    transition: opacity var(--duration-medium) var(--ease-out),
                visibility var(--duration-medium) var(--ease-out);
}
.drawer-backdrop.active {
    opacity: 1;
    visibility: visible;
}

/* Lock the page behind the drawer without losing position. */
body.drawer-open {
    overflow: hidden;
    touch-action: none;
}

@media (max-width: 768px) {

    .nav-links {
        /* Override the dropdown-panel geometry from style.css */
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: auto;
        width: min(var(--drawer-width), 86vw);
        max-height: none;
        height: 100dvh;
        display: flex;
        flex-direction: column;
        gap: var(--space-1);
        padding: calc(var(--header-height) + var(--space-4))
                 var(--space-4)
                 calc(var(--space-6) + env(safe-area-inset-bottom));
        background: var(--card-solid);
        border-bottom: 0;
        border-left: 1px solid var(--border);
        box-shadow: -8px 0 40px rgba(10, 22, 40, 0.16);
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        z-index: var(--z-drawer);

        transform: translateX(100%);
        visibility: hidden;
        transition: transform var(--duration-medium) var(--ease-out),
                    visibility var(--duration-medium) var(--ease-out);
    }

    .nav-links.active {
        display: flex;
        transform: translateX(0);
        visibility: visible;
    }

    /* Close control, injected by app.js */
    .drawer-close {
        position: absolute;
        top: var(--space-3);
        right: var(--space-3);
        width: var(--touch-target);
        height: var(--touch-target);
        display: inline-flex;
        align-items: center;
        justify-content: center;
        background: var(--neutral-100);
        border: 0;
        border-radius: var(--radius-full);
        color: var(--text);
        font-size: var(--text-lg);
        cursor: pointer;
        transition: background var(--duration-fast) var(--ease-out);
    }
    .drawer-close:hover { background: var(--neutral-200); }

    /* Section heading inside the drawer. */
    .drawer-heading {
        padding: var(--space-4) var(--space-4) var(--space-2);
        font-size: var(--text-caption);
        font-weight: var(--weight-bold);
        letter-spacing: var(--tracking-wide);
        text-transform: uppercase;
        color: var(--text-muted);
    }
}

/* =========================================================
   6. TABLES ON SMALL SCREENS
   style.css scrolls every table horizontally at a 700px
   floor. That is right for wide financial tables and wrong
   for short record lists, which read better stacked.
   Opt a table in by adding .table-cards to the wrapper.
   ========================================================= */

@media (max-width: 640px) {

    .table-cards .table,
    .table-cards .table tbody,
    .table-cards .table tr,
    .table-cards .table td {
        display: block;
        width: 100%;
        min-width: 0;
    }

    .table-cards .table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
    }

    .table-cards .table tr {
        margin-bottom: var(--space-3);
        padding: var(--space-2) var(--space-4);
        background: var(--card-solid);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow-sm);
    }

    .table-cards .table td {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: var(--space-4);
        padding: var(--space-3) 0;
        border: 0;
        border-bottom: 1px solid var(--border);
        text-align: right;
    }
    .table-cards .table td:last-child {
        border-bottom: 0;
    }

    /* Column name comes from data-label on the td. */
    .table-cards .table td::before {
        content: attr(data-label);
        flex-shrink: 0;
        font-size: var(--text-caption);
        font-weight: var(--weight-semibold);
        letter-spacing: var(--tracking-wide);
        text-transform: uppercase;
        color: var(--text-muted);
        text-align: left;
    }

    /* Rows whose last cell is an action bar. */
    .table-cards .table td.td-actions {
        justify-content: flex-end;
        padding-top: var(--space-4);
    }
    .table-cards .table td.td-actions::before { content: none; }
}

/* Scroll affordance for tables that stay horizontal. */
@media (max-width: 768px) {
    .table-responsive:not(.table-cards) {
        position: relative;
        scrollbar-width: thin;
    }
    .table-responsive:not(.table-cards)::after {
        content: "";
        position: sticky;
        top: 0;
        right: 0;
        float: right;
        width: 24px;
        height: 100%;
        margin-left: -24px;
        pointer-events: none;
        background: linear-gradient(
            to right,
            transparent,
            rgba(10, 22, 40, 0.06)
        );
    }
}

/* =========================================================
   7. EMPTY AND ERROR STATES
   style.css has .empty-state but no error variant, so
   failures currently fall back to a raw message.
   ========================================================= */

.state-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: var(--space-12) var(--space-5);
    background: var(--card-solid);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

.state-panel .state-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 64px;
    margin-bottom: var(--space-5);
    border-radius: var(--radius-full);
    font-size: var(--text-2xl);
}

.state-panel.is-empty .state-icon {
    background: var(--info-surface);
    color: var(--info-text);
}
.state-panel.is-error .state-icon {
    background: var(--danger-surface);
    color: var(--danger-text);
}

.state-panel h3 {
    margin-bottom: var(--space-2);
    font-size: var(--text-lg);
    font-weight: var(--weight-semibold);
    color: var(--text);
}

.state-panel p {
    max-width: 42ch;
    margin-bottom: var(--space-5);
    font-size: var(--text-body);
    line-height: var(--leading-normal);
    color: var(--text-light);
}

.state-panel p:last-child { margin-bottom: 0; }

/* =========================================================
   8. STATUS PILLS
   Token-backed replacements for the hardcoded Material
   hexes currently inlined across the dashboard pages.
   Each carries a dot as well as colour, so status is not
   communicated by hue alone.
   ========================================================= */

.pill {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    border: 1px solid transparent;
    border-radius: var(--radius-full);
    font-size: var(--text-caption);
    font-weight: var(--weight-semibold);
    line-height: 1.5;
    white-space: nowrap;
}

.pill::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    flex-shrink: 0;
}

.pill-success { background: var(--success-surface); border-color: var(--success-border); color: var(--success-text); }
.pill-warning { background: var(--warning-surface); border-color: var(--warning-border); color: var(--warning-text); }
.pill-danger  { background: var(--danger-surface);  border-color: var(--danger-border);  color: var(--danger-text);  }
.pill-info    { background: var(--info-surface);    border-color: var(--info-border);    color: var(--info-text);    }
.pill-neutral { background: var(--neutral-100);     border-color: var(--neutral-200);    color: var(--neutral-700);  }

/* =========================================================
   9. BUTTON LOADING STATE
   Prevents the double-submit that the payment and booking
   forms are currently open to on slow connections.
   ========================================================= */

.btn[data-loading="true"] {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.btn[data-loading="true"]::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    color: #fff;
    animation: btn-spin 0.6s linear infinite;
}

.btn-outline[data-loading="true"]::after { color: var(--primary); }

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

/* =========================================================
   10. OVERFLOW GUARDS
   body{overflow-x:hidden} in style.css hides overflow rather
   than preventing it, so clipped content fails silently.
   These stop the common causes at source.
   ========================================================= */

/* Long unbroken strings — emails, URLs, device serials. */
.table td,
.card p,
.dashboard-header h2,
.state-panel p,
.pill {
    overflow-wrap: anywhere;
}

/* Media and embeds never exceed their column. */
img, svg, video, canvas, iframe {
    max-width: 100%;
}

/* Charts need an explicit box or Chart.js overflows on
   first paint before its resize observer fires. */
.chart-container {
    position: relative;
    width: 100%;
    min-width: 0;
    overflow: hidden;
}
.chart-container canvas {
    max-width: 100% !important;
}

/* Grid and flex children default to min-width:auto, which
   is the single most common overflow cause in dashboards. */
.dashboard-content,
.stats-grid > *,
.grid-2 > *,
.grid-3 > *,
.grid-4 > * {
    min-width: 0;
}

/* =========================================================
   11. PRINT
   Reports and prescriptions are printed constantly in
   clinical settings; nothing currently accounts for it.
   ========================================================= */

@media print {
    .navbar,
    .sidebar,
    .drawer-backdrop,
    .nav-toggle,
    .footer,
    #toast-container,
    .btn,
    .no-print {
        display: none !important;
    }

    body {
        background: #fff;
        color: #000;
        font-size: 11pt;
    }

    .dashboard-content {
        margin-left: 0 !important;
        padding: 0 !important;
    }

    .card,
    .card-glass,
    .card-solid {
        border: 1px solid #ccc !important;
        box-shadow: none !important;
        break-inside: avoid;
    }

    a[href]::after {
        content: " (" attr(href) ")";
        font-size: 9pt;
        color: #555;
    }
    /* Internal links don't need the URL spelled out. */
    a[href^="#"]::after,
    a[href^="javascript:"]::after { content: ""; }
}
