:root {
  color-scheme: dark;
  --canvas: #18131f;
  --panel: #241c2d;
  --line: #3d3048;
  --text: #f3ebf8;
  --muted: #b5a7bf;
  --accent: #d9b9ff;
  --accent-ink: #281731;
  --positive: #9ee6bd;
  --caution: #f4ce9c;
  --negative: #f7a9b1;
  --app-width: 46rem;
  --safe-top: max(env(safe-area-inset-top, 0px), var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-content-safe-area-inset-bottom, 0px));
  font: 16px/1.45 system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(ellipse at top, #332140 0%, var(--canvas) 34rem);
  color: var(--text);
}

h1, h2, h3, p, dl, dd { margin: 0; }
button, input { font: inherit; }
a, button { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .55; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.shell {
  width: min(100%, var(--app-width));
  margin-inline: auto;
  padding-top: var(--safe-top);
}

.app-header {
  display: flex;
  align-items: center;
  gap: .7rem;
  min-height: 3.5rem;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--line);
}

.app-header h1 { font-size: 1rem; font-weight: 650; letter-spacing: .01em; }

.brand-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 1.9rem;
  height: 1.9rem;
  border-radius: .6rem;
  background: var(--accent);
  color: var(--accent-ink);
}

.brand-icon::before {
  content: '';
  margin-left: .1rem;
  border-block: .34rem solid transparent;
  border-left: .54rem solid currentColor;
}

.content { padding: 1.25rem 1rem calc(6rem + var(--safe-bottom)); }
.section-heading { display: flex; justify-content: space-between; align-items: center; gap: .75rem; margin-bottom: .9rem; }
h2 { font-size: clamp(1.4rem, 5vw, 1.75rem); font-weight: 650; letter-spacing: -.025em; }

.button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  min-height: 2.65rem;
  padding: .48rem .8rem;
  border: 1px solid transparent;
  border-radius: .7rem;
  color: var(--text);
  background: transparent;
  font-size: .875rem;
  font-weight: 650;
  text-decoration: none;
  white-space: nowrap;
}

.primary { color: var(--accent-ink); background: var(--accent); }
.primary:hover:not(:disabled) { background: #e6d0ff; }
.subtle { background: #31263a; border-color: var(--line); }
.subtle:hover:not(:disabled) { background: #3a2c45; }

.status { color: var(--muted); font-size: .875rem; margin-block: .6rem; }
.status:empty { display: none; }
.status[data-kind='error'] { color: var(--negative); }
.status[data-kind='success'] { color: var(--positive); }

.download-summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: .65rem;
  margin-bottom: .85rem;
}

.download-summary > div {
  min-width: 0;
  padding: .72rem .85rem;
  border: 1px solid var(--line);
  border-radius: .85rem;
  background: var(--panel);
}

.download-summary dt { color: var(--muted); font-size: .78rem; }
.download-summary dd { margin-top: .15rem; font-size: 1.22rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.cards { display: grid; gap: .65rem; padding: 0; margin: 0; list-style: none; }
.download-card, .movie-card { min-width: 0; border: 1px solid var(--line); border-radius: .85rem; background: var(--panel); }
.download-card { padding: .8rem .9rem; }
.download-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; }
.download-heading h3, .movie-card h3 { min-width: 0; font-size: .94rem; font-weight: 650; overflow-wrap: anywhere; }
.download-percent { flex: none; color: var(--accent); font-size: .9rem; font-weight: 600; font-variant-numeric: tabular-nums; }

progress {
  display: block;
  width: 100%;
  height: .34rem;
  margin-block: .55rem;
  overflow: hidden;
  appearance: none;
  border: 0;
  border-radius: 999px;
  background: #4a3c54;
}

progress::-webkit-progress-bar { background: #4a3c54; }
progress::-webkit-progress-value { border-radius: 999px; background: var(--accent); }
progress::-moz-progress-bar { border-radius: 999px; background: var(--accent); }
.download-card[data-done='true'] progress::-webkit-progress-value { background: var(--positive); }
.download-card[data-done='true'] progress::-moz-progress-bar { background: var(--positive); }

.download-details { display: flex; flex-wrap: wrap; align-items: center; column-gap: .65rem; row-gap: .15rem; color: var(--muted); font-size: .79rem; }
.download-state { font-weight: 600; }
.download-state[data-kind='active'] { color: var(--accent); }
.download-state[data-kind='complete'] { color: var(--positive); }
.download-state[data-kind='waiting'] { color: var(--caution); }
.download-state[data-kind='error'] { color: var(--negative); }

.search-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .55rem; margin-bottom: .75rem; }
input[type='search'] {
  width: 100%;
  min-width: 0;
  min-height: 2.7rem;
  padding: .55rem .75rem;
  outline-offset: 2px;
  border: 1px solid var(--line);
  border-radius: .7rem;
  background: var(--panel);
  color: var(--text);
}

input::placeholder { color: var(--muted); }
.library-query { margin-bottom: .7rem; }

.movie-card {
  display: grid;
  grid-template-columns: 3.75rem minmax(0, 1fr);
  align-items: start;
  gap: .75rem;
  padding: .7rem;
}

.poster {
  display: grid;
  place-items: center;
  width: 100%;
  overflow: hidden;
  aspect-ratio: 2 / 3;
  border-radius: .5rem;
  color: #c4a7de;
  background: linear-gradient(145deg, #493454, #2f2339);
}

.poster > * { grid-area: 1 / 1; }
.poster img { display: block; width: 100%; height: 100%; object-fit: cover; }
.movie-copy { min-width: 0; }
.movie-meta { margin-top: .12rem; color: var(--muted); font-size: .8rem; }
.movie-overview { display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 3; margin-top: .25rem; color: var(--muted); font-size: .8rem; overflow-wrap: anywhere; }
.movie-actions { display: flex; align-items: center; gap: .5rem; margin-top: .45rem; }
.movie-state { font-size: .82rem; color: var(--muted); }
.movie-state.ready { color: var(--positive); }
.more { display: flex; margin: .85rem auto 0; }

.tab-bar {
  position: fixed;
  z-index: 2;
  left: 50%;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: .25rem;
  width: min(100%, var(--app-width));
  padding: .45rem .65rem calc(.45rem + var(--safe-bottom));
  transform: translateX(-50%);
  border-top: 1px solid var(--line);
  background: #221a2a;
}

.tab-bar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .23rem;
  min-width: 0;
  min-height: 3.15rem;
  padding: .35rem .1rem;
  border: 0;
  border-radius: .65rem;
  background: transparent;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 600;
}

.tab-bar button[aria-selected='true'] { color: var(--accent); background: #39294a; }
.tab-icon { position: relative; display: inline-block; width: 1.05rem; height: 1.05rem; flex: none; }
.tab-icon::before, .tab-icon::after { position: absolute; content: ''; }
.download-icon { border-bottom: .1rem solid currentColor; }
.download-icon::before { left: 50%; top: 0; width: .1rem; height: .65rem; background: currentColor; transform: translateX(-50%); }
.download-icon::after { left: 50%; top: .25rem; width: .44rem; height: .44rem; transform: translateX(-50%) rotate(45deg); border: solid currentColor; border-width: 0 .1rem .1rem 0; }
.search-icon::before { top: .04rem; left: .04rem; width: .65rem; height: .65rem; border: .105rem solid currentColor; border-radius: 50%; }
.search-icon::after { top: .62rem; left: .58rem; width: .46rem; height: .105rem; transform: rotate(45deg); transform-origin: left; background: currentColor; }
.library-icon { border: .1rem solid currentColor; border-radius: .2rem; }
.library-icon::before { top: .2rem; bottom: .2rem; left: .2rem; border-left: .1rem solid currentColor; }
.library-icon::after { top: .23rem; right: .17rem; left: .43rem; height: .1rem; background: currentColor; box-shadow: 0 .25rem currentColor; }

.access { padding: 2rem 1rem; }
.access p { margin: .55rem 0 1rem; color: var(--muted); }

dialog {
  width: min(calc(100% - 1.5rem), 24rem);
  max-height: min(90dvh, 35rem);
  overflow-y: auto;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: #2b2035;
  color: var(--text);
}

dialog::backdrop { background: #130d19b5; }
.order-film { margin-top: .65rem; overflow-wrap: anywhere; font-weight: 650; }
.order-overview { margin-top: .35rem; color: var(--muted); font-size: .88rem; overflow-wrap: anywhere; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .55rem; margin-top: 1rem; }

@media (min-width: 42rem) {
  .app-header, .content, .access { padding-inline: 1.5rem; }
  #library-results { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
