:root {
  --bg: oklch(0.985 0.006 50);
  --surface: oklch(1 0 0);
  --surface-2: oklch(0.96 0.01 50);
  --ink: oklch(0.18 0.01 65);
  --ink-soft: oklch(0.45 0.009 68);
  --muted: oklch(0.48 0.011 68);
  --line: oklch(0.91 0.006 60);
  --accent: oklch(0.63 0.17 46);
  --accent-soft: oklch(0.94 0.04 50);
  --color-danger: oklch(0.45 0.18 22);
  --shadow: 0 1px 2px oklch(0.18 0.01 65 / 0.04), 0 6px 24px oklch(0.18 0.01 65 / 0.06);
  --radius: 16px;
  --sidebar-w: 250px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: oklch(0.13 0.008 50);
    --surface: oklch(0.16 0.007 55);
    --surface-2: oklch(0.19 0.009 52);
    --ink: oklch(0.93 0.006 60);
    --ink-soft: oklch(0.73 0.008 70);
    --muted: oklch(0.53 0.01 70);
    --line: oklch(0.23 0.008 58);
    --accent: oklch(0.67 0.16 45);
    --accent-soft: oklch(0.22 0.04 45);
    --color-danger: oklch(0.60 0.18 22);
    --shadow: 0 1px 2px oklch(0 0 0 / 0.25), 0 6px 24px oklch(0 0 0 / 0.38);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

button { font: inherit; cursor: pointer; border: none; border-radius: 10px; }
input, select, textarea { font: inherit; color: var(--ink); }

/* ---------- Login ---------- */
.login {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: var(--bg); padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 36px 32px;
  width: 100%; max-width: 360px;
  text-align: center;
}
.login-logo { font-size: 44px; }
.login-card h1 { margin: 8px 0 4px; font-size: 26px; }
.login-card p { margin: 0 0 20px; }
.login-card input {
  width: 100%; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface-2); margin-bottom: 12px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button { width: 100%; padding: 12px; background: var(--accent); color: #fff; font-weight: 600; }
.login-error { color: var(--color-danger); margin-top: 12px !important; }

/* ---------- App layout ---------- */
.app { display: flex; min-height: 100%; }

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface-2);
  border-right: 1px solid var(--line);
  padding: 20px 14px max(20px, calc(14px + env(safe-area-inset-bottom)));
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 8px 18px; font-weight: 700; font-size: 18px;
}
.brand-logo { font-size: 22px; }

.cats { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.cat {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 10px;
  cursor: pointer; color: var(--ink-soft); font-size: 14.5px;
  user-select: none;
  transition: background 0.14s ease, color 0.14s ease;
}
.cat:hover { background: color-mix(in oklch, var(--surface-2) 70%, var(--surface)); }
.cat.active {
  background: color-mix(in oklch, var(--cat-color, var(--accent)) 14%, var(--surface));
  color: var(--ink); font-weight: 600;
}
.cat-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.cat-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-count { font-size: 12px; color: var(--muted); }
.cat-edit {
  opacity: 0; background: none; color: var(--muted);
  font-size: 13px; padding: 8px;
  min-width: 32px; min-height: 32px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.cat:hover .cat-edit { opacity: 1; }

.add-cat {
  margin-top: 12px; padding: 9px 10px;
  background: none; color: var(--ink-soft);
  text-align: left; border: 1px dashed var(--line);
}
.add-cat:hover { color: var(--accent); border-color: var(--accent); }

.sidebar-footer { margin-top: auto; padding-top: 14px; }
.ghost-btn { background: none; color: var(--muted); padding: 8px 10px; width: 100%; text-align: left; }
.ghost-btn:hover { color: var(--ink); }

.sidebar-scrim { display: none; }

/* ---------- Main content ---------- */
.main {
  flex: 1; min-width: 0;
  padding: 18px clamp(16px, 4vw, 40px) max(60px, calc(24px + env(safe-area-inset-bottom)));
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 20px; flex-wrap: wrap;
}
.menu-btn { display: none; }
.icon-btn {
  background: var(--surface); border: 1px solid var(--line);
  padding: 9px 12px; font-size: 16px; flex-shrink: 0;
}

/* Mobile-only: view title inside topbar */
.mobile-header {
  display: none; flex: 1;
  align-items: center; justify-content: space-between; gap: 8px;
}
.mobile-view-label {
  font-size: 17px; font-weight: 600;
  margin: 0; letter-spacing: -0.01em; color: var(--ink);
}

.capture {
  flex: 1; min-width: 0;
  display: flex; flex-wrap: wrap; gap: 6px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 6px; box-shadow: var(--shadow);
}
.capture input {
  flex: 1; min-width: 0; border: none;
  background: none; outline: none; padding: 8px 10px;
}
.capture select {
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface-2); padding: 0 6px;
  min-width: 0; max-width: 160px;
}
.capture button {
  background: var(--accent); color: #fff;
  font-weight: 600; padding: 8px 18px; flex-shrink: 0;
}
.capture button:disabled { opacity: 0.6; cursor: default; }

.search input {
  border: 1px solid var(--line); background: var(--surface);
  border-radius: 12px; padding: 11px 14px; outline: none; width: 200px;
}
.search input:focus { border-color: var(--accent); }

.view-title { display: flex; align-items: baseline; gap: 10px; margin-bottom: 14px; }
.view-title h2 { margin: 0; font-size: 22px; }
.count { color: var(--muted); font-size: 14px; }

/* ---------- Cards ---------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden; cursor: pointer;
  transition: transform 0.18s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.18s ease;
  position: relative;
  animation: card-in 0.24s ease-out both;
  animation-delay: calc(var(--card-i, 0) * 28ms);
}
.card--media { border-color: transparent; }

/* Image-first: media container */
.card-media {
  position: relative; overflow: hidden;
  background: var(--surface-2);
}
.card-img { width: 100%; display: block; object-fit: cover; }
.card-media--fallback {
  min-height: 100px; display: flex; align-items: center; padding: 14px;
}
.card-fallback-title { font-size: 13px; color: var(--ink-soft); margin: 0; line-height: 1.4; }

/* Gradient overlay on image cards */
.card-overlay {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 36px 12px 10px;
  background: linear-gradient(to top, rgba(0,0,0,0.74) 0%, transparent 100%);
}
.card-fav {
  position: absolute; top: 10px; left: 10px;
  width: 20px; height: 20px; border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
}
.card-overlay-title {
  font-size: 13px; font-weight: 600; color: #fff;
  margin: 0; line-height: 1.35;
}

/* Text / note card */
.card-body { padding: 14px 15px; }
.card-body--note { min-height: 110px; }
.card-title { font-weight: 600; font-size: 15px; line-height: 1.35; margin: 0 0 6px; }
.card-text {
  white-space: pre-wrap; font-size: 14.5px; line-height: 1.5;
  color: var(--ink); margin: 0; overflow-wrap: anywhere;
}
.card-desc { font-size: 13px; color: var(--ink-soft); margin: 6px 0 0; line-height: 1.45; }
.card-meta {
  display: flex; align-items: center; gap: 7px;
  margin-top: 10px; font-size: 12px; color: var(--muted);
}
.card-favicon { width: 14px; height: 14px; border-radius: 3px; }

/* mymind-style: category dot + date below card */
.card-below {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px 10px;
}
.card-cat-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.card-cat-label {
  font-size: 11.5px; color: var(--ink-soft);
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-date { font-size: 11px; color: var(--muted); flex-shrink: 0; }

.empty { text-align: center; padding: 80px 20px; }
.empty p { margin: 4px 0; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 20px;
}
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(20, 18, 15, 0.45);
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
}
.modal-card {
  position: relative; background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 100%; max-width: 560px; max-height: 86vh; overflow-y: auto;
  animation: modal-pop 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}
.modal-close {
  position: absolute; top: 6px; right: 8px;
  background: rgba(0, 0, 0, 0.06);
  width: 44px; height: 44px; border-radius: 50%;
  font-size: 20px; line-height: 1; color: var(--ink); z-index: 2;
  display: flex; align-items: center; justify-content: center;
}
.modal-img { width: 100%; display: block; }
.modal-inner { padding: 22px 24px 24px; }
.modal-inner h2 { margin: 0 0 8px; font-size: 20px; }
.modal-link { color: var(--accent); text-decoration: none; word-break: break-all; font-size: 13.5px; }
.modal-link:hover { text-decoration: underline; }
.modal-text { white-space: pre-wrap; line-height: 1.6; margin: 14px 0; overflow-wrap: anywhere; }
.modal-field {
  margin: 16px 0 6px; font-size: 12px; font-weight: 600;
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em;
}
.modal-inner input, .modal-inner textarea, .modal-inner select {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; background: var(--surface-2); outline: none;
}
.modal-inner input:focus, .modal-inner textarea:focus, .modal-inner select:focus { border-color: var(--accent); }
.modal-inner textarea { min-height: 120px; resize: vertical; line-height: 1.5; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.btn-primary { background: var(--accent); color: #fff; font-weight: 600; padding: 10px 18px; }
.btn-secondary { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); padding: 10px 18px; }
.btn-danger { background: none; color: var(--color-danger); padding: 10px 14px; margin-left: auto; }
.btn-danger:hover { background: color-mix(in srgb, var(--color-danger) 10%, transparent); }
.open-link { display: inline-block; margin-top: 6px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: max(24px, calc(16px + env(safe-area-inset-bottom)));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 11px 18px; border-radius: 999px;
  font-size: 14px; box-shadow: var(--shadow);
  z-index: 100; white-space: nowrap;
}
.toast:not(.hidden) { animation: toast-pop 0.2s ease-out both; }

/* ---------- FAB ---------- */
.fab {
  display: none;
  position: fixed;
  bottom: max(82px, calc(74px + env(safe-area-inset-bottom)));
  right: 22px;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none;
  box-shadow: 0 4px 24px oklch(0.63 0.17 46 / 0.45);
  z-index: 25;
  align-items: center; justify-content: center; cursor: pointer;
  transition: transform 0.14s ease, box-shadow 0.14s ease;
}
.fab:active { transform: scale(0.91); box-shadow: 0 2px 10px oklch(0.63 0.17 46 / 0.28); }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(60px + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
  z-index: 30; align-items: stretch;
}
.bottom-nav-item {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 3px; flex: 1; padding: 8px 4px;
  background: none; color: var(--muted);
  font-size: 10.5px; font-weight: 500; border-radius: 0;
  letter-spacing: 0.01em;
  transition: color 0.14s ease;
}
.bottom-nav-item.active { color: var(--accent); }
.bottom-nav-item svg { width: 22px; height: 22px; }
.bottom-nav-item span { line-height: 1; }

/* ---------- Capture bottom sheet ---------- */
.capture-sheet {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-end;
}
.capture-sheet-backdrop {
  position: absolute; inset: 0;
  background: oklch(0 0 0 / 0.5);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
}
.capture-sheet-card {
  position: relative; background: var(--surface);
  border-radius: 24px 24px 0 0;
  padding: 16px 20px max(28px, calc(20px + env(safe-area-inset-bottom)));
  width: 100%;
  animation: sheet-up 0.32s cubic-bezier(0.32, 0.72, 0, 1) both;
}
.capture-sheet-handle {
  width: 36px; height: 4px; border-radius: 2px;
  background: var(--line); margin: 0 auto 20px;
}
#capture-sheet-input {
  width: 100%; border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 16px; background: var(--surface-2);
  font: inherit; font-size: 16px;
  min-height: 96px; resize: none; outline: none;
  color: var(--ink); line-height: 1.55; display: block;
}
#capture-sheet-input:focus { border-color: var(--accent); }
.capture-sheet-actions { display: flex; gap: 10px; margin-top: 12px; align-items: center; }
#capture-sheet-category {
  flex: 1; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; background: var(--surface-2);
  color: var(--ink); font: inherit; font-size: 16px; outline: none;
}
.capture-sheet-btn {
  background: var(--accent); color: #fff;
  font-weight: 600; padding: 10px 22px;
  font-size: 15px; flex-shrink: 0; border-radius: 10px;
}
.capture-sheet-btn:disabled { opacity: 0.6; cursor: default; }

/* ---------- Animations ---------- */
@keyframes card-in {
  from { opacity: 0; translate: 0 10px; }
  to   { opacity: 1; translate: 0 0; }
}
@keyframes modal-pop {
  from { opacity: 0; scale: 0.96; }
  to   { opacity: 1; scale: 1; }
}
@keyframes sheet-up {
  from { translate: 0 100%; }
  to   { translate: 0 0; }
}
@keyframes toast-pop {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---------- Hover / pointer ---------- */
@media (hover: hover) {
  .card:hover { transform: translateY(-3px); box-shadow: 0 12px 36px oklch(0 0 0 / 0.12); }
}
@media (hover: none) {
  .cat-edit { opacity: 1; }
  .card:active { transform: scale(0.97); }
  .fab:hover { transform: none; }
}
@media (pointer: coarse) {
  .capture input, .search input, .login-card input,
  .modal-inner input, .modal-inner textarea, .modal-inner select,
  #capture-sheet-input, #capture-sheet-category { font-size: 16px; }
  .cat { min-height: 44px; }
  .add-cat { min-height: 44px; }
  .cat-edit { min-width: 44px; min-height: 44px; }
}

/* ---------- Mobile (≤820px) ---------- */
@media (max-width: 820px) {
  /* Sidebar: fixed drawer, opened by Spaces tab */
  .sidebar {
    position: fixed; z-index: 40;
    left: 0; top: 0; height: 100dvh;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.22);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-scrim.open {
    display: block; position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.38); z-index: 35;
  }

  /* FAB + bottom nav appear */
  .fab { display: flex; }
  .bottom-nav { display: flex; }

  /* Main: no top pad (topbar provides it); bottom pad for nav */
  .main {
    padding-top: 0;
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
  }

  /* View title block hidden — mobile-header in topbar handles it */
  .view-title { display: none; }

  /* Topbar: sticky, full-bleed */
  .topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    background: var(--bg);
    border-bottom: 1px solid var(--line);
    padding: max(16px, calc(8px + env(safe-area-inset-top))) clamp(16px, 4vw, 40px) 14px;
    margin: 0 calc(-1 * clamp(16px, 4vw, 40px)) 20px;
    flex-wrap: nowrap;
  }
  .capture { display: none; }
  .search { display: none; }
  .menu-btn { display: none; }
  .mobile-header { display: flex; flex: 1; }

  /* 2-column masonry-feel grid */
  .cards { grid-template-columns: 1fr 1fr; gap: 10px; }

  /* Portrait aspect on image cards */
  .card-media { aspect-ratio: 3 / 4; }
  .card-img { height: 100%; width: 100%; object-fit: cover; }

  /* Search active: swap header for search input */
  .search-active .mobile-header { display: none; }
  .search-active .search { display: flex; flex: 1; }
  .search-active .search input { width: 100%; }

  /* Toast above bottom nav */
  .toast { bottom: max(76px, calc(68px + env(safe-area-inset-bottom))); }

  /* Modal as bottom sheet */
  .modal { align-items: flex-end; padding: 0; }
  .modal-card {
    max-width: 100%; border-radius: 20px 20px 0 0;
    max-height: 90dvh;
    padding-bottom: env(safe-area-inset-bottom);
    animation: sheet-up 0.32s cubic-bezier(0.32, 0.72, 0, 1) both;
  }
  .modal-close { top: 10px; right: 12px; }
}

@media (max-width: 440px) {
  .topbar { padding-left: 16px; padding-right: 16px; }
}

/* ---------- Accessibility ---------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
