/* ==========================================================================
   VistMe design system
   ========================================================================== */

:root {
    color-scheme: light;

    /* Brand */
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #7c3aed;
    --primary-soft: rgba(79, 70, 229, 0.08);
    --primary-ring: rgba(79, 70, 229, 0.35);

    /* Neutrals */
    --bg-color: #f6f7fb;
    --surface: #ffffff;
    --card-bg: rgba(255, 255, 255, 0.82);
    --text-main: #0f172a;
    --text-muted: #64748b;
    --text-subtle: #94a3b8;
    --border-color: rgba(15, 23, 42, 0.08);
    --border-strong: rgba(15, 23, 42, 0.14);

    /* Status */
    --success-color: #059669;
    --success-soft: rgba(5, 150, 105, 0.1);
    --danger-color: #dc2626;
    --danger-soft: rgba(220, 38, 38, 0.08);
    --info-color: #0284c7;

    /* Radii */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-xl: 24px;
    --radius-pill: 999px;

    /* Elevation */
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 3px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 4px 6px -2px rgba(15, 23, 42, 0.04), 0 12px 20px -8px rgba(15, 23, 42, 0.1);
    --shadow-lg: 0 8px 12px -4px rgba(15, 23, 42, 0.06), 0 24px 40px -12px rgba(15, 23, 42, 0.16);
    --shadow-primary: 0 6px 18px -4px rgba(79, 70, 229, 0.45);

    /* Motion */
    --ease: cubic-bezier(0.4, 0, 0.2, 1);
    --transition: 180ms var(--ease);
}

/* ==========================================================================
   Base
   ========================================================================== */

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    background-color: var(--bg-color);
    color: var(--text-main);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 0;
    padding: 0;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    background-image:
        radial-gradient(circle at 12% 18%, rgba(79, 70, 229, 0.1), transparent 42%),
        radial-gradient(circle at 88% 12%, rgba(124, 58, 237, 0.09), transparent 40%),
        radial-gradient(circle at 60% 95%, rgba(14, 165, 233, 0.07), transparent 45%);
    background-attachment: fixed;
}

h1, h2, h3, h4, h5 {
    margin-top: 0;
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.2;
}

h1 { font-size: clamp(1.75rem, 1.15rem + 2.6vw, 3rem); }
h2 { font-size: clamp(1.3rem, 1.05rem + 1.1vw, 1.9rem); }
h3 { font-size: clamp(1.15rem, 1rem + 0.7vw, 1.5rem); }
h4 { font-size: clamp(1.05rem, 0.98rem + 0.4vw, 1.25rem); }

p { line-height: 1.65; }

a {
    text-decoration: none;
    transition: color var(--transition);
}

/* Consistent, visible keyboard focus everywhere */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.btn:focus-visible {
    outline: 3px solid var(--primary-ring);
    outline-offset: 2px;
    box-shadow: none;
}

/* ==========================================================================
   Layout
   ========================================================================== */

.page-container {
    width: 100%;
    max-width: 1180px;
    margin-inline: auto;
    padding: clamp(1rem, 1.4vw + 0.7rem, 2.5rem);
}

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

main {
    flex: 1;
}

/* Page header: back button + title, wraps safely on narrow screens */
.page-header {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-bottom: clamp(1.25rem, 2vw, 2rem);
}

.page-header h2 {
    margin: 0;
}

/* ==========================================================================
   Surfaces
   ========================================================================== */

.glass-card {
    background: var(--card-bg);
    backdrop-filter: blur(16px) saturate(160%);
    -webkit-backdrop-filter: blur(16px) saturate(160%);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: clamp(1.25rem, 1vw + 1rem, 2rem);
    box-shadow: var(--shadow-md);
    transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
}

.text-gradient {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.text-muted {
    color: var(--text-muted) !important;
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn {
    border-radius: var(--radius-sm);
    font-weight: 600;
    transition: transform var(--transition), box-shadow var(--transition),
                background-color var(--transition), border-color var(--transition);
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: #fff;
    border: none;
    padding: 0.7rem 1.4rem;
    cursor: pointer;
    box-shadow: var(--shadow-primary);
}

.btn-primary:hover,
.btn-primary:focus {
    transform: translateY(-2px);
    box-shadow: 0 10px 24px -6px rgba(79, 70, 229, 0.55);
    color: #fff;
}

.btn-primary:active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

.btn-primary:disabled,
.btn-primary.disabled {
    opacity: 0.55;
    transform: none;
    box-shadow: none;
    cursor: not-allowed;
}

.btn-secondary,
.btn-outline-secondary {
    background: var(--surface);
    color: var(--text-main);
    border: 1px solid var(--border-strong);
    padding: 0.7rem 1.25rem;
    cursor: pointer;
}

.btn-secondary:hover,
.btn-outline-secondary:hover {
    background: #f8fafc;
    border-color: var(--primary-color);
    color: var(--primary-color);
    transform: translateY(-1px);
}

.btn-lg {
    padding: 0.85rem 1.6rem;
    font-size: 1.02rem;
}

/* ==========================================================================
   Forms
   ========================================================================== */

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

.form-label {
    display: block;
    margin-bottom: 0.4rem;
    font-weight: 600;
    font-size: 0.88rem;
    color: var(--text-muted);
}

.form-control {
    width: 100%;
    padding: 0.7rem 0.9rem;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text-main);
    font-size: 1rem;
    transition: border-color var(--transition), box-shadow var(--transition);
    box-shadow: none;
}

.form-control::placeholder {
    color: var(--text-subtle);
}

.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 4px var(--primary-soft);
    background: var(--surface);
}

/* ==========================================================================
   Dashboard
   ========================================================================== */

.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
    gap: clamp(0.9rem, 1.4vw, 1.75rem);
    margin-top: clamp(1.25rem, 2vw, 2.25rem);
}

.dashboard-card {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    padding: clamp(1.35rem, 1.4vw + 1rem, 2rem);
    text-decoration: none;
    color: var(--text-main);
    position: relative;
    overflow: hidden;
}

.dashboard-card::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    opacity: 0;
    transition: opacity var(--transition);
    pointer-events: none;
}

.dashboard-card:hover {
    transform: translateY(-4px);
    border-color: rgba(79, 70, 229, 0.35);
    box-shadow: var(--shadow-lg);
    color: var(--text-main);
}

.dashboard-card:hover::after {
    opacity: 0.03;
}

.dashboard-card h2 {
    font-size: 1.2rem;
    margin-bottom: 0.35rem;
}

.dashboard-card p {
    margin-bottom: 0;
    font-size: 0.92rem;
    color: var(--text-muted);
}

/* Icon tile */
.dashboard-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin-bottom: 1rem;
    font-size: 1.6rem;
    color: var(--primary-color);
    background: var(--primary-soft);
    border-radius: var(--radius-md);
    transition: transform var(--transition), background-color var(--transition);
}

.dashboard-card:hover .dashboard-icon {
    transform: scale(1.06);
    background: rgba(79, 70, 229, 0.14);
}

/* ==========================================================================
   Scanner & camera
   Layout here is load-bearing: the tracking overlay is absolutely positioned
   inside .camera-container and relies on overflow clipping + the mirror.
   ========================================================================== */

.camera-container {
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    border-radius: var(--radius-md);
    overflow: hidden;
    position: relative;
    background: #0f172a;
    border: 1px solid var(--border-color);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

video {
    width: 100%;
    display: block;
    object-fit: contain;
    transform: scaleX(-1);
    -webkit-transform: scaleX(-1);
}

#qr-reader {
    position: relative;
}

#qr-reader video {
    display: block;
    width: 100%;
    height: auto;
}

#reader {
    width: 100%;
    border-radius: var(--radius-md);
    overflow: hidden;
}

/* Live status pill */
.scanner-status {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.9rem;
    border-radius: var(--radius-pill);
    background: var(--success-soft);
    color: var(--success-color);
    font-size: 0.85rem;
    font-weight: 600;
}

.scanner-status .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

/* Fullscreen scanning overlay UI - only visible in fullscreen mode */
.scanner-overlay-ui {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2100;
    pointer-events: none;
    flex-direction: column;
    justify-content: space-between;
    /* Keep controls clear of notches and the browser's own chrome */
    padding: calc(1rem + env(safe-area-inset-top)) 1rem calc(1rem + env(safe-area-inset-bottom));
}

.scanner-overlay-ui .scanner-close {
    pointer-events: auto;
    align-self: flex-start;
    border-radius: var(--radius-pill);
    padding: 0.6rem 1.1rem;
    box-shadow: var(--shadow-md);
}

/* Fullscreen camera while scanning on phones */
@media (max-width: 767.98px) {
    body.scanner-active {
        overflow: hidden;
    }

    /* Phones get an unobstructed viewfinder - no copy, no status pill */
    .scanner-instructions {
        display: none;
    }

    body.scanner-active #qr-reader {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        max-width: none;
        margin: 0;
        border: none;
        border-radius: 0;
        background: #000;
        z-index: 2000;
    }

    /* html5-qrcode sets an inline pixel width on the video, so these need
       !important to win over it. */
    body.scanner-active #qr-reader video {
        width: 100% !important;
        height: 100% !important;
        object-fit: cover;
    }

    body.scanner-active .scanner-overlay-ui {
        display: flex;
    }
}

/* ==========================================================================
   Result states
   ========================================================================== */

.success-message {
    padding: clamp(1rem, 2vw, 1.75rem);
    background: var(--success-soft);
    border: 1px solid rgba(5, 150, 105, 0.22);
    color: var(--success-color);
    border-radius: var(--radius-lg);
    margin-top: 1rem;
    text-align: center;
}

.success-message h4 {
    color: var(--success-color);
}

.result-photos {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: clamp(0.75rem, 2vw, 1.5rem);
    margin: 1.25rem 0;
}

.result-photo {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
}

.result-photo img {
    max-height: 150px;
    max-width: 100%;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
}

.result-photo .caption {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

/* ==========================================================================
   History: tabs, filters, table
   ========================================================================== */

/* Bootstrap's .nav-pills .nav-link.active sets a blue background; without this
   the inherited muted/primary text colour lands on it with poor contrast. */
.nav-pills {
    gap: 0.4rem;
}

.nav-pills .nav-link {
    color: var(--text-muted);
    font-weight: 600;
    border-radius: var(--radius-pill);
    padding: 0.5rem 1.1rem;
    border: 1px solid transparent;
    transition: all var(--transition);
}

.nav-pills .nav-link:hover {
    color: var(--primary-color);
    background: var(--primary-soft);
}

.nav-pills .nav-link.active {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: #fff;
    box-shadow: var(--shadow-primary);
}

.filter-bar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.table {
    color: var(--text-main);
    margin-bottom: 0;
}

.table td,
.table th {
    border-color: var(--border-color);
    background: transparent;
    vertical-align: middle;
}

.table thead th {
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.78rem;
    letter-spacing: 0.06em;
    border-bottom: 1px solid var(--border-strong);
}

.table tbody tr {
    transition: background-color var(--transition);
}

.table tbody tr:hover {
    background: var(--primary-soft);
}

.thumb {
    width: 56px;
    height: 56px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: transform var(--transition);
}

.thumb:hover {
    transform: scale(1.06);
}

/* Parent QR payloads are long; keep them to one line with the full text on hover */
.qr-cell {
    display: inline-block;
    max-width: 22ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: bottom;
    font-weight: 600;
}

/* Stack the history table into cards on phones so nothing is cut off */
@media (max-width: 640px) {
    .table-responsive {
        overflow: visible;
    }

    .table thead {
        display: none;
    }

    .table,
    .table tbody,
    .table tr,
    .table td {
        display: block;
        width: 100%;
    }

    .table tr {
        background: var(--surface);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        box-shadow: var(--shadow-sm);
        padding: 0.5rem 0.85rem;
        margin-bottom: 0.85rem;
    }

    .table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
        border: none;
        border-bottom: 1px solid var(--border-color);
        padding: 0.6rem 0;
        text-align: right;
    }

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

    /* Label each cell using the header text carried in data-label */
    .table td::before {
        content: attr(data-label);
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        color: var(--text-muted);
        text-align: left;
        flex-shrink: 0;
    }

    /* Rows that only say "Loading" / "No entries" have no label to show */
    .table td[colspan]::before {
        content: none;
    }

    .table td[colspan] {
        justify-content: center;
        text-align: center;
        color: var(--text-muted);
    }
}

/* ==========================================================================
   Misc
   ========================================================================== */

.badge {
    font-weight: 600;
    border-radius: var(--radius-pill);
    padding: 0.4em 0.8em;
}

.alert {
    border-radius: var(--radius-md);
    border-width: 1px;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.45; }
}

.animated-pulse {
    animation: pulse 2s var(--ease) infinite;
}

@keyframes fade-up {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}

.glass-card,
.success-message {
    animation: fade-up 320ms var(--ease) both;
}

/* Respect users who ask for less motion */
@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;
    }

    .dashboard-card:hover,
    .btn-primary:hover {
        transform: none;
    }
}
