:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --line: #334155;
  --accent: #38bdf8;
  --accent-ink: #04283a;
  --danger: #f87171;
  --ok: #34d399;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body.public {
  color: #1f2937;
  background: #f8fafc;
}

a { color: var(--accent); }
body.public a { color: #0369a1; }

.wrap { max-width: 900px; margin: 0 auto; padding: 16px; }
.auth { max-width: 420px; margin: 6vh auto; padding: 16px; }
.logo { text-align: center; letter-spacing: .5px; }
.muted { color: var(--muted); }
body.public .muted { color: #64748b; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin: 0 0 16px;
}

label { display: block; margin: 0 0 12px; font-size: .92rem; color: var(--muted); }
input, textarea, button {
  font: inherit;
  color: var(--text);
}
input, textarea {
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
}
textarea { resize: vertical; min-height: 120px; }

button {
  cursor: pointer;
  padding: 10px 14px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
}
button.primary, button.primary:hover { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); border-color: #7f1d1d; }
button.linkbtn { background: none; border: none; color: var(--accent); padding: 8px; }

.err { background: #7f1d1d33; border: 1px solid #7f1d1d; color: #fecaca; padding: 10px 12px; border-radius: 8px; }
.ok  { background: #064e3b33; border: 1px solid #065f46; color: #a7f3d0; padding: 10px 12px; border-radius: 8px; }

.mono, code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.secret { letter-spacing: 2px; word-break: break-all; background: var(--panel-2); padding: 8px 10px; border-radius: 8px; }
.cmd { background: #020617; border: 1px solid var(--line); padding: 12px; border-radius: 8px; overflow-x: auto; }
.qr { display: block; background: #fff; padding: 8px; border-radius: 8px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar .brand { font-weight: 700; color: var(--text); text-decoration: none; }
.topbar nav { display: flex; align-items: center; gap: 12px; }

.row { display: flex; gap: 12px; align-items: center; }
.row.between { justify-content: space-between; flex-wrap: wrap; }
.inline { display: inline-flex; gap: 8px; align-items: center; margin: 0; }
.inline label { margin: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.newproj input { width: auto; }

.projects { list-style: none; padding: 0; margin: 0; }
.projects li { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.projects .pname { font-weight: 600; flex: 1; }
.badge { font-size: .72rem; text-transform: uppercase; letter-spacing: .5px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); }
.badge.public { color: var(--ok); border-color: #065f46; }
.badge.private { color: var(--muted); }
.count { font-size: .8rem; color: var(--muted); }

.share-url { cursor: text; }
.crumb { margin: 0 0 8px; }

/* Projects dashboard — responsive tile grid */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-top: 16px; }
.tile { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; color: var(--text); text-decoration: none; transition: border-color .15s ease, transform .15s ease; }
.tile:hover { border-color: var(--accent); transform: translateY(-2px); }
.tile-cover { position: relative; aspect-ratio: 4 / 3; background: #0b1220; }
.tile-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile-cover-empty { width: 100%; height: 100%; display: grid; place-items: center; color: var(--muted); font-size: .85rem; }
.tile-badge { position: absolute; top: 8px; right: 8px; background: #000a; }
.tile-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 3px; }
.tile-title { font-weight: 600; line-height: 1.3; }
.tile-desc { font-size: .85rem; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.tile-date { font-size: .75rem; color: var(--muted); margin-top: 2px; }
@media (max-width: 480px) {
  .tiles { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
  .tile-body { padding: 8px 10px 10px; }
}

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.asset { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.asset img { width: 100%; height: 130px; object-fit: cover; border-radius: 6px; background: #000; }
.asset .thumb-video { width: 100%; height: 130px; object-fit: cover; border-radius: 6px; background: #000; display: block; }
.asset input { font-size: .85rem; }
.filetile { height: 130px; display: grid; place-items: center; background: #0b1220; border-radius: 6px; color: var(--muted); font-weight: 700; }

.upload input[type=file] { padding: 8px; }
.upload-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.upload { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0; }
.upload-camera { margin: 0; }
.camera-btn {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 10px 14px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel-2); color: var(--text); white-space: nowrap;
}
.camera-btn:hover { border-color: var(--accent); }
.danger-zone { border-color: #7f1d1d; }

/* Public project page */
.public .gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin: 16px 0; }
.public figure { margin: 0; }
.public figure img { width: 100%; border-radius: 10px; display: block; }
.public figure .grid-video { width: 100%; border-radius: 10px; display: block; background: #000; }
.public figcaption { font-size: .85rem; color: #64748b; margin-top: 4px; }
.public .lead { font-size: 1.12rem; line-height: 1.55; color: #475569; margin: 0 0 20px; }
.public .md { line-height: 1.7; }
.public .md img { max-width: 100%; }
.public-home { display: grid; place-items: center; min-height: 70vh; opacity: .5; }
.docs { padding-left: 18px; }

/* Portfolio home (light public theme) */
.home-head { margin: 8px 0 20px; }
.home-head h1 { margin: 0 0 4px; }
body.public .tile { background: #fff; border-color: #e2e8f0; color: #1f2937; }
body.public .tile:hover { border-color: #0369a1; }
body.public .tile-cover { background: #eef2f7; }
body.public .tile-cover-empty { color: #94a3b8; }
body.public .tile-desc, body.public .tile-date { color: #64748b; }

/* Utilities */
.small { font-size: .82rem; }
.inline-check { display: flex; align-items: center; gap: 8px; }
.inline-check input { width: auto; }

/* Image cards — icon-only controls */
.asset { position: relative; }
.card-actions { display: flex; gap: 6px; justify-content: flex-end; align-items: center; margin-top: 2px; }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; color: var(--text); cursor: pointer; }
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.primary { color: var(--accent); border-color: #0369a1; }
.icon-btn.danger { color: var(--danger); border-color: #7f1d1d; }
.ico { display: block; }
.cap-text { font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; min-height: 1.2em; }
.cap-text:empty::before { content: "Add caption"; opacity: .5; }
.cap-text:hover { color: var(--accent); }
.cap-edit { gap: 6px; }
.cap-edit input { font-size: .85rem; }

/* Drag handle + dragging state (Pointer Events — works on touch and mouse) */
.reorderable .asset.dragging { opacity: .5; outline: 2px dashed var(--accent); }
.drag-handle { position: absolute; top: 6px; left: 6px; z-index: 2; width: 30px; height: 30px; display: grid; place-items: center; color: #e2e8f0; background: #000a; border: none; border-radius: 6px; cursor: grab; touch-action: none; }
.drag-handle:active { cursor: grabbing; }

/* Corner overlay actions on the image — same look as the drag handle. */
.corner-btn { position: absolute; top: 6px; z-index: 2; margin: 0; }
.corner-tl { left: 6px; }
.corner-tr { right: 6px; }
.corner-btn button { display: grid; place-items: center; width: 30px; height: 30px; padding: 0; color: #e2e8f0; background: #000a; border: none; border-radius: 6px; cursor: pointer; }
.corner-btn button:hover { color: #fff; background: #000d; }
.corner-btn.danger button:hover { color: #fca5a5; }

/* Album-cover star — bottom-right of gallery cards */
.cover-star { top: auto; bottom: 6px; right: 6px; }
.cover-star button:hover { color: #fbbf24; }
.cover-star.is-cover button { color: #fbbf24; background: #000d; }
.cover-star.is-cover .ico { fill: #fbbf24; }

/* Inbox bulk-select mode */
.inbox-tools { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.bulkbar { display: none; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 12px; padding: 8px 10px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; }
.selectable.selecting .bulkbar { display: flex; }
.bulkbar-actions { display: flex; gap: 8px; align-items: center; margin-left: auto; flex-wrap: wrap; }
.sel-count { color: var(--muted); font-size: .85rem; }
/* The selection checkmark — hidden until select mode, filled when picked */
.sel-check { display: none; position: absolute; top: 6px; left: 6px; z-index: 3; width: 30px; height: 30px; border-radius: 6px; place-items: center; background: #000a; color: #fff; opacity: .5; pointer-events: none; }
.selectable.selecting .sel-check { display: grid; }
.selectable.selecting .asset { cursor: pointer; }
.selectable.selecting .asset img { pointer-events: none; }
.selectable.selecting .asset.selected { outline: 2px solid var(--accent); outline-offset: 1px; }
.selectable.selecting .asset.selected .sel-check { opacity: 1; background: var(--accent); color: var(--accent-ink); }
/* While selecting, the whole card is a target — hide the per-card corner controls. */
.selectable.selecting .corner-btn { display: none; }
.selectable.selecting .cap-text { pointer-events: none; }

/* AI-inserted inline images — float-wrapped into the text, click to expand */
.md::after { content: ""; display: block; clear: both; }
.md .wimg { margin: 8px 0; }
.md .wimg-main img { width: 100%; height: auto; border-radius: 8px; display: block; cursor: zoom-in; }
.md .wimg-main .wimg-video { width: 100%; height: auto; border-radius: 8px; display: block; background: #000; }
.md .wimg figcaption { font-size: .82rem; color: var(--muted); margin-top: 4px; }
.md .wimg figcaption:empty { display: none; }
body.public .md .wimg figcaption { color: #64748b; }

/* Filmstrip of near-duplicate shots; click a thumb to promote it to main */
.md .wimg-strip { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.md .wimg-thumb { width: 52px; height: 52px; object-fit: cover; border-radius: 5px; cursor: pointer; opacity: .6; border: 2px solid transparent; }
.md .wimg-thumb:hover { opacity: 1; }
.md .wimg-thumb.is-active { opacity: 1; border-color: var(--accent); }

.md .wimg-left, .md .wimg-right { width: 320px; max-width: 42%; }
.md .wimg-left { float: left; clear: left; margin: 6px 20px 12px 0; }
.md .wimg-right { float: right; clear: right; margin: 6px 0 12px 20px; }

/* Click-to-expand: break out of the float to full width */
.md .wimg.expanded { float: none; clear: both; width: 100%; max-width: 100%; margin: 16px auto; }
.md .wimg.expanded img { cursor: zoom-out; }

/* On phones a floated 320px image beside wrapping text is cramped — stack it */
@media (max-width: 600px) {
  .md .wimg-left, .md .wimg-right { float: none; clear: both; width: 100%; max-width: 100%; margin: 12px 0; }
}

/* AI panel + provenance */
.ai-panel { margin: 8px 0 14px; }
.ai-note { background: #1e3a5f33; border: 1px solid #2563eb55; color: #bfdbfe; padding: 8px 12px; border-radius: 8px; }
.ai-running { font-size: .9rem; color: var(--muted); margin: 8px 0 0; }

/* Indeterminate "AI is working" line: a rainbow gradient flowing left→right.
   No percentage — the model can't report one; this signals live activity. */
.ai-progress {
  height: 4px;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #6366f1, #8b5cf6, #d946ef, #ec4899, #fb7185, #f59e0b, #22d3ee, #6366f1);
  background-size: 200% 100%;
  animation: ai-flow 2.4s linear infinite;
}
@keyframes ai-flow {
  from { background-position: 200% 0; }
  to { background-position: 0 0; }
}
@media (prefers-reduced-motion: reduce) {
  .ai-progress { animation-duration: 8s; }
}
.badge.vision { color: var(--accent); border-color: #0369a1; }
.badge.ai { color: var(--accent); border-color: #0369a1; }
.badge.manual { color: var(--muted); }
.model-pick { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.model-pick select { width: auto; min-width: 260px; max-width: 100%; }
.gen-form { flex-wrap: wrap; }
.detail-pick { display: inline-flex; align-items: center; gap: 6px; margin: 0; color: var(--muted); font-size: .85rem; }
.detail-pick select { width: auto; margin: 0; padding: 6px 8px; }

/* Revision history */
.revlist { list-style: none; padding: 0; margin: 0; }
.revlist li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.rev-id { font-family: ui-monospace, monospace; color: var(--muted); }
.revlinks { margin-left: auto; display: flex; gap: 12px; }
.diff { background: #020617; border: 1px solid var(--line); border-radius: 8px; padding: 12px; overflow-x: auto; white-space: pre; }
.diff span { display: block; }
.d-add { color: #86efac; background: #052e1633; }
.d-del { color: #fca5a5; background: #2e050533; }
.d-same { color: var(--muted); }
