/*
 * Layout and components are shared; the look comes from tokens.
 * body.is-android follows Material 3 (dynamic color injected by MainActivity.kt),
 * body.is-ios follows the iOS Human Interface Guidelines (44 pt touch targets, capsule buttons, inset groups).
 */

/* ===== Tokens: default (iOS / browser) ===== */
:root {
    color-scheme: light dark;

    --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --font-size: 15px;

    --bg: #f2f2f7;
    --header-bg: var(--bg);
    --header-bg-scrolled: var(--bg);
    --header-height: 52px;
    --surface: #ffffff;
    --surface-active: #e5e5ea;
    --text: #000000;
    --text-secondary: rgba(60, 60, 67, 0.6);
    --text-tertiary: rgba(60, 60, 67, 0.3);
    --accent: #007aff;
    --on-accent: #ffffff;
    --border: rgba(60, 60, 67, 0.29);
    --separator: rgba(60, 60, 67, 0.18);
    --track: rgba(120, 120, 128, 0.2);

    --radius-group: 12px;
    --radius-button: 10px;
    --radius-image: 12px;
    --button-height: 44px;
    --gap: 20px;

    /* Android reports system bar insets through --android-inset-* (see MainActivity.kt) */
    --safe-top: max(env(safe-area-inset-top, 0px), var(--android-inset-top, 0px));
    --safe-right: max(env(safe-area-inset-right, 0px), var(--android-inset-right, 0px));
    --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--android-inset-bottom, 0px));
    --safe-left: max(env(safe-area-inset-left, 0px), var(--android-inset-left, 0px));

    /* Material 3 baseline scheme; replaced by the device's dynamic colors on Android 12+ */
    --md-primary: #6750a4;
    --md-on-primary: #ffffff;
    --md-primary-container: #eaddff;
    --md-on-primary-container: #21005d;
    --md-secondary-container: #e8def8;
    --md-on-secondary-container: #1d192b;
    --md-surface: #fef7ff;
    --md-on-surface: #1d1b20;
    --md-on-surface-variant: #49454f;
    --md-outline: #79747e;
    --md-outline-variant: #cac4d0;
    --md-surface-container-low: #f7f2fa;
    --md-surface-container: #f3edf7;
    --md-surface-container-high: #ece6f0;
    --md-surface-container-highest: #e6e0e9;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #000000;
        --surface: #1c1c1e;
        --surface-active: #3a3a3c;
        --text: #ffffff;
        --text-secondary: rgba(235, 235, 245, 0.6);
        --text-tertiary: rgba(235, 235, 245, 0.3);
        --accent: #0a84ff;
        --border: rgba(84, 84, 88, 0.65);
        --separator: rgba(84, 84, 88, 0.6);
        --track: rgba(120, 120, 128, 0.36);

        --md-primary: #d0bcff;
        --md-on-primary: #381e72;
        --md-primary-container: #4f378b;
        --md-on-primary-container: #eaddff;
        --md-secondary-container: #4a4458;
        --md-on-secondary-container: #e8def8;
        --md-surface: #141218;
        --md-on-surface: #e6e0e9;
        --md-on-surface-variant: #cac4d0;
        --md-outline: #938f99;
        --md-outline-variant: #49454f;
        --md-surface-container-low: #1d1b20;
        --md-surface-container: #211f26;
        --md-surface-container-high: #2b2930;
        --md-surface-container-highest: #36343b;
    }
}

/* ===== Reset & base ===== */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    min-height: 100vh;
    background-color: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: var(--font-size);
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
}

button,
select,
input {
    font: inherit;
    color: inherit;
}

svg {
    display: block;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

/* ===== Header (toolbar / top app bar) ===== */
.app-header {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: calc(var(--header-height) + var(--safe-top));
    padding: var(--safe-top) calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
    background-color: var(--header-bg);
    transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

body.is-scrolled .app-header {
    background-color: var(--header-bg-scrolled);
}

h1 {
    flex: 1;
    min-width: 0;
    font-size: 17px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.icon-button {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--accent);
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.icon-button svg {
    width: 22px;
    height: 22px;
}

.back-button .icon-arrow {
    display: none;
}

/* ===== Layout ===== */
.container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--gap);
    padding: 12px calc(16px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(16px + var(--safe-left));
}

/* ===== Start screen ===== */
.drag-drop-area {
    min-height: 260px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 6px;
    padding: 32px 24px;
    border: 2px dashed var(--border);
    border-radius: var(--radius-group);
    background-color: var(--surface);
    text-align: center;
    cursor: pointer;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.drag-drop-area.drag-over {
    border-color: var(--accent);
    background-color: color-mix(in srgb, var(--accent) 10%, var(--surface));
}

.empty-icon {
    margin-bottom: 8px;
    color: var(--text-tertiary);
}

.upload-icon {
    width: 56px;
    height: 56px;
}

.drag-text {
    font-size: 1.15em;
    font-weight: 600;
}

.drag-subtext {
    color: var(--text-secondary);
}

/* ===== Buttons ===== */
.button-container,
.process-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
}

.action-button {
    position: relative;
    overflow: hidden;
    flex: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-width: 0;
    height: var(--button-height);
    padding: 0 20px;
    border: 1px solid transparent;
    border-radius: var(--radius-button);
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    background-color: var(--surface);
    color: var(--accent);
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, filter 0.15s ease;
}

/* The camera button in a desktop browser */
.action-button[hidden] {
    display: none;
}

.btn-icon {
    flex: none;
    width: 18px;
    height: 18px;
}

.action-button:active {
    background-color: var(--surface-active);
}

.camera-button,
.action-button.is-active {
    background-color: var(--accent);
    color: var(--on-accent);
}

.camera-button:active,
.action-button.is-active:active {
    background-color: var(--accent);
    filter: brightness(0.88);
}

.action-button:disabled {
    opacity: 0.4;
    cursor: default;
}

.text-button {
    border: none;
    background: none;
    color: var(--accent);
    font-size: 0.9375em;
    padding: 4px 0;
    cursor: pointer;
}

.text-button:disabled {
    color: var(--text-tertiary);
    cursor: default;
}

/* Crop and mask: ✕ and ✓ as round buttons of one size, with Reset between them */
.edit-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.edit-button {
    flex: none;
    position: relative;
    overflow: hidden;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--edit-button-size, 44px);
    height: var(--edit-button-size, 44px);
    padding: 0;
    border: none;
    border-radius: 50%;
    background-color: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: background-color 0.15s ease, filter 0.15s ease;
}

.edit-button svg {
    width: 24px;
    height: 24px;
}

.edit-button:active {
    background-color: var(--surface-active);
}

.edit-button.done-button {
    background-color: var(--accent);
    color: var(--on-accent);
}

.edit-button.done-button:active {
    filter: brightness(0.88);
}

/* A tool's hint stays close to the options it explains */
.edit-hint {
    margin-bottom: calc(var(--gap) / -2);
}

.edit-actions .reset-button {
    min-height: var(--edit-button-size, 44px);
    padding: 0 12px;
}

.edit-hint {
    text-align: center;
    font-size: 0.875em;
    color: var(--text-secondary);
}

/* ===== Image preview ===== */
.image-preview {
    position: relative;
    align-self: center;
    width: fit-content;
    max-width: 100%;
    min-height: 0;
    overflow: hidden;
    border-radius: var(--radius-image);
    background-color: var(--surface);
}

.image-preview img {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    -webkit-user-drag: none;
}

/* Rings around the detected birds, and the buttons over the photo */
.bird-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 19;
    pointer-events: none;
}

body.is-cropping .bird-overlay {
    display: none;
}

.preview-tools {
    position: absolute;
    right: 8px;
    bottom: 8px;
    z-index: 22;
    display: flex;
    gap: 8px;
}

body.is-cropping .preview-tools,
body.is-masking .preview-tools {
    display: none;
}

.overlay-button {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.55);
    color: #ffffff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.overlay-button svg {
    width: 22px;
    height: 22px;
}

.overlay-button:active {
    background: rgba(0, 0, 0, 0.75);
}

/* Sharing is native, so the buttons exist on iOS and Android only */
.share-button {
    display: none;
}

body.is-ios .share-button,
body.is-android .share-button {
    display: inline-flex;
}

.share-button:disabled {
    opacity: 0.4;
}

.icon-share-ios {
    display: none;
}

body.is-ios .icon-share-ios {
    display: block;
}

body.is-ios .icon-share-material {
    display: none;
}

.marks-toggle .icon-hidden,
.marks-toggle.is-off .icon-shown {
    display: none;
}

.marks-toggle.is-off .icon-hidden {
    display: block;
}

/* ===== Full resolution viewer ===== */
body.viewer-open {
    overflow: hidden;
}

.photo-viewer {
    position: fixed;
    inset: 0;
    z-index: 50;
    background: #000000;
}

.photo-viewer[hidden] {
    display: none;
}

.viewer-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: grab;
}

.viewer-canvas:active {
    cursor: grabbing;
}

.viewer-tools {
    position: absolute;
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    display: flex;
    gap: 12px;
}

.overlay-button.is-active,
.overlay-button.is-active:active {
    background: #ffffff;
    color: #000000;
}

/* The count, in a black bar along the bottom; the photo fits above it (measureViewerInset) */
.photo-viewer {
    --viewer-bar: 36px;
}

.viewer-count {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--viewer-bar) + var(--safe-bottom));
    box-sizing: border-box;
    margin: 0;
    padding: 0 calc(12px + var(--safe-right)) var(--safe-bottom) calc(12px + var(--safe-left));
    background: #000000;
    color: #ffffff;
    font-size: 15px;
    font-weight: 600;
    line-height: var(--viewer-bar);
    text-align: center;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
}

/* Detection settings over the photo: a half-transparent sheet along the bottom on tall
   screens (above the count bar), a column on the right on wide ones. Above the sheet the photo
   fits in the rest of the screen (measureViewerInset); it shows through the column, and the
   sheet when zoomed in. */
.viewer-settings {
    /* Always dark over the photo, whatever the system theme */
    color-scheme: dark;
    --text: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.75);
    --text-tertiary: rgba(255, 255, 255, 0.4);
    --accent: #64b5ff;
    --border: rgba(255, 255, 255, 0.35);
    --separator: rgba(255, 255, 255, 0.14);
    --surface: transparent;
    --track: rgba(255, 255, 255, 0.28);
    --fill: rgba(255, 255, 255, 0.12);
    --fill-active: rgba(255, 255, 255, 0.2);
    --switch-on: #30d158;
    --md-primary: #d0bcff;
    --md-on-primary: #381e72;
    --md-secondary-container: rgba(255, 255, 255, 0.3);
    --md-on-surface: #ffffff;
    --md-on-surface-variant: rgba(255, 255, 255, 0.75);
    --md-outline: rgba(255, 255, 255, 0.6);
    --md-outline-variant: rgba(255, 255, 255, 0.2);
    --md-surface-container-low: transparent;
    --md-surface-container-highest: rgba(255, 255, 255, 0.15);

    position: absolute;
    left: 0;
    right: 0;
    bottom: calc(var(--viewer-bar) + var(--safe-bottom));
    max-height: min(40%, 420px);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 0 var(--safe-right) 8px var(--safe-left);
    border-radius: 16px 16px 0 0;
    background: rgba(0, 0, 0, 0.5);
    color: var(--text);
    font-size: 14px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
}

.viewer-settings[hidden] {
    display: none;
}

/* The settings scroll under the handle */
.viewer-settings-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

/* A grabber, as on native sheets: drag it to move the panel anywhere over the photo */
.viewer-settings-handle {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 24px;
    cursor: grab;
    touch-action: none;
}

.viewer-settings-handle::before {
    content: "";
    width: 36px;
    height: 5px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.45);
    transition: background-color 0.15s ease;
}

.viewer-settings-handle:hover::before,
.viewer-settings-handle.is-dragging::before {
    background: rgba(255, 255, 255, 0.8);
}

.viewer-settings-handle.is-dragging {
    cursor: grabbing;
}

/* Where a moved panel may go (placeViewerSettings): the screen within the safe areas,
   above the count bar */
.viewer-settings-area {
    position: absolute;
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    bottom: calc(12px + var(--viewer-bar) + var(--safe-bottom));
    left: calc(12px + var(--safe-left));
    visibility: hidden;
    pointer-events: none;
}

@media (min-aspect-ratio: 1/1) {
    .viewer-settings {
        left: auto;
        top: calc(72px + var(--safe-top));
        right: calc(12px + var(--safe-right));
        bottom: auto;
        width: min(360px, 44%);
        max-height: calc(100% - 84px - var(--viewer-bar) - var(--safe-top) - var(--safe-bottom));
        padding: 0 0 6px;
        border-radius: 16px;
    }
}

/* Android and iOS: pulled up by its grabber, the bottom sheet grows to just under the viewer's
   buttons (the tall detent of onViewerSheetUp); the column of wide screens keeps its height */
@media not all and (min-aspect-ratio: 1/1) {
    .viewer-settings.is-expanded {
        max-height: calc(100% - 72px - var(--safe-top) - var(--viewer-bar) - var(--safe-bottom));
    }
}

/* A taller grabber is easier to catch with a finger */
body.is-android .viewer-settings-handle,
body.is-ios .viewer-settings-handle {
    height: 28px;
    cursor: default;
}

/* Moved by its handle: a card where it was let go (left and top set by placeViewerSettings),
   floating over the photo, which keeps the whole screen */
body .viewer-settings.is-floating {
    right: auto;
    bottom: auto;
    width: min(360px, calc(100% - 24px - var(--safe-left) - var(--safe-right)));
    max-height: min(60%, calc(100% - 24px - var(--viewer-bar) - var(--safe-top) - var(--safe-bottom)));
    padding: 0 0 6px;
    border-radius: 16px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

/* ===== Result header: the count, and the Result / Original switch ===== */
.count-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.count-text {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
    margin: 0;
    font-size: 1.1em;
    font-weight: 500;
}

#countValue {
    font-size: 2.4em;
    font-weight: 700;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
}

/* The header's edge squeezes the count while scrolling (scale set by updateCountPosition), then it
 * swaps to plain text under the photo. Collapsed, that line also cancels the container's gap, so
 * the photo keeps its usual distance from the tools. */
.count-text {
    transform-origin: left bottom;
}

.count-below {
    /* No fade out: the two counts must never show at once */
    transition: max-height 0.2s ease, margin-top 0.2s ease;
    max-height: 0;
    margin: calc(-1 * var(--gap)) 0 0;
    overflow: hidden;
    opacity: 0;
    transform: translateY(-10px) scale(0.8);
    transform-origin: top center;
    filter: blur(3px);
    line-height: 1.5;
    font-weight: 500;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* Comes out from under the photo, sharpening as it arrives, with a slight overshoot */
body.is-count-below .count-below {
    transition: opacity 0.25s ease, filter 0.25s ease, transform 0.45s cubic-bezier(0.34, 1.56, 0.64, 1),
        max-height 0.25s ease, margin-top 0.25s ease;
    max-height: 1.5em;
    margin-top: 0;
    opacity: 1;
    transform: none;
    filter: none;
}

/* ===== Tools under the photo, and the share button ===== */
.process-actions .tool-button {
    flex-direction: column;
    height: auto;
    min-height: 72px;
    padding: 12px 8px;
    gap: 6px;
    font-size: 0.8125em;
}

.tool-button .btn-icon {
    width: 24px;
    height: 24px;
}

/* ===== Crop =====
 * The photo sits on a dark mat so the handles have room at its edges. The frame dims the
 * photo around it with a shadow that .image-preview clips. Each handle is a 44 px hit area
 * centred on the frame line, drawn inside the frame so it stays visible at the photo's edge. */
body.is-cropping .image-preview {
    padding: 16px;
    background-color: #000000;
}

.crop-overlay {
    position: absolute;
    inset: 0;
    display: none;
    z-index: 20;
    touch-action: none;
    cursor: crosshair;
}

body.is-cropping .crop-overlay {
    inset: 16px;
}

.crop-overlay.is-active {
    display: block;
}

.crop-box {
    position: absolute;
    outline: 1px solid rgba(255, 255, 255, 0.9);
    box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.6);
    cursor: move;
}

/* Rule of thirds while dragging */
.crop-grid {
    --line: rgba(255, 255, 255, 0.55);
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease;
    background:
        linear-gradient(var(--line), var(--line)) 33.333% 0 / 1px 100% no-repeat,
        linear-gradient(var(--line), var(--line)) 66.667% 0 / 1px 100% no-repeat,
        linear-gradient(var(--line), var(--line)) 0 33.333% / 100% 1px no-repeat,
        linear-gradient(var(--line), var(--line)) 0 66.667% / 100% 1px no-repeat;
}

.crop-overlay.is-dragging .crop-grid {
    opacity: 1;
}

.crop-handle {
    position: absolute;
}

.crop-handle::before {
    content: "";
    position: absolute;
    filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.6));
}

.crop-corner {
    width: 44px;
    height: 44px;
}

.crop-corner::before {
    width: 22px;
    height: 22px;
    border: 0 solid #ffffff;
}

.crop-corner[data-handle="nw"] {
    left: -22px;
    top: -22px;
    cursor: nwse-resize;
}

.crop-corner[data-handle="nw"]::before {
    left: 22px;
    top: 22px;
    border-top-width: 3px;
    border-left-width: 3px;
}

.crop-corner[data-handle="ne"] {
    right: -22px;
    top: -22px;
    cursor: nesw-resize;
}

.crop-corner[data-handle="ne"]::before {
    right: 22px;
    top: 22px;
    border-top-width: 3px;
    border-right-width: 3px;
}

.crop-corner[data-handle="se"] {
    right: -22px;
    bottom: -22px;
    cursor: nwse-resize;
}

.crop-corner[data-handle="se"]::before {
    right: 22px;
    bottom: 22px;
    border-bottom-width: 3px;
    border-right-width: 3px;
}

.crop-corner[data-handle="sw"] {
    left: -22px;
    bottom: -22px;
    cursor: nesw-resize;
}

.crop-corner[data-handle="sw"]::before {
    left: 22px;
    bottom: 22px;
    border-bottom-width: 3px;
    border-left-width: 3px;
}

.crop-edge::before {
    background-color: #ffffff;
    border-radius: 2px;
}

.crop-edge[data-handle="n"],
.crop-edge[data-handle="s"] {
    left: 22px;
    right: 22px;
    height: 32px;
    cursor: ns-resize;
}

.crop-edge[data-handle="n"] {
    top: -16px;
}

.crop-edge[data-handle="s"] {
    bottom: -16px;
}

.crop-edge[data-handle="n"]::before,
.crop-edge[data-handle="s"]::before {
    left: 50%;
    width: 22px;
    height: 3px;
    margin-left: -11px;
}

.crop-edge[data-handle="n"]::before {
    top: 16px;
}

.crop-edge[data-handle="s"]::before {
    bottom: 16px;
}

.crop-edge[data-handle="e"],
.crop-edge[data-handle="w"] {
    top: 22px;
    bottom: 22px;
    width: 32px;
    cursor: ew-resize;
}

.crop-edge[data-handle="e"] {
    right: -16px;
}

.crop-edge[data-handle="w"] {
    left: -16px;
}

.crop-edge[data-handle="e"]::before,
.crop-edge[data-handle="w"]::before {
    top: 50%;
    width: 3px;
    height: 22px;
    margin-top: -11px;
}

.crop-edge[data-handle="e"]::before {
    right: 16px;
}

.crop-edge[data-handle="w"]::before {
    left: 16px;
}

/* ===== Mask overlay ===== */
.mask-overlay {
    position: absolute;
    inset: 0;
    display: none;
    pointer-events: none;
    z-index: 21;
}

.mask-overlay.is-active,
.mask-overlay.has-mask {
    display: block;
}

.mask-overlay.is-active {
    pointer-events: auto;
    touch-action: none;
    cursor: crosshair;
}

.mask-overlay canvas {
    width: 100%;
    height: 100%;
    display: block;
}

.mask-overlay.has-mask canvas {
    opacity: 0.9;
}

.mask-brush-cursor {
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    box-sizing: border-box;
    border: 2px solid rgba(226, 77, 92, 0.95);
    border-radius: 50%;
    background: rgba(226, 77, 92, 0.2);
    pointer-events: none;
}

.mask-brush-cursor.is-visible {
    display: block;
}

/* ===== History ===== */
.history-section {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.history-section[hidden] {
    display: none;
}

.history-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding-right: 16px;
}

.history-list {
    list-style: none;
}

.history-item {
    position: relative;
    display: flex;
    align-items: center;
}

.history-item + .history-item {
    border-top: 0.5px solid var(--separator);
}

.history-item.is-loading {
    opacity: 0.5;
    pointer-events: none;
}

.history-open {
    position: relative;
    overflow: hidden;
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0 10px 16px;
    border: none;
    background: none;
    text-align: left;
    cursor: pointer;
}

.history-open:active {
    background-color: var(--surface-active);
}

.history-thumb {
    flex: none;
    width: 56px;
    height: 56px;
    border-radius: 8px;
    object-fit: cover;
    background-color: var(--surface-active);
}

.history-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.history-count {
    font-weight: 600;
}

.history-meta {
    font-size: 0.8125em;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-more {
    flex: none;
    margin-right: 8px;
    color: var(--text-secondary);
}

/* Menu of a history row: Share, Delete */
:root {
    --menu-bg: #ffffff;
    --menu-shadow: 0 4px 20px rgba(0, 0, 0, 0.18), 0 0 0 0.5px rgba(0, 0, 0, 0.1);
    --danger: #d70015;
}

@media (prefers-color-scheme: dark) {
    :root {
        --menu-bg: #2c2c2e;
        --menu-shadow: 0 4px 20px rgba(0, 0, 0, 0.5), 0 0 0 0.5px rgba(255, 255, 255, 0.12);
        --danger: #ff453a;
    }
}

.history-menu {
    position: fixed;
    z-index: 60;
    display: flex;
    flex-direction: column;
    min-width: 180px;
    padding: 4px;
    border-radius: 10px;
    background: var(--menu-bg);
    box-shadow: var(--menu-shadow);
    color: var(--text);
}

.history-menu-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 40px;
    padding: 0 12px;
    border: none;
    border-radius: 6px;
    background: none;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.history-menu-item svg {
    flex: none;
    width: 20px;
    height: 20px;
}

.history-menu-item:active,
.history-menu-item:focus-visible {
    background-color: var(--surface-active);
    outline: none;
}

.history-menu-item.is-danger {
    color: var(--danger);
}

/* ===== Settings (grouped form) ===== */
.settings-panel {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.settings-title {
    padding: 0 16px;
    font-size: 0.8125em;
    font-weight: 400;
    text-transform: uppercase;
    color: var(--text-secondary);
}

.group {
    background-color: var(--surface);
    border-radius: var(--radius-group);
    overflow: hidden;
}

.settings-field {
    display: grid;
    grid-template-columns: minmax(110px, 38%) minmax(0, 1fr);
    align-items: center;
    gap: 4px 12px;
    min-height: 44px;
    padding: 6px 16px;
}

.group .settings-field+.settings-field,
.settings-advanced,
.settings-advanced-grid {
    border-top: 0.5px solid var(--separator);
}

.settings-advanced-grid> :first-child {
    border-top: none;
}

.settings-field label {
    color: var(--text);
}

.range-row {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.range-row input[type="range"] {
    flex: 1;
    min-width: 0;
    accent-color: var(--accent);
}

.range-value {
    min-width: 3em;
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
}

.select-wrap {
    position: relative;
    justify-self: end;
    max-width: 100%;
}

.settings-select {
    max-width: 100%;
    border: 1px solid var(--border);
    background-color: var(--surface);
    padding: 4px 8px;
    border-radius: 8px;
    cursor: pointer;
}

.toggle-field {
    display: block;
}

.toggle-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 32px;
    cursor: pointer;
}

.toggle-label input[type="checkbox"] {
    flex: none;
    accent-color: var(--accent);
}

/* ===== Setting tips: an ⓘ after each setting's name, and the tip it shows (initSettingTips) ===== */
.label-with-tip {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    min-width: 0;
}

/* A small mark with a bigger target: the negative margins keep the row's height */
.tip-button {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin: -6px -4px -6px 0;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: none;
    color: var(--text-tertiary);
    cursor: help;
    transition: color 0.15s ease;
}

.tip-button svg {
    width: 16px;
    height: 16px;
}

.tip-button:hover,
.tip-button[aria-describedby] {
    color: var(--accent);
}

.tip-button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

/* Inverted colors, as tooltips are on every platform; it never takes the pointer */
.setting-tip {
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    box-sizing: border-box;
    max-width: min(280px, calc(100vw - 16px));
    padding: 8px 12px;
    border-radius: 10px;
    background-color: color-mix(in srgb, var(--text) 90%, transparent);
    color: var(--bg);
    font-size: 0.8125rem;
    line-height: 1.4;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    pointer-events: none;
}

.setting-tip[hidden] {
    display: none;
}

/* Over the photo viewer's dark sheet */
.setting-tip.is-dark {
    background-color: rgba(28, 28, 30, 0.96);
    color: #ffffff;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45), inset 0 0 0 0.5px rgba(255, 255, 255, 0.2);
}

/* Material 3 plain tooltip: inverse surface, small corners */
body.is-android .setting-tip:not(.is-dark) {
    border-radius: 4px;
    background-color: var(--md-on-surface);
    color: var(--md-surface);
}

.advanced-summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 44px;
    padding: 0 16px;
    list-style: none;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.advanced-summary::-webkit-details-marker {
    display: none;
}

.expand-icon {
    width: 20px;
    height: 20px;
    color: var(--text-secondary);
    transition: transform 0.2s ease;
}

.settings-advanced[open] .expand-icon {
    transform: rotate(180deg);
}

/* Restore defaults: the last row of Advanced */
.settings-reset {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 44px;
    padding: 0 16px;
    border-top: 0.5px solid var(--separator);
}

.mask-brush-controls .settings-field {
    grid-template-columns: auto 1fr;
    border-top: none;
}

/* ===== Ripple (Android) ===== */
.ripple {
    position: absolute;
    border-radius: 50%;
    background-color: currentColor;
    opacity: 0.12;
    transform: scale(0);
    pointer-events: none;
    animation: ripple 450ms cubic-bezier(0.2, 0, 0, 1) forwards;
}

.ripple.is-fading {
    transition: opacity 250ms linear;
    opacity: 0;
}

@keyframes ripple {
    to {
        transform: scale(1);
    }
}

/* =====================================================================
 * Android — Material 3
 * ===================================================================== */
body.is-android {
    --font: Roboto, "Google Sans", system-ui, sans-serif;
    --font-size: 16px;

    --bg: var(--md-surface);
    --header-bg: var(--md-surface);
    --header-bg-scrolled: var(--md-surface-container);
    --header-height: 64px;
    --surface: var(--md-surface-container);
    --text: var(--md-on-surface);
    --text-secondary: var(--md-on-surface-variant);
    --text-tertiary: var(--md-outline);
    --accent: var(--md-primary);
    --on-accent: var(--md-on-primary);
    --border: var(--md-outline);
    --separator: var(--md-outline-variant);

    --radius-group: 16px;
    --radius-button: 9999px;
    --radius-image: 16px;
    --button-height: 40px;
    --gap: 16px;
    letter-spacing: 0.15px;
}

/* Top app bar */
body.is-android .app-header {
    padding-left: calc(4px + var(--safe-left));
    padding-right: calc(4px + var(--safe-right));
    gap: 0;
}

body.is-android h1 {
    padding-left: 12px;
    font-size: 22px;
    line-height: 28px;
    font-weight: 400;
    letter-spacing: 0;
}

body.is-android.has-back h1 {
    padding-left: 4px;
}

body.is-android .icon-button {
    width: 48px;
    height: 48px;
    color: var(--md-on-surface-variant);
}

body.is-android .icon-button svg {
    width: 24px;
    height: 24px;
}

body.is-android .back-button {
    color: var(--md-on-surface);
}

body.is-android .back-button .icon-chevron {
    display: none;
}

body.is-android .back-button .icon-arrow {
    display: block;
}

body.is-android .container {
    min-height: calc(100vh - var(--header-height) - var(--safe-top));
    padding-top: 8px;
}

/* Start screen: empty state with the actions at the bottom */
body.is-android .drag-drop-area {
    flex: 1;
    border: none;
    background: transparent;
    cursor: auto;
    gap: 8px;
}

body.is-android .empty-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 112px;
    height: 112px;
    margin-bottom: 16px;
    border-radius: 50%;
    background-color: var(--md-primary-container);
    color: var(--md-on-primary-container);
}

body.is-android .upload-icon {
    width: 48px;
    height: 48px;
}

body.is-android .drag-text {
    font-size: 24px;
    line-height: 32px;
    font-weight: 400;
}

body.is-android .drag-subtext {
    max-width: 320px;
    font-size: 14px;
    line-height: 20px;
    letter-spacing: 0.25px;
}

body.is-android .button-container {
    flex-direction: column;
    gap: 12px;
}

body.is-android .button-container .action-button {
    flex: none;
    height: 56px;
    font-size: 16px;
}

/* Buttons: filled, tonal, outlined toggle, text */
body.is-android .action-button {
    padding: 0 24px;
    border: none;
    font-size: 14px;
    line-height: 20px;
    font-weight: 500;
    letter-spacing: 0.1px;
    background-color: transparent;
    color: var(--md-primary);
}

body.is-android .action-button::before,
body.is-android .edit-button::before,
body.is-android .history-open::before,
body.is-android .icon-button::before,
body.is-android .advanced-summary::before {
    content: "";
    position: absolute;
    inset: 0;
    background-color: currentColor;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s linear;
}

body.is-android .action-button:active::before,
body.is-android .edit-button:active::before,
body.is-android .history-open:active::before,
body.is-android .icon-button:active::before,
body.is-android .advanced-summary:active::before {
    opacity: 0.1;
}

body.is-android .action-button:active,
body.is-android .camera-button:active,
body.is-android .action-button.is-active:active {
    filter: none;
}

body.is-android .camera-button {
    background-color: var(--md-primary);
    color: var(--md-on-primary);
}

body.is-android .gallery-button {
    background-color: var(--md-secondary-container);
    color: var(--md-on-secondary-container);
}

body.is-android .crop-button,
body.is-android .mask-button {
    padding: 0 16px;
    color: var(--md-on-surface);
    box-shadow: inset 0 0 0 1px var(--md-outline);
}

body.is-android .action-button.is-active {
    background-color: var(--md-secondary-container);
    color: var(--md-on-secondary-container);
    box-shadow: none;
}

body.is-android .count-label {
    font-size: 22px;
    line-height: 28px;
    font-weight: 400;
}

body.is-android #countValue {
    font-weight: 500;
    color: var(--md-primary);
}

/* Settings: list in a filled card */
body.is-android .settings-title {
    padding: 8px 16px 4px;
    font-size: 14px;
    line-height: 20px;
    font-weight: 500;
    letter-spacing: 0.1px;
    text-transform: none;
    color: var(--md-primary);
}

body.is-android .group {
    background-color: var(--md-surface-container-low);
}

body.is-android .settings-field {
    grid-template-columns: 1fr;
    gap: 0;
    padding: 12px 16px 4px;
}

body.is-android .group .settings-field+.settings-field,
body.is-android .settings-advanced-grid {
    border-top: none;
}

body.is-android .settings-advanced {
    border-top: 1px solid var(--md-outline-variant);
    margin-top: 8px;
}

body.is-android .settings-reset {
    border-top: none;
    padding: 8px 16px 12px;
}

body.is-android .settings-field label {
    font-size: 16px;
    line-height: 24px;
}

body.is-android .range-value {
    min-width: 2.75em;
    font-size: 14px;
    font-weight: 500;
    color: var(--md-on-surface-variant);
}

/* Slider */
body.is-android input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 44px;
    background: transparent;
    touch-action: pan-y;
}

body.is-android input[type="range"]::-webkit-slider-runnable-track {
    height: 4px;
    border-radius: 2px;
    background: linear-gradient(to right,
            var(--md-primary) 0 var(--fill, 50%),
            var(--md-secondary-container) var(--fill, 50%) 100%);
}

body.is-android input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    margin-top: -8px;
    border-radius: 50%;
    background-color: var(--md-primary);
    box-shadow: 0 0 0 0 color-mix(in srgb, var(--md-primary) 12%, transparent);
    transition: box-shadow 0.15s ease;
}

body.is-android input[type="range"]:active::-webkit-slider-thumb {
    box-shadow: 0 0 0 10px color-mix(in srgb, var(--md-primary) 12%, transparent);
}

/* Switch */
body.is-android .toggle-field {
    padding: 8px 16px;
}

body.is-android .toggle-label {
    min-height: 40px;
    font-size: 16px;
}

body.is-android .toggle-label input[type="checkbox"] {
    -webkit-appearance: none;
    appearance: none;
    position: relative;
    width: 52px;
    height: 32px;
    border-radius: 16px;
    border: 2px solid var(--md-outline);
    background-color: var(--md-surface-container-highest);
    transition: background-color 0.2s ease, border-color 0.2s ease;
}

body.is-android .toggle-label input[type="checkbox"]::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 6px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: var(--md-outline);
    transform: translateY(-50%);
    transition: left 0.2s cubic-bezier(0.2, 0, 0, 1), width 0.2s ease, height 0.2s ease,
        background-color 0.2s ease;
}

body.is-android .toggle-label input[type="checkbox"]:checked {
    border-color: var(--md-primary);
    background-color: var(--md-primary);
}

body.is-android .toggle-label input[type="checkbox"]:checked::before {
    left: 22px;
    width: 24px;
    height: 24px;
    background-color: var(--md-on-primary);
}

/* Outlined select field */
body.is-android .select-wrap {
    justify-self: stretch;
    margin: 8px 0;
}

body.is-android .select-wrap::after {
    content: "";
    position: absolute;
    right: 16px;
    top: 50%;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid var(--md-on-surface-variant);
    transform: translateY(-50%);
    pointer-events: none;
}

body.is-android .settings-select {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 56px;
    padding: 0 44px 0 16px;
    border: 1px solid var(--md-outline);
    border-radius: 4px;
    background: transparent;
    font-size: 16px;
}

body.is-android .advanced-summary {
    min-height: 56px;
    font-size: 16px;
}

body.is-android .expand-icon {
    width: 24px;
    height: 24px;
    color: var(--md-on-surface-variant);
}

body.is-android .image-preview img {
    touch-action: manipulation;
}

/* History: list items with a leading thumbnail */
body.is-android.has-history .drag-drop-area {
    flex: none;
    padding: 16px 24px 8px;
}

body.is-android .history-header {
    padding-right: 4px;
}

body.is-android .text-button {
    position: relative;
    overflow: hidden;
    min-height: 40px;
    padding: 0 12px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 0.1px;
    color: var(--md-primary);
}

body.is-android .history-item + .history-item {
    border-top: none;
}

body.is-android .history-open {
    padding: 8px 0 8px 16px;
    gap: 16px;
}

body.is-android .history-open:active {
    background-color: transparent;
}

body.is-android .history-thumb {
    border-radius: 12px;
}

body.is-android .history-count {
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
}

body.is-android .history-meta {
    font-size: 14px;
    line-height: 20px;
    letter-spacing: 0.25px;
}

body.is-android .viewer-tools .overlay-button {
    width: 48px;
    height: 48px;
}

/* =====================================================================
 * iOS — Human Interface Guidelines
 * ===================================================================== */
body.is-ios {
    --font: -apple-system, "SF Pro Text", system-ui, sans-serif;
    --font-size: 17px;

    --header-height: 54px;
    --header-bg-scrolled: color-mix(in srgb, var(--bg) 72%, transparent);
    --fill: rgba(120, 120, 128, 0.16);
    --fill-active: rgba(120, 120, 128, 0.28);
    --glass: rgba(255, 255, 255, 0.72);
    --glass-shadow: 0 1px 4px rgba(0, 0, 0, 0.08), 0 0 0 0.5px rgba(0, 0, 0, 0.06);
    --switch-on: #34c759;

    --radius-group: 24px;
    --radius-button: 999px;
    --radius-image: 20px;
    --button-height: 52px;
    --gap: 24px;
}

@media (prefers-color-scheme: dark) {
    body.is-ios {
        --fill: rgba(120, 120, 128, 0.32);
        --fill-active: rgba(120, 120, 128, 0.44);
        --glass: rgba(44, 44, 46, 0.72);
        --glass-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 0 0.5px rgba(255, 255, 255, 0.08);
        --switch-on: #30d158;
    }
}

/* No double-tap-to-zoom: taps register at once, as in a native app */
body.is-ios {
    touch-action: manipulation;
}

/* Navigation bar: centered title, round glass back button, language menu on the right */
body.is-ios .app-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 12px;
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    backdrop-filter: saturate(180%) blur(20px);
}

body.is-ios .back-button {
    grid-column: 1;
    justify-self: start;
}

body.is-ios h1 {
    grid-column: 2;
    text-align: center;
}

body.is-ios .icon-button{
    height: 44px;
    border: none;
    border-radius: var(--radius-button);
    background: var(--glass);
    box-shadow: var(--glass-shadow);
    color: var(--text);
}

body.is-ios .icon-button {
    width: 44px;
}

body.is-ios .icon-button svg {
    width: 20px;
    height: 20px;
}

body.is-ios .icon-button:active{
    background: var(--fill-active);
}

/* Start screen: empty state with the actions at the bottom, like a native empty view */
body.is-ios .container {
    min-height: calc(100vh - var(--header-height) - var(--safe-top));
}

body.is-ios .drag-drop-area {
    flex: 1;
    gap: 8px;
    border: none;
    background: transparent;
    cursor: auto;
}

body.is-ios .empty-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 104px;
    height: 104px;
    margin-bottom: 16px;
    border-radius: 50%;
    background-color: color-mix(in srgb, var(--accent) 14%, transparent);
    color: var(--accent);
}

body.is-ios .upload-icon {
    width: 46px;
    height: 46px;
}

body.is-ios .drag-text {
    font-size: 22px;
    line-height: 28px;
    font-weight: 700;
}

body.is-ios .drag-subtext {
    max-width: 300px;
    font-size: 15px;
    line-height: 20px;
}

body.is-ios .button-container {
    flex-direction: column;
    gap: 12px;
}

/* Buttons: capsules, filled for the main action and gray for the rest */
body.is-ios .action-button {
    flex: none;
    min-height: var(--button-height);
    height: auto;
    padding: 0 24px;
    border: none;
    font-size: 17px;
    background-color: var(--fill);
    color: var(--accent);
}

body.is-ios .process-actions .action-button {
    flex: 1;
}

body.is-ios .btn-icon {
    width: 20px;
    height: 20px;
}

body.is-ios .action-button:active {
    background-color: var(--fill-active);
}

body.is-ios .camera-button,
body.is-ios .action-button.is-active {
    background-color: var(--accent);
    color: var(--on-accent);
}

body.is-ios .camera-button:active,
body.is-ios .action-button.is-active:active {
    background-color: var(--accent);
    filter: brightness(0.85);
}

/* Result */
body.is-ios .count-label {
    font-size: 20px;
    font-weight: 600;
}

body.is-ios .viewer-tools .overlay-button {
    width: 44px;
    height: 44px;
}

/* History: inset grouped list with thumbnails */
body.is-ios.has-history .drag-drop-area {
    flex: none;
    padding: 24px 24px 8px;
}

body.is-ios .history-header {
    padding-right: 20px;
}

body.is-ios .history-header .settings-title {
    padding-left: 20px;
}

body.is-ios .text-button {
    font-size: 15px;
}

body.is-ios .history-open {
    padding: 10px 0 10px 20px;
}

body.is-ios .history-open:active {
    background-color: var(--fill);
}

body.is-ios .history-item + .history-item {
    border-top: none;
}

body.is-ios .history-item + .history-item::before {
    content: "";
    position: absolute;
    top: 0;
    left: 88px;
    right: 0;
    border-top: 0.5px solid var(--separator);
}

body.is-ios .history-thumb {
    border-radius: 10px;
}

body.is-ios .history-count {
    font-size: 17px;
}

body.is-ios .history-meta {
    font-size: 15px;
}

body.is-ios .history-more {
    background: transparent;
    box-shadow: none;
    color: var(--text-tertiary);
}

body.is-ios .history-more:active {
    background: var(--fill);
}

/* Settings: inset grouped list */
body.is-ios .settings-title {
    padding: 0 20px;
    font-size: 13px;
}

body.is-ios .settings-field {
    min-height: 52px;
    padding: 10px 20px;
    gap: 6px 12px;
}

body.is-ios .settings-reset {
    min-height: 44px;
    padding: 0 20px;
}

body.is-ios .range-row {
    gap: 14px;
}

body.is-ios .range-value {
    min-width: 2.6em;
}

/* Pop-up menu row: label on the left, value with an up/down chevron on the right */
body.is-ios .settings-field:has(.select-wrap) {
    grid-template-columns: auto minmax(0, 1fr);
}

body.is-ios .select-wrap {
    display: flex;
    align-items: center;
    min-width: 0;
    color: var(--text-secondary);
}

body.is-ios .select-wrap::after {
    content: "";
    flex: none;
    width: 9px;
    height: 15px;
    margin-left: 6px;
    background-color: currentColor;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 15'%3E%3Cpath d='M1 5.5 4.5 2 8 5.5M1 9.5 4.5 13 8 9.5' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 9 15'%3E%3Cpath d='M1 5.5 4.5 2 8 5.5M1 9.5 4.5 13 8 9.5' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
    pointer-events: none;
}

body.is-ios .settings-select {
    -webkit-appearance: none;
    appearance: none;
    min-width: 0;
    width: 100%;
    border: none;
    background: transparent;
    padding: 8px 0;
    font-size: 17px;
    color: inherit;
    text-align: right;
    text-align-last: right;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

body.is-ios .toggle-label {
    min-height: 32px;
}

body.is-ios .toggle-label input[type="checkbox"] {
    accent-color: var(--switch-on);
}

body.is-ios .advanced-summary {
    min-height: 52px;
    padding: 0 20px;
}

/* iOS WebKit ignores taps on the summary's own empty area, so let the label fill the row */
body.is-ios .advanced-summary > span {
    flex: 1;
}

body.is-ios .advanced-summary:active {
    background-color: var(--fill);
}

body.is-ios .expand-icon {
    color: var(--text-tertiary);
    transform: rotate(-90deg);
}

body.is-ios .settings-advanced[open] .expand-icon {
    transform: rotate(0deg);
}

/* ===== Wide screens: the tools in a side panel beside the photo =====
 * Wide windows, tablets and phones in landscape, on every platform. The photo comes first and
 * the panel sits on its trailing side, headed by the count, with the tools and the active tool's
 * options under it; the panel scrolls on its own. The photo takes the room the panel leaves and
 * the two are centered together, so a narrow (portrait) photo stays next to the panel. In one
 * column the groups (.editor-photo, .photo-stage, .editor-panel) are display: contents, so the
 * items keep the container's gap. */
.editor-photo,
.photo-stage,
.editor-panel {
    display: contents;
}

@media (min-width: 900px), (orientation: landscape) and (min-width: 560px) and (max-height: 600px) {
    body.has-back {
        min-height: 100dvh;
    }

    /* The container is the size container the photo fits into (its cq units are its content box) */
    body.has-back .container {
        --panel-width: clamp(272px, 30vw, 360px);
        --panel-gap: clamp(16px, 2vw, 24px);
        max-width: 1600px;
        height: calc(100dvh - var(--header-height) - var(--safe-top));
        min-height: 0;
        container-type: size;
        display: grid;
        grid-template-columns: minmax(0, auto) var(--panel-width);
        grid-template-rows: auto minmax(0, 1fr);
        justify-content: center;
        column-gap: var(--panel-gap);
        row-gap: 12px;
        padding-bottom: calc(16px + var(--safe-bottom));
    }

    /* The count and the panel reserve the same scrollbar gutter (none with overlay scrollbars),
       so they line up and the tools don't narrow when the panel starts to scroll */
    body.has-back #countLabel {
        grid-column: 2;
        grid-row: 1;
        overflow: hidden;
        scrollbar-gutter: stable;
        scrollbar-width: thin;
    }

    body.has-back .photo-stage {
        grid-column: 1;
        grid-row: 1 / -1;
        display: flex;
        /* The photo's top edge lines up with the top of the panel's column */
        align-items: flex-start;
        justify-content: center;
        min-width: 0;
        min-height: 0;
    }

    body.has-back .image-preview {
        flex: none;
        align-self: flex-start;
    }

    body.has-back .image-preview img {
        max-width: calc(100cqw - var(--panel-width) - var(--panel-gap));
        max-height: 100cqh;
    }

    /* The crop mat's padding comes out of the room for the photo */
    body.has-back.is-cropping .image-preview img {
        max-width: calc(100cqw - var(--panel-width) - var(--panel-gap) - 32px);
        max-height: calc(100cqh - 32px);
    }

    /* The count never scrolls away here (main.js shows it with an inline style) */
    body.has-back .count-below {
        display: none !important;
    }

    body.has-back .editor-panel {
        grid-column: 2;
        grid-row: 2;
        display: flex;
        flex-direction: column;
        gap: var(--gap);
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-gutter: stable;
        scrollbar-width: thin;
    }

    body.has-back .editor-panel > * {
        flex: none;
    }

    /* ✕ and ✓ follow the tool's options, and stay in reach when the panel scrolls */
    body.has-back .editor-panel > .edit-actions {
        position: sticky;
        bottom: 0;
        z-index: 1;
        padding-top: 4px;
        background-color: var(--bg);
    }

    /* The panel is narrow: each control gets its whole width, under its label, as on phones.
       iOS keeps its pop-up row (label, then the value on the trailing side). */
    body.has-back .editor-panel .settings-field {
        grid-template-columns: minmax(0, 1fr);
    }

    body.has-back:not(.is-ios) .editor-panel .select-wrap {
        justify-self: stretch;
    }

    body.has-back:not(.is-ios) .editor-panel .settings-select {
        width: 100%;
    }

    body.is-ios.has-back .editor-panel .settings-field:has(.select-wrap) {
        grid-template-columns: auto minmax(0, 1fr);
    }
}

/* ===== Narrow windows ===== */
@media (max-width: 600px) {
    body:not(.is-android) .button-container {
        flex-direction: column;
    }

    body:not(.is-android) .settings-field {
        grid-template-columns: 1fr;
    }

    .drag-drop-area {
        min-height: 220px;
    }
}

/* =====================================================================
 * Result screen per platform: count + switch, tools, share
 * ===================================================================== */

/* ----- Android: Material 3 segmented button, tonal tiles, filled share button ----- */
body.is-android .count-text {
    font-size: 22px;
    line-height: 28px;
    font-weight: 400;
    color: var(--md-on-surface);
}

body.is-android #countValue {
    font-size: 56px;
    line-height: 64px;
    font-weight: 400;
    color: var(--md-primary);
}

body.is-android .process-actions {
    gap: 8px;
}

body.is-android .process-actions .tool-button {
    min-height: 64px;
    padding: 12px 8px;
    border-radius: 16px;
    box-shadow: none;
    font-size: 12px;
    line-height: 16px;
    background-color: var(--md-surface-container-high);
    color: var(--md-on-surface);
}

body.is-android .process-actions .tool-button.is-active {
    background-color: var(--md-secondary-container);
    color: var(--md-on-secondary-container);
}

/* ----- iOS: system segmented control, gray tiles, capsule share button ----- */
body.is-ios {
    --segment-selected: #ffffff;
}

@media (prefers-color-scheme: dark) {
    body.is-ios {
        --segment-selected: #636366;
    }
}

body.is-ios .count-text {
    font-size: 17px;
    color: var(--text-secondary);
}

body.is-ios #countValue {
    font-size: 44px;
    font-weight: 700;
    color: var(--text);
}

body.is-ios .process-actions {
    gap: 10px;
}

body.is-ios .process-actions .tool-button {
    min-height: 76px;
    padding: 12px 8px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
}

/* ----- History menu per platform ----- */

body.is-android {
    --menu-bg: var(--md-surface-container);
    --danger: #b3261e;
}

@media (prefers-color-scheme: dark) {
    body.is-android {
        --danger: #f2b8b5;
    }
}

body.is-android .history-menu {
    min-width: 200px;
    padding: 8px 0;
    border-radius: 12px;
    box-shadow: 0 2px 6px 2px rgba(0, 0, 0, 0.15), 0 1px 2px rgba(0, 0, 0, 0.3);
    font-size: 16px;
}

body.is-android .history-menu-item {
    min-height: 48px;
    padding: 0 16px;
    border-radius: 0;
    color: var(--md-on-surface);
}

body.is-android .history-menu-item.is-danger {
    color: var(--danger);
}

body.is-android .history-menu-item:active {
    background-color: color-mix(in srgb, var(--md-on-surface) 10%, transparent);
}

body.is-ios .history-menu {
    min-width: 220px;
    padding: 0;
    border-radius: 14px;
    background: var(--glass);
    -webkit-backdrop-filter: blur(30px) saturate(1.8);
    backdrop-filter: blur(30px) saturate(1.8);
    box-shadow: var(--glass-shadow), 0 8px 30px rgba(0, 0, 0, 0.18);
    font-size: 17px;
}

body.is-ios .history-menu-item {
    min-height: 44px;
    padding: 0 16px;
    border-radius: 0;
}

body.is-ios .history-menu-item + .history-menu-item {
    border-top: 0.5px solid var(--separator);
}

body.is-ios .history-menu-item:active {
    background-color: var(--fill);
}

/* Material 3 icon buttons: tonal ✕, filled ✓ */
body.is-android .edit-actions {
    --edit-button-size: 48px;
}

body.is-android .edit-button {
    background-color: var(--md-surface-container-highest);
    color: var(--md-on-surface-variant);
}

body.is-android .edit-button.done-button {
    background-color: var(--md-primary);
    color: var(--md-on-primary);
}

body.is-android .edit-button:active {
    filter: none;
}

body.is-android .edit-actions .reset-button {
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
    letter-spacing: 0.1px;
    color: var(--md-primary);
}

body.is-android .edit-actions .reset-button:disabled {
    color: color-mix(in srgb, var(--md-on-surface) 38%, transparent);
}

/* iOS: gray ✕ and tinted ✓ circles, as in Photos */
body.is-ios .edit-button {
    background-color: var(--fill);
}

body.is-ios .edit-button:active {
    background-color: var(--fill-active);
}

body.is-ios .edit-button.done-button {
    background-color: var(--accent);
}

body.is-ios .edit-button.done-button:active {
    background-color: var(--accent);
    filter: brightness(0.85);
}

body.is-ios .edit-actions .reset-button {
    font-size: 17px;
}

/* ----- Header menu (Language, Share) and the rings toggle ----- */
.more-button {
    color: var(--text-secondary);
}

body.is-ios .more-button {
    grid-column: 3;
    justify-self: end;
    color: var(--text);
}

body.is-android .more-button {
    color: var(--md-on-surface-variant);
}

.count-label .marks-toggle {
    color: var(--text-secondary);
}

.history-menu-item:disabled {
    opacity: 0.4;
    pointer-events: none;
}

.history-menu-item svg:empty,
.history-menu-item .menu-icon-gap {
    display: none;
}

/* History grouped by day: the History heading above, then a titled group per day */
.history-days {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.history-day {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.history-title {
    font-size: 1.25em;
    font-weight: 700;
    text-transform: none;
    color: var(--text);
}

body.is-android .history-title {
    font-size: 22px;
    line-height: 28px;
    font-weight: 400;
    letter-spacing: 0;
    color: var(--md-on-surface);
}

body.is-android .history-days {
    gap: 8px;
}

body.is-android .history-day-title {
    padding-top: 12px;
}

body.is-ios .history-title {
    font-size: 22px;
    font-weight: 700;
}

body.is-ios .history-days {
    gap: 24px;
}

/* =====================================================================
 * Detection settings in the viewer: compact rows (label, slider, value on one line) so the
 * panel stays small. Last in the file and prefixed with body to win over the platform rules.
 * ===================================================================== */
body .viewer-settings .group {
    background: transparent;
    border-radius: 0;
    overflow: visible;
}

body .viewer-settings .advanced-summary {
    display: none;
}

body .viewer-settings .settings-advanced,
body .viewer-settings .settings-advanced-grid,
body .viewer-settings .group .settings-field + .settings-field,
body .viewer-settings .settings-reset {
    border-top: none;
    margin-top: 0;
}

/* Where the panel is wide enough, switches go two to a row (sliders keep a whole row) */
body .viewer-settings {
    container-type: inline-size;
}

@container (min-width: 520px) {
    body .viewer-settings .settings-advanced-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    body .viewer-settings .settings-advanced-grid > .settings-field:not(.toggle-field) {
        grid-column: 1 / -1;
    }
}

body .viewer-settings .settings-field {
    grid-template-columns: minmax(84px, 34%) minmax(0, 1fr);
    gap: 0 10px;
    min-height: 38px;
    padding: 0 16px;
}

/* Sliders get the whole width, under their label */
body .viewer-settings .settings-field:has(.range-row) {
    grid-template-columns: minmax(0, 1fr);
    min-height: 0;
    padding-top: 6px;
}

body .viewer-settings .settings-field:has(.range-row) input[type="range"] {
    height: 28px;
}

body .viewer-settings .settings-field label,
body .viewer-settings .toggle-label {
    font-size: 14px;
    line-height: 18px;
    color: var(--text);
}

body .viewer-settings .toggle-field {
    padding: 2px 16px;
}

body .viewer-settings .toggle-label {
    min-height: 36px;
}

body .viewer-settings .range-row {
    gap: 8px;
}

body .viewer-settings .range-value {
    min-width: 2.4em;
    font-size: 13px;
    color: var(--text-secondary);
}

body .viewer-settings input[type="range"] {
    height: 32px;
}

body .viewer-settings .select-wrap {
    margin: 2px 0;
}

/* The method's name is long: the select gets the whole width, under its label, as the sliders
   do. iOS keeps its pop-up row, where the value has the rest of the row. */
body:not(.is-ios) .viewer-settings .settings-field:has(.select-wrap) {
    grid-template-columns: minmax(0, 1fr);
    padding-top: 6px;
}

body:not(.is-ios) .viewer-settings .select-wrap {
    justify-self: stretch;
}

body:not(.is-ios) .viewer-settings .settings-select {
    width: 100%;
}

body .viewer-settings .settings-select {
    height: auto;
    min-height: 32px;
    font-size: 14px;
    color: var(--text);
}

body.is-android .viewer-settings .settings-select {
    padding: 0 36px 0 12px;
}

body .viewer-settings .settings-reset {
    min-height: 36px;
    padding: 0 16px 4px;
}

body .viewer-settings .text-button {
    font-size: 14px;
    text-shadow: none;
}

/* Android's switch is 52 px wide; make it a little smaller here */
body.is-android .viewer-settings .toggle-label input[type="checkbox"] {
    transform: scale(0.85);
    transform-origin: right center;
}

/* =====================================================================
 * Hover, where there is a pointer (desktop browsers, a trackpad on an iPad): a light tint of
 * the content color, as on desktop systems; on Android, Material 3's hover state layer.
 * ===================================================================== */
@media (hover: hover) and (pointer: fine) {
    body:not(.is-android) .action-button:not(:disabled):hover,
    body:not(.is-android) .edit-button:hover,
    body:not(.is-android) .history-open:hover,
    body:not(.is-android) .advanced-summary:hover,
    body:not(.is-android) .history-menu-item:hover,
    body:not(.is-android) .icon-button:hover {
        background-image: linear-gradient(color-mix(in srgb, currentColor 8%, transparent) 0 0);
    }

    body.is-android .action-button:not(:disabled):hover::before,
    body.is-android .edit-button:hover::before,
    body.is-android .history-open:hover::before,
    body.is-android .icon-button:hover::before,
    body.is-android .advanced-summary:hover::before {
        opacity: 0.08;
    }

    body.is-android .history-menu-item:hover {
        background-color: color-mix(in srgb, var(--md-on-surface) 8%, transparent);
    }

    .text-button:not(:disabled):hover {
        opacity: 0.75;
    }

    .overlay-button:hover {
        background-color: rgba(0, 0, 0, 0.7);
    }

    .overlay-button.is-active:hover {
        background-color: #ffffff;
    }
}

/* =====================================================================
 * Phones in landscape: little height, so the bars, the count and the tool tiles get compact,
 * as native compact-height layouts do. Last in the file to win over the platform rules.
 * ===================================================================== */
@media (orientation: landscape) and (max-height: 500px) {
    :root {
        --header-height: 44px;
    }

    body.is-android {
        --header-height: 56px;
    }

    body.is-ios {
        --header-height: 48px;
    }

    body.has-back .container {
        padding-top: 4px;
    }

    body.has-back .count-text {
        font-size: 1em;
    }

    body.has-back #countValue {
        font-size: 2em;
    }

    body.is-android.has-back .count-text {
        font-size: 18px;
        line-height: 24px;
    }

    body.is-android.has-back #countValue {
        font-size: 40px;
        line-height: 48px;
    }

    body.is-ios.has-back .count-text {
        font-size: 15px;
    }

    body.is-ios.has-back #countValue {
        font-size: 34px;
    }

    body.has-back .process-actions .tool-button {
        min-height: 56px;
        padding: 8px 6px;
        gap: 4px;
    }

    /* No room for the hints; the frame and the brush explain themselves */
    body.has-back .edit-hint {
        display: none !important;
    }

    /* Start screen: a smaller empty state with the two buttons side by side, and the history
       in a column of its own beside them */
    body:not(.has-back) .container {
        max-width: 1000px;
    }

    body:not(.has-back) .drag-drop-area {
        min-height: 0;
        padding: 8px 16px;
        gap: 4px;
    }

    body:is(.is-ios, .is-android):not(.has-back) .empty-icon {
        width: 72px;
        height: 72px;
        margin-bottom: 8px;
    }

    body:is(.is-ios, .is-android):not(.has-back) .upload-icon {
        width: 34px;
        height: 34px;
    }

    body.is-android:not(.has-back) .drag-text,
    body.is-ios:not(.has-back) .drag-text {
        font-size: 20px;
        line-height: 26px;
    }

    body:not(.has-back) .button-container {
        flex-direction: row;
    }

    body:not(.has-back) .button-container .action-button {
        flex: 1 1 0;
        padding: 0 16px;
    }

    body.has-history:not(.has-back) .container {
        height: calc(100dvh - var(--header-height) - var(--safe-top));
        min-height: 0;
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        grid-template-rows: minmax(0, 1fr) auto;
        column-gap: 24px;
        row-gap: 12px;
        padding-bottom: calc(12px + var(--safe-bottom));
    }

    body.has-history:not(.has-back) .drag-drop-area {
        grid-column: 1;
        grid-row: 1;
        align-self: center;
    }

    body.has-history:not(.has-back) .button-container {
        grid-column: 1;
        grid-row: 2;
    }

    body.has-history:not(.has-back) .history-section {
        grid-column: 2;
        grid-row: 1 / -1;
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
    }
}
