:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --fg: #1c1c1e;
  --muted: #6e6e73;
  --border: #d2d2d7;
  --accent: #0071e3;
  --danger: #d70015;
  --card-bg: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --fg: #f5f5f7;
    --muted: #98989d;
    --border: #3a3a3c;
    --accent: #409cff;
    --danger: #ff453a;
    --card-bg: #2c2c2e;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
}

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.auth-page {
  max-width: 360px;
  margin: 80px auto;
  padding: 0 20px;
  text-align: center;
}

h1 { font-size: 1.6rem; }

a { color: var(--accent); }

.nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.nav .brand {
  font-weight: 600;
  text-decoration: none;
  color: var(--fg);
}

.nav nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
}

.nav-email {
  display: inline-block;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

@media (max-width: 480px) {
  .nav-email { max-width: 44vw; }
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin-top: 20px;
}

.card {
  display: block;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card-bg);
  text-decoration: none;
  color: inherit;
}

.card h2 { margin: 0 0 6px; font-size: 1.1rem; }
.card p { margin: 0; color: var(--muted); font-size: 0.9rem; }

.empty { color: var(--muted); }

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.row-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

.filename { overflow-wrap: break-word; word-break: break-all; }

.summary { color: var(--muted); margin-top: 4px; }

/* Below this width, tables reflow into a stack of labeled cards instead of
   scrolling sideways -- the admin tables are dense with per-row forms, and
   horizontal scrolling while trying to tap small controls is a poor mobile
   experience. Each <td> needs a matching data-label attribute in the view. */
@media (max-width: 680px) {
  table, thead, tbody, tr, th, td { display: block; width: 100%; }
  thead { display: none; }

  tbody tr {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 12px;
    background: var(--card-bg);
  }

  tbody td {
    border-bottom: none;
    padding: 8px 0;
  }

  tbody td:not(:last-child) {
    border-bottom: 1px solid var(--border);
  }

  tbody td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--muted);
    margin-bottom: 4px;
  }

  .inline-form input, .inline-form select {
    flex: 1 1 auto;
    min-width: 0;
  }
}

.stacked-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 400px;
  margin-top: 12px;
}

.stacked-form label { font-size: 0.85rem; color: var(--muted); margin-top: 6px; }

input, select, button {
  font-size: 1rem;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--fg);
}

button {
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  border: none;
  margin-top: 6px;
}

.danger-button { background: var(--danger); }

.inline-form {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin: 0;
}

.link-button {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 0;
  background: none;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  font-size: 0.9rem;
}

.link-button.danger { color: var(--danger); }

.error { color: var(--danger); }

/* --- v2: folders, bulk toolbar, drag-drop, view modes --- */

.breadcrumb {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 8px;
  overflow-wrap: break-word;
}

.breadcrumb a { color: var(--accent); }

.upload-and-new {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  align-items: flex-start;
}

.new-folder-form { margin-top: 12px; }

.view-toggle {
  display: flex;
  gap: 8px;
  margin: 12px 0;
}

.view-btn {
  background: var(--card-bg);
  color: var(--fg);
  border: 1px solid var(--border);
}

.view-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.bulk-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin: 12px 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card-bg);
}

.bulk-toolbar select { flex: 1 1 160px; min-width: 0; }

tr.folder-row .filename a { text-decoration: none; color: inherit; }

/* Checkboxes stay hidden by default on wide viewports, where shift/ctrl-click
   (JS-enhanced) is available; shown on narrow viewports or once "Select" mode
   is toggled on, since there's no modifier-click on a touchscreen. */
td:has(input[type="checkbox"]) { width: 1%; }

@media (max-width: 680px) {
  #file-table thead { display: none; }
  #file-table tbody td:first-child::before { content: none; }
}

tbody tr.selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

#file-table.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: 4px;
}

/* Grid view reuses the same table -> stacked-card transformation the mobile
   breakpoint already applies, just triggered by a class instead of a media
   query, and arranged as a multi-column grid instead of one-per-row. Same
   markup, same data-label mechanism -- no second layout system to maintain. */
#file-table.view-grid,
#file-table.view-grid thead,
#file-table.view-grid tbody,
#file-table.view-grid tr,
#file-table.view-grid th,
#file-table.view-grid td {
  display: block;
}
#file-table.view-grid thead { display: none; }
#file-table.view-grid tbody {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}
#file-table.view-grid tr {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  background: var(--card-bg);
}
#file-table.view-grid td { border-bottom: none; padding: 6px 0; }
#file-table.view-grid td::before {
  content: attr(data-label);
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
}
#file-table.view-grid td:first-child::before { content: none; }

/* --- v3: Storage-style shell redesign (files.ejs only, via body.shell-page) --- */

body.shell-page {
  --shell-accent: #6C63FF;
  --shell-grad-a: #7B7CFA;
  --shell-grad-b: #5B8DEF;
  --shell-bg: #ffffff;
  --shell-panel: #f4f5fb;
  --shell-text: #1f2233;
  --shell-muted: #7b7f97;
  --shell-border: #e7e8f3;
  margin: 0;
  padding: 24px;
  min-height: 100vh;
  background: linear-gradient(135deg, var(--shell-grad-a), var(--shell-grad-b));
  color: var(--shell-text);
}

@media (prefers-color-scheme: dark) {
  body.shell-page {
    --shell-bg: #1b1c2a;
    --shell-panel: #232437;
    --shell-text: #eef0fb;
    --shell-muted: #9a9dbb;
    --shell-border: #33344a;
  }
}

.shell {
  display: flex;
  max-width: 1400px;
  margin: 0 auto;
  min-height: calc(100vh - 48px);
  background: var(--shell-bg);
  border-radius: 24px;
  box-shadow: 0 30px 60px -20px rgba(30, 20, 90, 0.35);
  overflow: hidden;
}

.sidebar {
  width: 220px;
  flex: 0 0 220px;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-right: 1px solid var(--shell-border);
  background: var(--shell-panel);
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--shell-accent);
  text-decoration: none;
  padding: 0 8px 20px;
}

.brand-mark { font-size: 1.2rem; }

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--shell-muted);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.92rem;
  min-height: 40px;
}

.sidebar-link:hover { background: rgba(108, 99, 255, 0.1); }

.sidebar-link.active {
  background: var(--shell-accent);
  color: #fff;
}

.sidebar-icon { width: 1.1em; text-align: center; }

/* expandable folder tree, built from <details>/<summary> -- no JS required
   to expand/collapse; the folder name is a plain link inside <summary>, so
   clicking the arrow toggles and clicking the name navigates. */
.tree-root {
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.tree-node { list-style: none; }
.tree-children { list-style: none; margin: 0; padding-left: 14px; }

.tree-summary, .tree-leaf-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 8px;
  min-height: 36px;
}
.tree-summary:hover, .tree-leaf-row:hover { background: rgba(108, 99, 255, 0.08); }

.tree-arrow {
  display: inline-block;
  width: 12px;
  flex: 0 0 auto;
  font-size: 0.65rem;
  color: var(--shell-muted);
  transition: transform 0.15s ease;
}
details[open] > .tree-summary .tree-arrow { transform: rotate(90deg); }

.tree-spacer { display: inline-block; width: 12px; flex: 0 0 auto; }

.tree-link {
  color: var(--shell-muted);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.88rem;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-link.active {
  background: var(--shell-accent);
  color: #fff;
  border-radius: 6px;
  padding: 2px 6px;
  margin: -2px -6px;
}

.sidebar-admin { margin-top: auto; }

.sidebar-storage {
  margin-top: 12px;
  padding: 14px;
  border-radius: 14px;
  background: var(--shell-bg);
  border: 1px solid var(--shell-border);
  font-size: 0.85rem;
  color: var(--shell-muted);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-storage strong { color: var(--shell-text); font-size: 1rem; }

.shell-main {
  flex: 1 1 auto;
  min-width: 0;
  padding: 28px 32px 48px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.search-form {
  position: relative;
  flex: 1 1 auto;
  max-width: 420px;
  margin: 0;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--shell-muted);
}

.search-form input {
  width: 100%;
  padding-left: 38px;
  border-radius: 999px;
  background: var(--shell-panel);
  border: 1px solid var(--shell-border);
  color: var(--shell-text);
  margin: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

body.shell-page summary { list-style: none; cursor: pointer; }
body.shell-page summary::-webkit-details-marker { display: none; }
body.shell-page summary::marker { content: ''; }

.btn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: 999px;
  background: var(--shell-accent);
  color: #fff;
  font-weight: 600;
  min-height: 44px;
}

.upload-menu, .user-menu { position: relative; }

.upload-panel, .user-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 20;
  background: var(--shell-bg);
  border: 1px solid var(--shell-border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 20px 40px -12px rgba(20, 10, 60, 0.3);
  min-width: 260px;
  text-align: left;
}

.avatar-btn {
  display: flex;
  align-items: center;
  gap: 8px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--shell-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex: 0 0 auto;
}

.user-name { font-weight: 600; font-size: 0.9rem; text-transform: capitalize; }

.user-email { font-size: 0.8rem; color: var(--shell-muted); word-break: break-all; margin: 0 0 10px; }
.user-panel a { display: block; margin-bottom: 8px; }

.content-header { margin-bottom: 8px; }
.content-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.content-title-row h1 { margin: 4px 0; font-size: 1.8rem; }

body.shell-page .breadcrumb { color: var(--shell-muted); }
body.shell-page .breadcrumb a { color: var(--shell-accent); text-decoration: none; }
body.shell-page .summary { color: var(--shell-muted); }
body.shell-page .empty { color: var(--shell-muted); }

body.shell-page .view-btn {
  background: var(--shell-panel);
  border: 1px solid var(--shell-border);
  color: var(--shell-muted);
  border-radius: 10px;
  min-height: 40px;
  padding: 6px 12px;
}
body.shell-page .view-btn.active {
  background: var(--shell-accent);
  color: #fff;
  border-color: var(--shell-accent);
}

body.shell-page .bulk-toolbar {
  background: var(--shell-panel);
  border: 1px solid var(--shell-border);
  border-radius: 14px;
}
body.shell-page .bulk-toolbar button { border-radius: 999px; }
body.shell-page .bulk-toolbar select { border-radius: 10px; }
body.shell-page .danger-button { background: #E0507A; }

/* icon badges -- purely cosmetic classification, never affects how a file is served */
.icon-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  font-size: 0.62rem;
  font-weight: 700;
  color: #fff;
  margin-right: 10px;
  vertical-align: middle;
  flex: 0 0 auto;
}
.kind-folder { background: #5B8DEF; font-size: 1rem; }
.kind-pdf { background: #EF5350; }
.kind-doc { background: #4C8DFF; }
.kind-xls { background: #2FB380; }
.kind-ppt { background: #FF8A3D; }
.kind-zip { background: #8D8FA3; }
.kind-img { background: #F2994A; }
.kind-vid { background: #B15CFF; }
.kind-aud { background: #FF6FA8; }
.kind-txt { background: #6C63FF; }
.kind-code { background: #23A6D5; }
.kind-file { background: #9098B1; }

body.shell-page #file-table .filename { display: flex; align-items: center; }

/* per-row kebab action menu, replacing v2's always-visible inline links */
.row-actions-menu { position: relative; }
.row-actions-menu summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--muted);
}
.row-actions-menu summary:hover { background: rgba(108, 99, 255, 0.12); }
.row-actions-panel {
  position: absolute;
  right: 0;
  top: 100%;
  z-index: 15;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  min-width: 140px;
  box-shadow: 0 12px 30px -10px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
}
.row-actions-panel a {
  padding: 8px 10px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--fg);
  font-size: 0.88rem;
}
.row-actions-panel a:hover { background: rgba(108, 99, 255, 0.1); }
.row-actions-panel a.danger { color: var(--danger); }
td.row-menu { text-align: right; position: relative; }

/* upload progress toast -- populated by public/js/uploadProgress.js via
   textContent only; the plain form still submits and redirects normally if
   JS is unavailable, this is a pure enhancement layered on top. */
.upload-toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 280px;
  max-width: calc(100vw - 32px);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 20px 50px -15px rgba(0, 0, 0, 0.35);
  padding: 14px 16px;
  z-index: 100;
}
.upload-toast-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
  margin-bottom: 10px;
}
.upload-toast-close {
  background: none;
  border: none;
  min-height: auto;
  padding: 2px 6px;
  color: var(--muted);
  margin: 0;
}
#upload-toast-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.upload-toast-item { display: flex; flex-direction: column; gap: 4px; }
.upload-toast-name { font-size: 0.85rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-toast-bar-track { height: 6px; border-radius: 999px; background: var(--border); overflow: hidden; }
.upload-toast-bar-fill { height: 100%; background: var(--shell-accent, var(--accent)); width: 0%; transition: width 0.15s linear; }
.upload-toast-status { font-size: 0.75rem; color: var(--muted); }

/* Grid mode: center the icon+name block at the top of each card, closer to
   the reference's icon-forward tile, while still reusing the same table ->
   stacked-card markup (no second layout system). */
#file-table.view-grid td.filename {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 6px;
}
#file-table.view-grid td.filename::before { content: none; }
#file-table.view-grid .icon-badge {
  width: 48px;
  height: 48px;
  font-size: 0.8rem;
  margin-right: 0;
}
#file-table.view-grid td:first-child,
#file-table.view-grid td.row-menu {
  position: absolute;
  top: 10px;
}
#file-table.view-grid td:first-child { left: 10px; }
#file-table.view-grid td.row-menu { right: 10px; }
#file-table.view-grid tr { position: relative; padding-top: 44px; }

@media (max-width: 900px) {
  body.shell-page { padding: 0; }
  .shell { flex-direction: column; border-radius: 0; min-height: 100vh; }
  .sidebar { width: auto; flex: 0 0 auto; flex-direction: row; flex-wrap: wrap; border-right: none; border-bottom: 1px solid var(--shell-border); }
  .sidebar-nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar-admin { margin-top: 0; }
  .sidebar-storage { display: none; }
  /* The nested tree assumes a tall, narrow desktop sidebar; on mobile the
     sidebar becomes a horizontal bar, so fall back to breadcrumb-only
     navigation (already present in the main content) instead of squeezing
     a deep indented tree sideways. */
  .tree-root { display: none; }
  .shell-main { padding: 20px 16px 40px; }
  .topbar { flex-wrap: wrap; }
  .search-form { max-width: none; flex: 1 1 100%; order: 3; }
  .upload-panel, .user-panel { right: auto; left: 0; }
  .upload-toast { left: 16px; right: 16px; width: auto; bottom: 16px; }
}
