:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efeae2;
  --ink: #1d1b18;
  --muted: #6b655c;
  --line: #e2dbcf;
  --accent: #2f6f5e;
  --accent-ink: #ffffff;
  --danger: #b3412e;
  --tile-l: 88%;
  --tile-s: 38%;
  --shadow: 0 1px 2px rgb(40 30 20 / 0.06), 0 4px 14px rgb(40 30 20 / 0.06);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151412;
    --surface: #201e1b;
    --surface-2: #2a2723;
    --ink: #f1ede6;
    --muted: #a39c90;
    --line: #36322c;
    --accent: #6fc2a8;
    --accent-ink: #10201b;
    --danger: #e27a66;
    --tile-l: 24%;
    --tile-s: 22%;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #151412; --surface: #201e1b; --surface-2: #2a2723; --ink: #f1ede6; --muted: #a39c90;
  --line: #36322c; --accent: #6fc2a8; --accent-ink: #10201b; --danger: #e27a66;
  --tile-l: 24%; --tile-s: 22%; --shadow: 0 1px 2px rgb(0 0 0 / 0.3); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* Header */
.top {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  padding: calc(8px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.bar { display: flex; align-items: center; gap: 6px; max-width: 1100px; margin: 0 auto; }
.bar h1 { flex: 1; margin: 0; font-size: 1.35rem; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 12px;
  border: 0; background: transparent; cursor: pointer; color: var(--ink);
}
.icon-btn:hover { background: var(--surface-2); }
.search {
  display: flex; align-items: center; gap: 8px; max-width: 1100px; margin: 8px auto 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px;
}
.search svg { color: var(--muted); width: 18px; height: 18px; flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; padding: 10px 0; outline: none; }

/* Status + banner */
.status, .banner { max-width: 1100px; margin: 12px auto 0; padding: 0 16px; }
.status-text { font-size: 0.9rem; color: var(--muted); margin-bottom: 6px; }
.progress { height: 4px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
.banner > div {
  background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--accent);
  border-radius: 12px; padding: 12px 14px; font-size: 0.95rem;
}
.banner button { margin-top: 8px; }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.section-label { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 8px 0 10px; }

/* Folder tiles */
.folders { display: grid; grid-template-columns: repeat(auto-fill, minmax(156px, 1fr)); gap: 12px; }
.folder {
  position: relative; text-align: left; border: 0; cursor: pointer; padding: 0; overflow: hidden;
  border-radius: var(--radius); background: hsl(var(--h) var(--tile-s) var(--tile-l)); box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-height: 168px;
}
.folder .collage { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; padding: 8px 8px 0; height: 104px; }
.folder .collage div { border-radius: 8px; background: rgb(255 255 255 / 0.35) top center/cover no-repeat; }
.folder .collage .link-chip { display: grid; place-items: center; color: hsl(var(--h) 40% 35%); }
.folder .meta { padding: 10px 12px 12px; }
.folder strong { display: block; font-size: 1rem; line-height: 1.2; }
.folder span { font-size: 0.85rem; color: var(--muted); }
.folder.todo { background: var(--surface); outline: 2px dashed var(--line); outline-offset: -2px; }

/* Item grid */
.items { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 10px; }
.item {
  position: relative; border: 0; padding: 0; cursor: pointer; border-radius: 12px; overflow: hidden;
  background: var(--surface); box-shadow: var(--shadow); aspect-ratio: 9 / 15; text-align: left;
}
.item img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; }
.item .cap {
  position: absolute; inset: auto 0 0 0; padding: 18px 8px 7px; font-size: 0.75rem; line-height: 1.25;
  color: #fff; background: linear-gradient(transparent, rgb(0 0 0 / 0.72));
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.item.link { display: flex; flex-direction: column; padding: 12px; gap: 8px; background: hsl(var(--h) var(--tile-s) var(--tile-l)); }
.item.link svg { color: var(--muted); }
.item.link .lt { font-size: 0.85rem; font-weight: 600; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.item.link .ls { margin-top: auto; font-size: 0.72rem; color: var(--muted); }
.item .badge {
  position: absolute; top: 6px; right: 6px; font-size: 0.7rem; padding: 2px 7px; border-radius: 99px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
.item .badge.err { background: var(--danger); color: #fff; }

.results-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.empty { text-align: center; padding: 48px 16px; color: var(--muted); max-width: 440px; margin: 0 auto; }
.empty h2 { color: var(--ink); font-size: 1.25rem; margin: 12px 0 6px; }
.empty .big { font-size: 48px; }

/* Floating add button */
.fab {
  position: fixed; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 6;
  display: flex; align-items: center; gap: 6px; padding: 14px 20px 14px 16px; border: 0; border-radius: 99px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600; cursor: pointer;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.18);
}

/* Buttons */
.primary, .ghost, .text-btn { border-radius: 10px; padding: 10px 16px; cursor: pointer; font-weight: 600; }
.primary { background: var(--accent); color: var(--accent-ink); border: 0; }
.ghost { background: var(--surface); border: 1px solid var(--line); }
.text-btn { background: transparent; border: 0; color: var(--accent); padding: 10px 8px; }
.danger { color: var(--danger); }
.row-end { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.row-wrap { display: flex; flex-wrap: wrap; gap: 8px; }
.small { padding: 6px 12px; font-size: 0.9rem; }

/* Sheets (dialogs) */
dialog.sheet {
  border: 0; padding: 0; background: var(--surface); color: var(--ink);
  width: min(560px, 100vw); max-width: 100vw; max-height: 88dvh; border-radius: 20px;
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}
dialog.sheet::backdrop { background: rgb(20 15 10 / 0.45); }
@media (max-width: 600px) {
  dialog.sheet { margin: auto 0 0; width: 100vw; border-radius: 20px 20px 0 0; max-height: 92dvh; }
}
.sheet-body { padding: 20px 16px calc(16px + env(safe-area-inset-bottom)); }
@media (min-width: 601px) { .sheet-body { padding: 24px; } }
.sheet h2 { margin: 0 0 14px; font-size: 1.2rem; }
.choice {
  display: block; width: 100%; text-align: left; padding: 14px; margin-bottom: 8px; cursor: pointer;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px;
}
.choice strong { display: block; }
.choice span { font-size: 0.88rem; color: var(--muted); }
.hint, .note, small { color: var(--muted); font-size: 0.88rem; }
small { display: block; margin-top: 4px; }
.steps { padding-left: 20px; font-size: 0.95rem; }
.steps li { margin-bottom: 8px; }

.field { display: block; margin-bottom: 14px; font-weight: 600; font-size: 0.92rem; }
.field em { font-weight: 400; color: var(--muted); }
.field input, .field textarea, .field select, .topic-row input {
  display: block; width: 100%; margin-top: 6px; padding: 10px 12px; font-weight: 400;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}
fieldset { border: 1px solid var(--line); border-radius: 14px; margin: 0 0 16px; padding: 12px 14px; }
legend { font-weight: 700; padding: 0 6px; }
.radio, .check { display: flex; gap: 10px; align-items: flex-start; margin: 8px 0; font-size: 0.93rem; }
.radio input, .check input { margin-top: 4px; accent-color: var(--accent); }
.topic-row { display: grid; grid-template-columns: 1fr 1.6fr auto; gap: 6px; margin-bottom: 6px; align-items: center; }
.topic-row input { margin: 0; padding: 8px 10px; font-size: 0.9rem; }
@media (max-width: 480px) { .topic-row { grid-template-columns: 1fr auto; } .topic-row .kw { grid-column: 1 / -1; grid-row: 2; } }
.sticky { position: sticky; bottom: 0; background: var(--surface); padding-top: 10px; margin-bottom: 0; }

/* Item detail */
.detail-media { margin: -20px -16px 14px; background: var(--surface-2); max-height: 52dvh; overflow: auto; }
@media (min-width: 601px) { .detail-media { margin: -24px -24px 16px; border-radius: 20px 20px 0 0; } }
.detail-media img { display: block; width: 100%; height: auto; }
.detail-link { display: flex; gap: 10px; align-items: center; padding: 12px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); margin-bottom: 12px; word-break: break-all; font-size: 0.88rem; }
.detail-link svg { flex: none; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 14px; }
.tag { font-size: 0.8rem; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); border: 0; cursor: pointer; }
.detail-text { font-size: 0.85rem; color: var(--muted); white-space: pre-wrap; max-height: 8em; overflow: auto; background: var(--bg); padding: 8px 10px; border-radius: 10px; }
.err-box { color: var(--danger); font-size: 0.9rem; margin-bottom: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 0.92rem;
  opacity: 0; pointer-events: none; transition: 0.25s; z-index: 20; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

body.dragging main { outline: 3px dashed var(--accent); outline-offset: -8px; border-radius: 20px; }
