/* ===== Design tokens ===== */
:root {
  --bg: #faf9f7;
  --bg-elevated: #ffffff;
  --bg-soft: #f1efec;
  --border: rgba(20, 18, 14, 0.1);
  --border-strong: rgba(20, 18, 14, 0.18);
  --text: #1c1a16;
  --text-secondary: #55524a;
  --text-muted: #8a8678;
  --accent: #c25a1e;
  --accent-soft: rgba(194, 90, 30, 0.12);
  --accent-strong: #9c4514;
  --danger: #b3261e;
  --danger-soft: rgba(179, 38, 30, 0.1);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(20, 18, 14, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 18, 14, 0.08);
  --shadow-lg: 0 20px 48px rgba(20, 18, 14, 0.16);
  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "PingFang TC", "Noto Sans TC", sans-serif;
  --font-mono: "SF Mono", "JetBrains Mono", monospace;
  --transition: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161512;
    --bg-elevated: #1f1d19;
    --bg-soft: #26241f;
    --border: rgba(255, 255, 255, 0.09);
    --border-strong: rgba(255, 255, 255, 0.16);
    --text: #f3f1ec;
    --text-secondary: #c9c5ba;
    --text-muted: #8f8b7e;
    --accent: #e3823f;
    --accent-soft: rgba(227, 130, 63, 0.16);
    --accent-strong: #f3985a;
    --danger: #f2685f;
    --danger-soft: rgba(242, 104, 95, 0.14);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.5);
  }
}

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

html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100dvh;
}

img { max-width: 100%; display: block; }
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }

.skip-link {
  position: absolute; top: -44px; left: 12px; z-index: 999;
  background: var(--accent); color: #fff; padding: 10px 14px;
  border-radius: var(--radius-sm); transition: top var(--transition);
}
.skip-link:focus { top: 12px; }

/* ===== Layout ===== */
.app-shell { max-width: 880px; margin: 0 auto; padding: 0 20px 80px; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 16px 0; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px); border-bottom: 1px solid transparent;
}
.topbar.scrolled { border-bottom-color: var(--border); }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; flex-shrink: 0; }
.brand-mark { color: var(--accent); font-size: 1.2rem; }

.search-wrap { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.search-wrap input {
  flex: 1; min-width: 0; background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 10px 14px; color: var(--text);
  transition: border-color var(--transition), background var(--transition);
}
.search-wrap input:focus { outline: none; border-color: var(--accent); background: var(--bg-elevated); }

.mode-toggle {
  flex-shrink: 0; border: 1px solid var(--border); background: var(--bg-soft);
  border-radius: var(--radius-md); padding: 10px 12px; color: var(--text-secondary);
  font-size: 0.82rem; font-weight: 600; transition: var(--transition);
}
.mode-toggle[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

.top-actions { display: flex; gap: 4px; flex-shrink: 0; }
.icon-btn {
  padding: 8px 12px; border-radius: var(--radius-sm); font-size: 0.85rem;
  color: var(--text-secondary); transition: var(--transition);
}
.icon-btn:hover { background: var(--bg-soft); color: var(--text); }
.icon-btn.active { color: var(--accent); font-weight: 700; }

/* ===== Capture panel ===== */
.capture-panel { margin: 28px 0 32px; }
#capture-form {
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 18px; box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition), border-color var(--transition);
}
#capture-form:focus-within { border-color: var(--accent); box-shadow: var(--shadow-md); }

#capture-input {
  width: 100%; border: none; resize: vertical; min-height: 64px;
  background: transparent; color: var(--text); font-size: 1rem;
}
#capture-input:focus { outline: none; }
#capture-input::placeholder { color: var(--text-muted); }

.capture-attachment {
  position: relative; margin-top: 10px; display: inline-block;
}
.capture-attachment img {
  max-height: 140px; border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.chip-remove {
  position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; border-radius: 50%;
  background: var(--text); color: var(--bg); border: 2px solid var(--bg-elevated);
  font-size: 0.7rem; line-height: 1; display: flex; align-items: center; justify-content: center;
}

.capture-row { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.file-btn {
  border: 1px dashed var(--border-strong); border-radius: var(--radius-sm);
  padding: 7px 12px; font-size: 0.82rem; color: var(--text-secondary); transition: var(--transition);
}
.file-btn:hover { border-color: var(--accent); color: var(--accent); }

.capture-status { flex: 1; font-size: 0.8rem; color: var(--text-muted); }

.primary-btn {
  background: var(--accent); color: #fff; border: none; border-radius: var(--radius-sm);
  padding: 9px 20px; font-weight: 600; font-size: 0.9rem; display: inline-flex; align-items: center; gap: 8px;
  transition: var(--transition);
}
.primary-btn:hover { background: var(--accent-strong); }
.primary-btn:disabled { opacity: 0.6; cursor: default; }

.spinner {
  width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.ghost-btn {
  background: transparent; border: 1px solid var(--border-strong); color: var(--text-secondary);
  border-radius: var(--radius-sm); padding: 9px 16px; font-size: 0.85rem; font-weight: 600;
  transition: var(--transition); display: inline-flex; align-items: center; gap: 6px;
}
.ghost-btn:hover { border-color: var(--accent); color: var(--accent); }

.danger-btn {
  background: var(--danger-soft); border: 1px solid var(--danger); color: var(--danger);
  border-radius: var(--radius-sm); padding: 9px 16px; font-weight: 600; font-size: 0.85rem;
}
.danger-btn:hover { background: var(--danger); color: #fff; }

/* ===== Filter bar ===== */
.filter-bar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
.kind-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.kind-tab {
  border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text-secondary);
  border-radius: 999px; padding: 6px 14px; font-size: 0.8rem; font-weight: 600; transition: var(--transition);
}
.kind-tab:hover { border-color: var(--accent); }
.kind-tab[aria-selected="true"] { background: var(--text); border-color: var(--text); color: var(--bg); }

.tag-cloud { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip {
  border: 1px solid var(--border); background: transparent; color: var(--text-muted);
  border-radius: 999px; padding: 4px 12px; font-size: 0.76rem; transition: var(--transition);
}
.tag-chip:hover { color: var(--accent); border-color: var(--accent); }
.tag-chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

/* ===== AI answer box ===== */
.ai-answer-box {
  background: var(--accent-soft); border: 1px solid var(--accent); border-radius: var(--radius-md);
  padding: 16px 18px; margin-bottom: 18px; font-size: 0.92rem; white-space: pre-wrap;
  animation: fadeIn 0.25s ease;
}
.ai-answer-box .ai-answer-label { display: block; font-size: 0.72rem; font-weight: 700; color: var(--accent-strong); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 6px; }

/* ===== Notes list ===== */
.notes-list { display: flex; flex-direction: column; gap: 10px; }

.note-card {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 14px 16px; cursor: pointer; transition: var(--transition), transform 0.15s ease;
  display: flex; gap: 14px; animation: fadeIn 0.3s ease;
}
.note-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-sm); transform: translateY(-1px); }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.note-thumb {
  width: 64px; height: 64px; border-radius: var(--radius-sm); object-fit: cover; flex-shrink: 0;
  border: 1px solid var(--border); background: var(--bg-soft);
}
.note-body { flex: 1; min-width: 0; }
.note-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; flex-wrap: wrap; }

.kind-badge {
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 2px 8px; border-radius: 999px; background: var(--bg-soft); color: var(--text-secondary);
}
.kind-badge.kind-idea { background: rgba(194, 90, 30, 0.12); color: var(--accent-strong); }
.kind-badge.kind-link { background: rgba(59, 130, 180, 0.12); color: #2b7ba0; }
.kind-badge.kind-tutorial { background: rgba(80, 140, 90, 0.14); color: #3f7a4c; }
.kind-badge.kind-quote { background: rgba(150, 100, 200, 0.14); color: #7c56b8; }
.kind-badge.kind-todo { background: rgba(200, 80, 80, 0.12); color: #b3463f; }
.kind-badge.kind-image { background: rgba(120, 120, 130, 0.14); color: var(--text-secondary); }

.note-time { font-size: 0.72rem; color: var(--text-muted); }
.note-summary { font-size: 0.92rem; color: var(--text); margin-bottom: 4px; word-break: break-word; }
.note-snippet { font-size: 0.85rem; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-word; }
.note-url { font-size: 0.78rem; color: var(--accent); margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.note-tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 6px; }
.note-tag { font-size: 0.7rem; color: var(--text-muted); background: var(--bg-soft); padding: 2px 8px; border-radius: 999px; }

.notes-empty { text-align: center; padding: 60px 20px; color: var(--text-muted); }

/* ===== Detail overlay ===== */
.detail-overlay {
  position: fixed; inset: 0; background: rgba(10, 8, 4, 0.4); backdrop-filter: blur(2px);
  display: flex; justify-content: flex-end; z-index: 100; animation: fadeIn 0.2s ease;
}
.detail-panel {
  width: min(480px, 100%); height: 100%; background: var(--bg-elevated); box-shadow: var(--shadow-lg);
  overflow-y: auto; padding: 24px; position: relative;
  animation: slideIn 0.22s cubic-bezier(0.4,0,0.2,1);
}
@keyframes slideIn { from { transform: translateX(24px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.detail-close { position: absolute; top: 16px; right: 16px; }

#detail-body h2 { font-size: 1.1rem; margin: 6px 0 14px; padding-right: 40px; }
#detail-body img.detail-image { width: 100%; border-radius: var(--radius-md); margin-bottom: 14px; border: 1px solid var(--border); }
#detail-body .detail-meta { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 16px; }
#detail-body .detail-section { margin-bottom: 18px; }
#detail-body .detail-section h3 { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin-bottom: 6px; }
#detail-body .detail-content { white-space: pre-wrap; word-break: break-word; font-size: 0.94rem; }
#detail-body .detail-url-card {
  border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px; display: flex; gap: 10px;
}
#detail-body .detail-url-card img { width: 56px; height: 56px; object-fit: cover; border-radius: var(--radius-sm); flex-shrink: 0; }
#detail-body .detail-tag-editor { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
#detail-body .detail-actions { display: flex; gap: 8px; margin-top: 20px; }
#detail-body textarea.detail-edit { width: 100%; min-height: 100px; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px; background: var(--bg-soft); color: var(--text); }

/* ===== Settings ===== */
.settings-panel { max-width: 560px; margin: 32px auto; }
.settings-panel h1 { font-size: 1.4rem; margin-bottom: 24px; }
.settings-group {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 20px; margin-bottom: 16px;
}
.settings-group.danger { border-color: var(--danger); }
.settings-group h2 { font-size: 0.95rem; margin-bottom: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; font-size: 0.85rem; color: var(--text-secondary); }
.field input {
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 9px 12px;
  background: var(--bg-soft); color: var(--text);
}
.field input:focus { outline: none; border-color: var(--accent); }
.field-hint { font-size: 0.78rem; color: var(--text-muted); margin: 4px 0 14px; }
.settings-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.settings-stats { display: flex; gap: 18px; flex-wrap: wrap; font-size: 0.85rem; color: var(--text-secondary); }
.settings-stats b { color: var(--text); font-size: 1.1rem; display: block; }

/* ===== Toast ===== */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: var(--radius-sm);
  font-size: 0.85rem; box-shadow: var(--shadow-md); z-index: 200; animation: fadeIn 0.2s ease;
}
.toast.error { background: var(--danger); color: #fff; }

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .app-shell { padding: 0 14px 80px; }
  .topbar { flex-wrap: wrap; gap: 10px; }
  .search-wrap { order: 3; width: 100%; }
  .top-actions { margin-left: auto; }
  .note-card { gap: 10px; padding: 12px; }
  .note-thumb { width: 52px; height: 52px; }
  .detail-panel { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
