/* ─── Variables ─────────────────────────────────────────── */
:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --tag-bg: #ede9fe;
  --tag-text: #4f46e5;
  --tag-active-bg: #4f46e5;
  --tag-active-text: #ffffff;
  --sidebar-width: 240px;
  --bottom-nav-height: 56px;
  --mobile-header-height: 52px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
}

/* ─── Reset ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.6; }
a { color: inherit; text-decoration: none; }
button { cursor: pointer; border: none; background: none; font: inherit; }
textarea { font: inherit; }
img { display: block; max-width: 100%; }

/* ─── Utilities ─────────────────────────────────────────── */
.hidden { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; padding: 8px 16px; border-radius: var(--radius);
  font-size: 14px; font-weight: 500; transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:not(:disabled):hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:not(:disabled):hover { background: var(--danger-hover); }
.btn-icon { padding: 6px; border-radius: 6px; color: var(--text-muted); line-height: 1; }
.btn-icon:hover { background: var(--bg); color: var(--text); }

/* ─── Toast ─────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 20px;
  border-radius: 20px; font-size: 14px; z-index: 1000;
  animation: fadeIn .2s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }

/* ─── Dialog ─────────────────────────────────────────────── */
.dialog-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 900; padding: 20px;
}
.dialog {
  background: var(--surface); border-radius: 12px;
  padding: 24px; max-width: 360px; width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}
.dialog p { margin-bottom: 20px; line-height: 1.5; }
.dialog-buttons { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

/* ─── Login Page ─────────────────────────────────────────── */
.login-page {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.login-card {
  background: var(--surface); border-radius: 16px;
  padding: 40px; width: 100%; max-width: 380px;
  box-shadow: 0 4px 24px rgba(0,0,0,.08);
}
.login-title { font-size: 24px; font-weight: 700; margin-bottom: 8px; }
.login-subtitle { color: var(--text-muted); font-size: 14px; margin-bottom: 32px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text-muted); }
.form-group input {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 15px; outline: none;
  transition: border-color .15s;
}
.form-group input:focus { border-color: var(--primary); }
.login-error { color: var(--danger); font-size: 13px; margin-bottom: 12px; min-height: 18px; }
.login-success { color: #16a34a; font-size: 13px; margin-bottom: 12px; min-height: 18px; }
.login-submit { width: 100%; padding: 12px; font-size: 15px; margin-top: 8px; }

/* ─── Layout ─────────────────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width); flex-shrink: 0;
  position: fixed; top: 0; left: 0; height: 100vh;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow-y: auto;
  z-index: 200; padding-bottom: 16px;
}
.sidebar-brand {
  font-size: 18px; font-weight: 700; padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.sidebar-nav { padding: 12px 12px 0; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius);
  font-size: 14px; font-weight: 500; color: var(--text-muted);
  transition: background .15s, color .15s; margin-bottom: 2px;
}
.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item.active { background: var(--tag-bg); color: var(--primary); }
.sidebar-section { padding: 16px 16px 0; }
.sidebar-section-title { font-size: 11px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 10px; }
.sidebar-footer { margin-top: auto; padding: 16px; border-top: 1px solid var(--border); }
.btn-logout { width: 100%; padding: 8px; font-size: 13px; color: var(--text-muted); border-radius: var(--radius); }
.btn-logout:hover { background: var(--bg); color: var(--danger); }

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

.layout-body { margin-left: var(--sidebar-width); flex: 1; display: flex; flex-direction: column; }
.mobile-header { display: none; }
.main-content { flex: 1; padding: 24px; max-width: 680px; margin: 0 auto; width: 100%; }
.bottom-nav { display: none; }

/* ─── Tag Cloud ──────────────────────────────────────────── */
.tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  display: inline-flex; align-items: center;
  padding: 3px 10px; border-radius: 20px;
  font-size: 13px; background: var(--tag-bg); color: var(--tag-text);
  cursor: pointer; transition: background .15s, color .15s;
  user-select: none;
}
.tag:hover { background: #ddd6fe; }
.tag.active { background: var(--tag-active-bg); color: var(--tag-active-text); }
.tag-clear { margin-top: 10px; font-size: 12px; color: var(--primary); cursor: pointer; }
.tag-clear:hover { text-decoration: underline; }

/* ─── Date Filter ────────────────────────────────────────── */
.date-filter { display: flex; flex-direction: column; gap: 8px; }
.date-filter label { font-size: 12px; color: var(--text-muted); }
.date-filter input[type="date"] {
  width: 100%; padding: 6px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 13px; outline: none;
  background: var(--surface);
}
.date-filter input[type="date"]:focus { border-color: var(--primary); }
.date-clear { font-size: 12px; color: var(--primary); cursor: pointer; margin-top: 2px; display: inline-block; }
.date-clear:hover { text-decoration: underline; }

/* ─── Compose Area ───────────────────────────────────────── */
.compose-card {
  background: var(--surface); border-radius: 12px;
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 20px;
}
.compose-textarea {
  width: 100%; min-height: 100px; resize: vertical;
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; font-size: 15px; outline: none;
  transition: border-color .15s; line-height: 1.6;
}
.compose-textarea:focus { border-color: var(--primary); }
.compose-meta {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 10px; gap: 8px; flex-wrap: wrap;
}
.compose-left { display: flex; align-items: center; gap: 8px; }
.compose-right { display: flex; align-items: center; gap: 8px; }
.char-count { font-size: 12px; color: var(--text-muted); }
.char-count.over { color: var(--danger); }
.btn-attach { color: var(--text-muted); font-size: 20px; line-height: 1; padding: 4px 8px; border-radius: 6px; }
.btn-attach:hover { background: var(--bg); color: var(--primary); }

.compose-previews { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.compose-preview-item {
  position: relative; width: 80px; height: 80px;
  border-radius: 6px; overflow: hidden; flex-shrink: 0;
}
.compose-preview-item img { width: 100%; height: 100%; object-fit: cover; }
.compose-preview-remove {
  position: absolute; top: 2px; right: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: rgba(0,0,0,.6); color: #fff;
  font-size: 12px; display: flex; align-items: center; justify-content: center;
}
.compose-preview-remove:hover { background: rgba(0,0,0,.8); }

/* ─── Note Card ──────────────────────────────────────────── */
.note-card {
  background: var(--surface); border-radius: 12px;
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
  animation: slideIn .2s ease;
}
@keyframes slideIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }
.note-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.note-time { font-size: 12px; color: var(--text-muted); }
.note-menu { position: relative; }
.note-menu-btn { font-size: 18px; color: var(--text-muted); padding: 2px 8px; border-radius: 6px; line-height: 1; }
.note-menu-btn:hover { background: var(--bg); color: var(--text); }
.note-menu-dropdown {
  position: absolute; right: 0; top: 100%; margin-top: 4px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,.12);
  min-width: 120px; z-index: 50; overflow: hidden;
}
.note-menu-item {
  display: block; width: 100%; padding: 10px 14px;
  font-size: 14px; text-align: left; color: var(--text);
  transition: background .1s;
}
.note-menu-item:hover { background: var(--bg); }
.note-menu-item.danger { color: var(--danger); }

.note-body { font-size: 15px; line-height: 1.7; word-break: break-word; margin-bottom: 12px; }
.note-body p { margin-bottom: 8px; }
.note-body p:last-child { margin-bottom: 0; }
.note-body h1, .note-body h2, .note-body h3 { margin: 12px 0 6px; }
.note-body ul, .note-body ol { padding-left: 20px; margin-bottom: 8px; }
.note-body code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.note-body pre { background: var(--bg); padding: 12px; border-radius: var(--radius); overflow-x: auto; margin-bottom: 8px; }
.note-body blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--text-muted); margin-bottom: 8px; }

.note-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }

/* ─── Edit Mode ──────────────────────────────────────────── */
.note-edit-area { width: 100%; }
.note-edit-textarea {
  width: 100%; min-height: 120px; resize: vertical;
  border: 1px solid var(--primary); border-radius: var(--radius);
  padding: 12px; font-size: 15px; outline: none; line-height: 1.6;
  margin-bottom: 10px;
}
.note-edit-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.note-edit-img-item {
  position: relative; width: 80px; height: 80px;
  border-radius: 6px; overflow: hidden;
}
.note-edit-img-item img { width: 100%; height: 100%; object-fit: cover; }
.note-edit-img-remove {
  position: absolute; top: 2px; right: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: rgba(0,0,0,.6); color: #fff;
  font-size: 12px; display: flex; align-items: center; justify-content: center;
}
.note-edit-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.note-edit-attach { font-size: 20px; color: var(--text-muted); padding: 4px 8px; border-radius: 6px; margin-right: auto; }
.note-edit-attach:hover { background: var(--bg); color: var(--primary); }

/* ─── Image Grid ─────────────────────────────────────────── */
.img-grid { display: grid; gap: 3px; margin-top: 10px; border-radius: var(--radius); overflow: hidden; }
.img-grid[data-cols="1"] { grid-template-columns: 1fr; max-width: 360px; }
.img-grid[data-cols="2"] { grid-template-columns: repeat(2, 1fr); }
.img-grid[data-cols="3"] { grid-template-columns: repeat(3, 1fr); }

.img-item {
  position: relative; overflow: hidden; cursor: pointer;
  background: var(--bg);
  aspect-ratio: 1;
}
.img-grid[data-cols="1"] .img-item { aspect-ratio: 16/9; max-height: 300px; }
.img-item img { width: 100%; height: 100%; object-fit: cover; transition: transform .2s; }
.img-item:hover img { transform: scale(1.03); }
.img-more {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.5); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 600;
}

/* ─── Note Highlight (goto-note) ────────────────────────── */
.note-highlight {
  animation: noteHighlight 2s ease forwards;
}
@keyframes noteHighlight {
  0%, 20% { box-shadow: 0 0 0 3px var(--primary); }
  100%     { box-shadow: var(--shadow); }
}

/* ─── Load More ──────────────────────────────────────────── */
.load-more-wrap { text-align: center; padding: 16px 0 32px; }
.btn-load-more { font-size: 14px; color: var(--primary); padding: 8px 24px; border-radius: 20px; border: 1px solid var(--border); background: var(--surface); }
.btn-load-more:hover { background: var(--tag-bg); }
.empty-state { text-align: center; color: var(--text-muted); padding: 60px 20px; font-size: 15px; }

/* ─── Lightbox ───────────────────────────────────────────── */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.92);
  z-index: 800; display: flex; align-items: center; justify-content: center;
}
.lightbox-img { max-width: 90vw; max-height: 90vh; object-fit: contain; border-radius: 4px; }
.lightbox-close {
  position: absolute; top: 16px; right: 20px;
  color: #fff; font-size: 28px; opacity: .7; line-height: 1; padding: 4px;
}
.lightbox-close:hover { opacity: 1; }
.lightbox-btn {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(255,255,255,.15); color: #fff;
  border-radius: 50%; width: 44px; height: 44px;
  font-size: 20px; display: flex; align-items: center; justify-content: center;
}
.lightbox-btn:hover { background: rgba(255,255,255,.25); }
.lightbox-prev { left: 16px; }
.lightbox-next { right: 16px; }
.lightbox-counter { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); color: rgba(255,255,255,.7); font-size: 13px; }

/* ─── Images Page ────────────────────────────────────────── */
.images-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.images-title { font-size: 18px; font-weight: 600; }
.images-count { font-size: 13px; color: var(--text-muted); }
.images-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.images-grid-item {
  position: relative; aspect-ratio: 1; overflow: hidden;
  border-radius: 6px; background: var(--bg); cursor: pointer;
}
.images-grid-item img { width: 100%; height: 100%; object-fit: cover; transition: transform .2s; }
.images-grid-item:hover img { transform: scale(1.05); }
.images-grid-item:hover .img-delete-btn { opacity: 1; }
.img-delete-btn {
  position: absolute; top: 4px; right: 4px;
  width: 26px; height: 26px; border-radius: 50%;
  background: rgba(239,68,68,.9); color: #fff;
  font-size: 14px; display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s;
}
.img-delete-btn:hover { background: var(--danger); }
.img-note-btn {
  position: absolute; bottom: 4px; left: 4px;
  padding: 2px 8px; border-radius: 10px;
  background: rgba(0,0,0,.6); color: #fff; font-size: 11px;
  opacity: 0; transition: opacity .15s; white-space: nowrap;
}
.images-grid-item:hover .img-note-btn { opacity: 1; }
.images-load-more { text-align: center; margin-top: 20px; padding-bottom: 32px; }

/* ─── Spinner ────────────────────────────────────────────── */
.spinner { text-align: center; padding: 40px; color: var(--text-muted); font-size: 14px; }

/* ─── Mobile ─────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 300;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay { display: block; opacity: 0; pointer-events: none; transition: opacity .25s; }
  .sidebar-overlay.visible { opacity: 1; pointer-events: auto; }

  .layout-body { margin-left: 0; }

  .mobile-header {
    display: flex; align-items: center; justify-content: space-between;
    height: var(--mobile-header-height); padding: 0 16px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 100;
    flex-shrink: 0;
  }
  .mobile-title { font-size: 16px; font-weight: 600; }

  .main-content { padding: 16px 12px; padding-bottom: calc(var(--bottom-nav-height) + 16px); }

  .bottom-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0;
    height: var(--bottom-nav-height);
    background: var(--surface); border-top: 1px solid var(--border);
    z-index: 100;
  }
  .bottom-nav-item {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    font-size: 12px; color: var(--text-muted); gap: 2px;
  }
  .bottom-nav-item.active { color: var(--primary); }

  .images-grid { grid-template-columns: repeat(3, 1fr); }

  .img-grid[data-cols="1"] { max-width: 100%; }

  /* Mobile grid: 4+ images → 3 cols */
  .img-grid[data-mobile-cols="3"] { grid-template-columns: repeat(3, 1fr) !important; }
}

@media (max-width: 480px) {
  .images-grid { grid-template-columns: repeat(3, 1fr); }
  .compose-card { padding: 12px; }
  .note-card { padding: 12px; }
}
