:root {
  color-scheme: light dark;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --bg: #ffffff;
  --ink: #15121c;
  --muted: #74707f;
  --faint: #b4b0bf;
  --line: #e8e5ee;
  --field: #f2f0f6;
  --surface: #ffffff;
  --accent: #7a3cf0;
  --accent-pressed: #6629d6;
  --accent-text: #6a2fdc;
  --danger: #c4302b;
  --scrim: rgba(21, 18, 28, .46);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --ink: #f5f2fa;
    --muted: #9b96a8;
    --faint: #5d5969;
    --line: #25222d;
    --field: #1b1922;
    --surface: #17151d;
    --accent: #8650f5;
    --accent-pressed: #7440e0;
    --accent-text: #a984ff;
    --danger: #ff6b61;
    --scrim: rgba(0, 0, 0, .62);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 17px/1.35 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button, input { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* A dialog moves focus into itself when it opens; the landing spot is its body, not the first button,
   so iOS does not draw a focus ring around a row nobody tapped. */
.dialog-body:focus { outline: none; }

/* ---------- категории ---------- */

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(18px);
  backdrop-filter: saturate(1.6) blur(18px);
}

.rail {
  display: flex;
  gap: 22px;
  overflow-x: auto;
  padding: 14px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  scroll-padding-inline: 16px;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.rail::-webkit-scrollbar { display: none; }

.rail button {
  flex: none;
  padding: 4px 0;
  border: 0;
  background: none;
  color: var(--faint);
  font-size: 28px;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -.022em;
  white-space: nowrap;
}
.rail button[aria-current="true"] { color: var(--ink); }
.rail .count {
  margin-left: 5px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0;
  vertical-align: top;
  position: relative;
  top: 4px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* ---------- лента ---------- */

.feed {
  display: grid;
  gap: 26px;
  padding: 6px max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 118px) max(16px, env(safe-area-inset-left));
}

@media (min-width: 700px) {
  .feed {
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    max-width: 1180px;
    margin: 0 auto;
  }
  .rail { max-width: 1180px; margin: 0 auto; }
}

.video { min-width: 0; }

.thumb {
  position: relative;
  display: block;
  aspect-ratio: 16 / 9;
  border-radius: 14px;
  overflow: hidden;
  background: var(--field);
}
.thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.thumb:active { opacity: .82; }

.dur {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 4px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .78);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.row {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin-top: 10px;
}
.text { flex: 1; min-width: 0; }

.title {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.28;
  letter-spacing: -.01em;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-wrap: pretty;
}

.meta {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--muted);
  min-width: 0;
}
.meta .channel {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.meta button {
  flex: none;
  padding: 0;
  border: 0;
  background: none;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent-text);
}
.meta .cat {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  max-width: 60%;
  padding: 3px 8px 3px 9px;
  margin: -3px 0;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}
.meta .cat:active { background: color-mix(in srgb, var(--accent) 24%, transparent); }
.meta .cat svg { flex: none; opacity: .8; }
.meta .working { animation: breathe 1.6s ease-in-out infinite; }
.meta .failed { color: var(--danger); }

@keyframes breathe { 50% { opacity: .45; } }

.more {
  flex: none;
  width: 40px;
  height: 40px;
  margin: -8px -10px 0 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  display: grid;
  place-items: center;
}
.more:active { background: var(--field); }

.empty {
  margin: 18vh auto 0;
  max-width: 300px;
  text-align: center;
  color: var(--muted);
}
.empty strong {
  display: block;
  margin-bottom: 6px;
  color: var(--ink);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -.015em;
}

/* ---------- кнопка добавления ---------- */

.fab {
  position: fixed;
  right: calc(env(safe-area-inset-right) + 20px);
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  z-index: 6;
  width: 66px;
  height: 66px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: 0 10px 28px -6px color-mix(in srgb, var(--accent) 70%, transparent), 0 2px 6px rgba(0, 0, 0, .18);
  transition: transform .12s ease, background-color .12s ease;
}
.fab:active { transform: scale(.94); background: var(--accent-pressed); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 104px);
  z-index: 7;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 11px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  font-weight: 500;
  text-align: center;
  animation: rise .2s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- общие контролы ---------- */

.primary {
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
}
.primary:active { background: var(--accent-pressed); }
.primary:disabled { background: var(--field); color: var(--faint); cursor: default; }

.icon {
  width: 40px;
  height: 40px;
  margin: -8px -10px -8px 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  display: grid;
  place-items: center;
}
.icon:active { background: var(--field); }

.error { margin: 0; color: var(--danger); font-size: 15px; }

/* ---------- окно добавления ---------- */

dialog {
  border: 0;
  padding: 0;
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: var(--scrim); }

.add {
  position: fixed;
  inset: auto;
  left: 50%;
  top: calc(var(--vv-top, 0px) + var(--vv-h, 100vh) / 2);
  transform: translate(-50%, -50%);
  width: min(calc(100vw - 32px), 420px);
  height: min(calc(var(--vv-h, 100vh) * .5), 460px);
  min-height: min(350px, calc(var(--vv-h, 100vh) - 24px));
  max-height: calc(var(--vv-h, 100vh) - 24px);
  margin: 0;
  border-radius: 26px;
  box-shadow: 0 24px 70px -12px rgba(0, 0, 0, .45);
}
.add[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translate(-50%, -50%) scale(.96); } }

.add form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  height: 100%;
  padding: 20px;
}

.add-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.add-head h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -.02em;
}

.field {
  display: flex;
  align-items: center;
  border-radius: 13px;
  background: var(--field);
}
.field:focus-within { outline: 2px solid var(--accent); outline-offset: 0; }
.field input {
  flex: 1;
  min-width: 0;
  height: 50px;
  padding: 0 0 0 14px;
  border: 0;
  background: none;
  font-size: 17px;
  outline: none;
}
.field input::placeholder { color: var(--muted); }
.field button {
  flex: none;
  height: 50px;
  padding: 0 14px;
  border: 0;
  background: none;
  color: var(--accent-text);
  font-size: 16px;
  font-weight: 600;
}

.add-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  font-size: 15px;
  color: var(--muted);
}
.add-body p { margin: 0; text-wrap: pretty; }
.add-body .error { font-size: 15px; }

.preview { display: flex; gap: 12px; align-items: flex-start; }
.preview img {
  flex: none;
  width: 128px;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 9px;
  background: var(--field);
}
.preview .p-title {
  color: var(--ink);
  font-weight: 600;
  line-height: 1.28;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}
.preview .p-channel { margin-top: 3px; font-size: 14px; }

/* ---------- лист действий ---------- */

.sheet {
  position: fixed;
  inset: auto 0 var(--kb, 0px) 0;
  width: 100%;
  max-width: 520px;
  max-height: calc(var(--vv-h, 100vh) * .76);
  margin: 0 auto;
  padding-bottom: calc(env(safe-area-inset-bottom) + 10px);
  border-radius: 22px 22px 0 0;
  overflow-y: auto;
}
.sheet[open] { animation: slide .2s ease-out; }
@keyframes slide { from { transform: translateY(24px); opacity: 0; } }

.sheet-title {
  margin: 0;
  padding: 18px 20px 6px;
  font-size: 15px;
  color: var(--muted);
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.sheet h3 {
  margin: 0;
  padding: 12px 20px 4px;
  font-size: 15px;
  font-weight: 600;
}
.sheet-list { margin: 0; padding: 0; list-style: none; }
.sheet-list button,
.sheet-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 50px;
  padding: 0 20px;
  border: 0;
  background: none;
  font-size: 17px;
  text-align: left;
}
.sheet-list button:active,
.sheet-action:active { background: var(--field); }
.sheet-list [aria-checked="true"] { font-weight: 600; }
.sheet-list [aria-checked="true"]::after {
  content: "";
  width: 7px;
  height: 13px;
  margin-right: 6px;
  border: solid var(--accent-text);
  border-width: 0 2.4px 2.4px 0;
  transform: rotate(45deg) translateY(-2px);
}
.sheet-new { display: flex; gap: 10px; padding: 4px 20px 8px; }
.sheet-new input {
  flex: 1;
  min-width: 0;
  height: 46px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: var(--field);
  font-size: 17px;
}
.sheet-new button {
  flex: none;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}
.sheet-sep { height: 1px; margin: 6px 20px; background: var(--line); }
.sheet-action.danger { color: var(--danger); }
.sheet-action svg { color: var(--muted); }
.sheet-action.accent { color: var(--accent-text); }

/* ---------- обновление лентой вниз ---------- */

.feed { will-change: transform; }

.ptr {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 58px);
  z-index: 4;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--accent-text);
  box-shadow: 0 3px 12px rgba(0, 0, 0, .18);
  display: grid;
  place-items: center;
  opacity: 0;
  transform: translate(-50%, -44px);
  pointer-events: none;
}
.ptr svg { grid-area: 1 / 1; transition: transform .2s, opacity .15s; }
.ptr .ptr-spinner { opacity: 0; }
.ptr.armed .ptr-arrow { transform: rotate(180deg); }
.ptr.spinning .ptr-arrow { opacity: 0; }
.ptr.spinning .ptr-spinner { opacity: 1; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- вход ---------- */

.login {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 360px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 14vh) 24px 24px;
}
.login h1 {
  margin: 0 0 10px;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -.025em;
}
.login label {
  display: grid;
  gap: 6px;
  font-size: 15px;
  color: var(--muted);
}
.login input {
  height: 50px;
  padding: 0 14px;
  border: 0;
  border-radius: 13px;
  background: var(--field);
  color: var(--ink);
  font-size: 17px;
}
.login .primary { margin-top: 6px; }

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