:root {
    --cam-bg: #000;
    --cam-fg: #fff;
    --cam-fg-muted: rgba(255, 255, 255, 0.65);
    --cam-accent: #ffd60a;
    --cam-scrim: rgba(0, 0, 0, 0.45);
    --cam-dim: rgba(0, 0, 0, 0.6);
    --cam-guide: rgba(255, 255, 255, 0.6);
    --cam-guide-shadow: rgba(0, 0, 0, 0.3);
    --cam-target: 44px;
    --cam-shutter: 76px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

body {
    min-height: 100vh;
    min-height: 100dvh;
}

body.camera-open {
    overflow: hidden;
    overscroll-behavior: none;
}

/* ---------- Home ---------- */

.home-actions {
    max-width: 28rem;
    margin: 0 auto;
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ---------- Shared camera controls ---------- */

.cam-icon-btn,
.cam-pill-btn,
.cam-text-btn {
    -webkit-tap-highlight-color: transparent;
    border: 0;
    background: transparent;
    color: var(--cam-fg);
    font: inherit;
}

.cam-icon-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--cam-target);
    height: var(--cam-target);
    border-radius: 50%;
    flex: none;
}

.cam-icon-btn svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.cam-icon-btn--round {
    background: rgba(255, 255, 255, 0.15);
    width: 48px;
    height: 48px;
}

.cam-icon-btn[aria-pressed="true"] {
    color: var(--cam-accent);
}

.cam-pill-btn {
    position: relative;
    min-width: var(--cam-target);
    height: 28px;
    padding: 0 10px;
    border: 1.5px solid currentColor;
    border-radius: 14px;
    font-size: 0.8125rem;
    font-weight: 600;
    line-height: 1;
}

/* Extends the touch target to 44px while the pill stays compact. */
.cam-pill-btn::before {
    content: "";
    position: absolute;
    inset: -8px 0;
}

.cam-text-btn {
    min-height: var(--cam-target);
    padding: 0 16px;
    font-size: 1.0625rem;
}

.cam-text-btn--primary {
    color: var(--cam-accent);
    font-weight: 600;
}

/* Flash icon states: auto shows "A" badge, off shows a slash. */
.cam-flash__slash,
.cam-flash__badge {
    display: none;
}

.cam-flash[data-mode="off"] .cam-flash__slash {
    display: inline;
}

.cam-flash[data-mode="on"] {
    color: var(--cam-accent);
}

.cam-flash[data-mode="auto"] .cam-flash__badge {
    display: block;
    position: absolute;
    right: 6px;
    bottom: 6px;
    font-size: 0.625rem;
    font-weight: 700;
    line-height: 1;
}

/* ---------- Camera view (full-screen, always dark) ---------- */

.camera-view,
.preview-view {
    position: fixed;
    inset: 0;
    z-index: 1040;
    display: flex;
    flex-direction: column;
    background: var(--cam-bg);
    color: var(--cam-fg);
    user-select: none;
    -webkit-user-select: none;
}

.camera-view.d-none,
.preview-view.d-none {
    display: none !important;
}

.camera-topbar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: calc(var(--safe-top) + 4px) calc(var(--safe-right) + 8px) 4px calc(var(--safe-left) + 8px);
    flex: none;
}

.camera-topbar__title {
    flex: 1;
    min-width: 0;
    text-align: center;
    font-size: 0.9375rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.camera-stage {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    touch-action: none;
}

.camera-viewport {
    position: relative;
    overflow: hidden;
    background: #111;
    transition: filter 0.2s ease-out;
}

.camera-viewport.is-switching {
    filter: blur(12px);
}

.camera-viewport__video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.camera-viewport__video.is-mirrored {
    transform: scaleX(-1);
}

/* Capture area. Everything outside it is dimmed (WYSIWYG for 1:1). */
.camera-frame {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    height: 100%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 0 9999px var(--cam-dim);
    pointer-events: none;
    transition: width 0.2s ease-out, height 0.2s ease-out;
}

/* ---------- Guides ---------- */

/* Thin, unfilled, white at ~60% with a faint dark shadow (ui-ux-rules.md §5.1). */
.camera-guides {
    position: absolute;
    inset: 0;
    transition: opacity 0.15s ease-out;
}

.camera-guides.is-capturing {
    opacity: 0;
    transition: none;
}

.camera-guides__grid,
.camera-guides__set {
    position: absolute;
    inset: 0;
}

.camera-guides__layer {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    filter: drop-shadow(0 0 1px var(--cam-guide-shadow));
    transition: opacity 0.15s ease-out;
}

.camera-guides__layer.is-entering,
.camera-guides__layer.is-leaving {
    opacity: 0;
}

.camera-guides__layer line,
.camera-guides__layer path,
.camera-guides__layer rect {
    fill: none;
    stroke: var(--cam-guide);
    vector-effect: non-scaling-stroke;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.camera-guides__layer .guide-line {
    stroke-width: 1.25;
}

.camera-guides__layer .guide-frame {
    stroke-width: 1.5;
    stroke-dasharray: 6 5;
}

/* The grid stays dimmer than the orthodontic guides (§5.3). */
.camera-guides__layer .guide-grid {
    stroke-width: 1;
    stroke: rgba(255, 255, 255, 0.35);
}

/* ---------- Level indicator (§6) ---------- */

.camera-level {
    --level-color: rgba(255, 255, 255, 0.85);
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    opacity: 0;
    transition: opacity 0.2s ease-out;
}

.camera-level.is-visible {
    opacity: 1;
}

.camera-level.is-level {
    --level-color: var(--cam-accent);
}

.camera-level > span {
    position: absolute;
    display: block;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.45);
}

.camera-level__side,
.camera-level__horizon {
    top: -1px;
    height: 2px;
    border-radius: 1px;
    background: var(--level-color);
}

.camera-level__side {
    width: 28px;
}

.camera-level__side--left {
    left: -80px;
}

.camera-level__side--right {
    left: 52px;
}

.camera-level__horizon {
    left: -40px;
    width: 80px;
    transition: transform 0.08s linear;
}

.camera-level .camera-level__target,
.camera-level .camera-level__bubble {
    display: none;
    width: 28px;
    height: 28px;
    margin: -14px 0 0 -14px;
}

.camera-level__target {
    border: 1.5px solid var(--level-color);
    border-radius: 50%;
}

.camera-level .camera-level__bubble {
    box-shadow: none;
    filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.45));
    background:
        linear-gradient(var(--level-color), var(--level-color)) center / 2px 100% no-repeat,
        linear-gradient(var(--level-color), var(--level-color)) center / 100% 2px no-repeat;
    transition: transform 0.08s linear;
}

.camera-level[data-mode="flat"] .camera-level__side,
.camera-level[data-mode="flat"] .camera-level__horizon {
    display: none;
}

.camera-level[data-mode="flat"] .camera-level__target,
.camera-level[data-mode="flat"] .camera-level__bubble {
    display: block;
}

/* ---------- More menu ---------- */

.camera-menu {
    position: absolute;
    top: calc(var(--safe-top) + 56px);
    right: calc(var(--safe-right) + 8px);
    z-index: 5;
    width: min(18rem, calc(100% - 16px));
    max-height: calc(100% - var(--safe-top) - 72px);
    overflow-y: auto;
    padding: 6px 0;
    border-radius: 14px;
    background: rgba(28, 28, 30, 0.92);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
}

.camera-menu.d-none {
    display: none;
}

.camera-menu__row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    min-height: var(--cam-target);
    padding: 0 14px;
    border: 0;
    background: transparent;
    color: var(--cam-fg);
    font-size: 0.9375rem;
    text-align: left;
    -webkit-tap-highlight-color: transparent;
}

.camera-menu__row[hidden] {
    display: none;
}

.camera-menu__switch {
    position: relative;
    flex: none;
    width: 40px;
    height: 24px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.25);
    transition: background-color 0.15s ease-out;
}

.camera-menu__switch::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #fff;
    transition: transform 0.15s ease-out;
}

.camera-menu__row[aria-checked="true"] .camera-menu__switch {
    background: var(--cam-accent);
}

.camera-menu__row[aria-checked="true"] .camera-menu__switch::after {
    transform: translateX(16px);
}

.camera-menu__title {
    margin: 6px 14px 2px;
    padding-top: 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.15);
    font-size: 0.6875rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--cam-fg-muted);
}

.camera-menu.is-guides-off #cameraMenuGuides {
    opacity: 0.45;
}

/* ---------- Focus / exposure ---------- */

.camera-focus {
    position: absolute;
    width: 72px;
    height: 72px;
    margin: -36px 0 0 -36px;
    pointer-events: none;
    transition: opacity 0.3s ease-out;
}

.camera-focus.is-fading {
    opacity: 0;
}

.camera-focus__box {
    position: absolute;
    inset: 0;
    border: 1.5px solid var(--cam-accent);
    animation: focusIn 0.3s ease-out;
}

.camera-focus__exposure {
    position: absolute;
    top: -12px;
    bottom: -12px;
    left: calc(100% + 10px);
    width: 22px;
}

.camera-focus__exposure.d-none {
    display: none;
}

.camera-focus__track {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 1px;
    background: var(--cam-accent);
    opacity: 0.6;
}

.camera-focus__sun {
    position: absolute;
    left: 0;
    top: 50%;
    width: 22px;
    height: 22px;
    margin-top: -11px;
    fill: var(--cam-accent);
    stroke: var(--cam-accent);
    stroke-width: 2;
    stroke-linecap: round;
}

@keyframes focusIn {
    from {
        transform: scale(1.4);
        opacity: 0.4;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

/* ---------- Transient preview overlays ---------- */

.camera-capture-flash {
    position: absolute;
    inset: 0;
    background: #000;
    opacity: 0;
    pointer-events: none;
}

.camera-capture-flash.is-active {
    animation: captureFlash 0.15s ease-out;
}

@keyframes captureFlash {
    0% { opacity: 0.85; }
    100% { opacity: 0; }
}

.camera-type-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 6px 14px;
    border-radius: 16px;
    background: var(--cam-scrim);
    font-size: 1.125rem;
    font-weight: 600;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease-out;
}

.camera-type-label.is-visible {
    opacity: 1;
}

.camera-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--cam-bg);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease-out;
}

.camera-loading.is-visible {
    opacity: 1;
}

/* Spinner appears only if startup takes longer than ~500 ms. */
.camera-loading__spinner {
    width: 28px;
    height: 28px;
    border: 2px solid rgba(255, 255, 255, 0.25);
    border-top-color: var(--cam-fg);
    border-radius: 50%;
    opacity: 0;
    animation: spin 0.9s linear infinite, fadeIn 0.2s ease-out 0.5s forwards;
}

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

@keyframes fadeIn {
    to { opacity: 1; }
}

.camera-error {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 24px;
    background: var(--cam-bg);
    text-align: center;
}

.camera-error.d-none {
    display: none;
}

.camera-error svg {
    width: 48px;
    height: 48px;
    fill: none;
    stroke: var(--cam-fg-muted);
    stroke-width: 1.5;
    stroke-linejoin: round;
}

.camera-error__text {
    max-width: 22rem;
    margin: 0;
    color: var(--cam-fg-muted);
}

.camera-error .cam-text-btn {
    color: var(--cam-accent);
    font-weight: 600;
}

/* ---------- Bottom controls ---------- */

.camera-bottom {
    flex: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 8px calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 16px) calc(var(--safe-left) + 16px);
}

.camera-zoom {
    display: flex;
    gap: 8px;
    padding: 4px;
    border-radius: 22px;
    background: var(--cam-scrim);
}

.camera-zoom.d-none {
    display: none;
}

.camera-zoom button {
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
    color: var(--cam-fg);
    font-size: 0.75rem;
    font-weight: 600;
    -webkit-tap-highlight-color: transparent;
}

.camera-zoom button[aria-pressed="true"] {
    color: var(--cam-accent);
    font-size: 0.8125rem;
}

.camera-modes {
    position: relative;
    display: flex;
    align-items: center;
    gap: 4px;
    width: 100%;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    /* Fade the strip edges like native mode pickers. */
    -webkit-mask-image: linear-gradient(90deg, transparent, #000 15%, #000 85%, transparent);
    mask-image: linear-gradient(90deg, transparent, #000 15%, #000 85%, transparent);
    padding: 0 45%;
}

.camera-modes::-webkit-scrollbar {
    display: none;
}

.camera-modes button {
    flex: none;
    min-height: 36px;
    padding: 0 10px;
    border: 0;
    background: transparent;
    color: var(--cam-fg);
    font-size: 0.8125rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    white-space: nowrap;
    scroll-snap-align: center;
    -webkit-tap-highlight-color: transparent;
}

.camera-modes button[aria-checked="true"] {
    color: var(--cam-accent);
}

.camera-modes__divider {
    flex: none;
    width: 1px;
    height: 14px;
    background: rgba(255, 255, 255, 0.3);
    margin: 0 4px;
}

.camera-controls {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    width: 100%;
    max-width: 24rem;
}

.camera-controls > :first-child {
    justify-self: start;
}

.camera-controls > :last-child {
    justify-self: end;
}

/* Keeps its slot so the shutter stays centered. */
.cam-icon-btn.is-unavailable {
    visibility: hidden;
}

.camera-thumb {
    width: 48px;
    height: 48px;
    padding: 0;
    border: 1.5px solid rgba(255, 255, 255, 0.8);
    border-radius: 8px;
    background: #222;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

.camera-thumb:disabled {
    border-color: rgba(255, 255, 255, 0.25);
    background: transparent;
}

.camera-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.camera-thumb.is-new img {
    animation: thumbIn 0.3s ease-out;
}

@keyframes thumbIn {
    from {
        transform: scale(1.6);
        opacity: 0;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

.camera-shutter {
    position: relative;
    width: var(--cam-shutter);
    height: var(--cam-shutter);
    padding: 0;
    border: 4px solid var(--cam-fg);
    border-radius: 50%;
    background: transparent;
    -webkit-tap-highlight-color: transparent;
}

.camera-shutter::after {
    content: "";
    position: absolute;
    inset: 4px;
    border-radius: 50%;
    background: var(--cam-fg);
    transition: transform 0.1s ease-out;
}

.camera-shutter:active::after {
    transform: scale(0.88);
}

.camera-shutter:disabled {
    opacity: 0.4;
}

.camera-shutter:focus-visible,
.cam-icon-btn:focus-visible,
.cam-pill-btn:focus-visible,
.cam-text-btn:focus-visible,
.camera-thumb:focus-visible,
.camera-modes button:focus-visible,
.camera-zoom button:focus-visible,
.camera-menu__row:focus-visible {
    outline: 2px solid var(--cam-accent);
    outline-offset: 2px;
}

/* ---------- Landscape: controls move to the right edge ---------- */

@media (orientation: landscape) and (max-height: 540px) {
    .camera-view {
        flex-direction: row;
    }

    .camera-topbar {
        flex-direction: column;
        padding: calc(var(--safe-top) + 8px) 4px calc(var(--safe-bottom) + 8px) calc(var(--safe-left) + 8px);
    }

    .camera-topbar__title {
        flex: 1;
        display: flex;
        align-items: center;
        writing-mode: vertical-rl;
        transform: rotate(180deg);
    }

    .camera-bottom {
        flex-direction: row-reverse;
        justify-content: center;
        padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 12px) calc(var(--safe-bottom) + 8px) 8px;
    }

    .camera-modes {
        flex-direction: column;
        width: auto;
        height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
        scroll-snap-type: y proximity;
        padding: 40vh 0;
        -webkit-mask-image: linear-gradient(180deg, transparent, #000 15%, #000 85%, transparent);
        mask-image: linear-gradient(180deg, transparent, #000 15%, #000 85%, transparent);
    }

    .camera-modes__divider {
        width: 14px;
        height: 1px;
        margin: 4px 0;
    }

    .camera-zoom {
        flex-direction: column;
    }

    .camera-controls {
        grid-template-columns: auto;
        grid-template-rows: 1fr auto 1fr;
        width: auto;
        height: 100%;
        max-width: none;
        justify-items: center;
    }

    .camera-controls > :first-child {
        order: 3;
        align-self: end;
        justify-self: center;
    }

    .camera-controls > :last-child {
        order: 1;
        align-self: start;
        justify-self: center;
    }

    .camera-shutter {
        order: 2;
    }
}

/* ---------- Preview view ---------- */

.preview-view__top {
    flex: none;
    padding: calc(var(--safe-top) + 10px) 16px 8px;
    text-align: center;
}

.preview-view__title {
    font-size: 0.9375rem;
    font-weight: 600;
}

.preview-view__meta {
    font-size: 0.75rem;
    color: var(--cam-fg-muted);
}

.preview-view__stage {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    touch-action: pinch-zoom;
}

.preview-view__image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.preview-view__actions {
    flex: none;
    display: flex;
    justify-content: space-between;
    padding: 12px calc(var(--safe-right) + 8px) calc(var(--safe-bottom) + 16px) calc(var(--safe-left) + 8px);
}

/* ---------- Settings ---------- */

.settings-group__title {
    margin: 0 0 6px 4px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--bs-secondary-color);
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 52px;
}

.settings-row .form-check-input {
    width: 2.75em;
    height: 1.5em;
    cursor: pointer;
}

.capability-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

/* ---------- Toast ---------- */

.app-toast {
    position: fixed;
    top: calc(var(--safe-top) + 64px);
    left: 50%;
    z-index: 1100;
    transform: translate(-50%, -8px);
    max-width: calc(100% - 32px);
    padding: 8px 16px;
    border-radius: 18px;
    background: rgba(30, 30, 30, 0.9);
    color: #fff;
    font-size: 0.875rem;
    font-weight: 500;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease-out, transform 0.2s ease-out;
}

.app-toast.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

/* ---------- Reduced motion ---------- */

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

    .camera-loading__spinner {
        opacity: 1;
    }
}
