:root {
  --paper: #f5f6f8;
  --surface: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --green: #0b3d2e;
  --gold: #c9a227;
  --danger: #e85d3b;
  --ok: #34c759;
  --chip: #eef2f0;
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --nav-h: 64px;
}
[data-theme="dark"] {
  --paper: #0c0f12;
  --surface: #151a1f;
  --ink: #f3f4f6;
  --muted: #9ca3af;
  --line: #2a3139;
  --chip: #1c242c;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

/* Privacy shield */
.privacy-shield {
  display: flex; position: fixed; inset: 0; z-index: 9999;
  background: #0b3d2e; color: #f4f7f5;
  align-items: center; justify-content: center;
  opacity: 0; visibility: hidden; pointer-events: none;
  transition: opacity .15s ease;
}
.privacy-shield.on {
  opacity: 1; visibility: visible; pointer-events: auto;
}
.shield-mark {
  width: 56px; height: 56px; border-radius: 16px; margin: 0 auto .75rem;
  background: linear-gradient(135deg, #c9a227, #d4af37);
  color: #0b3d2e; font-weight: 800; font-size: 1.15rem;
  display: grid; place-items: center;
}
.shield-inner { text-align: center; }

/* Auth */
.auth {
  min-height: 100dvh; display: grid; place-items: center;
  padding: calc(1.25rem + var(--safe-t)) 1.1rem calc(1.25rem + var(--safe-b));
  background:
    radial-gradient(800px 360px at 0% 0%, rgba(43,123,255,.12), transparent 55%),
    radial-gradient(700px 320px at 100% 0%, rgba(11,61,46,.14), transparent 50%),
    var(--paper);
}
.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 1.5rem 1.25rem 1.25rem;
  border-top: 3px solid var(--gold);
}
.brand-row { display: flex; gap: .85rem; align-items: center; margin-bottom: 1.1rem; }
.logo {
  width: 46px; height: 46px; border-radius: 13px;
  background: linear-gradient(145deg, #0b3d2e, #145a3c);
  color: #fff; display: grid; place-items: center; font-weight: 800; font-size: .95rem;
}
.brand-row h1 { margin: 0; font-size: 1.28rem; letter-spacing: -.02em; }
.brand-row p { margin: .15rem 0 0; color: var(--muted); font-size: .86rem; }
.field { margin-bottom: .85rem; }
.field label {
  display: block; font-size: .7rem; font-weight: 700;
  letter-spacing: .05em; text-transform: uppercase; color: var(--muted); margin-bottom: .32rem;
}
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--line); background: var(--paper);
  border-radius: 11px; padding: .72rem .85rem; outline: none;
  font-size: 16px; /* iOS: avoid zoom + ensure keyboard input works */
  color: var(--ink);
  caret-color: var(--ink);
  -webkit-user-select: text; user-select: text;
  touch-action: manipulation;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: #2B7BFF; box-shadow: 0 0 0 3px rgba(43,123,255,.15);
}
.field textarea { min-height: 84px; resize: vertical; }
.btn {
  border-radius: 12px; padding: .78rem 1rem; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
}
.btn-primary { background: #0b3d2e; color: #fff; width: 100%; }
.btn-primary:hover { background: #145a3c; }
.btn-gold { background: var(--gold); color: #132018; width: 100%; }
.btn-ghost {
  background: transparent; border: 1px solid var(--line); color: var(--muted); width: 100%;
}
.btn-danger { background: rgba(232,93,59,.12); color: var(--danger); border: 1px solid rgba(232,93,59,.25); }
.btn-sm { padding: .42rem .7rem; font-size: .8rem; border-radius: 9px; width: auto; }
.hint { font-size: .8rem; color: var(--muted); line-height: 1.45; margin: .4rem 0 .9rem; }
.err, .ok, .info {
  border-radius: 11px; padding: .7rem .8rem; font-size: .86rem; margin-bottom: .85rem;
}
.err { background: rgba(232,93,59,.1); color: var(--danger); border: 1px solid rgba(232,93,59,.25); }
.ok { background: rgba(52,199,89,.1); color: #1f8a5c; border: 1px solid rgba(52,199,89,.25); }
.info { background: rgba(43,123,255,.08); color: #1d4ed8; border: 1px solid rgba(43,123,255,.2); }
.sec-note {
  font-size: .75rem; color: var(--muted); line-height: 1.45; margin-top: 1rem;
  border-left: 3px solid var(--gold); padding-left: .65rem;
}
.pw-meter { height: 6px; border-radius: 99px; background: var(--line); overflow: hidden; margin-top: .4rem; }
.pw-meter > span { display: block; height: 100%; border-radius: 99px; transition: width .2s, background .2s; }
.unlock-progress {
  display: flex; align-items: center; gap: .55rem; margin-top: .75rem; color: var(--muted); font-size: .85rem;
}
.spinner {
  width: 16px; height: 16px; border: 2px solid rgba(0,0,0,.12); border-top-color: #0b3d2e;
  border-radius: 50%; animation: spin .7s linear infinite;
}
[data-theme="dark"] .spinner { border-color: rgba(255,255,255,.15); border-top-color: #34c759; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Shell */
.shell { min-height: 100dvh; display: flex; flex-direction: column; padding-bottom: calc(var(--nav-h) + var(--safe-b)); }
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--surface);
  padding: calc(.75rem + var(--safe-t)) 1rem .65rem;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
}
.topbar h2 { margin: 0; font-size: 1.05rem; font-weight: 800; letter-spacing: -.02em; }
.topbar .sub { color: var(--muted); font-size: .78rem; font-weight: 600; }
.top-actions { display: flex; gap: .35rem; align-items: center; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); display: grid; place-items: center; font-size: 1rem;
}
.main { width: min(560px, 100%); margin: 0 auto; padding: .9rem 1rem 1.25rem; flex: 1; }

.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: .35rem;
  background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: .25rem; margin-bottom: .85rem;
}
.seg button {
  border-radius: 9px; padding: .55rem; font-size: .78rem; font-weight: 700; color: var(--muted);
}
.seg button.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.header-row {
  display: flex; justify-content: space-between; align-items: baseline; margin-bottom: .75rem;
}
.header-row h3 { margin: 0; font-size: 1.05rem; font-weight: 800; letter-spacing: -.02em; }
.header-row .count { color: var(--muted); font-size: .9rem; font-weight: 600; }

.search-row { margin-bottom: .85rem; }
.search-row input {
  width: 100%; border: 1px solid var(--line); background: var(--surface);
  border-radius: 12px; padding: .75rem .9rem;
}

/* Coloured category panels — reference style */
.cat-list { display: grid; gap: .55rem; }
.cat-panel {
  display: flex; align-items: center; gap: .75rem;
  width: 100%; text-align: left;
  border-radius: 14px; padding: .85rem 1rem;
  color: #fff; font-weight: 650; min-height: 52px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
  transition: transform .12s ease, filter .12s ease;
}
.cat-panel:active { transform: scale(.985); }
.cat-panel .ci {
  width: 28px; height: 28px; border-radius: 8px;
  background: rgba(255,255,255,.18); display: grid; place-items: center;
  font-size: .72rem; font-weight: 800; flex-shrink: 0;
}
.cat-panel .cl { flex: 1; font-size: .98rem; }
.cat-panel .cc { font-size: .88rem; opacity: .95; font-weight: 600; }

/* Item list inside category */
.item-list { display: grid; gap: .45rem; }
.item-row {
  display: flex; align-items: center; gap: .7rem; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: .8rem .9rem; box-shadow: var(--shadow);
}
.item-row .dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
}
.item-row .body { flex: 1; min-width: 0; }
.item-row .t { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-row .s { color: var(--muted); font-size: .78rem; margin-top: .1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.star { color: var(--gold); }

.empty {
  text-align: center; padding: 2.2rem 1rem; color: var(--muted);
  border: 1px dashed var(--line); border-radius: 14px; background: var(--surface);
}
.empty-hero {
  padding: 2.75rem 1.25rem 2rem;
  border: none;
  background: linear-gradient(180deg, var(--surface) 0%, var(--paper) 100%);
  box-shadow: var(--shadow);
}
.empty-hero h3 {
  margin: .75rem 0 .4rem; color: var(--ink); font-size: 1.2rem; font-weight: 800; letter-spacing: -.02em;
}
.empty-hero p { margin: 0 auto; max-width: 16rem; line-height: 1.45; font-size: .92rem; }
.empty-mark {
  width: 52px; height: 52px; border-radius: 16px; margin: 0 auto;
  background: linear-gradient(145deg, #0b3d2e, #145a3c);
  color: #fff; font-weight: 800; display: grid; place-items: center; font-size: .95rem;
}
.color-grid {
  display: grid; grid-template-columns: repeat(6, 1fr); gap: .45rem;
}
.color-swatch {
  width: 100%; aspect-ratio: 1; border-radius: 10px; border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.08);
}
.color-swatch.on {
  border-color: var(--ink);
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--ink);
}

/* Health cards */
.health-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .55rem; margin-bottom: .9rem; }
.health-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: .85rem; box-shadow: var(--shadow);
}
.health-card .n { font-size: 1.45rem; font-weight: 800; letter-spacing: -.03em; }
.health-card .l { color: var(--muted); font-size: .75rem; font-weight: 600; margin-top: .15rem; }
.health-card.bad .n { color: var(--danger); }
.health-card.warn .n { color: #c9a227; }
.health-card.good .n { color: var(--ok); }

.alerts { display: grid; gap: .45rem; margin-bottom: .85rem; }
.alert {
  border-radius: 12px; padding: .7rem .85rem; font-size: .85rem;
  border: 1px solid var(--line); background: var(--surface);
}
.alert.warn { border-color: rgba(201,162,39,.4); background: rgba(201,162,39,.1); }
.alert.crit { border-color: rgba(232,93,59,.4); background: rgba(232,93,59,.1); }

/* Bottom nav + FAB */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  height: calc(var(--nav-h) + var(--safe-b));
  padding: .35rem .5rem var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: .2rem; align-items: start;
}
.nav button {
  color: var(--muted); padding: .35rem .2rem; border-radius: 10px;
  font-size: .68rem; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; gap: .12rem;
}
.nav button.active { color: var(--green); }
.nav .ni { font-size: 1.05rem; line-height: 1; }

.fab-wrap {
  position: fixed; right: 1rem; bottom: calc(var(--nav-h) + var(--safe-b) + .75rem); z-index: 45;
  display: flex; gap: .5rem; align-items: center;
}
.fab {
  width: 54px; height: 54px; border-radius: 50%;
  background: #fff; color: #111; border: 1px solid var(--line);
  box-shadow: 0 6px 20px rgba(0,0,0,.15);
  font-size: 1.6rem; font-weight: 400; display: grid; place-items: center;
}
.fab.menu {
  width: 44px; height: 44px; font-size: 1.1rem; border-radius: 12px;
}

/* Modal / editor — reference style */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 60; background: rgba(8,16,12,.45);
  display: flex; align-items: stretch; justify-content: center;
}
.modal {
  width: min(560px, 100%); max-height: 100dvh; overflow: auto;
  background: var(--surface); display: flex; flex-direction: column;
}
.editor-hero {
  padding: calc(.85rem + var(--safe-t)) 1rem 1.1rem;
  color: #fff; position: relative;
}
.editor-hero .top {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: .85rem;
}
.editor-hero .top button {
  color: #fff; font-weight: 700; font-size: .78rem; letter-spacing: .04em; opacity: .95;
}
.editor-hero .title-row {
  display: flex; gap: .85rem; align-items: flex-start;
}
.editor-hero input.title {
  flex: 1; background: transparent; border: none; outline: none;
  color: #fff; font-size: 16px; font-weight: 600; padding: 0;
  transform: scale(1.15); transform-origin: left center;
  -webkit-user-select: text; user-select: text;
}
.editor-hero input.title::placeholder { color: rgba(255,255,255,.55); }
.editor-hero .type-icon {
  width: 56px; height: 56px; border-radius: 14px;
  border: 2px solid rgba(255,255,255,.85);
  display: grid; place-items: center; font-weight: 800; font-size: .85rem;
  flex-shrink: 0; background: rgba(255,255,255,.1);
}
.editor-hero .tools {
  display: flex; gap: .45rem; margin-top: .9rem; align-items: center;
}
.editor-hero .pill {
  border: 1.5px solid rgba(255,255,255,.9); color: #fff; background: transparent;
  border-radius: 8px; padding: .28rem .55rem; font-size: .72rem; font-weight: 800;
}
.editor-body { padding: .4rem 0 1.25rem; flex: 1; }
.field-row {
  display: flex; align-items: center; gap: .55rem;
  padding: .75rem 1rem; border-bottom: 1px solid var(--line);
}
.field-row .remove {
  width: 28px; height: 28px; border-radius: 50%;
  background: #ff3b30; color: #fff; display: grid; place-items: center;
  font-size: 1.1rem; font-weight: 700; flex-shrink: 0; line-height: 1;
}
.field-row .flabel {
  width: 118px; flex-shrink: 0; color: var(--muted); font-size: .92rem; font-weight: 500;
}
.field-row .fval {
  flex: 1; border: none; outline: none; background: transparent; padding: .2rem 0;
  font-size: 16px; min-width: 0;
  -webkit-user-select: text; user-select: text;
  touch-action: manipulation;
}
.search-row input {
  font-size: 16px;
  -webkit-user-select: text; user-select: text;
  touch-action: manipulation;
}
.field-row .fval::placeholder { color: #c4c4c4; }
.field-row .key-btn {
  border: 1.5px solid #2B7BFF; color: #2B7BFF; border-radius: 8px;
  padding: .25rem .5rem; font-size: .68rem; font-weight: 800; flex-shrink: 0;
}
.field-row .key-btn.on-green { border-color: #34C759; color: #34C759; }
.field-row .drag { color: #c4c4c4; flex-shrink: 0; letter-spacing: -.05em; }
.add-field {
  display: flex; justify-content: center; align-items: center; gap: .4rem;
  width: 100%; padding: 1rem; color: var(--muted); font-weight: 600;
}
.note-area {
  margin: .5rem 1rem 0; border-top: 1px solid var(--line); padding-top: .75rem;
}
.note-area textarea {
  width: 100%; border: none; outline: none; min-height: 70px; resize: vertical;
  background: transparent; color: var(--ink);
}
.note-area textarea::placeholder { color: #c4c4c4; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 1.25rem);
  transform: translateX(-50%);
  background: #111; color: #fff; padding: .55rem .9rem; border-radius: 999px;
  font-size: .82rem; font-weight: 600; z-index: 80; opacity: 0; pointer-events: none;
  transition: opacity .2s;
}
.toast.show { opacity: 1; }

.sheet {
  background: var(--surface); border-radius: 18px 18px 0 0; padding: 1rem 1rem calc(1rem + var(--safe-b));
  width: min(560px, 100%); margin-top: auto; max-height: 85dvh; overflow: auto;
}
.sheet h3 { margin: 0 0 .85rem; font-size: 1.05rem; }
.gen-row { display: grid; grid-template-columns: 1fr auto; gap: .5rem; align-items: center; margin-bottom: .75rem; }
.gen-out {
  font-family: ui-monospace, monospace; font-size: .95rem; font-weight: 700;
  background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: .7rem .8rem;
  word-break: break-all;
}
.check { display: flex; gap: .75rem; flex-wrap: wrap; margin: .5rem 0 .9rem; font-size: .85rem; color: var(--muted); }
.check label { display: flex; gap: .3rem; align-items: center; }

.detail-actions { display: flex; gap: .4rem; flex-wrap: wrap; padding: .75rem 1rem; }
.detail-actions .btn { width: auto; }

.loading-block { opacity: .65; pointer-events: none; }

.explainer {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 1rem; margin-bottom: .65rem; box-shadow: var(--shadow);
}
.explainer h4 { margin: 0 0 .4rem; font-size: .95rem; }
.explainer p { margin: 0; color: var(--muted); font-size: .85rem; line-height: 1.5; }

.back-link {
  display: inline-flex; align-items: center; gap: .35rem;
  color: var(--muted); font-weight: 700; font-size: .88rem; margin-bottom: .65rem;
}
