:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1a1d23;
  --muted: #6b7280;
  --border: #dde1e7;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 12px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e6e8ec;
    --muted: #9aa3af;
    --border: #2b3039;
    --accent: #3b82f6;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
h2 { font-size: 1.05rem; margin: 1.2rem 0 .6rem; }
code { font-size: .85em; background: var(--border); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
.muted { color: var(--muted); font-size: .9rem; }
.center { text-align: center; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; }
.brand img { width: 28px; height: 28px; }

.tabs { display: flex; gap: 4px; padding: 8px 16px 0; max-width: 820px; margin: 0 auto; }
.tabs button { flex: 1; border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 10px 4px; }
.tabs button.active { border-bottom-color: var(--accent); color: var(--accent); font-weight: 600; }
.badge { display: inline-block; min-width: 1.4em; padding: 0 6px; border-radius: 99px; background: var(--border); font-size: .75rem; }

main { max-width: 820px; margin: 0 auto; padding: 12px 16px 40px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; color: var(--text); cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; min-height: 40px; white-space: nowrap;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); margin-right: auto; }
button.ghost { background: none; }
button.wide { width: 100%; margin: 12px 0; min-height: 48px; font-size: 1rem; }
button:disabled { opacity: .5; cursor: default; }
.chip { border-radius: 99px; font-size: .85rem; min-height: 34px; padding: 4px 12px; }
.chip.on { color: var(--ok); border-color: var(--ok); }

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.row.end { justify-content: flex-end; }
.row > input[type=search] { flex: 1; min-width: 0; }

input, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-top: 4px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 10px; }
label small { opacity: .8; }
label.check { display: flex; gap: 8px; align-items: flex-start; color: var(--text); }
label.check input { width: auto; margin-top: 3px; }

.slots { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.slot { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; min-width: 0; }
.slot .row { justify-content: center; margin-bottom: 0; }
.slot .row button, .slot .row .btn { padding: 6px 10px; font-size: .85rem; }
.preview {
  aspect-ratio: 1.6; border-radius: 8px; border: 2px dashed var(--border);
  display: grid; place-items: center; overflow: hidden; color: var(--muted); font-size: .85rem; position: relative;
}
.preview img { width: 100%; height: 100%; object-fit: contain; background: #000; }
.preview.has-img { border-style: solid; }
.preview a { position: absolute; bottom: 6px; right: 6px; font-size: .75rem; background: var(--surface); padding: 2px 8px; border-radius: 99px; color: var(--accent); text-decoration: none; }

.paste summary { cursor: pointer; color: var(--muted); font-size: .9rem; margin: 12px 0 4px; }

form#cardForm, form#settingsForm, .card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px 12px; margin-bottom: 12px; }
.chip.warn { color: #d97706; border-color: #d97706; }
.account { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 0; border-top: 1px solid var(--border); }
.account .row { margin: 0; }
.account.on .muted { color: var(--ok); }

select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px; min-height: 40px; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; margin: 8px 0; }
.g-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; min-width: 0; }
.g-photos { position: relative; aspect-ratio: 1.6; background: #111; cursor: zoom-in; }
.g-front { width: 100%; height: 100%; object-fit: contain; display: block; }
.g-back { position: absolute; right: 8px; bottom: 8px; width: 34%; aspect-ratio: 1.6; object-fit: cover; border: 2px solid #fff; border-radius: 6px; background: #333; box-shadow: 0 2px 8px rgba(0,0,0,.4); }
.g-initials { height: 100%; display: grid; place-items: center; font-size: 2rem; font-weight: 700; color: #9ca3af; background: var(--border); }
.g-info { padding: 10px 12px; display: grid; gap: 3px; font-size: .9rem; min-width: 0; }
.g-info b { font-size: 1rem; }
.g-info a { color: var(--accent); text-decoration: none; overflow-wrap: anywhere; }
.g-info .row { margin: 6px 0 0; }
.g-info .row .btn, .g-info .row button { min-height: 34px; padding: 4px 12px; font-size: .85rem; text-decoration: none; color: var(--text); }
.g-notes { margin: 4px 0; color: var(--muted); font-size: .85rem; white-space: pre-line; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

.overlay.lightbox { background: rgba(0,0,0,.94); color: #fff; }
.lb-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); }
.lb-bar button { background: rgba(255,255,255,.12); color: #fff; border-color: transparent; }
.lb-bar button:disabled { opacity: 1; background: rgba(255,255,255,.35); }
#lbImg { flex: 1; min-height: 0; width: 100%; object-fit: contain; padding: 0 8px max(8px, env(safe-area-inset-bottom)); }

.overlay.login {
  color: var(--text); align-items: center; justify-content: center; padding: 24px 16px; overflow-y: auto;
  background:
    radial-gradient(1200px 600px at 50% -10%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 70%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 400px; margin: auto; text-align: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 36px 28px 24px; box-shadow: 0 20px 60px rgba(15, 23, 42, .12);
}
.login-logo {
  width: 72px; height: 72px; margin: 0 auto 18px; border-radius: 20px; display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}
.login-logo img { width: 52px; height: 52px; }
.login-card h1 { font-size: 1.5rem; margin: 0 0 8px; letter-spacing: -.01em; }
.login-sub { color: var(--muted); margin: 0 auto 26px; max-width: 32ch; }
button.sso {
  width: 100%; min-height: 50px; margin-bottom: 10px; gap: 12px;
  font-weight: 600; border-radius: 12px; background: var(--surface); transition: background .15s, border-color .15s;
}
button.sso:hover { background: color-mix(in srgb, var(--accent) 6%, var(--surface)); }
button.sso svg { width: 20px; height: 20px; flex: none; }
.login-note { font-size: .85rem; color: var(--muted); background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .8rem; margin: 18px 0 8px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
button.link { border: 0; background: none; color: var(--accent); font-weight: 600; min-height: 36px; }
button.link:hover { text-decoration: underline; }
.login-fine { color: var(--muted); font-size: .8rem; margin: 0; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.span2 { grid-column: span 2; }

.list { list-style: none; padding: 0; margin: 8px 0; display: grid; gap: 8px; }
.list li {
  display: flex; gap: 12px; align-items: center; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px;
}
.list li:hover { border-color: var(--accent); }
.list .thumb { width: 80px; aspect-ratio: 1.6; border-radius: 6px; object-fit: cover; flex: none; background: var(--border); display: grid; place-items: center; font-weight: 700; color: var(--muted); }
.list .info { min-width: 0; }
.list .info b { display: block; }
.list .info div { color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag { display: inline-block; font-size: .72rem; padding: 0 7px; border-radius: 99px; background: var(--border); margin-right: 4px; }
.pending { color: #d97706; font-size: .75rem; }

.overlay { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.92); display: flex; flex-direction: column; }
.overlay[hidden] { display: none; }
#video { flex: 1; width: 100%; min-height: 0; object-fit: contain; }
.frame { position: absolute; left: 50%; top: 45%; width: min(86vw, 70vh * 1.6); aspect-ratio: 1.6; transform: translate(-50%, -50%); border: 2px solid rgba(255,255,255,.8); border-radius: 12px; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); pointer-events: none; }
.cam-bar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 16px 16px max(16px, env(safe-area-inset-bottom)); }
.cam-bar button:first-child { justify-self: start; }
.shutter { width: 70px; height: 70px; border-radius: 50%; background: #fff; border: 5px solid #9ca3af; }
.busy { align-items: center; justify-content: center; color: #fff; background: rgba(0,0,0,.55); }
.spinner { width: 44px; height: 44px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 10px;
  background: #111827; color: #fff; box-shadow: 0 6px 24px rgba(0,0,0,.25);
}
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

@media (max-width: 560px) {
  .slots, .grid { grid-template-columns: 1fr; }
  .tabs button { font-size: .9rem; padding: 10px 2px; }
  .span2 { grid-column: auto; }
}
