:root {
    color-scheme: light dark;
    --bg: #ffffff;
    --fg: #0b0b0c;
    --muted: #6b7280;
    --line: rgba(128, 128, 128, 0.28);
    --card: #ffffff;
    --accent: #2563eb;
    --accent-fg: #ffffff;
    --danger: #dc2626;
    --ok: #16a34a;
    --radius: 14px;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b0b0c;
        --fg: #f4f4f5;
        --muted: #9ca3af;
        --line: rgba(255, 255, 255, 0.16);
        --card: #161618;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.app {
    max-width: 720px; margin: 0 auto; padding: 1.25rem 1rem 2rem;
    /* Clear the home indicator and, in landscape, the notch. The webview runs
       full-screen (capacitor contentInset:"never"), so the insets are ours to
       apply exactly once. env() resolves to 0 in a desktop browser. */
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
    padding-bottom: calc(2rem + env(safe-area-inset-bottom));
}

/* Masthead: full-width sticky bar, content constrained to the page width. */
#topbar {
    position: sticky; top: 0; z-index: 20; background: var(--bg);
    border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top);
}
.topbar-inner {
    max-width: 720px; margin: 0 auto; padding: 0.65rem 1rem;
    /* Keep clear of the notch/Dynamic Island in landscape. env() is 0 on the web. */
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
img.avatar { object-fit: cover; }

h1 { font-size: 1.4rem; margin: 0.5rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
.muted { color: var(--muted); font-size: 0.9rem; }

.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; background: var(--card); margin-bottom: 1rem; }

label { display: block; font-weight: 600; margin: 0.75rem 0 0.35rem; }
input[type=text], input[type=password], input[type=email], input[type=number], textarea, select {
    width: 100%; padding: 0.7rem 0.8rem; border-radius: 10px;
    border: 1px solid var(--line); background: transparent; color: var(--fg); font: inherit;
}
/* Without an explicit rule a textarea falls back to the UA default: a monospace
   box about 20 columns wide, which is what every multi-line field in the app was
   rendering as. Sized to show a few lines and grow by dragging, never sideways. */
textarea { min-height: 6.5rem; line-height: 1.45; resize: vertical; }
select { -webkit-appearance: none; appearance: none; padding-right: 2.2rem;
    background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                      linear-gradient(135deg, currentColor 50%, transparent 50%);
    background-position: right 1.05rem center, right 0.8rem center;
    background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }

button, .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
    padding: 0.75rem 1.1rem; border-radius: 10px; border: 1px solid transparent;
    background: var(--accent); color: var(--accent-fg); font: inherit; font-weight: 650;
    cursor: pointer; -webkit-tap-highlight-color: transparent;
}
button:disabled { opacity: 0.5; cursor: default; }
.btn-secondary { background: transparent; color: var(--fg); border-color: var(--line); }
.btn-danger { background: var(--danger); }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.btn-row > * { flex: 1; }
/* Left-aligned actions that keep their natural width. Unlike .btn-row this does
   not stretch children, so a lone button stays button-sized. */
.toolbar { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; }
.btn-sm { padding: 0.45rem 0.85rem; font-size: 0.9rem; border-radius: 9px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.75rem; }
.ref {
    border: 2px solid var(--line); border-radius: 12px; overflow: hidden; cursor: pointer;
    /* A <button> inherits the global button colours, so without this the caption
       was --accent-fg (white) on a white --card: invisible in light mode, and
       legible only because dark mode happens to make white text correct. */
    background: var(--card); color: var(--fg); text-align: left; padding: 0;
    /* A <button> does not lay out block children the way a <div> does: WebKit
       centres them on a single line, which put the image and the caption side by
       side and clipped the name. Stacking explicitly fixes it on every engine. */
    display: flex; flex-direction: column; align-items: stretch; width: 100%;
}
.ref.selected { border-color: var(--accent); }
.ref img { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; display: block; background: #0002; flex: none; }
/* min-width:0 lets long names wrap instead of forcing the cell wider. */
.ref .meta { padding: 0.5rem 0.6rem; min-width: 0; width: 100%; }
.ref .meta .name { font-weight: 650; font-size: 0.92rem; overflow-wrap: anywhere; }
.ref .meta .notes {
    color: var(--muted); font-size: 0.78rem; overflow-wrap: anywhere; margin-top: 0.15rem;
    /* Keep cells even: show at most two lines of notes. */
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* Brand mark in the masthead (replaces the old wordmark). */
.brand { display: inline-flex; align-items: center; padding: 0; }
.brand-logo { height: 28px; width: auto; display: block; }

/* Preferences tabs. Scrolls sideways rather than wrapping if more are added. */
.tabs {
    display: flex; gap: 0.25rem; overflow-x: auto; -webkit-overflow-scrolling: touch;
    border-bottom: 1px solid var(--line); margin-bottom: 0.25rem;
}
.tab {
    background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0;
    color: var(--muted); font: inherit; font-weight: 650; cursor: pointer;
    padding: 0.6rem 0.9rem; white-space: nowrap; flex: none;
}
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }
.tabpanel[hidden] { display: none; }

/* Keeps the primary action reachable as the style grid grows and the page scrolls. */
.sticky-actions {
    position: sticky; bottom: 0; z-index: 10; background: var(--bg);
    padding-top: 0.75rem; padding-bottom: calc(0.25rem + env(safe-area-inset-bottom));
}

.square { width: 100%; aspect-ratio: 1 / 1; border-radius: 12px; overflow: hidden; background: #0003; }
.square img { width: 100%; height: 100%; object-fit: contain; display: block; }

/* Cropper */
.crop-frame {
    position: relative; width: 100%; aspect-ratio: 1 / 1; overflow: hidden;
    border-radius: 12px; background: #000; touch-action: none; user-select: none;
}
.crop-frame img { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; pointer-events: none; }
.crop-frame::after {
    content: ""; position: absolute; inset: 0; pointer-events: none;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,0.35);
    background-image: linear-gradient(rgba(255,255,255,0.18) 1px, transparent 1px),
                      linear-gradient(90deg, rgba(255,255,255,0.18) 1px, transparent 1px);
    background-size: 33.33% 33.33%;
}
input[type=range] { width: 100%; }

.spinner {
    width: 34px; height: 34px; border-radius: 50%;
    border: 3px solid var(--line); border-top-color: var(--accent);
    animation: spin 0.9s linear infinite; margin: 1.5rem auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.center { text-align: center; }
.stack { display: flex; flex-direction: column; gap: 0.75rem; }
.pill { display: inline-block; font-size: 0.72rem; padding: 0.1rem 0.55rem; border-radius: 999px; background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.alert { padding: 0.7rem 0.9rem; border-radius: 10px; margin-bottom: 1rem; border: 1px solid var(--line); }
.alert-error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }

.toast {
    /* Sits above the home indicator rather than under it. */
    position: fixed; left: 50%; bottom: calc(1.5rem + env(safe-area-inset-bottom)); transform: translateX(-50%);
    background: var(--fg); color: var(--bg); padding: 0.65rem 1rem; border-radius: 999px;
    font-size: 0.9rem; z-index: 50; max-width: 90%;
}

a { color: var(--accent); }
.linklike { background: none; border: none; color: var(--accent); padding: 0; cursor: pointer; font: inherit; }

/* Nav: brand + Home on the left, avatar menu on the right */
.topbar-left { display: flex; align-items: center; gap: 1.1rem; }
.navlink { background: none; border: 0; color: var(--fg); font: inherit; font-weight: 600; cursor: pointer; padding: 0.3rem 0.1rem; }
.navlink:hover { text-decoration: underline; }
.menu { position: relative; }
.avatar-btn { display: flex; align-items: center; gap: 0.5rem; background: transparent; border: 1px solid var(--line); border-radius: 999px; padding: 0.28rem 0.7rem 0.28rem 0.32rem; color: var(--fg); cursor: pointer; font: inherit; }
.avatar-btn:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--accent-fg); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; text-transform: uppercase; flex: none; }
.avatar-name { font-weight: 600; font-size: 0.92rem; max-width: 12ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { opacity: 0.6; font-size: 0.7rem; }
.menu-panel { position: absolute; right: 0; top: calc(100% + 0.4rem); background: var(--card); border: 1px solid var(--line); border-radius: 12px; min-width: 210px; box-shadow: 0 12px 34px rgba(0,0,0,0.22); overflow: hidden; z-index: 30; }
.menu-panel button, .menu-panel a { display: flex; width: 100%; align-items: center; justify-content: flex-start; gap: 0.55rem; padding: 0.72rem 0.95rem; background: transparent; border: 0; color: var(--fg); text-decoration: none; font: inherit; text-align: left; cursor: pointer; }
.menu-panel button:hover, .menu-panel a:hover { background: color-mix(in srgb, var(--fg) 7%, transparent); }
.menu-panel .sep { height: 1px; padding: 0; background: var(--line); }
.menu-panel .danger { color: var(--danger); }

/* ---- Saved prints: dense layout ------------------------------------------ */

/* Heading and its action share one row rather than costing two. */
.gal-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; margin: 0.5rem 0 0.9rem; }
.gal-head h1 { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.gal-head .count { font-size: 0.85rem; font-weight: 500; color: var(--muted); margin-left: 0.4rem; }

/* A collection reads as a pile: one card's worth of space for the whole group. */
.stackwrap { position: relative; }
.stacklayer {
    position: absolute; inset: 0; border-radius: 12px;
    border: 2px solid var(--line); background: var(--card); pointer-events: none;
}
.stacklayer.l2 { transform: translate(5px, 5px) rotate(1.8deg); opacity: 0.45; }
.stacklayer.l1 { transform: translate(2.5px, 2.5px) rotate(0.9deg); opacity: 0.7; }
.stacktop { position: relative; }
.badge {
    position: absolute; top: 0.4rem; right: 0.4rem;
    background: rgba(0, 0, 0, 0.72); color: #fff; border-radius: 999px;
    padding: 0.05rem 0.5rem; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.2px;
}
/* A tick that reads at a glance without shrinking the thumbnail. */
.ref .tick {
    position: absolute; top: 0.4rem; left: 0.4rem; width: 24px; height: 24px; border-radius: 50%;
    background: var(--accent); color: var(--accent-fg); display: none;
    align-items: center; justify-content: center; font-size: 0.85rem; font-weight: 800;
}
.ref.selected .tick { display: flex; }
.ref { position: relative; }

/* Back chip for the collection filter: one tap in, one tap out, no new screen. */
.chip {
    display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.3rem 0.7rem;
    border-radius: 999px; border: 1px solid var(--line); background: transparent;
    color: var(--fg); font: inherit; font-size: 0.85rem; font-weight: 600; cursor: pointer;
}
.chip:hover { background: color-mix(in srgb, var(--fg) 7%, transparent); }

/* Selection actions: only present while selecting, so they cost nothing otherwise. */
.selbar {
    position: sticky; bottom: 0; z-index: 15; background: var(--bg);
    border-top: 1px solid var(--line); display: flex; gap: 0.5rem; align-items: center;
    padding: 0.6rem 0 calc(0.6rem + env(safe-area-inset-bottom));
    margin-top: 0.75rem;
}
.selbar select { flex: 1; min-width: 0; padding: 0.55rem 0.6rem; border-radius: 9px; border: 1px solid var(--line); background: transparent; color: var(--fg); font: inherit; }
.selbar .btn-sm { flex: none; }

/* Confirmation sheet. Used instead of window.confirm so the copy can be
   specific and the destructive step can look destructive. */
.sheet-back {
    position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 60;
    display: flex; align-items: flex-end; justify-content: center; padding: 1rem;
}
.sheet {
    background: var(--card); border: 1px solid var(--line); border-radius: 16px;
    padding: 1.1rem; width: 100%; max-width: 420px;
    margin-bottom: env(safe-area-inset-bottom);
}
.sheet h2 { margin: 0 0 0.4rem; }
.sheet p { margin: 0 0 1rem; color: var(--muted); font-size: 0.92rem; }
@media (min-width: 560px) { .sheet-back { align-items: center; } }

/* Label text for screen readers only: the control is obvious visually but still
   needs a programmatic name. */
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Gallery grid: denser than the style picker (more prints visible per screen),
   and captions sit ON the thumbnail so a named tile is exactly as tall as an
   unnamed one. Captions below the image made rows ragged and wasted a strip of
   white on every unnamed print. */
.grid-tight { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 0.6rem; }
.grid-tight .ref .meta {
    position: absolute; left: 0; right: 0; bottom: 0;
    padding: 1.5rem 0.5rem 0.4rem; width: auto;
    background: linear-gradient(to top, rgba(0,0,0,0.78), rgba(0,0,0,0));
    pointer-events: none;
}
.grid-tight .ref .meta .name { color: #fff; font-size: 0.8rem; line-height: 1.25; }
.grid-tight .ref img { aspect-ratio: 1 / 1; }

/* An explicit keyboard focus ring. Nothing suppressed the browser default, but
   the default is low-contrast against the accent-coloured buttons, and the
   cropper is a focusable div where a clear ring matters most. */
:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
    border-radius: 10px;
}
.ref:focus-visible { outline-offset: 3px; }
