:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel-hover: #f1f5f9;
  --border: #e2e8f0;
  --border-focus: #2563eb;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eff6ff;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-hover: #334155;
  --border: #334155;
  --border-focus: #3b82f6;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #1e3a8a;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header & Navigation */
.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand {
  font-weight: 700;
  font-size: 18px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text);
}

/* Search Box */
.searchbox {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 8px;
}

.searchbox:focus-within {
  border-color: var(--border-focus);
}

.searchbox input {
  width: 100%;
  background: transparent;
  border: none;
  padding: 8px;
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.searchbox select {
  background: transparent;
  border: none;
  border-left: 1px solid var(--border);
  padding: 6px 8px;
  font-size: 13px;
  color: var(--text-muted);
  outline: none;
  cursor: pointer;
}

.search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 320px;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  z-index: 200;
}

.search-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.search-item:last-child { border-bottom: none; }
.search-item:hover { background: var(--panel-hover); }

.search-item-info { display: flex; flex-direction: column; gap: 2px; }
.search-item-title { font-size: 13px; font-weight: 600; color: var(--text); }
.search-item-sub { font-size: 11px; color: var(--text-muted); }

/* Buttons & Controls */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
  border: none;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  min-height: 38px;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  min-height: 36px;
}

.btn-secondary:hover { background: var(--panel-hover); }

.btn-icon {
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  min-height: 36px;
  white-space: nowrap;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  font-weight: 500;
}

.btn-accent { background: var(--accent); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost-sm { background: transparent; border: none; color: inherit; cursor: pointer; padding: 4px 8px; font-size: 13px; }

/* Sub Header & Breadcrumbs */
.sub-header {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.sub-header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}

.breadcrumb { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.breadcrumb a { color: var(--accent); text-decoration: none; cursor: pointer; font-weight: 500; }
.breadcrumb-sep { color: var(--text-muted); }

.batch-actions { display: flex; align-items: center; gap: 8px; }
.batch-count { font-weight: 600; color: var(--accent); font-size: 12px; }

/* Main Container */
.main-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  width: 100%;
  flex: 1;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 10px;
  flex-wrap: wrap;
}

.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 8px; }

/* Grid / Folder / File Cards */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  cursor: pointer;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 90px;
  transition: border-color 0.15s, background-color 0.15s;
}

.card:hover {
  border-color: var(--border-focus);
  background: var(--panel-hover);
}

.card-checkbox {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 18px;
  height: 18px;
  cursor: pointer;
  z-index: 5;
}

.card .category-badge {
  font-size: 10px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.card .name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  word-break: break-word;
  line-height: 1.3;
}

.card .meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
}

.card-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}

.card-actions button {
  padding: 3px 8px;
  font-size: 11px;
}

/* List Table View */
.list-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.list-table th, .list-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.list-table th { background: var(--bg); color: var(--text-muted); font-weight: 600; }
.list-table tr:last-child td { border-bottom: none; }
.list-table tr:hover td { background: var(--panel-hover); }

/* Gallery / Media Grid */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.gallery-item {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
}

.gallery-preview {
  width: 100%;
  height: 140px;
  object-fit: cover;
  background: #000;
  display: block;
}

.gallery-info {
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
}

.gallery-title {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

/* Upload Drawer */
.upload-drawer {
  position: fixed;
  bottom: 16px;
  right: 16px;
  width: 360px;
  max-width: calc(100vw - 32px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 500;
  overflow: hidden;
}

.drawer-header {
  background: var(--accent);
  color: #fff;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
}

.drawer-body {
  padding: 12px;
  max-height: 240px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.upload-file-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--bg);
  padding: 8px;
  border-radius: 4px;
  font-size: 12px;
}

.upload-file-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.upload-progress-bg {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.upload-progress-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.15s;
}

/* Drag Drop Overlay */
.dropzone {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(2px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.dropzone-box {
  background: var(--panel);
  border: 2px dashed var(--accent);
  border-radius: var(--radius);
  padding: 30px;
  text-align: center;
}

.dropzone-title { font-size: 18px; font-weight: 600; color: var(--accent); }
.dropzone-sub { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.dropzone-warning {
  font-size: 12px;
  color: #b45309;
  background: #fffbeb;
  border: 1px solid #fcd34d;
  border-radius: 6px;
  padding: 8px 12px;
  margin-top: 14px;
  max-width: 420px;
  line-height: 1.5;
}
.dropzone-warning a { color: #b45309; text-decoration: underline; }

/* Modal & Previewers */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  background: var(--panel);
  border-radius: var(--radius);
  max-width: 800px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  z-index: 10;
  font-size: 16px;
}

.modal-content {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.viewer-image {
  max-width: 100%;
  max-height: 65vh;
  object-fit: contain;
  transition: transform 0.2s;
}

.viewer-controls {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
  justify-content: center;
}

.code-preview {
  width: 100%;
  background: #0f172a;
  color: #f8fafc;
  padding: 12px;
  border-radius: 6px;
  font-family: monospace;
  font-size: 13px;
  max-height: 60vh;
  overflow: auto;
  white-space: pre-wrap;
}

/* Khung xem ảnh (đơn giản, ổn định) */
.img-viewer-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.img-viewer-frame {
  width: 100%;
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--bg);
}

.img-viewer-tag {
  display: block;
  max-width: 100%;
  max-height: 65vh;
  object-fit: contain;
  border-radius: 8px;
}

.img-viewer-loading {
  font-size: 13px;
  color: var(--text-muted);
  padding: 24px 0;
}

/* Tên file & khung video/nhạc dùng chung */
.media-viewer-name {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 10px;
}

.video-viewer-tag {
  display: block;
  max-width: 100%;
  max-height: 60vh;
  border-radius: 8px;
}

.audio-viewer-tag {
  width: 100%;
  max-width: 420px;
  margin: 16px 0;
}

.lightbox-count {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

/* Empty State */
.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
  font-size: 14px;
}

.hidden { display: none !important; }

/* Deep Mobile Responsiveness */
@media (max-width: 640px) {
  .topbar-inner {
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 12px;
  }

  .brand {
    font-size: 16px;
  }

  .searchbox {
    order: 3;
    width: 100%;
    flex: 100%;
  }

  .searchbox input {
    font-size: 13px;
    padding: 6px;
  }

  .topbar-actions {
    margin-left: auto;
  }

  .btn-primary, .btn-secondary, .btn-icon {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 13px;
  }

  .sub-header-inner {
    padding: 8px 12px;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .main-container {
    padding: 12px;
  }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 8px;
  }

  .card {
    padding: 10px;
    min-height: 80px;
  }

  .card .name {
    font-size: 12px;
  }

  .card-actions button {
    padding: 4px 6px;
    font-size: 11px;
  }

  .gallery {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .gallery-preview {
    height: 110px;
  }

  .modal-overlay {
    padding: 8px;
  }

  .modal-content {
    padding: 12px;
  }

  .viewer-controls button, .viewer-controls a {
    flex: 1;
    min-width: 100px;
    text-align: center;
  }

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

/* Footer đơn vị phát triển */
.app-footer {
  text-align: center;
  padding: 20px 16px 28px;
  font-size: 12px;
  color: var(--text-muted);
}

.app-footer a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

.app-footer a:hover {
  text-decoration: underline;
}

/* Trang xem trước chính sách trước khi tải lên (bắt buộc cuộn hết) */
.policy-page {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 640px;
  margin: 0 auto;
}

.policy-page-head h2 {
  font-size: 17px;
  margin-bottom: 4px;
}

.policy-page-sub {
  font-size: 13px;
  color: var(--text-muted);
}

.policy-filelist {
  list-style: none;
  margin: 0;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 120px;
  overflow-y: auto;
  font-size: 13px;
}

.policy-filelist li {
  padding: 4px 0;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.policy-filelist li span {
  color: var(--text-muted);
  white-space: nowrap;
}

.policy-scroll {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg);
}

.policy-scroll-text {
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  color: var(--text);
}

.policy-scroll-end {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  padding-top: 16px;
  font-weight: 600;
}

.policy-page-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.policy-page-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

@media (max-width: 640px) {
  .policy-page-actions {
    flex-direction: column-reverse;
  }
  .policy-page-actions button {
    width: 100%;
  }
}