:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e5e7eb;
  --text: #1f2330;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.12);
  --modal-backdrop: rgba(15,17,26,0.55);
  --auth-gradient: linear-gradient(135deg, #eef0ff 0%, #f5f6fa 60%);
}

[data-theme="dark"] {
  --bg: #14161f;
  --surface: #1e2130;
  --surface-2: #262a3b;
  --border: #333850;
  --text: #e9eaf2;
  --text-muted: #9195a8;
  --primary: #6366f1;
  --primary-dark: #7c7ff5;
  --danger: #f87171;
  --danger-bg: #3f2020;
  --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.5);
  --modal-backdrop: rgba(0,0,0,0.65);
  --auth-gradient: linear-gradient(135deg, #1b1d2b 0%, #14161f 60%);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background .15s, color .15s;
}

/* ---------- Auth pages (setup/login/share) ---------- */
.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--auth-gradient);
  padding: 20px;
  position: relative;
}
.theme-toggle-fixed {
  position: absolute;
  top: 16px; right: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  width: 40px; height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 17px;
  box-shadow: var(--shadow);
}
.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 36px 28px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}
.auth-logo { font-size: 40px; margin-bottom: 8px; }
.auth-card h1 { margin: 0 0 6px; font-size: 22px; }
.auth-sub { color: var(--text-muted); font-size: 14px; margin: 0 0 20px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form input {
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
}
.auth-form input:focus { border-color: var(--primary); }
.auth-error {
  background: var(--danger-bg);
  color: var(--danger);
  border-radius: 8px;
  padding: 10px;
  font-size: 13px;
  margin-bottom: 14px;
}
.share-card .share-preview { margin-top: 14px; }
.share-preview img, .share-preview video { width: 100%; border-radius: 10px; display: block; }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  text-decoration: none;
  transition: background .15s, transform .05s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-ghost:hover { background: var(--border); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { filter: brightness(0.95); }
.btn-icon-toggle {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  width: 38px; height: 38px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.btn-icon-toggle:hover { background: var(--border); }

/* ---------- App shell ---------- */
#app { max-width: 1200px; margin: 0 auto; padding: 0 20px 40px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 0;
  flex-wrap: wrap;
}
.brand {
  font-size: 19px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-wrap { flex: 1; min-width: 160px; }
.search-wrap input {
  width: 100%;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 14px;
  outline: none;
}
.search-wrap input:focus { border-color: var(--primary); }
.top-actions { display: flex; gap: 8px; align-items: center; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 10px;
  flex-wrap: wrap;
}
.breadcrumb { font-size: 14px; color: var(--text-muted); display: flex; gap: 4px; flex-wrap: wrap; }
.breadcrumb a { color: var(--text-muted); text-decoration: none; cursor: pointer; }
.breadcrumb a:hover { color: var(--primary); }
.breadcrumb .sep { margin: 0 2px; }
.breadcrumb .current { color: var(--text); font-weight: 600; }

.view-toggle { display: flex; gap: 4px; }
.view-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 34px; height: 34px;
  cursor: pointer;
  color: var(--text-muted);
}
.view-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---------- Drop zone / grid ---------- */
.drop-zone { position: relative; min-height: 300px; }
.drop-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(79,70,229,0.12);
  border: 3px dashed var(--primary);
  z-index: 40;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: var(--primary-dark);
}
.drop-zone.dragging .drop-overlay { display: flex; }

.file-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
}
.file-grid.list-mode {
  grid-template-columns: 1fr;
  gap: 4px;
}

.item-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 10px;
  cursor: pointer;
  text-align: center;
  position: relative;
  transition: box-shadow .15s, border-color .15s;
  user-select: none;
}
.item-card:hover { box-shadow: var(--shadow); border-color: #d1d5db; }
.item-thumb {
  width: 100%;
  height: 84px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  margin-bottom: 8px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-2);
}
.item-thumb img { width: 100%; height: 100%; object-fit: cover; }
.item-name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.item-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.item-menu-btn {
  position: absolute;
  top: 6px; right: 6px;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  font-weight: 700;
  display: none;
  box-shadow: var(--shadow);
}
.item-card:hover .item-menu-btn { display: block; }

/* list mode row layout */
.file-grid.list-mode .item-card {
  display: flex;
  align-items: center;
  text-align: left;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
}
.file-grid.list-mode .item-thumb { width: 36px; height: 36px; font-size: 20px; margin: 0; flex-shrink: 0; }
.file-grid.list-mode .item-info { flex: 1; min-width: 0; }
.file-grid.list-mode .item-menu-btn { position: static; display: block; background: transparent; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-icon { font-size: 46px; margin-bottom: 10px; }
.empty-sub { font-size: 13px; }

/* ---------- Upload progress ---------- */
.upload-progress {
  position: fixed;
  bottom: 20px; right: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 14px 16px;
  width: 260px;
  z-index: 50;
}
.progress-bar { background: var(--surface-2); border-radius: 6px; height: 8px; margin-top: 8px; overflow: hidden; }
.progress-fill { background: var(--primary); height: 100%; width: 0%; transition: width .2s; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: var(--modal-backdrop); }
.modal-content {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 640px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.modal-small { max-width: 380px; }
.modal-close {
  position: absolute; top: 12px; right: 12px;
  border: none; background: var(--surface-2); color: var(--text); width: 30px; height: 30px;
  border-radius: 50%; cursor: pointer; font-size: 14px;
}
.preview-body { display: flex; align-items: center; justify-content: center; min-height: 120px; margin-top: 10px; }
.preview-body img, .preview-body video { max-width: 100%; max-height: 60vh; border-radius: 8px; }
.preview-body audio { width: 100%; }
.preview-body iframe { width: 100%; height: 60vh; border: 0; border-radius: 8px; background: #fff; }
.preview-file-generic { text-align: center; color: var(--text-muted); }
.preview-file-generic .big-icon { font-size: 60px; }
.preview-note { font-size: 12.5px; margin-top: 6px; max-width: 280px; }
.text-preview-loading { color: var(--text-muted); font-size: 13px; padding: 30px; }
.code-preview {
  width: 100%;
  max-height: 60vh;
  overflow: auto;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  margin: 0;
}
.modal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; justify-content: center; }
.prompt-input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  margin: 12px 0;
  outline: none;
  background: var(--surface);
  color: var(--text);
}
.prompt-input:focus { border-color: var(--primary); }
.share-link-box { display: flex; gap: 6px; margin-bottom: 8px; }
.share-link-box input {
  flex: 1;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 12px;
  background: var(--surface-2);
  color: var(--text);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 20px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 100;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Responsive ---------- */

/* Tablet */
@media (max-width: 900px) {
  .file-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
}

/* Mobile: topbar jadi sticky ringkas, aksi utama pindah ke tombol melayang (FAB),
   dan modal tampil sebagai bottom-sheet supaya nyaman dijangkau jempol */
@media (max-width: 640px) {
  #app { padding: 0 12px 90px; }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    background: var(--bg);
    padding: 12px 0 8px;
    gap: 8px;
  }
  .brand { font-size: 17px; }
  .brand span { display: inline; }

  /* Sembunyikan tombol folder-baru & unggah di topbar, pindah ke FAB */
  .top-actions #newFolderBtn,
  .top-actions #uploadBtn span { display: none; }
  .top-actions #uploadBtn {
    display: none;
  }
  .top-actions {
    gap: 6px;
  }
  .top-actions .btn {
    padding: 9px 10px;
  }

  .search-wrap input { padding: 12px 14px; font-size: 16px; } /* 16px cegah auto-zoom iOS */

  .toolbar { margin-bottom: 10px; }
  .breadcrumb { font-size: 13px; }

  .file-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
  .item-thumb { height: 64px; font-size: 28px; }
  .item-card { padding: 10px 8px; }
  .item-name { font-size: 12.5px; }
  .item-meta { font-size: 10.5px; }
  /* Tombol menu titik-tiga selalu tampak di mobile (tidak ada hover) */
  .item-menu-btn { display: flex; align-items: center; justify-content: center; }

  .file-grid.list-mode .item-card { padding: 12px 10px; }

  /* Tombol aksi melayang: buka folder baru / unggah */
  .fab-wrap {
    position: fixed;
    right: 18px;
    bottom: 22px;
    z-index: 45;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
  }
  .fab-mini {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    width: 44px; height: 44px;
    border-radius: 50%;
    box-shadow: var(--shadow-lg);
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 18px;
  }
  .fab-main {
    background: var(--primary);
    color: #fff;
    width: 56px; height: 56px;
    border-radius: 50%;
    border: none;
    box-shadow: var(--shadow-lg);
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform .15s;
  }
  .fab-wrap.open .fab-mini { display: flex; }
  .fab-wrap.open .fab-main { transform: rotate(45deg); }

  /* Modal jadi bottom-sheet: nempel di bawah layar, mudah dijangkau jempol */
  .modal { align-items: flex-end; padding: 0; }
  .modal-content {
    border-radius: 18px 18px 0 0;
    max-width: 100%;
    width: 100%;
    max-height: 85vh;
    padding-bottom: max(20px, env(safe-area-inset-bottom));
  }
  .modal-small { max-width: 100%; }
  .modal-actions { justify-content: stretch; }
  .modal-actions .btn { flex: 1; justify-content: center; }
  .preview-body img, .preview-body video { max-height: 45vh; }
  .preview-body iframe { height: 45vh; }

  .upload-progress { left: 12px; right: 12px; width: auto; bottom: 90px; }
}

@media (max-width: 400px) {
  .file-grid { grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 8px; }
}
