/* =====================================================================
   Notekori – styles.css
   ===================================================================== */

/* ── CSS Variables ── */
:root {
  --bg:         #f5f6fa;
  --surface:    #ffffff;
  --surface-2:  #f0f1f6;
  --border:     #e2e4ec;
  --primary:    #4f6ef7;
  --primary-h:  #3a56d4;
  --danger:     #e05260;
  --danger-h:   #c43b49;
  --text:       #1a1d2e;
  --text-2:     #6b7280;
  --text-inv:   #ffffff;
  --shadow:     0 2px 12px rgba(0,0,0,.08);
  --shadow-lg:  0 8px 32px rgba(0,0,0,.14);
  --radius:     10px;
  --radius-sm:  6px;
  --sidebar-w:  260px;
  --nav-h:      56px;
  --transition: .18s ease;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}
img { max-width: 100%; display: block; }
button { cursor: pointer; font: inherit; border: none; background: none; }
input, textarea { font: inherit; }
a { color: var(--primary); }
ul, ol { list-style: none; }

/* ── Utilities ── */
.hidden { display: none !important; }
.sr-only { position: absolute; width:1px; height:1px; overflow:hidden; clip:rect(0,0,0,0); white-space:nowrap; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }

/* =====================================================================
   BUTTONS
   ===================================================================== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: .45rem 1rem;
  border-radius: var(--radius-sm);
  font-size: .875rem; font-weight: 500;
  transition: background var(--transition), color var(--transition), box-shadow var(--transition);
  white-space: nowrap;
}
.btn-primary  { background: var(--primary); color: var(--text-inv); }
.btn-primary:hover  { background: var(--primary-h); }
.btn-danger   { background: var(--danger);  color: var(--text-inv); }
.btn-danger:hover   { background: var(--danger-h); }
.btn-ghost    { background: transparent; color: var(--text-2); }
.btn-ghost:hover    { background: var(--surface-2); color: var(--text); }
.btn-outline  { background: transparent; color: var(--primary); border: 1px solid var(--primary); }
.btn-outline:hover  { background: var(--primary); color: var(--text-inv); }
.btn-sm       { padding: .3rem .7rem; font-size: .8rem; }
.btn-full     { width: 100%; justify-content: center; }
.icon-btn {
  padding: .4rem; border-radius: var(--radius-sm);
  color: var(--text-2); transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* =====================================================================
   LOGIN OVERLAY
   ===================================================================== */
.overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: linear-gradient(135deg, #4f6ef7 0%, #7c5cbf 100%);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.login-card {
  background: var(--surface);
  border-radius: calc(var(--radius) * 1.5);
  box-shadow: var(--shadow-lg);
  padding: 2.5rem 2rem;
  width: 100%; max-width: 400px;
}
.login-logo {
  text-align: center;
  margin-bottom: 2rem;
}
.login-logo .logo-icon { font-size: 2.5rem; }
.login-logo h1 { font-size: 1.75rem; font-weight: 700; margin-top: .25rem; }
.login-logo .login-sub { color: var(--text-2); margin-top: .25rem; }
.auth-form h2 { font-size: 1.15rem; margin-bottom: 1.25rem; }
.auth-form .form-note { font-size: .85rem; color: var(--text-2); margin-bottom: 1rem; }
.field { margin-bottom: 1rem; }
.field label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: .35rem; }
.field input {
  width: 100%;
  padding: .55rem .75rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color var(--transition);
}
.field input:focus { border-color: var(--primary); }
.form-error {
  font-size: .83rem; color: var(--danger);
  margin-bottom: .75rem;
}

/* =====================================================================
   APP LAYOUT
   ===================================================================== */
.app { display: flex; flex-direction: column; height: 100vh; }

/* ── Navbar ── */
.navbar {
  height: var(--nav-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1rem;
  position: sticky; top: 0; z-index: 100;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
}
.navbar-left  { display: flex; align-items: center; gap: .5rem; }
.navbar-brand { font-size: 1.1rem; font-weight: 700; color: var(--primary); }
.navbar-right { display: flex; align-items: center; gap: .75rem; }
.nav-username { font-size: .85rem; color: var(--text-2); }

/* ── Main layout ── */
.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* =====================================================================
   SIDEBAR
   ===================================================================== */
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow-y: auto;
  transition: width var(--transition), transform var(--transition);
  flex-shrink: 0;
}
.sidebar.collapsed { width: 0; overflow: hidden; }

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .875rem 1rem .625rem;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 5;
}
.sidebar-title { font-weight: 600; font-size: .95rem; }

/* Manage toolbar */
.manage-toolbar {
  padding: .5rem .75rem;
  border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: .25rem;
  background: var(--surface-2);
}
.toolbar-btn {
  display: flex; align-items: center; gap: .45rem;
  padding: .45rem .6rem;
  border-radius: var(--radius-sm);
  font-size: .82rem; color: var(--text);
  transition: background var(--transition);
  text-align: left; width: 100%;
}
.toolbar-btn:hover { background: var(--border); }

/* ── Course Tree ── */
.course-tree { padding: .5rem 0; flex: 1; }

/* Semester item */
.semester-header {
  display: flex; align-items: center;
  padding: .45rem .75rem .45rem .6rem;
  cursor: pointer; gap: .3rem;
  user-select: none;
  transition: background var(--transition);
}
.semester-header:hover { background: var(--surface-2); }
.semester-chevron {
  display: inline-block; transition: transform var(--transition);
  color: var(--text-2); font-size: .75rem; flex-shrink: 0;
  line-height: 1;
}
.semester-item.open > .semester-header .semester-chevron { transform: rotate(90deg); }
.semester-name { flex: 1; font-size: .9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-item-actions {
  display: flex; gap: .15rem; opacity: 0; transition: opacity var(--transition);
  flex-shrink: 0;
}
.semester-header:hover .tree-item-actions,
.course-row:hover .tree-item-actions { opacity: 1; }
.tree-action-btn {
  padding: .2rem .3rem; border-radius: 4px; font-size: .75rem;
  color: var(--text-2); transition: background var(--transition), color var(--transition);
  line-height: 1;
}
.tree-action-btn:hover { background: var(--border); color: var(--text); }
.tree-action-btn.del:hover { color: var(--danger); }

.semester-courses { padding-left: 1.1rem; }
.semester-item:not(.open) > .semester-courses { display: none; }

/* Loose (no-semester) courses */
.loose-courses { padding: .25rem 0; }

/* Course row */
.course-row {
  display: flex; align-items: center;
  padding: .4rem .75rem .4rem .6rem;
  border-radius: var(--radius-sm);
  cursor: pointer; gap: .35rem;
  transition: background var(--transition);
  margin: 1px .25rem;
}
.course-row:hover  { background: var(--surface-2); }
.course-row.active { background: #eef1fe; color: var(--primary); }
.course-row.active .course-name { color: var(--primary); font-weight: 600; }
.course-icon { font-size: .85rem; flex-shrink: 0; }
.course-name { flex: 1; font-size: .875rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* =====================================================================
   NOTE PANEL
   ===================================================================== */
.note-panel {
  flex: 1; overflow-y: auto;
  padding: 1.5rem;
  min-width: 0;
}

/* Empty state */
.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100%; text-align: center; color: var(--text-2); gap: .75rem;
  min-height: 60vh;
}
.empty-state .empty-icon { font-size: 3.5rem; }
.empty-state h2 { font-size: 1.3rem; color: var(--text); }

/* Course view */
.course-header { margin-bottom: 1.25rem; }
.course-title-row {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .75rem;
  margin-bottom: .35rem;
}
.course-title-row h2 { font-size: 1.4rem; }
.course-actions { display: flex; gap: .5rem; }
.note-count { font-size: .83rem; color: var(--text-2); }

/* ── Note Gallery ── */
.note-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}
.note-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
  transition: transform var(--transition), box-shadow var(--transition);
  cursor: pointer;
}
.note-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.note-thumb {
  width: 100%; aspect-ratio: 3/4;
  object-fit: cover;
  display: block;
  background: var(--surface-2);
}
.note-card-footer {
  padding: .5rem .65rem;
  display: flex; align-items: center; justify-content: space-between;
  gap: .35rem;
}
.note-card-name {
  font-size: .78rem; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.note-card-actions { display: flex; gap: .2rem; }
.note-card-btn {
  padding: .2rem .28rem; border-radius: 4px; font-size: .75rem; line-height: 1;
  color: var(--text-2); transition: background var(--transition), color var(--transition);
}
.note-card-btn:hover { background: var(--surface-2); color: var(--text); }
.note-card-btn.del:hover { color: var(--danger); }
/* Drag-and-drop reorder feedback */
.note-card.drag-over { outline: 2px dashed var(--primary); }
.note-card.dragging  { opacity: .45; }

/* Gallery empty state */
.gallery-empty {
  text-align: center; color: var(--text-2);
  padding: 3rem 1rem;
  display: flex; flex-direction: column; align-items: center; gap: .75rem;
  font-size: .95rem;
}
.gallery-empty span { font-size: 2.5rem; }

/* =====================================================================
   MODALS
   ===================================================================== */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 900;
  background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 1.75rem 1.5rem;
  width: 100%; max-width: 380px;
}
.modal h3 { font-size: 1.1rem; margin-bottom: 1rem; }
.modal p  { font-size: .9rem; color: var(--text-2); margin-bottom: 1.25rem; line-height: 1.55; }
.modal-actions { display: flex; justify-content: flex-end; gap: .65rem; margin-top: 1.25rem; }

/* =====================================================================
   LIGHTBOX
   ===================================================================== */
.lightbox {
  position: fixed; inset: 0; z-index: 1100;
  display: flex; align-items: center; justify-content: center;
}
.lightbox-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.88);
}
.lightbox-content {
  position: relative; z-index: 1;
  display: flex; flex-direction: column; align-items: center;
  max-width: 90vw; max-height: 92vh;
}
.lightbox-content img {
  max-width: 88vw; max-height: 80vh;
  border-radius: var(--radius-sm);
  object-fit: contain;
  box-shadow: var(--shadow-lg);
}
.lightbox-close {
  position: absolute; top: -2.25rem; right: 0;
  color: #fff; font-size: 1.75rem; line-height: 1;
  padding: .2rem .5rem;
  opacity: .8; transition: opacity var(--transition);
}
.lightbox-close:hover { opacity: 1; }
.lightbox-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  color: #fff; font-size: 2.5rem; line-height: 1;
  padding: .25rem .5rem;
  opacity: .7; transition: opacity var(--transition);
  background: rgba(0,0,0,.25); border-radius: 4px;
  z-index: 2;
}
.lightbox-nav:hover { opacity: 1; }
.lightbox-prev { left: -3.5rem; }
.lightbox-next { right: -3.5rem; }
.lightbox-caption {
  margin-top: .75rem;
  color: rgba(255,255,255,.75);
  font-size: .83rem;
  display: flex; gap: 1rem;
}

/* =====================================================================
   RESPONSIVE
   ===================================================================== */
@media (max-width: 640px) {
  :root { --sidebar-w: 100vw; }

  .sidebar {
    position: fixed;
    top: var(--nav-h); left: 0; bottom: 0;
    z-index: 200;
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.collapsed {
    transform: translateX(-100%);
    width: var(--sidebar-w);
  }

  .note-panel { padding: 1rem; }

  .note-gallery { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .65rem; }

  .lightbox-prev { left: .25rem; }
  .lightbox-next { right: .25rem; }
  .lightbox-close { top: .4rem; right: .4rem; background: rgba(0,0,0,.5); border-radius: 4px; }
}

@media (max-width: 400px) {
  .login-card { padding: 2rem 1.25rem; }
}

/* =====================================================================
   SHARED VIEW
   ===================================================================== */
.shared-view {
  display: flex; flex-direction: column; min-height: 100vh;
  background: var(--bg);
}
.shared-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .875rem 1.25rem;
  display: flex; align-items: center; gap: 1rem;
  flex-wrap: wrap;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
}
.shared-logo {
  font-size: 1.1rem; font-weight: 700; color: var(--primary);
  white-space: nowrap;
}
.shared-title-block {
  flex: 1; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
}
.shared-course-title {
  font-size: 1.1rem; font-weight: 600;
}
.shared-term-badge {
  background: var(--primary); color: #fff;
  padding: .15rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 500;
}
.shared-by {
  font-size: .83rem; color: var(--text-2);
}
.shared-header-actions { display: flex; gap: .5rem; flex-shrink: 0; }
.shared-content {
  flex: 1; padding: 1.5rem;
}
.shared-footer {
  text-align: center; padding: 1rem;
  font-size: .83rem; color: var(--text-2);
  border-top: 1px solid var(--border);
}

/* =====================================================================
   AUTH LINKS & LINK BUTTON
   ===================================================================== */
.auth-links {
  display: flex; flex-direction: column; align-items: center;
  gap: .4rem; margin-top: 1rem;
}
.link-btn {
  background: none; border: none; cursor: pointer;
  color: var(--primary); font-size: .83rem; text-decoration: underline;
  padding: .1rem 0; font: inherit;
}
.link-btn:hover { color: var(--primary-h); }

/* =====================================================================
   FORM MESSAGES
   ===================================================================== */
.form-success {
  font-size: .83rem; color: #16a34a;
  margin-bottom: .75rem;
}
.form-msg {
  font-size: .83rem;
  margin-bottom: .75rem; margin-top: .35rem;
}
.form-msg.form-error   { color: var(--danger); }
.form-msg.form-success { color: #16a34a; }

/* =====================================================================
   SETTINGS MODAL
   ===================================================================== */
.modal-wide { max-width: 480px; }
.settings-section {
  border-top: 1px solid var(--border);
  padding-top: 1rem; margin-top: 1rem;
}
.settings-section:first-of-type { border-top: none; margin-top: 0; padding-top: 0; }
.settings-section h4 {
  font-size: .9rem; font-weight: 600; margin-bottom: .75rem; color: var(--text-2);
}
.readonly-input {
  background: var(--surface-2); cursor: default;
  color: var(--text-2);
}
.readonly-input:focus { border-color: var(--border); }

/* =====================================================================
   LOADING STATE & SPINNER
   ===================================================================== */
.loading-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 60vh; gap: 1rem; color: var(--text-2); font-size: .95rem;
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =====================================================================
   PDF EXPORT
   ===================================================================== */
.pdf-options {
  display: flex; gap: 1.25rem; flex-wrap: wrap; margin-bottom: 1rem;
}
.radio-option {
  display: flex; align-items: center; gap: .45rem;
  font-size: .9rem; cursor: pointer; user-select: none;
}
.radio-option input { cursor: pointer; accent-color: var(--primary); }
.pdf-picker-hint {
  font-size: .83rem; color: var(--text-2); margin-bottom: .75rem;
}
.pdf-page-picker { margin-top: .5rem; }
.pdf-page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: .65rem;
  max-height: 280px; overflow-y: auto;
  padding-right: .25rem;
}
.pdf-page-thumb {
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  opacity: .45;
  transition: opacity var(--transition), border-color var(--transition);
  display: flex; flex-direction: column;
}
.pdf-page-thumb.selected {
  opacity: 1;
  border-color: var(--primary);
}
.pdf-page-thumb img {
  width: 100%; aspect-ratio: 3/4; object-fit: cover; display: block;
}
.pdf-page-thumb span {
  font-size: .7rem; text-align: center; padding: .2rem .3rem;
  background: var(--surface-2); color: var(--text-2);
}

/* =====================================================================
   SHARE MODAL
   ===================================================================== */
.share-link-row {
  display: flex; gap: .5rem; align-items: center; margin-top: .5rem;
}
.share-link-text {
  flex: 1;
  font-size: .8rem;
  padding: .45rem .65rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  cursor: text;
  font: inherit;
}

/* =====================================================================
   LIGHTBOX ACTIONS
   ===================================================================== */
.lightbox-actions {
  margin-top: .65rem; display: flex; justify-content: center;
}
.lightbox-replace-label {
  cursor: pointer;
  color: rgba(255,255,255,.8);
  border: 1px solid rgba(255,255,255,.3);
  border-radius: var(--radius-sm);
  padding: .35rem .75rem;
  font-size: .8rem;
  display: inline-flex; align-items: center; gap: .35rem;
  transition: background var(--transition), color var(--transition);
}
.lightbox-replace-label:hover {
  background: rgba(255,255,255,.12);
  color: #fff;
}

/* =====================================================================
   SIDEBAR TERMS (Midterm / Final)
   ===================================================================== */
.course-wrapper {}
.term-list {
  display: none;
  padding-left: 1.4rem;
  padding-bottom: .15rem;
}
.course-wrapper.course-open .term-list { display: block; }
.term-row {
  display: flex; align-items: center; gap: .3rem;
  padding: .35rem .75rem .35rem .5rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin: 1px .25rem;
  transition: background var(--transition);
}
.term-row:hover  { background: var(--surface-2); }
.term-row.active {
  background: #eef1fe; color: var(--primary);
}
.term-row.active .term-name { color: var(--primary); font-weight: 600; }
.term-icon { font-size: .8rem; flex-shrink: 0; }
.term-name { font-size: .84rem; }

/* Loose courses separator */
.loose-sep {
  font-size: .75rem; font-weight: 600; color: var(--text-2);
  text-transform: uppercase; letter-spacing: .05em;
  padding: .65rem .75rem .3rem;
}

/* Course row active state now from course-wrapper (no direct active class needed) */
.course-row { position: relative; }

/* =====================================================================
   RESPONSIVE ADDITIONS
   ===================================================================== */
@media (max-width: 640px) {
  .shared-header { padding: .65rem .875rem; }
  .pdf-page-grid  { grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); }
  .modal-wide { max-width: 100%; }
}

#confirmModal.modal-backdrop { z-index: 9999 !important; }

/* Sidebar Search Bar */
.sidebar-search {
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.sidebar-search input {
  width: 100%;
  padding: 0.45rem 0.65rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  background: var(--surface-2);
  outline: none;
  transition: border-color var(--transition);
}
.sidebar-search input:focus {
  border-color: var(--primary);
  background: var(--surface);
}

/* Reorder Mode Arrows */
.tree-action-btn.reorder-btn {
  font-weight: 800;
  font-size: 0.95rem;
  padding: 0.1rem 0.35rem;
}
.tree-action-btn.reorder-btn:hover {
  background: var(--primary);
  color: #fff;
}


/* =====================================================================
   BULK SELECTION STYLES
   ===================================================================== */
.selection-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  background: #eef1fe; border: 1px solid var(--primary);
  padding: 0.75rem 1rem; border-radius: var(--radius-sm);
  margin-bottom: 1.25rem;
}
.selection-actions { display: flex; gap: 0.5rem; }

.note-card.selectable { cursor: pointer; transition: transform 0.1s; }
.note-card.selectable:hover { outline: 2px solid var(--border); }
.note-card.selected { outline: 3px solid var(--primary) !important; transform: scale(0.96); }

.note-checkmark {
  position: absolute; top: 10px; left: 10px;
  background: var(--primary); color: white;
  width: 24px; height: 24px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: bold; font-size: 14px;
  opacity: 0; transition: opacity 0.2s;
  pointer-events: none; z-index: 10;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}
.note-card.selected .note-checkmark { opacity: 1; }

/* Hide standard buttons when in select mode */
.gallery-select-mode .note-card-actions { display: none; }
