:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #dbeafe;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok: #16a34a;
  --warn: #d97706;
  --folder: #3b82f6;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.06), 0 8px 24px rgb(15 23 42 / 0.08);
  --radius: 10px;
  --row-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --ease: cubic-bezier(0.2, 0, 0, 1);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1e293b;
    --border: #253047;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #3b82f6;
    --accent-hover: #60a5fa;
    --accent-soft: #1e3a8a66;
    --danger: #f87171;
    --danger-soft: #7f1d1d55;
    --folder: #60a5fa;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 8px 24px rgb(0 0 0 / 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { display: block; flex: none; }
a { color: var(--accent); }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font-weight: 500; text-decoration: none; white-space: nowrap;
  transition: background-color 120ms var(--ease), border-color 120ms var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex: none;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted);
  text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- вход ---------- */
.login {
  min-height: 100vh; min-height: 100dvh;
  display: grid; place-items: center; padding: 24px 16px;
}
.login-card {
  width: 100%; max-width: 360px; padding: 32px 24px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); text-align: center;
}
.login-card img { border-radius: 14px; }
.login-card h1 { margin: 16px 0 24px; font-size: 22px; }
.field { display: grid; gap: 6px; text-align: left; margin-bottom: 12px; }
.field > span { font-size: 14px; color: var(--muted); font-weight: 500; }
.field input, .field select {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
  font-size: 16px; /* iOS не зумит поле */
}
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.form-error { min-height: 20px; margin: 0 0 12px; color: var(--danger); font-size: 14px; text-align: left; }

/* ---------- шапка ---------- */
.app { min-height: 100vh; padding-bottom: calc(96px + var(--safe-b)); }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: calc(8px + var(--safe-t)) calc(16px + var(--safe-r)) 8px calc(16px + var(--safe-l));
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 600; font-size: 17px; min-width: 0; }
.brand img { border-radius: 6px; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.disk { margin-left: auto; width: 220px; display: grid; gap: 4px; }
.disk-bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.disk-bar span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width 300ms var(--ease); }
.disk.warn .disk-bar span { background: var(--warn); }
.disk.crit .disk-bar span { background: var(--danger); }
.disk-text { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 4px; }

/* ---------- панель инструментов ---------- */
.toolbar { padding: 12px calc(16px + var(--safe-r)) 8px calc(16px + var(--safe-l)); display: grid; gap: 12px; }
.crumbs { display: flex; align-items: center; flex-wrap: wrap; gap: 2px; font-size: 15px; min-height: 32px; }
.crumbs a, .crumbs span.cur {
  padding: 4px 8px; border-radius: 6px; text-decoration: none; color: var(--muted);
  max-width: 40vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.crumbs a:hover { background: var(--surface-2); color: var(--text); }
.crumbs span.cur { color: var(--text); font-weight: 600; }
.crumbs .sep { color: var(--muted); opacity: 0.6; }
.toolbar-row { display: flex; gap: 8px; align-items: center; }
.search { position: relative; flex: 1; min-width: 0; }
.search input {
  width: 100%; height: 40px; padding: 0 12px 0 36px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  font-size: 16px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.search-ico { position: absolute; left: 10px; top: 10px; color: var(--muted); pointer-events: none; }
.sort-select { display: none; height: 40px; padding: 0 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); font-size: 16px; max-width: 150px; }

/* ---------- панель выделения ---------- */
.selbar {
  position: sticky; top: calc(57px + var(--safe-t)); z-index: 15;
  display: flex; align-items: center; gap: 8px;
  margin: 0 calc(16px + var(--safe-r)) 8px calc(16px + var(--safe-l)); padding: 4px 8px 4px 4px;
  background: var(--accent-soft); border-radius: var(--radius);
}
.sel-count { font-weight: 600; white-space: nowrap; }
.selbar-actions { margin-left: auto; display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.selbar .icon-btn { color: var(--text); }

/* ---------- список ---------- */
.list-wrap { padding: 0 calc(16px + var(--safe-r)) 0 calc(16px + var(--safe-l)); }
.list-head, .row {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) 110px 170px 40px;
  align-items: center; gap: 8px;
}
.list-head { height: 36px; border-bottom: 1px solid var(--border); color: var(--muted); font-size: 13px; }
.sort-btn { border: 0; background: none; padding: 4px 0; text-align: left; color: inherit; font-weight: 500; display: inline-flex; align-items: center; gap: 4px; }
.sort-btn:hover { color: var(--text); }
.sort-btn.active { color: var(--text); }
.list-head .col-size, .row .col-size { justify-content: flex-end; text-align: right; }
.chk { display: flex; align-items: center; justify-content: center; width: 40px; height: 40px; cursor: pointer; }
.chk input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.row { min-height: var(--row-h); border-bottom: 1px solid var(--border); }
.row:hover { background: var(--surface-2); }
.row.selected { background: var(--accent-soft); }
.row-main {
  display: flex; align-items: center; gap: 12px; min-width: 0;
  border: 0; background: none; padding: 8px 0; text-align: left; height: 100%;
}
.row-ico { width: 32px; height: 32px; display: grid; place-items: center; color: var(--muted); flex: none; }
.row-ico.dir { color: var(--folder); }
.row-ico.image { color: #db2777; }
.row-ico.video { color: #7c3aed; }
.row-ico.audio { color: #0891b2; }
.row-ico.pdf { color: #dc2626; }
.row-ico.archive { color: #b45309; }
.nm { min-width: 0; display: grid; }
.nm .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nm .sub { display: none; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nm .where { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.col-size, .col-date { color: var(--muted); font-size: 14px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.empty { padding: 64px 16px; text-align: center; color: var(--muted); display: grid; justify-items: center; gap: 12px; }
.empty h2 { margin: 0; font-size: 17px; color: var(--text); }
.empty p { margin: 0; max-width: 320px; }
.list-loading { padding: 32px; text-align: center; color: var(--muted); }
.search-note { padding: 12px 0; color: var(--muted); font-size: 14px; }

/* ---------- меню ---------- */
.menu {
  position: fixed; z-index: 60; min-width: 220px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu button {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 40px; padding: 0 12px; border: 0; border-radius: 6px; background: none; text-align: left;
}
.menu button:hover, .menu button:focus-visible { background: var(--surface-2); outline: none; }
.menu button.danger { color: var(--danger); }
.menu button svg { color: var(--muted); }
.menu button.danger svg { color: var(--danger); }
.menu-title { padding: 8px 12px 4px; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.menu-backdrop { position: fixed; inset: 0; z-index: 55; }

/* ---------- диалоги ---------- */
dialog.dlg {
  width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  padding: 0; border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog.dlg::backdrop { background: rgb(2 6 23 / 0.5); }
.dlg-form { display: flex; flex-direction: column; max-height: calc(100dvh - 34px); }
.dlg-head { padding: 20px 24px 8px; }
.dlg-head h2 { margin: 0; font-size: 18px; overflow-wrap: anywhere; }
.dlg-body { padding: 8px 24px; overflow: auto; }
.dlg-body p { margin: 0 0 12px; overflow-wrap: anywhere; }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 16px 24px 20px; }
.dlg-list { margin: 0 0 12px; padding-left: 20px; color: var(--muted); }
.dlg-list li { overflow-wrap: anywhere; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; inset: 0; }
.chip span { display: inline-flex; align-items: center; min-height: 36px; padding: 0 14px; border: 1px solid var(--border); border-radius: 18px; cursor: pointer; }
.chip input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.link-box { display: flex; gap: 8px; margin-bottom: 16px; }
.link-box input { flex: 1; min-width: 0; min-height: 40px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-size: 14px; }
.share-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.share-item { display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 12px; border: 1px solid var(--border); border-radius: 8px; }
.share-item .info { flex: 1; min-width: 0; }
.share-item .info b { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.share-item .info small { color: var(--muted); }
.section-label { margin: 16px 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); }

.picker-crumbs { display: flex; flex-wrap: wrap; gap: 2px; margin-bottom: 8px; font-size: 14px; }
.picker-crumbs button { border: 0; background: none; padding: 4px 6px; border-radius: 6px; color: var(--muted); }
.picker-crumbs button:last-child { color: var(--text); font-weight: 600; }
.picker-list { border: 1px solid var(--border); border-radius: 8px; height: min(320px, 45dvh); overflow: auto; }
.picker-list button {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 44px; padding: 0 12px;
  border: 0; border-bottom: 1px solid var(--border); background: none; text-align: left;
}
.picker-list button:hover { background: var(--surface-2); }
.picker-list button span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.picker-list button svg { color: var(--folder); }
.picker-list .picker-empty { padding: 24px; text-align: center; color: var(--muted); }

/* ---------- загрузки ---------- */
.uploads {
  position: fixed; z-index: 30; right: calc(16px + var(--safe-r)); bottom: calc(16px + var(--safe-b));
  width: min(420px, calc(100vw - 32px)); max-height: min(60vh, 520px);
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  overflow: hidden;
}
.uploads-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 4px; }
.uploads-title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; border: 0; background: none; padding: 6px 12px; text-align: left; font-weight: 600; }
.uploads-title span:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.uploads-chevron { margin-left: auto; color: var(--muted); transition: transform 160ms var(--ease); }
.uploads.collapsed .uploads-chevron { transform: rotate(180deg); }
.uploads.collapsed .uploads-list { display: none; }
.progress { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 2px; transition: width 200ms linear; }
.progress.total { margin: 0 16px 8px; }
.uploads-list { list-style: none; margin: 0; padding: 0 0 4px; overflow: auto; border-top: 1px solid var(--border); }
.up { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 8px; padding: 10px 8px 10px 16px; border-bottom: 1px solid var(--border); }
.up:last-child { border-bottom: 0; }
.up-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; align-self: center; }
.up-actions { display: flex; grid-row: span 3; align-items: center; }
.up-actions .icon-btn { width: 36px; height: 36px; }
.up .progress { grid-column: 1; }
.up-meta { grid-column: 1; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.up.done .progress span { background: var(--ok); }
.up.error .progress span, .up.error .up-meta { background: none; }
.up.error .up-meta { color: var(--danger); }
.up.error .progress span { background: var(--danger); }
.up.paused .progress span, .up.stale .progress span { background: var(--muted); }

/* ---------- drag-and-drop ---------- */
.drop { position: fixed; inset: 0; z-index: 70; display: grid; place-items: center; background: rgb(37 99 235 / 0.12); padding: 16px; pointer-events: none; }
.drop-inner {
  display: grid; justify-items: center; gap: 12px; padding: 40px 48px;
  border: 2px dashed var(--accent); border-radius: 16px; background: var(--surface); color: var(--accent);
  font-weight: 600; font-size: 17px; box-shadow: var(--shadow);
}

/* ---------- просмотр ---------- */
.viewer { position: fixed; inset: 0; z-index: 80; background: #000; color: #fff; display: flex; flex-direction: column; }
.viewer-bar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(6px + var(--safe-t)) calc(8px + var(--safe-r)) 6px calc(8px + var(--safe-l));
  background: rgb(0 0 0 / 0.6);
}
.viewer .icon-btn { color: #e2e8f0; }
.viewer .icon-btn:hover { background: rgb(255 255 255 / 0.12); color: #fff; }
.viewer-title { flex: 1; min-width: 0; display: grid; }
.viewer-title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.viewer-title small { color: #94a3b8; font-size: 12px; }
.viewer-stage { flex: 1; min-height: 0; display: grid; place-items: center; position: relative; padding-bottom: var(--safe-b); }
.viewer-stage img, .viewer-stage video { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-stage video { width: 100%; height: 100%; }
.viewer-stage audio { width: min(560px, calc(100% - 32px)); }
.viewer-stage iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.viewer-stage pre {
  width: 100%; height: 100%; margin: 0; padding: 16px calc(16px + var(--safe-r)) 16px calc(16px + var(--safe-l));
  overflow: auto; background: #0b1120; color: #e2e8f0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.viewer-msg { display: grid; justify-items: center; gap: 16px; padding: 24px; text-align: center; color: #cbd5e1; }
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 48px; height: 64px; border: 0; border-radius: 8px;
  background: rgb(0 0 0 / 0.35); color: #fff; display: grid; place-items: center;
}
.viewer-nav:hover { background: rgb(0 0 0 / 0.6); }
.viewer-nav.prev { left: calc(8px + var(--safe-l)); }
.viewer-nav.next { right: calc(8px + var(--safe-r)); }
.viewer-pdf-actions { position: absolute; bottom: calc(16px + var(--safe-b)); left: 50%; transform: translateX(-50%); }

/* ---------- уведомления ---------- */
.toasts {
  position: fixed; z-index: 90; left: 50%; transform: translateX(-50%);
  top: calc(12px + var(--safe-t)); display: grid; gap: 8px; width: min(420px, calc(100vw - 32px)); pointer-events: none;
}
.toast {
  padding: 12px 16px; border-radius: 10px; background: #0f172a; color: #f8fafc; box-shadow: var(--shadow);
  font-size: 14px; pointer-events: auto; animation: toast-in 160ms var(--ease);
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* ---------- телефон ---------- */
@media (max-width: 720px) {
  .topbar { gap: 8px; }
  .brand span { display: none; }
  .disk { width: auto; flex: 1; margin-left: 4px; }
  .list-head { display: none; }
  .row { grid-template-columns: 40px minmax(0, 1fr) 40px; gap: 4px; }
  .row .col-size, .row .col-date { display: none; }
  .nm .sub { display: block; }
  .sort-select { display: block; }
  .toolbar-row { flex-wrap: wrap; }
  .search { flex-basis: 100%; }
  .toolbar-row .btn { flex: 1; }
  .sort-select { flex: 1; max-width: none; }
  .toolbar-row .btn .lbl { display: inline; }
  .crumbs a, .crumbs span.cur { max-width: 60vw; }
  .selbar { top: calc(57px + var(--safe-t)); gap: 4px; padding-right: 4px; }
  .selbar-actions { flex-wrap: nowrap; gap: 0; }
  .selbar .icon-btn { width: 36px; }
  .uploads { left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r)); bottom: calc(8px + var(--safe-b)); width: auto; }
  .menu {
    left: 8px !important; right: 8px; top: auto !important;
    bottom: calc(8px + var(--safe-b)); min-width: 0; padding: 8px; border-radius: 14px;
  }
  .menu button { min-height: 48px; font-size: 16px; }
  .menu-backdrop { background: rgb(2 6 23 / 0.4); }
  .viewer-nav { display: none; }
  dialog.dlg { margin: auto auto calc(8px + var(--safe-b)); width: calc(100vw - 16px); }
}
@media (max-width: 420px) {
  .toolbar-row .btn .lbl { display: none; }
  .toolbar-row .btn { flex: 0 0 48px; padding: 0; }
}

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