/* Benji PWA — mobile-first shell. Self-contained tokens for now; once the
   design system stabilizes we can share newtab.css's variables. */
:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f242e;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --text-dim: #99a1b3;
  --accent: #5b4fff;
  --danger: #ef4444;
  --ok: #22c55e;
  --press: #262b36;        /* active/pressed surface (was hardcoded) */
  --chip-on-bg: #221f3d;   /* selected status chip fill */
  --warn: #d9a441;         /* muted amber (owner prefers muted over high-contrast) */
  --danger-border: #52262a;
  --radius: 12px;
  --tap: 44px; /* min touch target */
  color-scheme: dark;
}
/* R10: light counterpart of the token set above. data-theme is always resolved
   to "dark"/"light" by the pre-paint script + app.js (System = live matchMedia),
   so no prefers-color-scheme duplication is needed here. */
:root[data-theme="light"] {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --border: #d7dae2;
  --text: #1c202a;
  --text-dim: #5c6474;
  --danger: #dc2626;
  --ok: #16a34a;
  --press: #e2e5ec;
  --chip-on-bg: #e6e3ff;
  --warn: #9a6b00;
  --danger-border: #f0c3c7;
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
.app { display: flex; flex-direction: column; min-height: 100dvh; }
.loading { padding: 2rem; text-align: center; color: var(--text-dim); }

/* ── Top bar ───────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: .5rem;
  padding: max(.6rem, env(safe-area-inset-top)) .8rem .6rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar__back {
  background: none; border: 0; color: var(--text); font-size: 1.4rem;
  width: var(--tap); height: var(--tap); border-radius: 8px; cursor: pointer;
}
.topbar__back:active { background: var(--surface-2); }
.topbar__title { flex: 1; font-size: 1.05rem; font-weight: 600; margin: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar__meta { color: var(--text-dim); font-size: .8rem; margin-left: .25rem; }
.icon-btn {
  background: none; border: 0; color: var(--text-dim);
  width: var(--tap); height: var(--tap); border-radius: 8px; cursor: pointer;
  display: grid; place-items: center; font-size: 1.2rem;
}
.icon-btn:active { background: var(--surface-2); color: var(--text); }
.icon-btn--active { color: var(--accent); }  /* R11: a non-manual sort is in effect */

.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-dim); flex: 0 0 auto; }
.sync-dot.is-active { background: var(--ok); }
.sync-dot.is-paused { background: var(--text-dim); }
.sync-dot.is-error  { background: var(--danger); }
.sync-dot.is-offline { background: var(--warn); }   /* R30 */

/* ── Lists ─────────────────────────────────────────────────── */
.list { flex: 1; overflow-y: auto; padding: .5rem 0 6rem; -webkit-overflow-scrolling: touch; }
.row {
  display: flex; align-items: center; gap: .65rem;
  min-height: var(--tap); padding: .55rem .9rem;
  border-bottom: 1px solid var(--border);
  color: var(--text); text-decoration: none; cursor: pointer;
}
.row:active { background: var(--surface-2); }
.row__swatch { width: 12px; height: 12px; border-radius: 3px; flex: 0 0 auto; }
.row__title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row__count { color: var(--text-dim); font-size: .82rem; }
.row__chev { color: var(--text-dim); }

.item { font-size: .95rem; }
.item--heading { font-weight: 600; color: var(--text); background: var(--surface);
  text-transform: none; letter-spacing: .01em; }
.item--heading:active { background: var(--surface); }
/* Tappable open-area inside a row; sits beside the kebab. */
.row__open { flex: 1; min-width: 0; display: flex; align-items: center; gap: .65rem;
  color: inherit; text-decoration: none; }
.row__open--heading { cursor: default; }
.row__kebab { background: none; border: 0; color: var(--text-dim);
  width: 34px; height: 34px; margin: -.4rem -.3rem -.4rem 0; border-radius: 8px;
  cursor: pointer; flex: 0 0 auto; display: grid; place-items: center; font-size: 1.2rem; }
.row__kebab:active { background: var(--surface-2); color: var(--text); }
/* Drag-to-reorder grip. touch-action:none so the gesture starts a drag, not a
   list scroll. */
/* Grips sit on the RIGHT edge, just before the ⋮ (owner decision 2026-07-05). */
.row__grip { background: none; border: 0; color: var(--text-dim);
  width: 28px; height: 34px; margin: -.4rem 0; border-radius: 8px;
  cursor: grab; flex: 0 0 auto; display: grid; place-items: center; font-size: 1.05rem;
  touch-action: none; }
.row__grip:active { cursor: grabbing; color: var(--text); }
/* R28: disclosure caret on rows with children. */
.row__caret { background: none; border: 0; color: var(--text-dim);
  width: 28px; height: 34px; margin: -.4rem .05rem -.4rem -.3rem; border-radius: 8px;
  cursor: pointer; flex: 0 0 auto; display: grid; place-items: center; font-size: .85rem; }
.row__caret:active { background: var(--surface-2); color: var(--text); }
/* R35: one-tap indent/outdent arrows (manual sort only; inapplicable arrows
   are not rendered). */
.row__nest { background: none; border: 0; color: var(--text-dim);
  width: 28px; height: 34px; margin: -.4rem 0; border-radius: 8px;
  cursor: pointer; flex: 0 0 auto; display: grid; place-items: center; font-size: 1rem; }
.row__nest:active { background: var(--surface-2); color: var(--text); }

/* R29: at-a-glance row indicators (status dot + notes/attachment marks). */
.row__inds { flex: 0 0 auto; display: inline-flex; align-items: center; gap: .3rem;
  margin-left: .45rem; }
.ind-dot { width: 8px; height: 8px; border-radius: 50%; }
.ind-glyph { font-size: .7rem; color: var(--text-dim); }
.row.is-dragging { opacity: .95; background: var(--surface-2); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.5); position: relative; z-index: 25; }
.drop-line { position: fixed; left: 0; right: 0; height: 2px; background: var(--accent);
  z-index: 24; pointer-events: none; box-shadow: 0 0 6px var(--accent); }
.item__fav { width: 16px; height: 16px; border-radius: 3px; flex: 0 0 auto; background: var(--surface-2); }
.item__sub { color: var(--text-dim); font-size: .75rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
/* Title stack inside the tappable open-area. */
.row__text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: .1rem; }
.row__text .row__title { flex: none; }

.empty { padding: 2.5rem 1.5rem; text-align: center; color: var(--text-dim); }

/* ── Views (R2) ────────────────────────────────────────────── */
.section-head { padding: 1rem .9rem .4rem; font-size: .78rem; font-weight: 600;
  letter-spacing: .06em; text-transform: uppercase; color: var(--text-dim); }
.row__viewicon { width: 20px; flex: 0 0 auto; text-align: center; color: var(--text-dim); }
.row__viewicon--saved { color: var(--accent); }

/* ── Archive & Trash (R4) ──────────────────────────────────── */
.list--foot { margin-top: .6rem; padding-bottom: 5.5rem; }  /* clear the FAB */
.row--muted .row__title { color: var(--text-dim); }
.empty--tight { padding: .8rem 1.5rem; text-align: left; font-size: .85rem; }
/* .sbtn is flex:1 for the edit sheet's action row; pin it inside list rows. */
.row--result .sbtn { flex: 0 0 auto; padding: 0 .9rem; }

/* ── Search (R1) ───────────────────────────────────────────── */
.search-wrap { padding: .7rem .9rem 0; }
.search-input {
  width: 100%; padding: .7rem .8rem; font-size: 1rem;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
}
.search-input:focus { outline: none; border-color: var(--accent); }
/* Flat result rows: no grip, breadcrumb line under the title. */
.row--result { padding-left: 14px; }
.row__crumb { color: var(--text-dim); font-size: .72rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }

/* ── Floating add button ───────────────────────────────────── */
.fab {
  position: fixed; right: 1rem; bottom: calc(1rem + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; font-size: 1.8rem; line-height: 1;
  box-shadow: 0 6px 20px rgba(0,0,0,.4); cursor: pointer; z-index: 20;
}
.fab:active { transform: scale(.96); }

/* ── R31: select mode + bulk action bar ────────────────────── */
.row__check { width: 22px; height: 22px; margin-right: .35rem; flex: 0 0 auto;
  border: 2px solid var(--text-dim); border-radius: 6px;
  display: grid; place-items: center; font-size: .8rem; line-height: 1;
  color: transparent; }
.row__check.is-on { background: var(--accent); border-color: var(--accent); color: #fff; }
.row.is-selected { background: var(--chip-on-bg); }
.bulkbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: center; gap: .4rem;
  padding: .55rem .7rem calc(.55rem + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0,0,0,.25);
}
.bulkbar__count { flex: 0 0 auto; font-size: .85rem; color: var(--text-dim);
  min-width: 4.6em; }
.bulkbar .sbtn { flex: 1; min-height: 0; padding: .5rem .3rem; font-size: .88rem; }
.bulkbar .sbtn[disabled] { opacity: .4; pointer-events: none; }
.bulkbar__done { flex: 0 0 auto; padding: .5rem .7rem; color: var(--accent); }
/* keep the last rows reachable above the fixed bar */
#app:has(.bulkbar) .list { padding-bottom: 5.5rem; }

/* ── Sheet / forms ─────────────────────────────────────────── */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 520px; background: var(--surface);
  border-radius: 16px 16px 0 0; padding: 1.1rem 1.1rem calc(1.1rem + env(safe-area-inset-bottom));
  border: 1px solid var(--border); border-bottom: 0;
}
.sheet { max-height: 88dvh; overflow-y: auto; }
/* R41: overlay containers get tabindex="-1" for dialog focus — no visible ring. */
[role="dialog"]:focus { outline: none; }
.sheet__grip { width: 36px; height: 4px; border-radius: 2px; background: var(--border); margin: 0 auto .9rem; }
.sheet h2 { margin: 0 0 .9rem; font-size: 1.1rem; }
.field { margin-bottom: .8rem; }
.field label { display: block; font-size: .8rem; color: var(--text-dim); margin-bottom: .3rem; }
/* Inline status note in a field label (e.g. the share screen's "Fetching title…"). */
.field__hint { margin-left: .5rem; font-style: italic; opacity: .85; }
/* A9: "already saved" heads-up on the Link field — amber, upright (not a suggestion). */
.field__hint--dup { color: var(--warn); font-style: normal; opacity: 1; }
/* R26: the dup hint's inline "Add note to it" action. */
.hint-btn { margin-left: .5rem; padding: .1rem .5rem; font: inherit; font-size: .85em;
  color: var(--accent); background: transparent; border: 1px solid var(--accent);
  border-radius: 999px; }
.hint-btn[disabled] { opacity: .4; }
.field input, .field textarea, .field select {
  width: 100%; padding: .7rem .8rem; font-size: 1rem;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font-family: inherit;
}
.field textarea { resize: vertical; }
.field select { appearance: none; cursor: pointer; }
.field input:focus, .field textarea:focus, .field select:focus {
  outline: 2px solid var(--accent); border-color: transparent;
}

/* R32: fullscreen attachment image viewer (deliberately dark in both themes —
   photo-viewer convention). z-index above the pp popover (40). */
.imgview-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 50;
  display: flex; flex-direction: column;
}
.imgview-bar {
  display: flex; align-items: center; gap: .6rem; flex: 0 0 auto;
  padding: calc(.6rem + env(safe-area-inset-top)) .8rem .6rem;
}
.imgview-name {
  flex: 1; min-width: 0; color: #fff; font-size: .9rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.imgview-close {
  width: 40px; height: 40px; flex: 0 0 auto; border: 0; border-radius: 50%;
  background: rgba(255,255,255,.12); color: #fff; font-size: 1.4rem; line-height: 1;
  cursor: pointer;
}
.imgview-img {
  flex: 1; min-height: 0; width: 100%; object-fit: contain;
  padding: 0 .5rem calc(.8rem + env(safe-area-inset-bottom));
}
/* R33: read-only Added / Opened line in the edit sheet. */
.sheet-meta {
  display: flex; gap: 1.2rem; flex-wrap: wrap;
  margin: -.2rem 0 .8rem; font-size: .8rem; color: var(--text-dim);
}
.sheet-meta b { font-weight: 600; color: var(--text); }

.attach-thumb--tap { cursor: pointer; }
.attach-thumb--tap:focus-visible { outline: 2px solid var(--accent); }

/* ── Custom "Under" parent picker ──────────────────────────────
   Native <select> dropdowns are OS-drawn on Android and ignore page CSS, so
   nesting depth is invisible there. This picker is a button that opens a styled
   popover listbox with real indent rails + section/link styling. */
.pp {
  width: 100%; display: flex; align-items: center; gap: .5rem;
  padding: .7rem .8rem; font-size: 1rem; text-align: left; font-family: inherit;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; cursor: pointer;
}
.pp:focus { outline: 2px solid var(--accent); border-color: transparent; }
.pp__value { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pp__chev { color: var(--text-dim); font-size: .8rem; flex: 0 0 auto; }

.pp-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40;
  display: flex; align-items: center; justify-content: center; padding: 1.2rem;
}
.pp-list {
  width: 100%; max-width: 480px; max-height: 70dvh; overflow-y: auto;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 18px 50px rgba(0,0,0,.6);
  -webkit-overflow-scrolling: touch;
}
.pp-opt {
  display: flex; align-items: center; gap: .4rem; min-height: 40px;
  padding: .5rem .8rem; border-bottom: 1px solid var(--border);
  font-size: .9rem; cursor: pointer;
}
.pp-opt:last-child { border-bottom: 0; }
.pp-opt:active { background: var(--press); }
.pp-opt--sel { background: rgba(91,79,255,.16); }
.pp-opt--sel::after { content: "✓"; margin-left: auto; color: var(--accent); font-size: .85rem; }
.pp-opt--top { font-weight: 600; }
/* depth rails: one left-border bar per nesting level */
.pp__rail { display: inline-flex; flex: 0 0 auto; align-self: stretch; }
.pp__rail i { width: 15px; border-left: 1px solid var(--border); margin-left: 7px; }
.pp__label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pp__label--section { font-weight: 600; color: var(--text); }
.pp__label--link { font-size: .82rem; color: var(--text-dim); }
.pp__kind {
  margin-left: auto; flex: 0 0 auto; font-size: .66rem; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: .04em;
}
.pp-opt--sel .pp__kind { display: none; }

/* Status chips */
.statusrow { display: flex; gap: .45rem; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; gap: .35rem; padding: .4rem .7rem;
  font-size: .85rem; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim); cursor: pointer; }
.chip.is-on { border-color: var(--accent); color: var(--text); background: var(--chip-on-bg); }
.chip .dot { width: 9px; height: 9px; border-radius: 50%; flex: 0 0 auto; }

/* R16: statuses editor rows */
.st-row { display: flex; align-items: center; gap: .6rem; padding: .3rem 0; }
.st-row__dot { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 auto; }
.st-row__label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-row .sbtn { flex: 0 0 auto; min-height: 0; padding: .45rem .7rem; }
.st-row .sbtn[disabled] { opacity: .4; pointer-events: none; }

/* Shared-image preview on the share landing */
.share-preview { margin: 0 0 .9rem; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); padding: .4rem; display: flex; justify-content: center; }
.share-preview img { max-width: 100%; max-height: 220px; border-radius: 6px; object-fit: contain; }

/* Attachments (edit sheet) */
.attach { display: flex; flex-direction: column; gap: .5rem; }
.attach-list { display: flex; flex-direction: column; gap: .4rem; }
.attach-empty { font-size: .82rem; color: var(--text-dim); }
.attach-row { display: flex; align-items: center; gap: .6rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: .4rem; }
.attach-thumb { width: 44px; height: 44px; flex: 0 0 auto; border-radius: 8px;
  background: var(--bg) center/cover no-repeat; display: grid; place-items: center; font-size: 1.1rem; }
.attach-meta { flex: 1; min-width: 0; }
.attach-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .9rem; }
.attach-size { color: var(--text-dim); font-size: .75rem; }
.attach-del { flex: 0 0 auto; width: 34px; height: 34px; border: 0; border-radius: 8px;
  background: none; color: var(--text-dim); font-size: 1.3rem; cursor: pointer; }
.attach-del:active { background: var(--border); color: var(--text); }
.attach-mv { font-size: 1rem; }
.attach-move { display: flex; flex-direction: column; gap: .5rem; margin-top: .6rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: .6rem; }
.attach-move__label { font-size: .8rem; color: var(--text-dim); }
.attach-move__btns { display: flex; gap: .5rem; justify-content: flex-end; }

/* Section divider + label inside a sheet */
.hr { height: 1px; background: var(--border); margin: 1rem 0; }
.section-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-dim); margin: .2rem 0 .5rem; }

/* R11: sort-mode sheet option column */
.sortcol { display: flex; flex-direction: column; gap: .5rem; }
.sortcol .sbtn { justify-content: flex-start; padding: 0 .9rem; }
.sbtn--sel { border-color: var(--accent); color: var(--text); background: var(--chip-on-bg); }
.sbtn--sel::after { content: "✓"; margin-left: auto; color: var(--accent); }

/* Lifecycle (archive / trash / delete) action row */
.dangerrow { display: flex; gap: .5rem; }
.sbtn { flex: 1; min-height: var(--tap); display: flex; align-items: center;
  justify-content: center; gap: .4rem; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface-2); color: var(--text);
  font-size: .92rem; cursor: pointer; }
.sbtn:active { background: var(--border); }
.sbtn--danger { color: var(--danger); border-color: var(--danger-border); }
.btn {
  min-height: var(--tap); padding: .65rem 1rem; font-size: 1rem; font-weight: 600;
  border: 1px solid var(--border); border-radius: 10px; cursor: pointer;
  background: var(--surface-2); color: var(--text);
}
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--ghost { background: none; }
.btn:disabled { opacity: .5; }
.sheet__actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1rem; }
.status { font-size: .85rem; margin-top: .4rem; min-height: 1.2em; }
.status.is-ok { color: var(--ok); }
.status.is-error { color: var(--danger); }
.hint { font-size: .8rem; color: var(--text-dim); margin: -.3rem 0 .9rem; }

/* R7: sync-conflict revision cards */
.conf-card { margin: .35rem .9rem; padding: .6rem .75rem; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface); }
.conf-card--winner { border-color: var(--accent); }
.conf-tag { font-size: .72rem; color: var(--text-dim); text-transform: uppercase;
  letter-spacing: .04em; margin-bottom: .25rem; }
.conf-line { font-size: .82rem; color: var(--text); overflow-wrap: anywhere; padding: .1rem 0; }
.conf-line b { color: var(--text-dim); font-weight: 500; }
.conf-actions { margin-top: .45rem; }
