:root {
  --bg: #0b0d13;
  --panel: rgba(20, 24, 33, 0.6);
  --text: #e6f0ff;
  --muted: #9fb0c8;
  --primary: #6ee7ff;
  --primary-2: #7c5cff;
  --danger: #ff6b6b;
  --glass: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.08);
  --shadow: rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial;
  background: radial-gradient(1200px 600px at 10% 10%, rgba(108, 66, 255, 0.12), transparent),
              radial-gradient(900px 500px at 90% 30%, rgba(0, 191, 255, 0.12), transparent),
              var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

/* Global dark form controls */
input, select, textarea, button { color: var(--text); }
input::placeholder, textarea::placeholder { color: rgba(230,240,255,0.55); }
select, option { background-color: rgba(16, 19, 26, 0.98); color: var(--text); }
select { border: 1px solid var(--border); border-radius: 10px; background: rgba(255,255,255,0.05); }
select:focus, input:focus, textarea:focus { outline: 2px solid rgba(124,92,255,0.5); outline-offset: 2px; }

#bg-canvas {
  position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; pointer-events: none;
}

.glass {
  background: var(--glass);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  border: 1px solid var(--border);
  box-shadow: 0 8px 30px var(--shadow);
}

.subtle { border-style: dashed; }

.app-header { position: sticky; top: 0; z-index: 10; padding: 16px; }
.brand-title {
  margin: 0 0 10px 0;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, var(--primary), var(--primary-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  text-shadow: 0 0 20px rgba(124, 92, 255, 0.35), 0 0 10px rgba(110, 231, 255, 0.25);
}

.top-nav { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.subject-tabs { display: flex; gap: 8px; flex: 1; min-width: 240px; }
.subject-tab {
  padding: 8px 12px; border-radius: 999px; border: 1px solid var(--border);
  background: rgba(255,255,255,0.03); color: var(--text); cursor: pointer; transition: 180ms ease;
}
.subject-tab.active, .subject-tab:hover { box-shadow: 0 0 15px rgba(110,231,255,0.25); border-color: rgba(110,231,255,0.45); }

.actions { display: flex; gap: 8px; align-items: center; }
.btn { padding: 8px 12px; border-radius: 10px; border: 1px solid var(--border); cursor: pointer; color: var(--text); background: rgba(255,255,255,0.03); transition: 180ms ease; }
.btn:hover { transform: translateY(-1px); box-shadow: 0 8px 24px rgba(124, 92, 255, 0.22); }
.btn.primary { background: linear-gradient(135deg, rgba(110,231,255,0.15), rgba(124,92,255,0.2)); border-color: rgba(124,92,255,0.35); }
.btn.ghost { background: transparent; }
.btn.danger { background: rgba(255, 107, 107, 0.12); border-color: rgba(255, 107, 107, 0.35); }
.file-label { display: inline-flex; align-items: center; gap: 8px; }

.filters { display: flex; gap: 10px; margin-top: 12px; }
.filters input, .filters select {
  width: 100%; max-width: 520px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: rgba(255,255,255,0.05); color: var(--text);
}
.modal-content input[type="text"], .modal-content input[type="search"], .modal-content select {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border);
  background: rgba(255,255,255,0.05); color: var(--text);
}

/* Remove stacking context from main so overlays can sit above header */
main { position: relative; padding: 16px; margin: 0 auto; max-width: 1200px; }

.drop-zone { margin-bottom: 16px; border-radius: 16px; padding: 18px; text-align: center; color: var(--muted); }
.drop-zone.drag { border-color: rgba(110,231,255,0.6); box-shadow: 0 0 24px rgba(110,231,255,0.35) inset; }

.dashboard { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.subject-card {
  border-radius: 16px; padding: 20px; min-height: 120px; position: relative; overflow: hidden; cursor: pointer; transition: 200ms ease;
}
.subject-card:hover { transform: translateY(-2px) scale(1.01); box-shadow: 0 12px 30px rgba(124,92,255,0.25); }
.subject-card h3 { margin: 0 0 8px 0; }
.subject-card p { margin: 0; color: var(--muted); font-size: 14px; }

.sections-view h2.subject-title { margin: 4px 0 14px; font-weight: 700; }
.section-block { margin-bottom: 12px; }
.section-block h3 { margin: 0 0 10px; color: var(--muted); font-weight: 600; }
.section-block .section-link { cursor: pointer; display: inline-block; padding: 6px 10px; border-radius: 10px; transition: 160ms ease; }
.section-block .section-link:hover { background: rgba(255,255,255,0.05); box-shadow: 0 6px 20px rgba(124,92,255,0.18); }
.count-badge { margin-left: 8px; font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px,1fr)); gap: 12px; }

.file-card { border-radius: 12px; overflow: hidden; position: relative; background: rgba(255,255,255,0.04); border: 1px solid var(--border); cursor: pointer; transition: 160ms ease; }
.file-card:hover { transform: translateY(-2px); box-shadow: 0 10px 24px rgba(110,231,255,0.18); }
.thumb { width: 100%; height: 120px; object-fit: cover; display: block; background: rgba(255,255,255,0.06); }
.file-meta { padding: 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.file-name { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; padding: 2px 6px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }

.modal { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,0.5); z-index: 1100; }
.modal.hidden { display: none; }
.modal-content { width: 95vw; max-width: 560px; border-radius: 16px; padding: 18px; }
.modal-content.preview { width: 96vw; max-width: 960px; height: 85vh; }
.modal-content h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; margin-top: 8px; }
.icon-btn { position: absolute; top: 8px; right: 8px; border: 1px solid var(--border); background: rgba(255,255,255,0.05); color: var(--text); border-radius: 10px; padding: 6px 10px; cursor: pointer; }

#previewContainer { width: 100%; height: calc(100% - 56px); overflow: hidden; display: grid; place-items: center; }
#previewContainer img { max-width: 100%; max-height: 100%; border-radius: 8px; }
#previewContainer iframe { width: 100%; height: 100%; border: 0; background: #0e1118; }

.progress { position: fixed; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(255,255,255,0.08); z-index: 30; }
.progress.hidden { display: none; }
.progress .bar { height: 100%; width: 0%; background: linear-gradient(90deg, var(--primary), var(--primary-2)); box-shadow: 0 0 16px rgba(110,231,255,0.5); transition: width 120ms ease; }

.footer { position: fixed; right: 10px; bottom: 10px; z-index: 5; color: var(--muted); font-size: 12px; opacity: 0.8; }

.hidden { display: none; }

/* Section detail overlay with smooth transition */
.section-detail {
  position: fixed;
  inset: 0; /* cover entire viewport */
  border-radius: 0;
  padding: calc(14px + env(safe-area-inset-top)) 12px calc(14px + env(safe-area-inset-bottom)) 12px;
  z-index: 1000; /* above header; below preview modal */
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 220ms ease, transform 220ms ease;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.section-detail.show {
  opacity: 1;
  transform: translateY(0);
}
.section-detail-header { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; position: sticky; top: 0; z-index: 1; padding: 6px 4px; background: rgba(20, 24, 33, 0.55); backdrop-filter: blur(10px) saturate(140%); border-bottom: 1px solid var(--border); border-radius: 12px; }
.section-detail-filters { margin-left: auto; display: flex; gap: 8px; }
.section-detail-filters select { padding: 6px 10px; border-radius: 10px; border: 1px solid var(--border); background: rgba(255,255,255,0.05); color: var(--text); }

/* Scrim for outside-tap close */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 950; opacity: 0; transition: opacity 220ms ease; }
.scrim.show { opacity: 1; }

/* Preview modal height tuned for mobile safe area */
.modal-content.preview { height: 90dvh; }

@media (max-width: 640px) {
  .actions { width: 100%; justify-content: flex-end; }
  .filters { flex-direction: column; }
  .modal-content.preview { height: 92dvh; }
}
