:root {
  color-scheme: light dark;
  font: 16px/1.5 system-ui, sans-serif;
  --bg: #0b1120;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger-bg: #fee2e2;
  --danger-text: #991b1b;
  --success-text: #059669;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
/* The `hidden` attribute's UA-stylesheet `display:none` loses to any author
   rule that also sets `display` on the same element (e.g. .lightbox's
   `display:flex`) at equal specificity. Force it to always win. */
[hidden] { display: none !important; }

button { border: 0; border-radius: 9px; background: var(--accent); color: #fff; padding: 12px 18px; font: inherit; font-weight: 700; cursor: pointer; }
button:hover { background: var(--accent-hover); }
button.secondary { background: #e5e7eb; color: var(--text); }
button.secondary:hover { background: #d1d5db; }
button.danger { background: var(--danger-text); line-height: 1.25; text-align: center; }
button.danger:hover { background: #7f1d1d; }
.danger-sub { display: block; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; opacity: .85; margin-top: 2px; }
button:disabled { opacity: .45; cursor: default; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.05rem; }
.brand-icon { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; background: var(--text); color: #fff; font-size: 16px; flex: none; }

/* --- Sign-in screen --- */
.auth-screen { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.card { width: min(440px, 100%); padding: 34px; border-radius: 18px; background: var(--surface); box-shadow: 0 20px 60px #0007; }
.card .brand { margin-bottom: 22px; }
.card h1 { margin: 0 0 4px; font-size: 1.4rem; }
.card p#message { color: var(--muted); margin-top: 0; }
#spinner { color: var(--accent); font-weight: 700; margin-top: 20px; }
#error, #galleryError { background: var(--danger-bg); color: var(--danger-text); padding: 12px; border-radius: 8px; margin-top: 16px; }
#inviteForm .hint { color: var(--muted); font-size: 14px; }
#inviteRow { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
#inviteCode { flex: 1 1 220px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px; font: inherit; letter-spacing: 1px; }

/* --- Gallery screen --- */
.gallery-screen { min-height: 100vh; }
.gallery-header {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 16px 28px; background: var(--surface); box-shadow: 0 2px 10px #0003;
}
.gallery-header .brand-icon { background: var(--accent); }
.gallery-actions { display: flex; align-items: center; gap: 10px; }
.upload-button { display: inline-flex; }
.upload-button span { border-radius: 9px; background: var(--accent); color: #fff; padding: 12px 18px; font-weight: 700; cursor: pointer; }
.upload-button:hover span { background: var(--accent-hover); }
.upload-button.busy span { opacity: .6; pointer-events: none; }

.gallery-body { display: flex; align-items: flex-start; }
.gallery-main { flex: 1; min-width: 0; }

/* --- Folder sidebar --- */
.folder-pane { width: 220px; flex: none; background: var(--surface); border-right: 1px solid var(--border); padding: 16px 0; min-height: calc(100vh - 69px); }
.folder-pane.collapsed { display: none; }
.folder-pane-header { padding: 0 16px 10px; font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.folder-list { list-style: none; margin: 0; padding: 0 8px; }
.folder-item { display: flex; align-items: center; gap: 2px; }
.folder-item-name { flex: 1; text-align: left; background: none; color: var(--text); font-weight: 600; font-size: 14px; padding: 9px 8px; border-radius: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-item-name:hover { background: #f3f4f6; }
.folder-item.active .folder-item-name { background: var(--accent); color: #fff; }
.folder-item-rename { flex: none; background: none; color: var(--muted); width: 30px; height: 30px; padding: 0; border-radius: 8px; font-size: 13px; }
.folder-item-rename:hover { background: #f3f4f6; color: var(--text); }

/* --- Chat (folder-level, or scoped to one file) --- */
/* Always a fixed overlay, not a layout sibling of the grid — this is what
   lets the exact same pane also show on top of the lightbox/doc viewer when
   opened from there (see .lightbox-controls/.doc-viewer in app.js), rather
   than needing a second, separate chat UI just for file-scoped threads. */
.chat-pane {
  width: 320px; background: var(--surface); border-left: 1px solid var(--border);
  position: fixed; top: 69px; right: 0; bottom: 0; z-index: 55;
  display: flex; flex-direction: column; box-shadow: -4px 0 16px #0003;
}
.chat-pane-header {
  padding: 16px 16px 10px; font-weight: 700; font-size: 12px; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted); display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.chat-pane-scope-name {
  color: var(--text); text-transform: none; letter-spacing: normal;
  max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-pane-back {
  padding: 3px 9px; font-size: 11px; font-weight: 700; text-transform: none; letter-spacing: normal;
  border-radius: 999px; background: #e5e7eb; color: var(--text);
}
.chat-pane-back:hover { background: #d1d5db; }
.chat-pane .chat-empty-state { margin: 40px 16px; font-size: .95rem; }
.chat-messages { flex: 1; overflow-y: auto; padding: 0 16px; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.chat-message { max-width: 90%; align-self: flex-start; }
.chat-message.own { align-self: flex-end; }
.chat-message-meta { display: flex; gap: 8px; align-items: baseline; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.chat-message.own .chat-message-meta { flex-direction: row-reverse; }
.chat-message-sender { font-weight: 700; }
.chat-message-text {
  background: #f3f4f6; color: var(--text); padding: 8px 12px; border-radius: 12px;
  font-size: 14px; overflow-wrap: break-word; white-space: pre-wrap;
}
.chat-message.own .chat-message-text { background: var(--accent); color: #fff; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 12px 16px 16px; border-top: 1px solid var(--border); }
.chat-form textarea {
  flex: 1; resize: none; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  font: inherit; font-size: 14px; max-height: 120px;
}
.chat-form button { padding: 10px 16px; }

/* --- Backup key folder selection --- */
.backup-folder-list { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
.backup-folder-choice { display: flex; align-items: center; gap: 8px; font-size: 14px; }

#galleryError { margin: 16px 28px 0; }
.empty-state { text-align: center; color: #9ca3af; margin: 80px 20px; font-size: 1.05rem; }

/* --- Add backup key --- */
#backupKeyForm .hint { color: var(--muted); font-size: 14px; margin: 0; }
#backupKeyRow { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
#backupInviteCode { flex: 1 1 220px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px; font: inherit; letter-spacing: 1px; }
#backupKeyStatus { margin: 10px 0 0; font-weight: 600; }
#backupKeyStatus.success { color: var(--success-text); }
#backupKeyStatus.error { color: var(--danger-text); }

/* --- Upload progress --- */
.upload-progress { margin: 20px 28px 0; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px 18px; }
.upload-progress-header { display: flex; align-items: center; justify-content: space-between; font-weight: 700; font-size: 14px; margin-bottom: 8px; }
#uploadProgressPercent { color: var(--accent); }
.progress-bar { height: 8px; border-radius: 999px; background: var(--border); overflow: hidden; }
.progress-bar.small { height: 5px; margin-top: 4px; }
.progress-bar-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width .25s ease; }
.progress-bar-fill.failed { background: var(--danger-text); }
.upload-progress-list { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; max-height: 220px; overflow-y: auto; }
.upload-progress-item-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; font-size: 13px; }
.upload-item-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-item-status { flex: none; color: var(--muted); font-weight: 600; }

.gallery-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 18px; padding: 24px 28px 40px;
}
/* A plain div, not a button — it hosts two independent buttons (open the
   file, open its chat) side by side, and buttons can't nest in valid HTML. */
.photo-card {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; transition: transform .12s ease, box-shadow .12s ease;
}
.photo-card:hover { transform: translateY(-3px); box-shadow: 0 12px 24px #0003; }
.photo-card-open {
  display: flex; flex-direction: column; align-items: center; gap: 10px; width: 100%;
  background: none; padding: 22px 14px; text-align: center;
}
.photo-card-chat {
  position: absolute; top: 8px; right: 8px; width: 28px; height: 28px; padding: 0;
  background: none; color: var(--muted); border-radius: 8px; display: grid; place-items: center;
}
.photo-card-chat svg { width: 16px; height: 16px; }
.photo-card-chat:hover { background: #f3f4f6; color: var(--text); }
.photo-card-icon { width: 52px; height: 52px; color: var(--accent); }
.photo-card-icon svg { width: 100%; height: 100%; }
.photo-card-name { font-weight: 600; font-size: 14px; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.photo-card-date { font-size: 12px; color: var(--muted); }

/* --- Lightbox / carousel --- */
body.lightbox-open { overflow: hidden; }
.lightbox {
  position: fixed; inset: 0; z-index: 50; background: rgba(5, 8, 16, .96);
  display: flex; flex-direction: column;
}
.lightbox:fullscreen, .lightbox:-webkit-full-screen { background: #05080f; }
.lightbox-top { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; gap: 16px; }
.lightbox-caption { color: #e5e7eb; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lightbox-controls { display: flex; gap: 8px; flex: none; }
.icon-button {
  width: 40px; height: 40px; padding: 0; border-radius: 999px; background: #ffffff1a; color: #fff;
  display: grid; place-items: center; font-size: 18px; line-height: 1;
}
.icon-button:hover { background: #ffffff33; }
.icon-button.danger:hover { background: var(--danger-text); }
.lightbox-stage { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; position: relative; padding: 0 70px; }
#lightboxImg { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.lightbox-spinner { position: absolute; color: #e5e7eb; font-weight: 600; }
.lightbox-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 2;
  width: 52px; height: 52px; border-radius: 999px; background: #ffffff1a; color: #fff;
  font-size: 28px; line-height: 1; display: grid; place-items: center; padding: 0;
}
.lightbox-nav:hover { background: #ffffff33; }
.lightbox-prev { left: 14px; }
.lightbox-next { right: 14px; }

/* --- Document viewer/editor (spreadsheets, Word docs, PDFs, text, HTML) --- */
/* Reuses .lightbox-top/.lightbox-caption/.lightbox-controls/.icon-button
   for the header bar; only the body differs, since Univer, docx-preview, the
   PDF pages and the text/HTML previews all read best on a light background
   rather than the lightbox's dark one. */
.doc-viewer {
  position: fixed; inset: 0; z-index: 50; background: rgba(5, 8, 16, .96);
  display: flex; flex-direction: column;
}
.doc-viewer-body { flex: 1; min-height: 0; background: #fff; overflow: auto; position: relative; }
.doc-viewer-loading { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: var(--muted); font-weight: 600; }
.sheet-editor-container { position: absolute; inset: 0; }
.docx-preview-container { max-width: 900px; margin: 0 auto; padding: 24px 16px 60px; }
.pdf-preview-container {
  max-width: 900px; margin: 0 auto; padding: 24px 16px 60px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  background: #e5e7eb; min-height: 100%;
}
.pdf-page-canvas { max-width: 100%; box-shadow: 0 4px 16px #0003; background: #fff; }
.text-preview-container {
  max-width: 900px; margin: 0 auto; padding: 24px 28px 60px;
  font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap; word-break: break-word; color: var(--text);
}
.html-preview-frame { display: block; width: 100%; height: 100%; border: 0; background: #fff; }
.doc-viewer-save-status { color: #e5e7eb; font-size: 13px; font-weight: 600; }

@media (max-width: 640px) {
  .gallery-header { padding: 14px 16px; }
  #galleryError, .upload-progress { margin-left: 16px; margin-right: 16px; }
  .gallery-grid { padding: 18px 16px 32px; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
  .lightbox-stage { padding: 0 54px; }
  .lightbox-nav { width: 42px; height: 42px; font-size: 22px; }
  .gallery-actions span, .gallery-actions button { padding: 10px 14px; }
  .folder-pane { position: fixed; top: 69px; left: 0; bottom: 0; z-index: 4; box-shadow: 4px 0 16px #0003; }
  .chat-pane { left: 0; width: auto; } /* already position:fixed unconditionally — just go full-width on narrow screens */
}
