*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent: #007AFF;
  --accent-dark: #0051D5;
  --danger: #FF3B30;
  --bg: #F2F2F7;
  --surface: #FFFFFF;
  --surface2: #F2F2F7;
  --border: rgba(0,0,0,0.1);
  --text: #000000;
  --text2: #3C3C43;
  --text3: #6C6C72;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.16);
  --radius: 12px;
  --sidebar-w: 260px;
  --topbar-h: 56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --surface2: #2C2C2E;
    --border: rgba(255,255,255,0.1);
    --text: #FFFFFF;
    --text2: #EBEBF5;
    --text3: #8E8E93;
    --shadow: 0 2px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.6);
  }
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }
input, textarea, select { font: inherit; color: inherit; }
img { display: block; max-width: 100%; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ─── Layout ─── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  margin-left: 0;
  transition: margin-left 0.3s ease;
}

@media (min-width: 768px) {
  .app-shell { margin-left: var(--sidebar-w); }
}

/* ─── Sidebar ─── */
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 200;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  padding-bottom: env(safe-area-inset-bottom);
}

.sidebar.open { transform: translateX(0); }

@media (min-width: 768px) {
  .sidebar { transform: translateX(0); }
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 190;
}

.sidebar-overlay.visible { display: block; }

@media (min-width: 768px) {
  .sidebar-overlay { display: none !important; }
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
  padding-top: calc(16px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 { font-size: 18px; font-weight: 700; }

.folder-nav { flex: 1; overflow-y: auto; padding: 8px 0; }

.folder-all {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 10px;
  margin: 2px 8px;
  cursor: pointer;
  font-weight: 500;
  color: var(--text2);
  transition: background 0.15s;
}

.folder-all:hover { background: var(--surface2); }
.folder-all.active { background: var(--accent); color: white; }

.folder-group { margin-bottom: 4px; }

.folder-item {
  display: flex;
  align-items: center;
  padding: 8px 8px 8px 16px;
  border-radius: 10px;
  margin: 2px 8px;
  cursor: pointer;
  color: var(--text2);
  transition: background 0.15s;
}

.folder-item:hover { background: var(--surface2); }
.folder-item.active { background: var(--accent); color: white; }

.folder-item .folder-name { flex: 1; font-size: 14px; font-weight: 500; }
.folder-item .folder-badge { font-size: 12px; color: var(--text3); margin-right: 4px; }
.folder-item.active .folder-badge { color: rgba(255,255,255,0.7); }

.folder-item .folder-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s;
}

.folder-item:hover .folder-actions { opacity: 1; }
.folder-item.active .folder-actions { opacity: 1; }

.folder-action-btn {
  width: 28px; height: 28px;
  border-radius: 6px;
  font-size: 13px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s;
}

.folder-action-btn:hover { background: rgba(0,0,0,0.1); }
.folder-item.active .folder-action-btn:hover { background: rgba(255,255,255,0.2); }

.subfolder-list { padding-left: 16px; }

.subfolder-item {
  display: flex;
  align-items: center;
  padding: 7px 8px 7px 12px;
  border-radius: 8px;
  margin: 1px 8px;
  cursor: pointer;
  color: var(--text3);
  transition: background 0.15s;
}

.subfolder-item:hover { background: var(--surface2); }
.subfolder-item.active { background: var(--accent); color: white; }
.subfolder-item .folder-name { flex: 1; font-size: 13px; }
.subfolder-item .folder-actions { display: flex; gap: 2px; opacity: 0; transition: opacity 0.15s; }
.subfolder-item:hover .folder-actions,
.subfolder-item.active .folder-actions { opacity: 1; }
.subfolder-item.active .folder-badge { color: rgba(255,255,255,0.7); }

/* ─── Topbar ─── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 0 4px;
  height: var(--topbar-h);
  padding-top: env(safe-area-inset-top);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-title { flex: 1; font-size: 18px; font-weight: 700; padding-left: 4px; }

@media (min-width: 768px) {
  .menu-btn { display: none; }
}

/* ─── Icon buttons ─── */
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  transition: background 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover { background: var(--surface2); }
.icon-btn:active { background: var(--border); }

/* ─── Search bar ─── */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.search-bar input {
  flex: 1;
  background: var(--surface2);
  border: none;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 15px;
  outline: none;
}

.search-bar input::placeholder { color: var(--text3); }

.hidden { display: none !important; }

/* ─── Tag filter strip ─── */
.tag-strip {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}

.tag-strip::-webkit-scrollbar { display: none; }

.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text2);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

.tag-chip:hover { background: var(--surface2); }
.tag-chip.active { background: var(--accent); color: white; border-color: var(--accent); }

/* ─── Notes area ─── */
.notes-area {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  padding-bottom: calc(80px + env(safe-area-inset-bottom));
}

.section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 4px 4px 8px;
}

.notes-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

@media (min-width: 540px) { .notes-grid { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 900px) { .notes-grid { grid-template-columns: repeat(4, 1fr); } }
@media (min-width: 1200px) { .notes-grid { grid-template-columns: repeat(5, 1fr); } }

.note-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  display: flex;
  flex-direction: column;
}

.note-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.note-card:active { transform: scale(0.98); }

.note-thumb {
  aspect-ratio: 9/16;
  overflow: hidden;
  background: var(--surface2);
  position: relative;
}

.note-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.note-body { padding: 8px; display: flex; flex-direction: column; gap: 4px; }

.note-annotation {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.note-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.tag {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 500;
  background: var(--surface2);
  color: var(--accent);
  border: 1px solid rgba(0, 122, 255, 0.2);
}

.note-folder {
  font-size: 10px;
  color: var(--text3);
  display: flex;
  align-items: center;
  gap: 3px;
}

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 60px 20px;
  color: var(--text3);
  text-align: center;
}

.empty-state .empty-icon { font-size: 48px; }
.empty-state p { font-size: 15px; }

/* ─── FAB ─── */
.fab {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 28px;
  background: var(--accent);
  color: white;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0,122,255,0.4);
  z-index: 100;
  transition: transform 0.2s, box-shadow 0.2s;
  line-height: 1;
}

.fab:hover { transform: scale(1.05); box-shadow: 0 6px 20px rgba(0,122,255,0.5); }
.fab:active { transform: scale(0.96); }

/* ─── Sheet (bottom drawer) ─── */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: flex-end;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.4);
}

.sheet-panel {
  position: relative;
  width: 100%;
  max-height: 92dvh;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  padding-bottom: env(safe-area-inset-bottom);
}

.sheet.open .sheet-panel { transform: translateY(0); }

@media (min-width: 600px) {
  .sheet { align-items: center; justify-content: center; }
  .sheet-panel {
    width: 480px;
    max-height: 90dvh;
    border-radius: 20px;
    transform: scale(0.95);
    opacity: 0;
    transition: transform 0.25s, opacity 0.25s;
  }
  .sheet.open .sheet-panel { transform: scale(1); opacity: 1; }
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 10px auto 0;
  flex-shrink: 0;
}

@media (min-width: 600px) { .sheet-handle { display: none; } }

.sheet-inner {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sheet-header h2 { font-size: 18px; font-weight: 700; }

/* ─── Upload area ─── */
.upload-area {
  position: relative;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s;
  background: var(--surface2);
  min-height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.upload-area:hover { border-color: var(--accent); }

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 32px;
  cursor: pointer;
  color: var(--text3);
  text-align: center;
}

.upload-placeholder span:first-child { font-size: 40px; }
.upload-placeholder span:last-child { font-size: 14px; font-weight: 500; }

.upload-preview {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  background: var(--surface2);
}

/* ─── Forms ─── */
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 13px; font-weight: 600; color: var(--text3); }

.form-group input,
.form-group textarea,
.form-group select {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus { border-color: var(--accent); }

.form-group textarea { resize: none; line-height: 1.5; }

.btn-primary {
  background: var(--accent);
  color: white;
  padding: 13px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  width: 100%;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary:hover { background: var(--accent-dark); }
.btn-primary:disabled { opacity: 0.4; cursor: default; }

.btn-danger {
  background: var(--danger);
  color: white;
  padding: 13px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  width: 100%;
  transition: background 0.15s;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  padding: 13px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  width: 100%;
  border: 1px solid var(--border);
  transition: background 0.15s;
}

.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }

/* ─── Modal (note detail) ─── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
}

.modal-panel {
  position: relative;
  background: var(--surface);
  border-radius: 20px;
  width: 100%;
  max-width: 500px;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 8px;
  flex-shrink: 0;
}

.modal-header h3 { font-size: 17px; font-weight: 700; padding-left: 4px; }

.modal-header-actions { display: flex; gap: 4px; }

.modal-image-wrap {
  overflow: hidden;
  background: #000;
  max-height: 55dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-image { width: 100%; object-fit: contain; max-height: 55dvh; }

.modal-body {
  padding: 14px 16px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-annotation {
  font-size: 15px;
  line-height: 1.5;
  color: var(--text2);
  white-space: pre-wrap;
}

.modal-meta { font-size: 12px; color: var(--text3); }

.tag-list { display: flex; flex-wrap: wrap; gap: 5px; }

/* ─── Confirm dialog ─── */
.confirm-dialog {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.confirm-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.5); }

.confirm-panel {
  position: relative;
  background: var(--surface);
  border-radius: 16px;
  padding: 24px 20px 20px;
  width: 100%;
  max-width: 320px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.15s ease;
}

.confirm-panel h3 { font-size: 17px; font-weight: 700; margin-bottom: 8px; }
.confirm-panel p { font-size: 14px; color: var(--text3); margin-bottom: 20px; }

/* ─── Loading ─── */
.loading-bar {
  height: 2px;
  background: var(--accent);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  transition: width 0.3s ease;
}

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