:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --text: #1c1c22;
  --muted: #6b6b76;
  --border: #e2e2e8;
  --accent: #6d5dfc;
  --accent-text: #ffffff;
  --accent-soft: #ecebff;
  --ok: #1f9d55;
  --warn: #c98a00;
  --error: #d64545;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131317;
    --surface: #1c1c22;
    --surface-2: #25252d;
    --text: #ececf1;
    --muted: #9a9aa6;
    --border: #2f2f38;
    --accent: #8b7eff;
    --accent-text: #0f0f14;
    --accent-soft: #2a2745;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 20px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--error); margin: 8px 0 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--error); }
.btn.danger:hover { border-color: var(--error); }
.btn.small { padding: 5px 10px; font-size: 12px; }

.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 5px 12px; font: inherit; cursor: pointer;
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
textarea { width: 100%; resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }

/* вход */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; display: grid; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px;
}
.login-card p { margin: 0; }

/* каркас */
.topbar {
  position: sticky; top: 0; z-index: 2; height: 56px;
  display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; }
.brand span { color: var(--accent); }
.usage { margin-left: auto; color: var(--muted); font-size: 13px; text-align: right; }
#tabs { margin-left: 8px; }
.main { padding: 20px; display: grid; gap: 16px; align-content: start; max-width: 1400px; margin: 0 auto; }
.empty { max-width: 520px; margin: 60px auto; text-align: center; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.toolbar #search { flex: 1 1 220px; min-width: 0; }

/* очередь загрузок */
.queue, .pending {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 16px; display: grid; gap: 10px;
}
.queue-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.up { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; }
.up-name { min-width: 0; }
.up-meta { grid-column: 1; color: var(--muted); font-size: 12px; }
.up-meta.error { color: var(--error); margin: 0; }
.up-actions { grid-column: 2; grid-row: 1 / span 3; display: flex; gap: 4px; }
.bar { grid-column: 1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.up.done .bar > i { background: var(--ok); }
.up.error .bar > i { background: var(--error); }

/* сетка */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.tile {
  position: relative; display: grid; gap: 6px; padding: 0; text-align: left; align-content: start; width: 100%;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  color: inherit; font: inherit; cursor: pointer; overflow: hidden;
}
.tile:hover { border-color: var(--accent); }
.tile-img { position: relative; aspect-ratio: 1; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.tile-img img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
.tile-img .noimg { color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.tile-caption { padding: 0 10px 10px; display: grid; gap: 2px; min-width: 0; }
.tag {
  position: absolute; bottom: 8px; right: 8px; padding: 1px 7px; border-radius: 6px;
  background: rgb(0 0 0 / .6); color: #fff; font-size: 12px;
}
.tag.left { right: auto; left: 8px; }

/* диалоги */
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 0; }
dialog::backdrop { background: rgb(0 0 0 / .55); }
.confirm form { padding: 20px; max-width: 440px; display: grid; gap: 16px; }
.confirm p { margin: 0; }

.item-dialog { width: min(96vw, 1200px); max-height: 96vh; overflow: hidden; }
.item-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.item-body { display: grid; grid-template-columns: 1fr 340px; }
.item-preview {
  background: #0d0d10; min-height: 300px; height: min(78vh, 860px);
  display: grid; place-items: center; position: relative; overflow: hidden;
}
.item-preview img, .item-preview video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.item-preview .note {
  position: absolute; left: 12px; right: 12px; bottom: 12px; margin: 0; padding: 8px 12px;
  border-radius: 8px; background: rgb(0 0 0 / .65); color: #fff; font-size: 13px;
}
.item-side { padding: 16px; display: grid; gap: 12px; align-content: start; overflow-y: auto; max-height: min(78vh, 860px); }
.item-side label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
.item-side label input, .item-side label textarea { color: var(--text); font-size: 14px; }
.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 13px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; overflow-wrap: anywhere; }

/* вкладки */
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: none; color: var(--muted); font: inherit; padding: 8px 12px; cursor: pointer; border-radius: 8px; }
.tab:hover { color: var(--text); }
.tab.on { color: var(--text); background: var(--surface-2); font-weight: 600; }
.count { color: var(--muted); font-weight: 400; }

/* выделение */
.tile-wrap { position: relative; }
.tile-check {
  position: absolute; top: 8px; left: 8px; z-index: 1; width: 28px; height: 28px; border-radius: 8px;
  display: grid; place-items: center; background: rgb(0 0 0 / .45); cursor: pointer;
}
.tile-check input { width: 18px; height: 18px; margin: 0; cursor: pointer; accent-color: var(--accent); }
.tile-wrap.selected .tile { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.selection {
  position: sticky; top: 64px; z-index: 1; display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  padding: 10px 14px; border-radius: var(--radius); background: var(--accent-soft); border: 1px solid var(--accent);
}
.selection #selection-text { font-weight: 600; margin-right: auto; }

/* ссылки */
.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.off { background: color-mix(in srgb, var(--error) 13%, transparent); color: var(--error); }
.shares { display: grid; gap: 10px; }
.share-row {
  display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: center; text-align: left;
  padding: 10px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  color: inherit; font: inherit; cursor: pointer; width: 100%;
}
.share-row:hover { border-color: var(--accent); }
.share-row > div { min-width: 0; display: grid; gap: 2px; }
.strip { display: flex; gap: 4px; }
.strip img, .strip .noimg { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; background: var(--surface-2); display: block; }
.item-shares { display: grid; gap: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.item-shares .row { justify-content: space-between; }
.mini-share { display: flex; gap: 8px; align-items: center; justify-content: space-between; font-size: 13px; }
.mini-share a { cursor: pointer; }

.share-dialog { width: min(96vw, 640px); }
.share-body { padding: 16px; display: grid; gap: 14px; max-height: min(84vh, 900px); overflow-y: auto; }
.share-body label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
.share-body label input, .share-body label textarea, .share-body label select { color: var(--text); font-size: 14px; width: 100%; }
.share-body section { display: grid; gap: 10px; padding-top: 14px; border-top: 1px solid var(--border); }
.share-body h3 { font-size: 14px; }
.link-row { display: flex; gap: 8px; }
.link-row input { flex: 1; min-width: 0; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.history { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; font-size: 13px; }

/* папки */
.crumbs { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; min-height: 32px; }
.crumb-path { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 15px; }
.crumb { padding: 4px 6px; border-radius: 6px; border: 1px solid transparent; }
.crumb.link { background: none; color: var(--accent); font: inherit; cursor: pointer; }
.crumb.link:hover { text-decoration: underline; }
.crumb.drop-on, .tile-wrap.drop-on .tile { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); background: var(--accent-soft); }
.folder-img { background: var(--accent-soft); }
.folder-img img { opacity: .35; }
.folder-icon {
  position: relative; width: 64px; height: 48px; border-radius: 4px 8px 8px 8px; background: var(--accent);
  box-shadow: 0 6px 16px rgb(0 0 0 / .18);
}
.folder-icon::before {
  content: ''; position: absolute; left: 0; top: -8px; width: 26px; height: 10px;
  border-radius: 4px 4px 0 0; background: var(--accent);
}
.tile-wrap[draggable="true"] { cursor: grab; }
.tree { display: grid; gap: 2px; max-height: 50vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; padding: 4px; min-width: min(80vw, 380px); }
.tree-row { text-align: left; border: 0; background: none; color: var(--text); font: inherit; padding: 7px 12px; border-radius: 6px; cursor: pointer; }
.tree-row:hover { background: var(--surface-2); }
.tree-row.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tree-row:disabled { color: var(--muted); opacity: .5; cursor: not-allowed; }
.prompt-label { display: grid; gap: 8px; min-width: min(80vw, 380px); }

/* перетаскивание файлов в окно */
.drop {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border: 3px dashed var(--accent);
}
.drop > div { padding: 16px 24px; border-radius: 12px; background: var(--surface); font-size: 18px; font-weight: 600; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 30;
  max-width: min(92vw, 560px); padding: 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); box-shadow: 0 8px 24px rgb(0 0 0 / .2);
}
.toast.error { background: var(--error); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

@media (max-width: 860px) {
  .main { padding: 16px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .item-dialog { overflow-y: auto; }
  .item-body { grid-template-columns: 1fr; }
  .item-preview { height: 50vh; }
  .item-side { max-height: none; }
  .usage { display: none; }
  .topbar { gap: 8px; padding: 0 10px; }
  .brand span { display: none; }
  .share-row { grid-template-columns: 1fr; }
  .link-row { flex-wrap: wrap; }
}

/* ссылки на папки */
.crumbs .shared-note { flex-basis: 100%; margin: 0; border-left-color: var(--accent); }
.folder-mini { background: var(--accent-soft) !important; }
.notice { margin: 0; padding: 8px 12px; border-left: 3px solid var(--warn); background: var(--surface-2); border-radius: 6px; font-size: 13px; }
