/* =========================
   design tokens (themes)
   ========================= */

:root {
  /* light theme */
  --bg:            #f9f9fb;
  --panel:         #ffffff;
  --panel-2:       #f3f4f7;
  --text:          #1a1b1f;
  --muted:         #70757f;
  --accent:        #F94354;
  --accent-ink:    #ffffff;
  --border:        0, 0, 0;
  --border-a:      0.08;
  --shadow:        0 4px 12px rgba(0,0,0,.10);
  --ring:          0, 102, 255;          /* focus ring – rgb triplet */

  --space-1: 6px;
  --space-2: 10px;
  --space-3: 14px;
  --radius:  14px;

  --grid-min: 220px;

  --bg-gradient: radial-gradient(1200px 600px at 12% -14%, #e8ebff 0%, transparent 60%) no-repeat;

  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg:            #0b0b0e;
  --panel:         #12131a;
  --panel-2:       #1a1b25;
  --text:          #e8e9ef;
  --muted:         #9aa1ad;
  --accent:        #88a0ff;
  --accent-ink:    #0b0b0e;
  --border:        255, 255, 255;
  --border-a:      0.08;
  --shadow:        0 8px 20px rgba(0,0,0,.35);
  --ring:          136, 160, 255;
  --bg-gradient:   radial-gradient(1200px 600px at 10% -10%, #151625 0%, transparent 60%) no-repeat;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg:            #0b0b0e;
    --panel:         #12131a;
    --panel-2:       #1a1b25;
    --text:          #e8e9ef;
    --muted:         #9aa1ad;
    --accent:        #88a0ff;
    --accent-ink:    #0b0b0e;
    --border:        255, 255, 255;
    --border-a:      0.08;
    --shadow:        0 8px 20px rgba(0,0,0,.35);
    --ring:          136, 160, 255;
    --bg-gradient:   radial-gradient(1200px 600px at 10% -10%, #151625 0%, transparent 60%) no-repeat;
    color-scheme: dark;
  }
}

/* =========================
   reset / base
   ========================= */

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

html, body {
  margin: 0;
  padding: 0;
}

body {
  font: .8rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

img { display: block; }

/* links */
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* button reset – shared base for all <button> elements */
button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

/* reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration:       0.01ms !important;
    animation-iteration-count: 1     !important;
    transition-duration:      0.01ms !important;
    scroll-behavior:          auto   !important;
  }
}

/* =========================
   accessibility
   ========================= */

/* Ensure hidden attribute hides elements unconditionally */
[hidden] { display: none !important; }

/* Focus ring for keyboard navigation */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid rgba(var(--ring), 1);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Screen-reader only */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* =========================
   layout helpers
   ========================= */

.container { margin: 0 auto; padding: 2rem; }
.muted     { color: var(--muted); }
.empty     { color: var(--muted); }

/* =========================
   app shell: sidebar + content
   ========================= */

.app {
  display: grid;
  grid-template-columns: 200px 1fr;
  min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;            /* taglist scrolls internally */
  padding: 1rem;
  background: var(--panel);
  border-right: 1px solid rgba(var(--border), var(--border-a));
}

/* Tag list fills available space and scrolls when long */
.taglist {
  flex: 1;
  min-height: 0;               /* required for overflow to work in flex */
  overflow-y: auto;
  display: grid;
  gap: 4px;
  align-content: start;
  margin-top: 4px;
}

/* Bookmarklet link pinned to sidebar bottom */
.side-head {
  flex-shrink: 0;
  padding-top: .75rem;
  margin-top: auto;
  border-top: 1px solid rgba(var(--border), var(--border-a));
}
.side-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ── Tag rows in sidebar ── */
.tagrow {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  transition: background-color .12s ease;
}
.tagrow:hover { background: rgba(var(--border), .06); text-decoration: none; }
.tagrow.is-active { font-weight: 600; text-decoration: underline; }
.tagname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.badge   { font-variant-numeric: tabular-nums; color: var(--muted); flex-shrink: 0; }

/* ── Right content area ── */
.content { padding: 20px 16px; }

/* ── Sidebar toggle (mobile only) ── */
.sidebar-toggle { display: none; }

.sidebar-chevron {
  transition: transform .2s ease;
}
.sidebar.is-open .sidebar-chevron {
  transform: rotate(180deg);
}

/* Responsive: sidebar becomes a collapsible dropdown on small screens */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }

  .sidebar {
    position: static;
    height: auto;
    overflow: visible;
    border-right: none;
    border-bottom: 1px solid rgba(var(--border), var(--border-a));
    padding: 0;
  }

  .sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 10px 1rem;
    color: var(--muted);
    font-size: .8rem;
  }

  /* Hide sidebar content by default */
  .sidebar .taglist,
  .sidebar .side-head {
    display: none;
  }

  /* Show when open */
  .sidebar.is-open .taglist {
    display: grid;
    max-height: 50vh;
    overflow-y: auto;
    padding: 0 1rem;
  }
  .sidebar.is-open .side-head {
    display: block;
    padding: 0 1rem .75rem;
    margin-top: .5rem;
  }
}

/* =========================
   masonry grid (JS-positioned)
   ========================= */

/* Wrapper: JS sets explicit height; cards are placed absolutely inside */
.grid.masonry {
  position: relative;
}

.grid.masonry .card {
  position: absolute;          /* coordinates set by masonry.js */
  margin: 0;
  will-change: transform;
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 6px;
  overflow: hidden;
  background: var(--panel);
  transition: transform .12s ease, box-shadow .12s ease;
}

.grid.masonry .card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.grid.masonry .card img {
  display: block;
  width: 100%;
  height: auto;                /* preserve natural aspect ratio */
  background: var(--panel-2);
}

/* =========================
   forms / inputs
   ========================= */

.input,
input[type="text"],
input[type="password"],
input[type="url"],
input[type="search"],
textarea,
select {
  width: 100%;
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid rgba(var(--border), var(--border-a));
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-size: .8rem;
  transition: border-color .15s ease, box-shadow .15s ease;
  -webkit-appearance: none;
  appearance: none;
}

.input:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid rgba(var(--ring), 1);
  outline-offset: 0;
  border-color: transparent;
}

label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: .75rem;
}

.row { display: flex; gap: 8px; align-items: center; }
.row .input { flex: 1; }

.err { color: #d53b3b; margin-bottom: 8px; font-size: .8rem; }

/* =========================
   buttons
   ========================= */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: .45rem .75rem;
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  line-height: 1.3;
  font: inherit;
  font-size: .8rem;
  transition: border-color .15s ease, background-color .15s ease, box-shadow .15s ease;
}
.btn:hover {
  border-color: rgba(var(--border), calc(var(--border-a) + .08));
  text-decoration: none;
}

.btn.ghost { background: transparent; }

.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}
.btn.primary:hover { filter: brightness(.95); }

.btn.danger {
  background: #d74e4e;
  color: #fff;
  border-color: transparent;
}
.btn.danger:hover { background: #c13f3f; }

@media (pointer: coarse) {
  .btn { padding: 10px 14px; }
}

/* =========================
   modals
   ========================= */

/* Prevent invisible overlays from intercepting clicks */
.modal-overlay,
.modal { pointer-events: none; }

.modal-overlay.show,
.modal.show { pointer-events: auto; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .45);
  opacity: 0;
  transition: opacity .15s ease;
  z-index: 900;
}
.modal-overlay.show { opacity: 1; }

.modal {
  position: fixed; inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 901;
  opacity: 0;
  transform: translateY(6px) scale(.98);
  transition: opacity .15s ease, transform .15s ease;
}
.modal.show { opacity: 1; transform: none; }

.modal-content {
  width: min(480px, 92vw);
  background: var(--panel);
  color: var(--text);
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-header {
  padding: 14px 16px;
  border-bottom: 1px solid rgba(var(--border), var(--border-a));
}
.modal-header h3 { margin: 0; font-size: 15px; }

.modal-body { padding: 14px 16px; color: var(--muted); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid rgba(var(--border), var(--border-a));
}

/* =========================
   login page
   ========================= */

.login-wrap {
  max-width: 420px;
  margin: 80px auto;
  padding: 24px;
  background: var(--panel);
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.login-wrap h2 { margin: 0 0 1.25rem; font-size: 1.1rem; }
.actions { display: flex; gap: 8px; align-items: center; }

/* =========================
   view page
   ========================= */

.view-content { max-width: 1020px; }

/* top bar: back nav + action buttons */
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.view-header-actions {
  display: flex;
  gap: 8px;
}

/* image + info panel side by side */
.view-layout {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 24px;
  align-items: start;
}

@media (max-width: 720px) {
  .view-layout { grid-template-columns: 1fr; }
}

/* image area */
.view-image-wrap {
  background: var(--panel-2);
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 120px;
}

.view-image-wrap a { display: contents; }

.view-image-wrap img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 80vh;
  object-fit: contain;
}

/* info panel */
.view-info {
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: sticky;
  top: 20px;
}

.view-title {
  margin: 0;
  font-size: .95rem;
  font-weight: 600;
  line-height: 1.45;
}

.view-title a {
  color: var(--text);
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  text-decoration: none;
}
.view-title a:hover { text-decoration: underline; }
.view-title a:hover .ext-icon { opacity: 1; }

.ext-icon {
  flex-shrink: 0;
  opacity: .4;
  transition: opacity .12s ease;
  vertical-align: -.1em;
}

.view-source,
.view-date {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-size: .75rem;
  color: var(--muted);
}

.view-source svg,
.view-date svg { flex-shrink: 0; opacity: .6; }

.view-source a {
  color: var(--muted);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.view-source a:hover { color: var(--text); text-decoration: underline; }

/* prev / next navigation */
.view-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid rgba(var(--border), var(--border-a));
}

.view-nav-slot { flex: 1; }

.view-nav-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .75rem;
  color: var(--muted);
  text-decoration: none;
  padding: 4px 0;
  transition: color .12s ease;
}
.view-nav-link:hover { color: var(--text); text-decoration: none; }

/* =========================
   tag components
   ========================= */

/* Tag chips on view page */
.view-tags { display: flex; flex-wrap: wrap; gap: 6px; }

.tagchip {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 999px;
  font-size: 11px;
  text-decoration: none;
  color: var(--muted);
  background: var(--panel);
  transition: border-color .12s ease, color .12s ease;
}
.tagchip:hover {
  border-color: rgba(var(--border), calc(var(--border-a) + .1));
  color: var(--text);
  text-decoration: none;
}

/* Tiny inline tags (used in card meta – currently commented out in HTML but kept for future use) */
.tags-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
.tags-inline .t {
  display: inline-flex;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid rgba(var(--border), var(--border-a));
  font-size: 11px;
  text-decoration: none;
  color: var(--muted);
}
.tags-inline .t:hover {
  border-color: rgba(var(--border), calc(var(--border-a) + .08));
  color: var(--text);
}

/* =========================
   clipper (image picker popup)
   ========================= */

.clip-header {
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid rgba(var(--border), var(--border-a));
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
}

.clip-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  padding: 10px;
}

.clip-item {
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.clip-item img {
  display: block;
  width: 100%;
  height: 140px;
  object-fit: cover;
  background: var(--panel-2);
}

.clip-item button {
  border-top: 1px solid rgba(var(--border), var(--border-a));
  padding: 8px 10px;
  background: var(--panel);
  color: var(--text);
  font-size: .75rem;
  transition: background-color .12s ease;
}
.clip-item button:hover { background: var(--panel-2); }
.clip-item button:disabled { opacity: .5; cursor: default; }

/* Tag autocomplete dropdown */
.tag-suggest {
  position: fixed;
  background: var(--panel);
  border: 1px solid rgba(var(--border), var(--border-a));
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 9999;
}

.tag-suggest-item {
  display: inline-flex;
  align-items: center;
  padding: 5px 10px;
  margin: 3px;
  border-radius: 999px;
  border: 1px solid rgba(var(--border), var(--border-a));
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: .75rem;
  cursor: pointer;
  transition: border-color .12s ease, background-color .12s ease;
}
.tag-suggest-item:hover {
  background: var(--panel-2);
  border-color: rgba(var(--border), calc(var(--border-a) + .08));
}

/* =========================
   topbar (bookmarklet page)
   ========================= */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .75rem 1rem;
  gap: 1rem;
  border-bottom: 1px solid rgba(var(--border), var(--border-a));
  background: var(--panel);
}

.topbar h1 { margin: 0; font-size: 1rem; font-weight: 600; }
.topbar nav { display: flex; gap: .5rem; }

/* =========================
   card meta (for future use – content currently commented out in HTML)
   ========================= */

.card .meta { padding: 10px 12px; }

.card h3 {
  margin: 0 0 4px;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card .url {
  margin: 0;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;           /* required for text-overflow to work */
  text-overflow: ellipsis;
}

/* =========================
   flash-card (scripts.js scroll restoration)
   ========================= */

@keyframes flash-highlight {
  0%   { outline: 3px solid rgba(var(--ring), .7); outline-offset: 1px; }
  100% { outline: 3px solid rgba(var(--ring),  0); outline-offset: 1px; }
}
.flash-card { animation: flash-highlight 1s ease-out; }

/* =========================
   hover tooltip (scripts.js)
   ========================= */

.ib-tooltip {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  background: var(--panel);
  color: var(--text);
  border: 1px solid rgba(var(--border), var(--border-a));
  box-shadow: var(--shadow);
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 12px;
  line-height: 1.3;
  max-width: min(60ch, 60vw);
  white-space: nowrap;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .12s ease, transform .12s ease;
}
.ib-tooltip.show {
  opacity: 1;
  transform: translateY(0);
}

/* =========================
   index: search bar
   ========================= */

.search-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  max-width: 360px;
}

.search-count {
  font-size: .75rem;
  white-space: nowrap;
  flex-shrink: 0;
}

/* =========================
   edit page
   ========================= */

.edit-layout {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 28px;
  align-items: start;
}

@media (max-width: 720px) {
  .edit-layout { grid-template-columns: 1fr; }
  .edit-preview { order: -1; }
}

.edit-nav { display: flex; gap: 8px; margin-bottom: 16px; }

.edit-form { display: flex; flex-direction: column; gap: 12px; }

.form-field { display: flex; flex-direction: column; gap: 6px; }

.field-hint { margin: 0; font-size: .73rem; color: var(--muted); }

.edit-actions { display: flex; gap: 8px; }

.edit-preview img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid rgba(var(--border), var(--border-a));
}

.edit-meta { margin: 8px 0 0; font-size: 11px; }

/* =========================
   bookmarklet page
   ========================= */

.bm-main { max-width: 780px; margin: 32px auto; padding: 0 16px; }

.bm-section { margin-bottom: 2rem; }
.bm-section h2 { font-size: .95rem; margin: 0 0 .5rem; font-weight: 600; }
.bm-section p { margin: 0 0 .75rem; }
.bm-section ol { margin: 0 0 .75rem; padding-left: 1.25rem; line-height: 1.8; }

.bm-code {
  width: 100%;
  height: 72px;
  resize: vertical;
  font-family: monospace;
  font-size: 11px;
}

.bm-copy-row { display: flex; align-items: center; gap: .5rem; margin-top: .5rem; }

.bm-copy-done { font-size: .75rem; color: var(--muted); opacity: 0; transition: opacity .3s; }

.bm-hint { margin-top: 1.5rem; }
