/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ---- Themes ---- */
:root {
  --bg:        #0f1117;
  --surface:   #1a1d27;
  --surface2:  #22263a;
  --border:    #2c2f3f;
  --accent:    #4f8ef7;
  --accent-hov:#3a74e0;
  --text:      #e2e4ed;
  --muted:     #7b7f96;
  --danger:    #e05555;
  --radius:    8px;
  --card-w:    220px;
  --sidebar-w: 210px;
  --header-h:  52px;
}

[data-theme="light"] {
  --bg:        #f0f2f8;
  --surface:   #ffffff;
  --surface2:  #e8eaf5;
  --border:    #d4d7e8;
  --accent:    #2563eb;
  --accent-hov:#1d4ed8;
  --text:      #1e2130;
  --muted:     #5c6280;
  --danger:    #dc2626;
}

[data-theme="hc"] {
  --bg:        #000000;
  --surface:   #0d0d0d;
  --surface2:  #1a1a1a;
  --border:    #ffffff;
  --accent:    #ffff00;
  --accent-hov:#e6e600;
  --text:      #ffffff;
  --muted:     #cccccc;
  --danger:    #ff4444;
}

html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  transition: background .2s, color .2s;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---- Auth pages ---- */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 360px;
}

.auth-card h1 {
  font-size: 1.4rem;
  margin-bottom: 1.5rem;
  text-align: center;
}

.auth-card p {
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 1rem;
  text-align: center;
}

/* ---- Forms ---- */
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

input[type=text],
input[type=password],
input[type=search],
input[type=email] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  padding: .5rem .75rem;
  width: 100%;
  outline: none;
  transition: border-color .15s;
}

input:focus { border-color: var(--accent); }

button, .btn {
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  padding: .55rem 1.2rem;
  transition: background .15s;
  width: 100%;
}

button:hover, .btn:hover { background: var(--accent-hov); }
button:disabled, .btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-sm {
  font-size: 0.8rem;
  padding: .35rem .8rem;
  width: auto;
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  width: auto;
}
.btn-ghost:hover { background: var(--border); }

.btn-danger {
  background: var(--danger);
}
.btn-danger:hover { filter: brightness(1.1); }

/* ---- Icon button ---- */
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text);
  cursor: pointer;
  border-radius: var(--radius);
  padding: .35rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  font-size: 1rem;
  transition: background .1s;
}
.icon-btn:hover { background: var(--border); }

/* ---- Layout ---- */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* ---- Header ---- */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: var(--header-h);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: .75rem;
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  white-space: nowrap;
}

.header-brand h1 {
  font-size: 1.05rem;
  font-weight: 700;
}

.header-center {
  display: flex;
  justify-content: center;
}

#search-input {
  width: 100%;
  max-width: 520px;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: .5rem;
  position: relative;
}

/* ---- User menu ---- */
.user-menu-wrap {
  position: relative;
}

.avatar-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  width: auto;
  transition: opacity .15s;
}
.avatar-btn:hover { opacity: .85; }

.avatar-circle {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  overflow: hidden;
  flex-shrink: 0;
  border: 2px solid var(--border);
  user-select: none;
}
.avatar-circle.lg {
  width: 72px;
  height: 72px;
  font-size: 2.4rem;
  border-width: 3px;
}
.avatar-circle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 200px;
  box-shadow: 0 8px 28px rgba(0,0,0,.35);
  overflow: hidden;
  z-index: 200;
}
.user-menu.hidden { display: none; }

.user-menu-header {
  padding: .9rem 1rem .75rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
}

.user-menu-item {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .65rem 1rem;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
  background: none;
  border: none;
  width: 100%;
  text-align: left;
  text-decoration: none;
  border-radius: 0;
  font-weight: normal;
  transition: background .1s;
}
.user-menu-item:hover { background: var(--surface2); text-decoration: none; }
.user-menu-item.danger { color: var(--danger); }

/* ---- Sidebar ---- */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  transition: width .2s ease;
  display: flex;
  flex-direction: column;
  position: relative;
}

#sidebar-resize-handle {
  position: absolute;
  top: 0;
  right: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
}

#sidebar-resize-handle:hover,
#sidebar.resizing #sidebar-resize-handle {
  background: var(--accent);
  opacity: .4;
}

#sidebar.resizing { transition: none; user-select: none; }

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1rem .5rem;
  flex-shrink: 0;
}

#sidebar h2 {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-toggle-btn {
  flex-shrink: 0;
  color: var(--muted);
  font-size: 1rem;
  padding: .2rem;
}

#sidebar.collapsed {
  width: 52px;
}

#sidebar.collapsed h2,
#sidebar.collapsed .cat-label,
#sidebar.collapsed .category-count { display: none; }

#sidebar.collapsed .category-item {
  justify-content: center;
  padding: .55rem 0;
}

#sidebar.collapsed .cat-icon { font-size: 1.3rem; }
#sidebar.collapsed #sidebar-header { justify-content: center; padding: .75rem 0 .5rem; }

.category-item {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .42rem 1rem;
  cursor: pointer;
  font-size: 0.88rem;
  color: var(--text);
  transition: background .1s;
  white-space: nowrap;
}

.cat-icon {
  font-size: 1rem;
  flex-shrink: 0;
  line-height: 1;
}

.cat-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.category-item:hover { background: var(--border); }
.category-item.active { background: var(--accent); color: #fff; }

.category-count {
  font-size: 0.72rem;
  color: var(--muted);
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 99px;
  flex-shrink: 0;
}

.category-item.active .category-count {
  background: rgba(255,255,255,.25);
  color: #fff;
}

/* ---- Content ---- */
#content {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
}

/* ---- Grid ---- */
#grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}

@media (max-width: 600px) {
  #grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---- Cards ---- */
.model-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, transform .1s;
}

.model-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.card-thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
  background: var(--bg);
}

.card-thumb-placeholder {
  width: 100%;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  font-size: 2rem;
}

.card-viewer {
  width: 100%;
  aspect-ratio: 1;
  position: relative;
  background: var(--bg);
  overflow: hidden;
}

.card-viewer canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

.card-3d-loader {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.card-snap { cursor: grab; }
.card-snap:active { cursor: grabbing; }

.card-snap-hint {
  position: absolute;
  bottom: 6px;
  right: 6px;
  background: rgba(0,0,0,.55);
  color: rgba(255,255,255,.75);
  font-size: 0.68rem;
  padding: 2px 7px;
  border-radius: 99px;
  pointer-events: none;
  user-select: none;
}

.card-info {
  padding: .6rem .75rem;
}

.card-name {
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-category {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 2px;
}

/* ---- Pagination ---- */
#pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: .3rem;
  padding: 1.25rem 0 .5rem;
  flex-wrap: wrap;
}

.page-btn {
  min-width: 36px;
  height: 36px;
  padding: 0 .5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .88rem;
  font-weight: 500;
  cursor: pointer;
  width: auto;
  transition: background .12s, border-color .12s;
}

.page-btn:hover:not(:disabled) { background: var(--surface2); border-color: var(--accent); }
.page-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.page-btn:disabled { opacity: .35; cursor: not-allowed; }

/* ---- Status bar ---- */
#status-bar {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: .75rem;
}

/* ---- Detail page ---- */
#detail-page {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 1.5rem;
  max-width: 1400px;
}

#detail-top {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 1.5rem;
}

#viewer-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  aspect-ratio: 4/3;
}

#viewer-container canvas,
#viewer-container img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

#viewer-status {
  position: absolute;
  bottom: .75rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.6);
  color: var(--muted);
  font-size: 0.75rem;
  padding: .25rem .75rem;
  border-radius: 99px;
}

.view-3d-btn {
  position: absolute;
  bottom: .75rem;
  right: .75rem;
  background: rgba(0,0,0,.6);
  color: rgba(255,255,255,.85);
  border: 1px solid rgba(255,255,255,.2);
  font-size: 0.75rem;
  padding: .25rem .75rem;
  border-radius: 99px;
  cursor: pointer;
}
.view-3d-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#detail-meta {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#detail-meta h1 { font-size: 1.4rem; }

.meta-row { font-size: 0.85rem; }

.meta-label {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 3px;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  margin-top: .25rem;
}

.tag {
  background: var(--border);
  border-radius: 99px;
  font-size: 0.78rem;
  padding: .2rem .6rem;
  cursor: pointer;
}

.tag:hover { background: var(--accent); color: #fff; }

.tag-auto {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}

#tag-input {
  width: 100%;
  margin-top: .4rem;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: .5rem;
}

.back-link:hover { color: var(--text); }

.file-list {
  list-style: none;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: .3rem;
}

.file-list a { word-break: break-all; }

/* ---- Detail gallery ---- */
#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: .75rem;
}

.gallery-thumb-wrap {
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  transition: border-color .15s;
}

.gallery-thumb-wrap:hover { border-color: var(--accent); }

.gallery-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform .2s;
}

.gallery-thumb-wrap:hover .gallery-thumb { transform: scale(1.04); }

/* ---- Lightbox ---- */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.9);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#lightbox.hidden { display: none; }

#lightbox-img {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
}

#lightbox-close,
#lightbox-prev,
#lightbox-next {
  position: fixed;
  background: rgba(255,255,255,.12);
  border: none;
  color: #fff;
  cursor: pointer;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  font-size: 1.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  padding: 0;
}

#lightbox-close:hover,
#lightbox-prev:hover,
#lightbox-next:hover { background: rgba(255,255,255,.25); }

#lightbox-close { top: 1rem; right: 1rem; font-size: 1rem; }
#lightbox-prev  { left: 1rem; top: 50%; transform: translateY(-50%); }
#lightbox-next  { right: 1rem; top: 50%; transform: translateY(-50%); }

/* ---- Detail files section ---- */
#detail-files {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.detail-files-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1.5rem;
}

/* ---- STL file rows with preview picker ---- */
.stl-file-row {
  display: flex;
  align-items: center;
  gap: .4rem;
  padding: .15rem 0;
}

.stl-file-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: .82rem;
  word-break: break-all;
  white-space: normal;
}

.stl-preview-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 0 .2rem;
  width: auto;
  line-height: 1;
  transition: color .15s;
}

.stl-preview-btn:hover { color: var(--accent); }
.stl-preview-btn.active { color: #f5c518; }

/* ---- Responsive ---- */
@media (max-width: 900px) {
  #detail-top { grid-template-columns: 1fr; }
  .detail-files-grid { grid-template-columns: 1fr; }
}

/* ---- Preferences page ---- */
.prefs-layout {
  max-width: 680px;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 2.5rem;
}

.prefs-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.prefs-section-title {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  padding: .85rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}

.prefs-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* Theme picker */
.theme-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: .75rem;
}

.theme-option {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s;
}

.theme-option.selected { border-color: var(--accent); }

.theme-preview {
  height: 60px;
  display: flex;
  align-items: stretch;
}

.theme-preview-dark   { background: #0f1117; }
.theme-preview-light  { background: #f0f2f8; }
.theme-preview-hc     { background: #000000; }

.theme-preview-bar {
  width: 30%;
  opacity: .6;
}
.theme-preview-dark  .theme-preview-bar { background: #1a1d27; border-right: 1px solid #2c2f3f; }
.theme-preview-light .theme-preview-bar { background: #ffffff; border-right: 1px solid #d4d7e8; }
.theme-preview-hc    .theme-preview-bar { background: #0d0d0d; border-right: 1px solid #fff; }

.theme-preview-dots {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 4px;
  padding: 8px;
}

.theme-dot {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}
.theme-preview-dark  .theme-dot { background: #4f8ef7; }
.theme-preview-light .theme-dot { background: #2563eb; }
.theme-preview-hc    .theme-dot { background: #ffff00; }

.theme-label {
  padding: .4rem .6rem;
  font-size: 0.82rem;
  font-weight: 600;
  border-top: 1px solid var(--border);
  text-align: center;
}

/* Avatar picker */
.avatar-current {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.avatar-presets {
  display: flex;
  gap: .5rem;
  flex-wrap: wrap;
}

.avatar-preset-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  background: none;
  padding: 0;
  transition: border-color .15s, transform .1s;
  width: 48px;
}

.avatar-preset-btn:hover { border-color: var(--accent); transform: scale(1.08); }
.avatar-preset-btn.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,142,247,.3); }

/* ---- Spinner ---- */
.spinner {
  display: inline-block;
  width: 1.2em;
  height: 1.2em;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: middle;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .6rem 1.25rem;
  font-size: 0.9rem;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
  z-index: 999;
  animation: toast-in .2s ease;
}
.toast.success { border-color: #4caf50; color: #4caf50; }
.toast.error   { border-color: var(--danger); color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---- Empty state ---- */
.empty {
  color: var(--muted);
  text-align: center;
  padding: 4rem 0;
  font-size: 1rem;
}

/* ---- Divider ---- */
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: .5rem 0;
}

/* ---- Users table ---- */
.users-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
.users-table th {
  text-align: left;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: .4rem .6rem;
  border-bottom: 1px solid var(--border);
}
.users-table td {
  padding: .6rem .6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.users-table tr:last-child td { border-bottom: none; }
.users-table-name {
  display: flex;
  align-items: center;
  gap: .6rem;
}
.users-actions {
  display: flex;
  gap: .4rem;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.users-you { color: var(--muted); font-size: .8rem; }

.role-badge {
  font-size: .75rem;
  font-weight: 600;
  padding: .15rem .5rem;
  border-radius: 99px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.role-admin  { background: #2a3f6e; color: #7aadff; }
.role-viewer { background: var(--surface2); color: var(--muted); }

[data-theme="light"] .role-admin { background: #dbeafe; color: #1d4ed8; }

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal-backdrop.hidden { display: none; }
.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 100%;
  max-width: 360px;
}
.modal-title {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: .75rem;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: .5rem;
  margin-top: 1rem;
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
  #sidebar { display: none; }
  .theme-options { grid-template-columns: 1fr; }
  header { grid-template-columns: auto 1fr auto; }
  .users-actions { justify-content: flex-start; }
}
