/* FieldTrack PWA test — Material 3 base, modern CSS (light-dark, @starting-style, popover, dialog). */

@layer tokens, base, components, motion;

@layer tokens {
    :root {
        color-scheme: light dark;

        --primary: light-dark(oklch(0.49 0.2 264), oklch(0.78 0.11 264));
        --on-primary: light-dark(#fff, oklch(0.25 0.08 264));
        --primary-container: light-dark(oklch(0.93 0.04 264), oklch(0.33 0.1 264));
        --on-primary-container: light-dark(oklch(0.3 0.12 264), oklch(0.92 0.04 264));
        --danger: light-dark(oklch(0.55 0.2 27), oklch(0.75 0.13 27));
        --on-danger: light-dark(#fff, oklch(0.25 0.08 27));
        --danger-container: light-dark(oklch(0.94 0.04 27), oklch(0.35 0.09 27));
        --good: light-dark(oklch(0.55 0.14 150), oklch(0.78 0.13 150));
        --good-container: light-dark(oklch(0.94 0.05 150), oklch(0.33 0.07 150));
        --warn: light-dark(oklch(0.6 0.14 70), oklch(0.82 0.12 80));
        --warn-container: light-dark(oklch(0.95 0.05 85), oklch(0.36 0.06 70));

        --bg: light-dark(oklch(0.98 0.005 264), oklch(0.16 0.015 264));
        --surface: light-dark(#fff, oklch(0.21 0.015 264));
        --surface-2: light-dark(oklch(0.955 0.008 264), oklch(0.25 0.015 264));
        --text: light-dark(oklch(0.22 0.02 264), oklch(0.94 0.01 264));
        --muted: light-dark(oklch(0.5 0.02 264), oklch(0.72 0.02 264));
        --outline: light-dark(oklch(0.86 0.01 264), oklch(0.38 0.015 264));

        --radius-sm: 8px;
        --radius: 16px;
        --radius-lg: 28px;
        --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 16px rgb(0 0 0 / 0.06);
        --ease: cubic-bezier(0.2, 0, 0, 1);
        --ease-emphasized: cubic-bezier(0.05, 0.7, 0.1, 1);
        --nav-h: 80px;
        --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', 'Noto Sans Devanagari', sans-serif;
    }
}

@layer base {
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }
    html {
        -webkit-text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
    }
    body {
        margin: 0;
        font: 400 16px/1.5 var(--font);
        background: var(--bg);
        color: var(--text);
        overscroll-behavior-y: contain;
        min-height: 100dvh;
    }
    h1,
    h2 {
        margin: 0;
        text-wrap: balance;
    }
    h2 {
        font-size: 1.05rem;
        font-weight: 600;
    }
    p {
        margin: 0;
        text-wrap: pretty;
    }
    button,
    input,
    textarea {
        font: inherit;
        color: inherit;
    }
    button {
        touch-action: manipulation;
        user-select: none;
    }
    :focus-visible {
        outline: 3px solid var(--primary);
        outline-offset: 2px;
    }
    [hidden] {
        display: none !important;
    }
    .muted {
        color: var(--muted);
    }
    .small {
        font-size: 0.85rem;
    }
    .warn {
        color: var(--warn);
        font-weight: 600;
    }
    .label {
        font-size: 0.8rem;
        font-weight: 600;
        letter-spacing: 0.02em;
        color: var(--muted);
        text-transform: uppercase;
    }
}

@layer components {
    /* Top app bar */
    .top-bar {
        position: sticky;
        top: 0;
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
        background: color-mix(in oklch, var(--bg) 88%, transparent);
        backdrop-filter: blur(12px);
        border-bottom: 1px solid color-mix(in oklch, var(--outline) 60%, transparent);
    }
    .top-bar__title {
        display: flex;
        align-items: baseline;
        gap: 6px;
    }
    .top-bar__title strong {
        font-size: 1.15rem;
        color: var(--primary);
    }
    .top-bar__title span {
        color: var(--muted);
        font-size: 0.9rem;
    }
    .top-bar__status {
        display: flex;
        gap: 6px;
    }

    main {
        max-width: 640px;
        margin-inline: auto;
        padding: 12px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 24px);
    }
    .tab {
        display: grid;
        gap: 12px;
        view-transition-name: tab;
    }
    .tab__title {
        font-size: 1.5rem;
        font-weight: 600;
        margin: 8px 4px 4px;
    }

    /* Cards */
    .card {
        display: grid;
        gap: 10px;
        padding: 16px;
        background: var(--surface);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
    }
    .card--hero {
        background: var(--primary-container);
        color: var(--on-primary-container);
    }
    .card--hero .muted,
    .card--hero .label {
        color: color-mix(in oklch, var(--on-primary-container) 75%, transparent);
    }
    .card__head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
    }
    .hero {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 16px;
    }
    .hero__value {
        font-size: 1.6rem;
        font-weight: 700;
        line-height: 1.2;
    }

    /* Buttons */
    .actions {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    .actions--end {
        justify-content: flex-end;
    }
    .btn {
        position: relative;
        min-height: 44px;
        padding: 0 20px;
        border-radius: 999px;
        border: 0;
        font-weight: 600;
        font-size: 0.95rem;
        cursor: pointer;
        overflow: hidden;
        transition: background-color 0.2s var(--ease), transform 0.1s var(--ease);
    }
    .btn::after {
        /* state layer */
        content: '';
        position: absolute;
        inset: 0;
        background: currentColor;
        opacity: 0;
        transition: opacity 0.15s var(--ease);
    }
    .btn:hover::after {
        opacity: 0.08;
    }
    .btn:active::after {
        opacity: 0.14;
    }
    .btn:active {
        transform: scale(0.98);
    }
    .btn:disabled {
        opacity: 0.4;
        cursor: not-allowed;
    }
    .btn--filled {
        background: var(--primary);
        color: var(--on-primary);
    }
    .btn--tonal {
        background: var(--primary-container);
        color: var(--on-primary-container);
    }
    .btn--outlined {
        background: transparent;
        color: var(--primary);
        box-shadow: inset 0 0 0 1px var(--outline);
    }
    .btn--text {
        background: transparent;
        color: var(--primary);
        padding: 0 12px;
    }
    .btn--danger.btn--filled {
        background: var(--danger);
        color: var(--on-danger);
    }
    .btn--danger.btn--outlined {
        color: var(--danger);
    }
    .btn--wide {
        width: 100%;
        min-height: 52px;
        font-size: 1.05rem;
    }

    /* Extended FAB for the one main action */
    .fab {
        flex: none;
        min-width: 120px;
        min-height: 64px;
        padding: 0 22px;
        border: 0;
        border-radius: 20px;
        background: var(--primary);
        color: var(--on-primary);
        font-size: 1.05rem;
        font-weight: 700;
        box-shadow: 0 6px 16px color-mix(in oklch, var(--primary) 35%, transparent);
        cursor: pointer;
        transition: transform 0.12s var(--ease), background-color 0.3s var(--ease);
    }
    .fab:active {
        transform: scale(0.96);
    }
    .fab--out {
        background: var(--danger);
        color: var(--on-danger);
        box-shadow: 0 6px 16px color-mix(in oklch, var(--danger) 35%, transparent);
    }
    .fab:disabled {
        opacity: 0.6;
    }

    /* Chips and pills */
    .chip {
        display: inline-flex;
        align-items: center;
        height: 28px;
        padding: 0 10px;
        border-radius: 8px;
        font-size: 0.8rem;
        font-weight: 600;
        background: var(--surface-2);
        color: var(--muted);
        white-space: nowrap;
    }
    .chip--good {
        background: var(--good-container);
        color: var(--good);
    }
    .chip--warn {
        background: var(--warn-container);
        color: var(--warn);
    }
    .chip--soft {
        background: var(--primary-container);
        color: var(--on-primary-container);
    }
    .pill {
        flex: none;
        padding: 2px 10px;
        border-radius: 999px;
        font-size: 0.8rem;
        font-weight: 600;
        background: var(--surface-2);
    }
    .pill--warn {
        background: var(--warn-container);
        color: var(--warn);
    }

    /* Timeline */
    .timeline {
        list-style: none;
        margin: 0;
        padding: 0;
        display: grid;
    }
    .tl {
        position: relative;
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 10px 0 10px 22px;
        border-top: 1px solid color-mix(in oklch, var(--outline) 50%, transparent);
    }
    .tl:first-child {
        border-top: 0;
    }
    .tl::before {
        content: '';
        position: absolute;
        left: 2px;
        top: 50%;
        width: 10px;
        height: 10px;
        margin-top: -5px;
        border-radius: 50%;
        background: var(--primary);
    }
    .tl--out::before {
        background: var(--danger);
    }
    .tl--trip::before {
        background: var(--warn);
    }
    .tl--visit::before {
        background: var(--good);
    }
    .tl__body {
        display: grid;
        flex: 1;
        min-width: 0;
    }
    .tl__body .muted {
        font-size: 0.85rem;
    }
    .empty {
        padding: 12px 0;
        color: var(--muted);
        list-style: none;
    }
    .thumb {
        flex: none;
        width: 44px;
        height: 44px;
        padding: 0;
        border: 0;
        border-radius: var(--radius-sm);
        overflow: hidden;
        cursor: pointer;
        background: var(--surface-2);
    }
    .thumb img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    /* Lists */
    .list {
        list-style: none;
        margin: 0;
        padding: 0;
    }
    .list__row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 10px 0;
        border-top: 1px solid color-mix(in oklch, var(--outline) 50%, transparent);
    }
    .list__row:first-child {
        border-top: 0;
    }
    .list__row > div {
        display: grid;
        min-width: 0;
    }
    .list__row .muted {
        font-size: 0.85rem;
    }

    /* Trip stats */
    .trip-head {
        display: flex;
        align-items: center;
        gap: 10px;
    }
    .dot {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        background: var(--outline);
    }
    .dot--live {
        background: var(--good);
        animation: pulse 1.6s var(--ease) infinite;
    }
    .stats {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }
    .stat {
        display: grid;
        padding: 12px;
        border-radius: 12px;
        background: var(--surface-2);
    }
    .stat__value {
        font-size: 1.3rem;
        font-weight: 700;
        font-variant-numeric: tabular-nums;
    }
    .stat__label {
        font-size: 0.8rem;
        color: var(--muted);
    }

    /* Facts, tables, capabilities */
    .facts {
        display: grid;
        grid-template-columns: auto 1fr;
        gap: 6px 16px;
        margin: 0;
    }
    .facts dt {
        color: var(--muted);
    }
    .facts dd {
        margin: 0;
        font-weight: 600;
        overflow-wrap: anywhere;
    }
    .table-wrap {
        overflow-x: auto;
    }
    .data {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.9rem;
        font-variant-numeric: tabular-nums;
    }
    .data th,
    .data td {
        padding: 8px 6px;
        text-align: left;
        border-bottom: 1px solid color-mix(in oklch, var(--outline) 60%, transparent);
        white-space: nowrap;
    }
    .data th {
        color: var(--muted);
        font-weight: 600;
        font-size: 0.8rem;
    }
    .summary {
        font-weight: 600;
    }
    .caps {
        list-style: none;
        margin: 0;
        padding: 0;
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
    }
    .cap {
        display: inline-flex;
        gap: 6px;
        align-items: center;
        padding: 4px 10px;
        border-radius: 8px;
        font-size: 0.85rem;
    }
    .cap--ok {
        background: var(--good-container);
        color: var(--good);
    }
    .cap--no {
        background: var(--surface-2);
        color: var(--muted);
    }
    .hint {
        font-size: 0.85rem;
        color: var(--muted);
    }

    /* Form fields */
    .field {
        display: grid;
        gap: 4px;
    }
    .field span,
    .segmented legend {
        font-size: 0.85rem;
        font-weight: 600;
        color: var(--muted);
    }
    .field input,
    .field textarea {
        width: 100%;
        min-height: 48px;
        padding: 10px 14px;
        border: 1px solid var(--outline);
        border-radius: 12px;
        background: var(--surface);
        field-sizing: content;
        resize: none;
    }
    .field textarea {
        min-height: 72px;
    }
    .field input:focus,
    .field textarea:focus {
        outline: 2px solid var(--primary);
        outline-offset: -1px;
        border-color: transparent;
    }
    .check {
        display: flex;
        align-items: center;
        gap: 10px;
        min-height: 44px;
    }
    .check input {
        width: 20px;
        height: 20px;
        accent-color: var(--primary);
    }
    .segmented {
        border: 0;
        margin: 0;
        padding: 0;
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
    }
    .segmented legend {
        margin-bottom: 6px;
        padding: 0;
    }
    .segmented label {
        position: relative;
    }
    .segmented input {
        position: absolute;
        opacity: 0;
        inset: 0;
    }
    .segmented span {
        display: inline-flex;
        align-items: center;
        min-height: 40px;
        padding: 0 14px;
        border-radius: 8px;
        border: 1px solid var(--outline);
        font-size: 0.9rem;
        font-weight: 600;
        transition: background-color 0.15s var(--ease);
    }
    .segmented input:checked + span {
        background: var(--primary-container);
        color: var(--on-primary-container);
        border-color: transparent;
    }
    .segmented input:focus-visible + span {
        outline: 3px solid var(--primary);
    }

    /* Dialogs and bottom sheets */
    dialog {
        border: 0;
        padding: 20px;
        color: var(--text);
        background: var(--surface);
        max-width: min(560px, 100vw);
    }
    dialog h2 {
        margin-bottom: 12px;
    }
    dialog form {
        display: grid;
        gap: 14px;
    }
    dialog::backdrop {
        background: rgb(0 0 0 / 0.45);
    }
    .dialog {
        width: min(400px, calc(100vw - 32px));
        border-radius: var(--radius-lg);
    }
    .dialog p {
        margin-bottom: 16px;
    }
    .sheet {
        position: fixed;
        inset: auto 0 0 0;
        width: 100%;
        max-height: 92dvh;
        margin: 0 auto;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
        overflow-y: auto;
        overscroll-behavior: contain;
    }
    .sheet::before {
        content: '';
        display: block;
        width: 32px;
        height: 4px;
        margin: -8px auto 14px;
        border-radius: 2px;
        background: var(--outline);
    }
    #camera-video {
        display: block;
        width: 100%;
        max-height: 55dvh;
        object-fit: cover;
        border-radius: var(--radius);
        background: #000;
        margin-bottom: 10px;
    }
    #photo-view {
        display: block;
        width: 100%;
        border-radius: var(--radius);
        margin-bottom: 8px;
    }
    #camera-status {
        margin-bottom: 12px;
    }

    /* Snackbar (popover) */
    .snackbar {
        position: fixed;
        inset: auto 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px) 16px;
        margin: 0 auto;
        max-width: 560px;
        padding: 14px 16px;
        border: 0;
        border-radius: 12px;
        background: light-dark(oklch(0.27 0.02 264), oklch(0.9 0.02 264));
        color: light-dark(#fff, oklch(0.2 0.02 264));
        box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
        font-size: 0.95rem;
    }

    /* Update banner */
    .update-banner {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        margin: 8px 16px 0;
        padding: 8px 8px 8px 16px;
        border-radius: 12px;
        background: var(--primary-container);
        color: var(--on-primary-container);
    }

    /* Bottom navigation bar */
    .bottom-nav {
        position: fixed;
        inset: auto 0 0 0;
        z-index: 10;
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        height: calc(var(--nav-h) + env(safe-area-inset-bottom));
        padding: 0 8px env(safe-area-inset-bottom);
        background: color-mix(in oklch, var(--surface-2) 92%, transparent);
        backdrop-filter: blur(12px);
        border-top: 1px solid color-mix(in oklch, var(--outline) 50%, transparent);
    }
    .bottom-nav button {
        display: grid;
        justify-items: center;
        align-content: center;
        gap: 4px;
        border: 0;
        background: none;
        color: var(--muted);
        font-size: 0.75rem;
        font-weight: 600;
        cursor: pointer;
    }
    .bottom-nav__icon {
        display: grid;
        place-items: center;
        width: 64px;
        height: 32px;
        border-radius: 16px;
        transition: background-color 0.25s var(--ease);
    }
    .bottom-nav svg {
        width: 24px;
        height: 24px;
        fill: currentColor;
    }
    .bottom-nav [aria-current='page'] {
        color: var(--text);
    }
    .bottom-nav [aria-current='page'] .bottom-nav__icon {
        background: var(--primary-container);
        color: var(--on-primary-container);
    }

    @media (min-width: 600px) {
        .stats {
            grid-template-columns: repeat(4, 1fr);
        }
    }
}

@layer motion {
    @keyframes pulse {
        0%,
        100% {
            box-shadow: 0 0 0 0 color-mix(in oklch, var(--good) 60%, transparent);
        }
        50% {
            box-shadow: 0 0 0 8px color-mix(in oklch, var(--good) 0%, transparent);
        }
    }

    /* Dialog and sheet entry/exit with @starting-style */
    dialog,
    dialog::backdrop {
        transition:
            opacity 0.25s var(--ease),
            translate 0.35s var(--ease-emphasized),
            scale 0.25s var(--ease),
            overlay 0.35s allow-discrete,
            display 0.35s allow-discrete;
        opacity: 0;
    }
    dialog[open],
    dialog[open]::backdrop {
        opacity: 1;
    }
    .sheet {
        translate: 0 100%;
    }
    .sheet[open] {
        translate: 0 0;
    }
    .dialog {
        scale: 0.94;
    }
    .dialog[open] {
        scale: 1;
    }
    @starting-style {
        dialog[open],
        dialog[open]::backdrop {
            opacity: 0;
        }
        .sheet[open] {
            translate: 0 100%;
        }
        .dialog[open] {
            scale: 0.94;
        }
    }

    /* Snackbar entry/exit */
    .snackbar {
        transition:
            opacity 0.2s var(--ease),
            translate 0.3s var(--ease-emphasized),
            overlay 0.3s allow-discrete,
            display 0.3s allow-discrete;
        opacity: 0;
        translate: 0 16px;
    }
    .snackbar:popover-open {
        opacity: 1;
        translate: 0 0;
    }
    @starting-style {
        .snackbar:popover-open {
            opacity: 0;
            translate: 0 16px;
        }
    }

    /* Tab change cross-fade */
    ::view-transition-old(tab),
    ::view-transition-new(tab) {
        animation-duration: 0.2s;
    }

    @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after,
        ::view-transition-group(*) {
            animation: none !important;
            transition: none !important;
        }
    }
}
