@layer reset, tokens, base, components, pages;

@view-transition { navigation: auto; }

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  img, video { display: block; max-width: 100%; }
  button, input, select, textarea { font: inherit; color: inherit; }
  table { border-collapse: collapse; width: 100%; }
}

@layer tokens {
  :root {
    color-scheme: dark;
    --bg: oklch(15% 0.012 275);
    --surface: oklch(20% 0.016 275);
    --surface-2: oklch(26% 0.02 275);
    --text: oklch(96% 0.006 275);
    --muted: oklch(72% 0.02 275);
    --brand: oklch(63% 0.235 20);
    --brand-hover: oklch(69% 0.235 20);
    --ok: oklch(72% 0.17 155);
    --warn: oklch(80% 0.16 80);
    --bad: oklch(66% 0.22 25);
    --line: color-mix(in oklch, white 12%, transparent);
    --radius: 14px;
    --gutter: clamp(1rem, 4vw, 3.5rem);
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
    --font: "Inter var", Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  }
}

@layer base {
  html { scrollbar-width: thin; scrollbar-color: var(--surface-2) transparent; }
  body {
    min-height: 100dvh;
    display: grid;
    grid-template-rows: auto 1fr auto;
    background: var(--bg);
    color: var(--text);
    font: 400 1rem/1.55 var(--font);
    -webkit-font-smoothing: antialiased;
  }
  h1, h2, h3 { line-height: 1.15; text-wrap: balance; }
  h1 { font-size: clamp(1.8rem, 1.2rem + 2.5vw, 3.2rem); font-weight: 800; letter-spacing: -0.02em; }
  h2 { font-size: 1.25rem; font-weight: 700; }
  p { text-wrap: pretty; }
  a { color: inherit; text-decoration-color: color-mix(in oklch, currentColor 35%, transparent); text-underline-offset: 3px; }
  a:hover { text-decoration-color: currentColor; }
  code { font: 0.85em ui-monospace, monospace; background: var(--surface-2); padding: 0.1em 0.4em; border-radius: 6px; }
  :focus-visible { outline: 2px solid var(--brand); outline-offset: 3px; border-radius: 6px; }
  main { padding-block-end: 3rem; }
  .muted { color: var(--muted); }
  small { color: var(--muted); }
}

@layer components {
  /* ---------- nav ---------- */
  .nav {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 1.25rem;
    padding: 0.8rem var(--gutter);
    background: linear-gradient(to bottom, color-mix(in oklch, var(--bg) 92%, transparent), color-mix(in oklch, var(--bg) 55%, transparent));
    backdrop-filter: blur(14px) saturate(1.4);
    border-block-end: 1px solid var(--line);

    .logo { font-weight: 900; font-size: 1.35rem; letter-spacing: -0.03em; color: var(--brand); text-decoration: none; }
    nav { display: flex; gap: 1rem; }
    nav a { text-decoration: none; color: var(--muted); font-weight: 600; padding-block: 0.25rem;
      &:hover, &[aria-current] { color: var(--text); }
      &[aria-current] { box-shadow: 0 2px 0 var(--brand); } }
    .search { margin-inline-start: auto; }
    .search input { width: clamp(8rem, 22vw, 18rem); padding: 0.5rem 0.9rem; border-radius: 999px; border: 1px solid var(--line); background: color-mix(in oklch, var(--surface) 80%, transparent); transition: width 0.25s var(--ease), border-color 0.2s;
      &:focus { width: clamp(10rem, 32vw, 26rem); border-color: var(--brand); outline: none; } }
  }

  .menu { position: relative;
    summary { list-style: none; cursor: pointer; &::-webkit-details-marker { display: none; } }
    .avatar { display: grid; place-items: center; inline-size: 2.2rem; block-size: 2.2rem; border-radius: 50%; background: var(--brand); font-weight: 800; }
    .menu-pop { position: absolute; inset-inline-end: 0; margin-block-start: 0.6rem; min-inline-size: 15rem; padding: 1rem; display: grid; gap: 0.6rem;
      background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 20px 50px #0008; }
    .menu-pop a { text-decoration: none; }
  }
  .linklike { background: none; border: 0; padding: 0; cursor: pointer; color: var(--muted); &:hover { color: var(--text); } }

  /* ---------- buttons / badges ---------- */
  .btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem;
    padding: 0.6rem 1.15rem; border-radius: 10px; border: 1px solid var(--line);
    background: var(--surface-2); font-weight: 650; text-decoration: none; cursor: pointer;
    transition: background 0.2s, transform 0.15s var(--ease);
    &:hover { background: color-mix(in oklch, var(--surface-2) 80%, white); }
    &:active { transform: scale(0.97); }
    &:disabled { opacity: 0.5; cursor: not-allowed; }
    &.primary { background: var(--brand); border-color: transparent; color: white; &:hover { background: var(--brand-hover); } }
    &.danger { color: var(--bad); &:hover { background: color-mix(in oklch, var(--bad) 20%, var(--surface-2)); } }
    &.big { font-size: 1.15rem; padding: 0.8rem 1.9rem; }
    &.small { padding: 0.3rem 0.7rem; font-size: 0.85rem; }
  }
  .badge { display: inline-block; padding: 0.1rem 0.6rem; border-radius: 999px; font-size: 0.75rem; font-weight: 700; background: var(--surface-2); color: var(--muted);
    &.ok { background: color-mix(in oklch, var(--ok) 22%, transparent); color: var(--ok); }
    &.warn { background: color-mix(in oklch, var(--warn) 22%, transparent); color: var(--warn); } }

  /* ---------- forms ---------- */
  .stack { display: grid; gap: 1rem; }
  label { display: grid; gap: 0.35rem; font-weight: 600; font-size: 0.9rem; color: var(--muted); }
  input:not([type=checkbox]):not([type=file]):not([type=range]), select, textarea {
    padding: 0.65rem 0.85rem; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); inline-size: 100%;
    &:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px color-mix(in oklch, var(--brand) 25%, transparent); }
  }
  textarea { field-sizing: content; min-block-size: 6rem; resize: vertical; }
  .check { display: flex; align-items: center; gap: 0.6rem; color: var(--text); accent-color: var(--brand); }
  .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 1rem; }
  fieldset.upload { border: 1px dashed var(--line); border-radius: var(--radius); padding: 1rem; display: grid; gap: 0.6rem;
    &:has(input:disabled) { opacity: 0.6; }
    legend { padding-inline: 0.5rem; font-weight: 700; }
    progress { inline-size: 100%; block-size: 0.5rem; accent-color: var(--brand); }
    .status.ok { color: var(--ok); } .status.bad { color: var(--bad); } }

  .alert { padding: 0.8rem 1rem; border-radius: 10px; background: color-mix(in oklch, var(--bad) 18%, var(--surface)); border: 1px solid color-mix(in oklch, var(--bad) 50%, transparent); }
  .toast { position: fixed; inset-block-end: 1.25rem; inset-inline: 0; margin-inline: auto; inline-size: fit-content; max-inline-size: 90vw; z-index: 50;
    padding: 0.8rem 1.4rem; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); box-shadow: 0 10px 40px #0009;
    animation: toast-out 0.4s 4s forwards;
    @starting-style { opacity: 0; translate: 0 1rem; }
    transition: opacity 0.3s, translate 0.3s var(--ease);
    &.warn { border-color: var(--warn); } &.ok { border-color: var(--ok); } }
  @keyframes toast-out { to { opacity: 0; translate: 0 1rem; visibility: hidden; } }

  /* ---------- shelves & cards ---------- */
  .shelf { padding-inline: var(--gutter); margin-block-start: 2rem; display: grid; gap: 0.8rem; }
  .row-wrap { position: relative; margin-inline: calc(var(--gutter) * -1); }
  .row { display: grid; grid-auto-flow: column; grid-auto-columns: clamp(9rem, 16vw, 13rem); gap: 0.9rem;
    overflow-x: auto; scroll-snap-type: x proximity; scroll-padding-inline: var(--gutter); padding: 0.4rem var(--gutter) 1rem; scrollbar-width: none; overscroll-behavior-x: contain; }
  .row > * { scroll-snap-align: start; }
  .row-btn { position: absolute; inset-block: 0 1rem; z-index: 5; inline-size: var(--gutter); min-inline-size: 2.5rem; border: 0; cursor: pointer; font-size: 2rem; color: white; opacity: 0; transition: opacity 0.2s;
    background: linear-gradient(to right, color-mix(in oklch, var(--bg) 85%, transparent), transparent);
    &.prev { inset-inline-start: 0; }
    &.next { inset-inline-end: 0; background: linear-gradient(to left, color-mix(in oklch, var(--bg) 85%, transparent), transparent); }
  }
  .row-wrap:hover .row-btn, .row-btn:focus-visible { opacity: 1; }
  @media (hover: none) { .row-btn { display: none; } }

  .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)); gap: 1.2rem; }

  .card { display: grid; gap: 0.5rem; text-decoration: none; container-type: inline-size;
    .poster { position: relative; aspect-ratio: 2 / 3; border-radius: var(--radius); overflow: hidden; background: var(--surface);
      transition: transform 0.3s var(--ease), box-shadow 0.3s; }
    .poster img { inline-size: 100%; block-size: 100%; object-fit: cover; }
    &:hover .poster, &:focus-visible .poster { transform: translateY(-4px) scale(1.04); box-shadow: 0 18px 40px #000a; }
    .card-info { display: grid; line-height: 1.3; }
    .card-info strong { font-size: 0.95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .bar { position: absolute; inset: auto 0 0 0; block-size: 4px; background: #fff3;
      &::after { content: ""; position: absolute; inset: 0 auto 0 0; inline-size: var(--p); background: var(--brand); } }
    @container (max-width: 9.5rem) { .card-info small { display: none; } }
  }
  .ph { inline-size: 100%; block-size: 100%; display: grid; place-items: end start; padding: 0.9rem; font-weight: 800; line-height: 1.1; text-wrap: balance;
    background: linear-gradient(160deg, oklch(45% 0.17 var(--h)), oklch(22% 0.07 calc(var(--h) + 40))); }

  .chips { display: flex; gap: 0.5rem; flex-wrap: wrap; padding: 1.5rem var(--gutter) 0;
    a { display: inline-flex; gap: 0.4rem; padding: 0.35rem 0.9rem; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); text-decoration: none; font-weight: 600; font-size: 0.9rem;
      &:hover { background: var(--surface-2); } } }

  /* ---------- tables ---------- */
  .table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  th, td { text-align: start; padding: 0.7rem 1rem; border-block-end: 1px solid var(--line); vertical-align: middle; }
  th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
  tr:last-child td { border-block-end: 0; }
  .row-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: flex-end; }
  .row-actions form { display: contents; }
  .inline-reset { display: flex !important; gap: 0.3rem; input { inline-size: 9rem; padding: 0.3rem 0.6rem; } }
}

@layer pages {
  .hero { position: relative; isolation: isolate; min-block-size: min(72vh, 40rem); display: grid; align-items: end; overflow: hidden;
    background: linear-gradient(135deg, oklch(40% 0.16 var(--h)), oklch(18% 0.05 calc(var(--h) + 50)));
    &::after { content: ""; position: absolute; inset: 0; z-index: -1; background:
      linear-gradient(to top, var(--bg) 4%, transparent 60%), linear-gradient(to right, color-mix(in oklch, var(--bg) 80%, transparent), transparent 70%); }
    .hero-bg { position: absolute; inset: 0; z-index: -2; inline-size: 100%; block-size: 100%; object-fit: cover; object-position: center 20%; filter: saturate(1.1); }
    .hero-copy { padding: 4rem var(--gutter) 3rem; max-inline-size: 46rem; display: grid; gap: 0.9rem; justify-items: start; }
    .meta { color: var(--muted); font-weight: 600; }
    .lede { font-size: 1.1rem; color: color-mix(in oklch, var(--text) 85%, transparent); display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
  }

  .watch { display: grid; gap: 1.5rem;
    video, .player-missing { inline-size: 100%; max-block-size: 78vh; aspect-ratio: 16 / 9; background: #000; }
    .player-missing { display: grid; place-items: center; text-align: center; padding: 2rem; color: var(--muted);
      background: linear-gradient(135deg, oklch(30% 0.1 var(--h)), oklch(15% 0.03 var(--h))); }
    .watch-info { padding-inline: var(--gutter); display: grid; gap: 0.7rem; max-inline-size: 60rem; }
    .meta { display: flex; gap: 0.7rem; align-items: center; color: var(--muted); font-weight: 600; }
    .lede { color: color-mix(in oklch, var(--text) 85%, transparent); }
  }

  .auth { max-inline-size: 26rem; margin: clamp(2rem, 10vh, 6rem) auto; padding: 2rem; display: grid; gap: 1.2rem;
    background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: 0 30px 80px #0007; }

  .empty-state { display: grid; justify-items: center; gap: 1rem; text-align: center; padding: 8rem var(--gutter); }
  .empty { padding: 2rem 0; color: var(--muted); }

  .admin { padding: 2rem var(--gutter); display: grid; gap: 1.5rem; max-inline-size: 78rem; margin-inline: auto; inline-size: 100%;
    &.narrow { max-inline-size: 46rem; }
    .admin-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
    .actions { display: flex; gap: 0.6rem; }
    .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 1rem; }
    .stat { padding: 1rem 1.2rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); display: grid;
      strong { font-size: 2rem; line-height: 1; } span { color: var(--muted); } }
    .cols-form { grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); align-items: end; }
  }

  .foot { padding: 1.5rem var(--gutter); color: var(--muted); font-size: 0.85rem; border-block-start: 1px solid var(--line); }

  @media (max-width: 40rem) {
    .nav { flex-wrap: wrap; gap: 0.6rem 1rem; .search { order: 5; flex: 1 1 100%; margin: 0; } .search input { inline-size: 100%; } }
    .hero { min-block-size: 60vh; }
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
  }
}
