:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev-2: #1c2330;
  --border: #2a3342;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #4f8cff;
  --accent-2: #7c5cff;
  --danger: #f85149;
  --ok: #3fb950;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: radial-gradient(1200px 600px at 70% -10%, #16233d 0%, var(--bg) 55%);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------------- Buttons ---------------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn .bi { margin-right: 6px; vertical-align: -1px; }
.btn:hover { border-color: var(--accent); background: var(--bg-elev-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
}
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); border-color: var(--border); background: var(--bg-elev); }
.btn.danger { border-color: transparent; background: rgba(248, 81, 73, 0.15); color: var(--danger); }
.btn.danger:hover { background: rgba(248, 81, 73, 0.28); }
.btn.block { width: 100%; padding: 11px; font-size: 15px; }

/* ---------------- Views ---------------- */
.view { min-height: 100vh; }

/* ---------------- Login ---------------- */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card h1 { margin: 8px 0 0; font-size: 22px; }
.subtitle { color: var(--muted); margin: 4px 0 24px; font-size: 14px; }
.field { display: block; text-align: left; margin-bottom: 14px; }
.field span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input, #search, .field input[type="text"], .field input[type="email"], .field input[type="password"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus, #search:focus { border-color: var(--accent); }
.error { color: var(--danger); font-size: 13px; margin: 0 0 12px; }
.hint { color: var(--muted); font-size: 12px; margin: 14px 0 0; }

/* ---------------- Avatar / brand ---------------- */
.avatar {
  display: block;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-elev-2);
}
.login-avatar {
  width: 64px;
  height: 64px;
  margin: 0 auto 6px;
  border: 2px solid var(--accent);
  box-shadow: 0 6px 20px rgba(79, 140, 255, 0.35);
}
.top-avatar {
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  cursor: pointer;
}
.avatar-wrap { position: relative; display: inline-flex; }
.avatar-btn {
  position: relative;
  background: none;
  border: none;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  line-height: 0;
}
.avatar-badge {
  position: absolute;
  right: -2px;
  bottom: -2px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--bg);
  pointer-events: none;
}
.avatar-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: auto;
  z-index: 60;
  min-width: 190px;
  max-width: calc(100vw - 24px);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
  flex-direction: column;
}
.menu-item {
  appearance: none;
  border: none;
  background: none;
  color: var(--text);
  text-align: left;
  padding: 9px 10px;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}
.menu-item .bi { margin-right: 8px; vertical-align: -1px; }
.menu-item:hover { background: var(--bg-elev-2); }
.menu-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.menu-item.danger { color: var(--danger); }
@media (max-width: 640px), (pointer: coarse) {
  .menu-item { padding: 12px 12px; font-size: 15px; }
}

/* ---------------- Topbar ---------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 20px;
  background: rgba(13, 17, 23, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  overflow: visible;
}
.brand { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-name { font-size: 15px; font-weight: 700; }
.brand-sub { color: var(--muted); font-size: 11px; font-weight: 500; }
.breadcrumbs {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
}
.breadcrumbs::-webkit-scrollbar { display: none; }
.crumb {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  white-space: nowrap;
}
.crumb { background: none; border: none; color: var(--muted); font-size: 14px; cursor: pointer; padding: 4px 6px; border-radius: 6px; white-space: nowrap; }
.crumb:hover { color: var(--text); background: var(--bg-elev); }
.crumb.current { color: var(--text); font-weight: 600; }
.crumb-sep { color: var(--border); font-size: 10px; }
.search-wrap { position: relative; flex: 0 1 200px; min-width: 130px; }
.search-wrap .bi-search { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; pointer-events: none; }
#search { width: 100%; padding: 8px 12px 8px 32px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }
.btn.icon { padding: 8px 10px; }
.btn.icon .bi { margin-right: 0; }

/* ---------------- Grid ---------------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  padding: 20px;
}
.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-height: 150px;
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.15s, transform 0.15s, background 0.15s;
  color: var(--text);
}
.tile:hover { border-color: var(--accent); transform: translateY(-2px); background: var(--bg-elev-2); }
.tile .thumb {
  width: 100%;
  height: 96px;
  object-fit: cover;
  border-radius: 6px;
  background: #000;
}
.tile .icon { font-size: 42px; line-height: 96px; }
.tile .name {
  font-size: 13px;
  text-align: center;
  word-break: break-word;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  max-width: 100%;
}
.tile .size { color: var(--muted); font-size: 11px; }
.tile-delete {
  position: absolute;
  top: 6px;
  right: 6px;
  border: none;
  background: rgba(0, 0, 0, 0.55);
  color: var(--muted);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 12px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
}
.tile:hover .tile-delete { opacity: 1; }
.tile-delete:hover { color: var(--danger); }
.tile.folder .icon { color: var(--accent); }

/* Format colours, so a glance sorts the document types apart. */
.cat-doc { color: #4a86ff; }
.cat-sheet { color: #3fb950; }
.cat-slides { color: #f0883e; }
.cat-pdf { color: #f85149; }
.cat-archive { color: #d2a8ff; }
.cat-audio { color: #56d4dd; }

/* Drag & drop: the grabbed tile fades, the target folder lights up. */
.tile.dragging { opacity: 0.4; }
.tile.drop-target,
.crumb.drop-target {
  border-color: var(--accent) !important;
  background: rgba(79, 140, 255, 0.18);
  color: var(--text);
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
}
.grid-footer { padding: 0 20px 40px; text-align: center; color: var(--muted); }
.empty { margin: 40px 0; color: var(--muted); font-size: 15px; }
.empty-icon { font-size: 52px; color: var(--border); margin-bottom: 10px; display: block; }

/* ---------------- Modal ---------------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-frame {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}
.modal-stage {
  position: relative;
  width: 100%;
  max-width: 920px;
}
.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 6;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(22, 27, 34, 0.92);
  color: var(--text);
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.nav-btn:hover { color: var(--accent); border-color: var(--accent); }
.nav-prev { left: 6px; }
.nav-next { right: 6px; }
#modal .modal-card { height: 92vh; }
.modal-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 900px;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-head-actions { display: flex; gap: 4px; flex-shrink: 0; }
.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-title h2 { margin: 0; font-size: 17px; word-break: break-all; }
.modal-title p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.modal-preview {
  flex: 1 1 auto;
  min-height: 120px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #05070a;
  overflow: auto;
}
.modal-preview img, .modal-preview video {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}
.modal-preview img { touch-action: pan-y; -webkit-user-select: none; user-select: none; }
.modal-preview audio { width: 90%; margin: 20px; }
.modal-preview iframe { width: 100%; height: 100%; min-height: 240px; border: none; }
.modal-preview pre {
  margin: 0;
  padding: 20px;
  width: 100%;
  max-height: 100%;
  align-self: stretch;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
}
.modal-preview .placeholder { color: var(--muted); padding: 40px; text-align: center; }
.modal-preview .placeholder p { margin: 0; }
.placeholder-icon { font-size: 56px; display: block; margin-bottom: 14px; }
.placeholder-title { color: var(--text); font-weight: 600; margin-bottom: 6px !important; }

/* ---------------- Move-to picker ---------------- */
.picker-card { max-width: 460px; }
.picker-list {
  padding: 8px;
  overflow-y: auto;
  max-height: 46vh;
  min-height: 120px;
}
.picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  appearance: none;
  border: none;
  background: none;
  color: var(--text);
  text-align: left;
  font-size: 15px;
  padding: 12px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.picker-row:hover { background: var(--bg-elev-2); }
.picker-row .bi-folder-fill { color: var(--accent); }
.picker-row span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-go { color: var(--muted); font-size: 12px; }
.picker-empty { color: var(--muted); font-size: 13px; padding: 16px 12px; margin: 0; }

/* ---------------- Queued upload batches ---------------- */
.batch-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  justify-content: center;
  max-width: calc(100vw - 24px);
  font-size: 13px;
}
.batch-bar .bi-hourglass-split { color: var(--accent); }
.modal-foot {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.folder-card { max-width: 420px; }
.folder-body { padding: 18px 20px; }
.folder-body .field { margin: 0; }

/* ---------------- Bulk select ---------------- */
.tile-check {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: rgba(0, 0, 0, 0.5);
  color: transparent;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 2;
  pointer-events: none;
}
.tile:hover .tile-check { opacity: 1; }
body.selecting .tile-check { opacity: 1; }
body.selecting .tile { border-color: var(--border); }
.tile.selected { border-color: var(--accent) !important; background: var(--bg-elev-2); }
.tile.selected .tile-check { background: var(--accent); border-color: var(--accent); color: #fff; }
/* Both bottom bars share one rail so they stack instead of overlapping. */
.bottom-bars {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 18px;
  z-index: 45;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  pointer-events: none;
}
.bottom-bars > * { pointer-events: auto; }
.select-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  justify-content: center;
  max-width: calc(100vw - 24px);
}
.select-count { font-size: 13px; color: var(--muted); font-weight: 600; }

/* ---------------- Empty state ---------------- */
.empty.fun {
  font-size: 17px;
  font-weight: 600;
  max-width: 480px;
  margin: 8px auto 0;
  line-height: 1.6;
}

/* ---------------- Upload / drag ---------------- */
.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(79, 140, 255, 0.12);
  border: 3px dashed var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
  pointer-events: none;
}
.upload-progress {
  position: fixed;
  bottom: 18px;
  right: 18px;
  z-index: 70;
  width: 280px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}
.upload-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px; }
.upload-head span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-track { height: 6px; border-radius: 3px; background: var(--bg-elev-2); overflow: hidden; }
.upload-fill { height: 100%; width: 0; border-radius: 3px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 0.2s; }

/* ---------------- Toast ---------------- */
.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  background: rgba(248, 81, 73, 0.12);
  border: 1px solid var(--danger);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  max-width: 80vw;
  box-shadow: var(--shadow);
}
.toast.success {
  background: rgba(63, 185, 80, 0.12);
  border-color: var(--ok);
}

/* ---------------- Confirm dialog ---------------- */
.confirm-card { max-width: 440px; }
.confirm-body { padding: 18px 20px; }
.confirm-body p { margin: 0; font-size: 15px; color: var(--text); word-break: break-word; }
.confirm-warn { color: var(--danger); font-size: 26px; flex-shrink: 0; }
#confirm-modal .modal-title { display: flex; gap: 12px; align-items: flex-start; }
#confirm-modal .modal-title div { min-width: 0; }

/* ---------------- Fullscreen ---------------- */
#modal:fullscreen { padding: 0; background: #000; }
#modal:fullscreen .modal-frame { height: 100vh; }
#modal:fullscreen .modal-stage { max-width: 100vw; height: 100%; }
#modal:fullscreen .modal-card {
  max-width: 100vw;
  max-height: 100vh;
  height: 100%;
  border-radius: 0;
  border: none;
  box-shadow: none;
}
#modal:fullscreen .modal-preview { flex: 1 1 auto; min-height: 0; }
#modal:fullscreen .modal-preview img,
#modal:fullscreen .modal-preview video {
  max-height: 100%;
  max-width: 100%;
}
#modal:fullscreen .modal-preview iframe { height: 100%; }

/* Fallback fullscreen for browsers without Element.requestFullscreen (iOS). */
#modal.faux-full {
  padding: 0;
  background: #000;
}
#modal.faux-full .modal-frame { height: 100%; }
#modal.faux-full .modal-stage { max-width: 100vw; height: 100%; }
#modal.faux-full .modal-card {
  max-width: 100vw;
  max-height: 100%;
  height: 100%;
  border-radius: 0;
  border: none;
  box-shadow: none;
}
#modal.faux-full .modal-preview { flex: 1 1 auto; min-height: 0; }
#modal.faux-full .modal-preview iframe { height: 100%; }

/* ---------------- Responsive ---------------- */
@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 12px; }
  .breadcrumbs { order: 3; flex-basis: 100%; }
  .topbar-actions { order: 2; margin-left: auto; }
  .search-wrap { flex: 1 1 140px; }
}
@media (max-width: 640px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; padding: 12px; }
  .topbar-actions { width: 100%; justify-content: flex-end; flex-wrap: wrap; }
  .modal { padding: 8px; }
  #modal .modal-card { height: calc(100dvh - 16px); max-height: none; }
  .modal-head { padding: 12px 14px; }
  .modal-title h2 { font-size: 15px; }
  /* The other dialogs keep full-width stacked buttons. */
  .modal-foot { flex-direction: column; }
  .modal-foot .btn { width: 100%; }
  /* The viewer keeps its three actions on one row, phone-gallery style. */
  #modal .modal-foot {
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 8px;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  #modal .modal-foot .btn {
    flex: 1 1 0;
    min-width: 0;
    width: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 9px 4px;
    font-size: 11px;
    line-height: 1.2;
    white-space: normal;
    text-align: center;
  }
  #modal .modal-foot .btn .bi { margin-right: 0; font-size: 17px; vertical-align: 0; }
  .nav-btn { width: 36px; height: 36px; font-size: 16px; }
  .nav-prev { left: 2px; }
  .nav-next { right: 2px; }
  #picker-modal .modal-card { max-height: calc(100dvh - 16px); }
  .picker-list { max-height: 50vh; }
  /* Bottom bars go full width, with compact icon-over-label buttons. */
  .bottom-bars { bottom: max(12px, env(safe-area-inset-bottom)); gap: 8px; }
  .batch-bar, .select-bar { width: 100%; max-width: none; }
  .batch-bar .btn { flex: 1 1 auto; }
  .select-bar {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
    padding: 10px;
  }
  .select-count { grid-column: 1 / -1; text-align: center; }
  .select-bar .btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    padding: 9px 2px;
    font-size: 10px;
    line-height: 1.15;
    white-space: normal;
    text-align: center;
  }
  .select-bar .btn .bi { margin-right: 0; font-size: 15px; vertical-align: 0; }
}

/* Long labels get a short form on narrow screens. */
.btn-label-short { display: none; }
@media (max-width: 640px) {
  .btn-label-full { display: none; }
  .btn-label-short { display: inline; }
}
