:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1c1b19;
  --muted: #6d6a64;
  --line: #e3e0d9;
  --accent: #e8590c;
  --accent-press: #c94a06;
  --accent-ink: #ffffff;
  --ok: #2f9e44;
  --warn: #e67700;
  --err: #d6336c;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1c1c1f;
    --surface-2: #26262a;
    --text: #f1f0ee;
    --muted: #9a9893;
    --line: #2e2e33;
    --accent: #ff6b1a;
    --accent-press: #e8590c;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
  min-height: 100dvh;
}
svg { width: 22px; height: 22px; flex: none; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: calc(10px + var(--safe-t)) 12px 10px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
}
.titles { flex: 1; min-width: 0; padding-left: 4px; }
.titles h1 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subtitle:empty { display: none; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: transparent; color: var(--text); text-decoration: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.dark { color: #fff; background: rgba(0,0,0,.35); }
.menu {
  position: absolute; right: 12px; top: calc(54px + var(--safe-t));
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); padding: 6px; min-width: 160px;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 12px; border-radius: 8px; }
.menu button:active { background: var(--surface-2); }

main { padding: 16px 16px calc(120px + var(--safe-b)); max-width: 900px; margin: 0 auto; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 14px; padding: 12px 18px; min-height: 48px;
  font-weight: 650; text-decoration: none; transition: transform .08s ease, background .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:active { background: var(--accent-press); }
.btn.secondary { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.btn.big { min-height: 56px; font-size: 17px; border-radius: 16px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .55; }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 4px 6px; }

/* ---------- projects ---------- */
.create { display: flex; gap: 8px; margin-bottom: 22px; }
.create input, .login input {
  flex: 1; min-width: 0; height: 48px; padding: 0 14px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface);
  outline: none;
}
.create input:focus, .login input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 4px 10px; font-weight: 650; }
.project-list { display: grid; gap: 10px; }
@media (min-width: 640px) { .project-list { grid-template-columns: 1fr 1fr; } }
.project-card {
  display: flex; align-items: center; gap: 14px; width: 100%;
  text-align: left; padding: 12px; border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow);
}
.project-card:active { transform: scale(.99); }
.cover {
  width: 64px; height: 64px; border-radius: 12px; flex: none; overflow: hidden;
  background: var(--surface-2); display: grid; place-items: center; color: var(--muted);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.project-card .info { flex: 1; min-width: 0; }
.project-card strong { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-card .meta { display: block; color: var(--muted); font-size: 14px; margin-top: 2px; }
.project-card .chev { color: var(--muted); width: 18px; height: 18px; }
.badge { display: inline-block; font-size: 12px; font-weight: 650; padding: 2px 8px; border-radius: 99px; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); margin-left: 6px; vertical-align: 1px; }
.skeleton { height: 90px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; border: 1.5px dashed var(--line); border-radius: var(--radius); }
.empty svg { width: 40px; height: 40px; margin-bottom: 8px; opacity: .7; }
.empty strong { display: block; color: var(--text); font-size: 17px; margin-bottom: 4px; }

/* ---------- gallery ---------- */
.day { display: flex; align-items: baseline; justify-content: space-between; margin: 18px 4px 10px; }
.day:first-child { margin-top: 4px; }
.day h3 { margin: 0; font-size: 16px; font-weight: 700; }
.day span { color: var(--muted); font-size: 13px; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; } }
.tile {
  position: relative; aspect-ratio: 1; border: 0; padding: 0; overflow: hidden;
  border-radius: 8px; background: var(--surface-2); display: block;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; transition: opacity .2s; }
.tile img.loading { opacity: 0; }
.tile .note-dot {
  position: absolute; left: 6px; bottom: 6px; padding: 1px 6px; border-radius: 6px;
  font-size: 11px; font-weight: 700; background: rgba(0,0,0,.55); color: #fff;
}
.tile .broken { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.tile.pending::after { content: ""; position: absolute; inset: 0; background: rgba(0,0,0,.28); }
.tile .prog {
  position: absolute; left: 8px; right: 8px; bottom: 8px; height: 5px; border-radius: 3px;
  background: rgba(255,255,255,.35); overflow: hidden; z-index: 1;
}
.tile .prog span { display: block; height: 100%; width: 0; background: #fff; transition: width .2s; }
.tile .tag {
  position: absolute; top: 6px; right: 6px; z-index: 1; font-size: 11px; font-weight: 700;
  padding: 2px 7px; border-radius: 6px; background: rgba(0,0,0,.6); color: #fff;
}
.tile.failed::after { background: rgba(214,51,108,.35); }
.tile.failed .tag { background: var(--err); }

/* ---------- upload queue ---------- */
.queue {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.queue-head { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; }
.queue-head #queue-summary { flex: 1; }
.queue-bar { height: 6px; border-radius: 3px; background: var(--surface-2); margin-top: 10px; overflow: hidden; }
.queue-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s; }
.queue.done .queue-bar span { background: var(--ok); }
.queue.has-failed .queue-bar span { background: var(--err); }
.queue-list { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 8px; max-height: 40vh; overflow: auto; }
.queue-list li { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.queue-list img { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; background: var(--surface-2); }
.queue-list .q-main { flex: 1; min-width: 0; }
.queue-list .q-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.queue-list .q-state { font-size: 12px; color: var(--muted); }
.queue-list .q-state.err { color: var(--err); }
.queue-list .q-state.ok { color: var(--ok); }
.mini-bar { height: 4px; border-radius: 2px; background: var(--surface-2); margin-top: 4px; overflow: hidden; }
.mini-bar span { display: block; height: 100%; background: var(--accent); width: 0; }

/* ---------- action bar ---------- */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  display: flex; gap: 10px; padding: 12px 16px calc(12px + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, color-mix(in srgb, var(--bg) 0%, transparent));
}
.actionbar .btn.secondary { flex: 0 0 auto; padding: 0 16px; }
.actionbar .btn.primary { flex: 1; box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 35%, transparent); }
@media (min-width: 900px) { .actionbar { max-width: 900px; margin: 0 auto; } }

.drop-overlay {
  position: fixed; inset: 12px; z-index: 30; display: grid; place-items: center;
  border: 3px dashed var(--accent); border-radius: 20px; font-size: 20px; font-weight: 700;
  background: color-mix(in srgb, var(--bg) 85%, transparent); pointer-events: none;
}

/* ---------- capture session ---------- */
.capture { position: fixed; left: 0; right: 0; top: 0; height: 100vh; height: 100dvh; z-index: 50; background: #000; color: #fff; display: flex; flex-direction: column; }
.capture video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.cap-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.cap-flash.on { animation: flash .25s ease-out; }
@keyframes flash { 0% { opacity: .85; } 100% { opacity: 0; } }
.cap-top {
  position: relative; z-index: 2; display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + var(--safe-t)) 14px 12px;
  background: linear-gradient(to bottom, rgba(0,0,0,.55), transparent);
}
.pill {
  border: 0; border-radius: 99px; padding: 9px 16px; font-weight: 700; font-size: 15px;
  background: #fff; color: #000; text-decoration: none;
}
.pill.ghost { background: rgba(0,0,0,.45); color: #fff; font-weight: 600; }
.cap-bottom {
  position: relative; z-index: 2; margin-top: auto;
  padding: 10px 0 calc(18px + var(--safe-b));
  background: linear-gradient(to top, rgba(0,0,0,.7), transparent);
}
.cap-strip { display: flex; gap: 6px; overflow-x: auto; padding: 0 14px 12px; min-height: 64px; scrollbar-width: none; }
.cap-strip::-webkit-scrollbar { display: none; }
.cap-strip .st { position: relative; flex: none; width: 52px; height: 52px; border-radius: 8px; overflow: hidden; border: 2px solid rgba(255,255,255,.85); background: #222; }
.cap-strip .st img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cap-strip .st i { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); width: 0; transition: width .2s; }
.cap-strip .st.ok i { background: #51cf66; width: 100% !important; }
.cap-strip .st.err { border-color: var(--err); }
.cap-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 0 20px; }
.cap-res { text-align: right; font-size: 12px; color: rgba(255,255,255,.75); }
.shutter {
  width: 78px; height: 78px; border-radius: 50%; border: 4px solid #fff; background: transparent; padding: 4px;
}
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active span { transform: scale(.88); }
.shutter:disabled { opacity: .4; }
.cap-fallback {
  position: relative; z-index: 2; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; gap: 16px;
}
.cap-fallback p { max-width: 320px; color: rgba(255,255,255,.85); margin: 0; }
.cap-fallback .big-icon { width: 56px; height: 56px; opacity: .8; }
.capture.fallback .shutter, .capture.fallback #cap-flip, .capture.fallback video { display: none; }

/* ---------- viewer ----------
   Flex column: top bar / image area (flex:1, min-height:0) / note panel. The image is absolutely
   positioned inside its own area with object-fit:contain, so it can never spill over the note panel.
   Height follows the *visible* viewport: 100dvh, or --vvh from visualViewport (keyboard open),
   so the note panel stays above Safari's toolbar and above the keyboard. */
.viewer {
  position: fixed; left: 0; right: 0; top: var(--vv-top, 0px); z-index: 40;
  height: 100vh;                     /* fallback for old browsers */
  height: 100dvh;
  height: var(--vvh, 100dvh);
  background: #0b0b0c; color: #fff;
  display: flex; flex-direction: column; overflow: hidden;
}
.viewer-top {
  flex: none; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + var(--safe-t)) max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
}
.v-counter { font-weight: 600; font-size: 15px; color: rgba(255,255,255,.85); }
.v-actions { display: flex; gap: 6px; }
.v-actions .pill { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; padding: 8px 12px; }
.v-actions .pill svg { width: 16px; height: 16px; }
.v-actions .pill.on { background: #fff; color: #000; }
.v-actions .pill.on svg path { fill: #f59f00; stroke: #f59f00; }
.viewer-stage { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; touch-action: pan-y; }
.viewer-stage img {
  position: absolute; inset: 0; width: 100%; height: 100%; max-height: 100%;
  object-fit: contain; user-select: none; -webkit-user-drag: none;
}
.nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 1; width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.4); color: #fff; display: grid; place-items: center;
}
.nav.prev { left: 10px; } .nav.next { right: 10px; }
@media (hover: none) { .nav { background: rgba(0,0,0,.25); } }
.note-panel {
  flex: none; position: relative; z-index: 2; background: #18181b; border-top: 1px solid #2a2a2e;
  padding: 12px max(16px, env(safe-area-inset-right)) calc(14px + var(--safe-b)) max(16px, env(safe-area-inset-left));
}
.viewer.kb .note-panel { padding-bottom: 10px; }   /* keyboard open: home indicator is hidden */
.note-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.note-head label { font-weight: 700; font-size: 14px; }
.note-status { flex: 1; text-align: right; font-size: 13px; color: rgba(255,255,255,.6); }
.note-status.saving { color: #ffd43b; }
.note-status.saved { color: #69db7c; }
.note-status.error { color: #ff8787; }
.note-status.dirty { color: rgba(255,255,255,.6); }
.viewer textarea {
  width: 100%; resize: none; border-radius: 12px; border: 1px solid #333; background: #232327; color: #fff;
  padding: 10px 12px; min-height: 52px; max-height: 30dvh; outline: none; font-size: 16px; display: block;
}
.v-meta { margin: 6px 0 0; font-size: 12px; color: rgba(255,255,255,.45); }

/* ---------- install tip ---------- */
.install-tip {
  display: flex; align-items: center; gap: 12px; margin-bottom: 16px; padding: 12px 8px 12px 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
.install-tip img { border-radius: 10px; flex: none; }
.install-tip p { margin: 0; flex: 1; font-size: 14px; line-height: 1.4; }
.install-tip svg.inline { width: 16px; height: 16px; vertical-align: -3px; color: #0a84ff; }
.install-tip .btn { min-height: 38px; padding: 6px 14px; }
.small { font-size: 13px; margin: 4px 0 0; }

/* In a browser tab (not installed), keep the bottom buttons clear of Safari's toolbar hit zone. */
@media (display-mode: browser) {
  .actionbar { padding-bottom: calc(22px + var(--safe-b)); }
  main { padding-bottom: calc(132px + var(--safe-b)); }
}
/* Installed (standalone): content runs edge to edge, so honor the side insets too. */
@media (display-mode: standalone) {
  .actionbar { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translate(-50%, 20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--err); }

/* ---------- login ---------- */
.login-body { display: grid; place-items: center; padding: 24px; }
.login { width: 100%; max-width: 380px; padding: 0; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 22px; padding: 28px 22px; box-shadow: var(--shadow); display: grid; gap: 10px; }
.login-card h1 { margin: 4px 0 0; font-size: 24px; }
.login-card .muted { margin: 0 0 8px; }
.logo { width: 52px; height: 52px; border-radius: 14px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.logo svg { width: 28px; height: 28px; }
.field-label { font-size: 14px; font-weight: 600; }
.login input { width: 100%; flex: none; margin-bottom: 8px; }
.error { color: var(--err); margin: 0; font-weight: 600; }
