/* ============================================================
   kyu — Apple Human Interface Guidelines
   System colors, SF-style type scale, native component idioms
   (nav bar large-title collapse, segmented control, sheets,
   action sheet, grouped lists). Light + dark via prefers-color-scheme.
   ============================================================ */

:root {
  /* System colors — light */
  --system-background: #ffffff;
  --secondary-system-background: #f2f2f7;
  --grouped-background: #f2f2f7;
  --secondary-grouped-background: #ffffff;

  --label: #000000;
  --secondary-label: rgba(60, 60, 67, 0.6);
  --tertiary-label: rgba(60, 60, 67, 0.3);
  --quaternary-label: rgba(60, 60, 67, 0.18);

  --separator: rgba(60, 60, 67, 0.29);
  --opaque-separator: #c6c6c8;

  --fill: rgba(120, 120, 128, 0.2);
  --fill-secondary: rgba(120, 120, 128, 0.16);
  --fill-tertiary: rgba(118, 118, 128, 0.12);

  --tint: #007aff;      /* systemBlue */
  --green: #34c759;     /* systemGreen */
  --red: #ff3b30;       /* systemRed */
  --orange: #ff9500;    /* systemOrange */
  --segment-fill: #ffffff;

  --nav-blur-bg: rgba(255, 255, 255, 0.82);
  --sheet-scrim: rgba(0, 0, 0, 0.4);
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 10px rgba(0, 0, 0, 0.08);

  --nav-height: 44px;

  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --system-background: #000000;
    --secondary-system-background: #1c1c1e;
    --grouped-background: #000000;
    --secondary-grouped-background: #1c1c1e;

    --label: #ffffff;
    --secondary-label: rgba(235, 235, 245, 0.6);
    --tertiary-label: rgba(235, 235, 245, 0.3);
    --quaternary-label: rgba(235, 235, 245, 0.16);

    --separator: rgba(84, 84, 88, 0.6);
    --opaque-separator: #38383a;

    --fill: rgba(120, 120, 128, 0.36);
    --fill-secondary: rgba(120, 120, 128, 0.32);
    --fill-tertiary: rgba(118, 118, 128, 0.24);

    --tint: #0a84ff;
    --green: #30d158;
    --red: #ff453a;
    --orange: #ff9f0a;
    --segment-fill: #636366;

    --nav-blur-bg: rgba(28, 28, 30, 0.78);
    --sheet-scrim: rgba(0, 0, 0, 0.6);
    --card-shadow: none;
  }
}

/* ---------- Type scale (HIG text styles) ---------- */
.text-large-title { font-size: 34px; line-height: 41px; font-weight: 700; letter-spacing: -0.4px; }
.text-title2      { font-size: 22px; line-height: 28px; font-weight: 700; letter-spacing: -0.2px; }
.text-headline    { font-size: 17px; line-height: 22px; font-weight: 600; }
.text-body        { font-size: 17px; line-height: 22px; font-weight: 400; }
.text-subhead     { font-size: 15px; line-height: 20px; font-weight: 400; }
.text-footnote    { font-size: 13px; line-height: 18px; font-weight: 400; }
.text-caption2    { font-size: 11px; line-height: 13px; font-weight: 600; letter-spacing: 0.06px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  margin: 0;
  background: var(--grouped-background);
  color: var(--label);
  min-height: 100vh;
  font-size: 17px;
  line-height: 1.3;
}

input, select, button, textarea { font-family: inherit; }

:focus-visible {
  outline: 2px solid var(--tint);
  outline-offset: 2px;
  border-radius: 4px;
}

button { -webkit-appearance: none; appearance: none; border: none; background: none; color: inherit; cursor: pointer; }
button:active { opacity: 0.6; }

/* ---------- Nav bar (large-title collapse) ---------- */
.nav {
  position: sticky;
  top: 0;
  z-index: 20;
  padding-top: env(safe-area-inset-top, 0px);
}

.nav-bar {
  height: var(--nav-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 0 4px;
  position: relative;
  background: transparent;
  border-bottom: 1px solid transparent;
  transition: background-color 0.25s ease, border-color 0.25s ease;
}

.nav.scrolled .nav-bar {
  background: var(--nav-blur-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom-color: var(--separator);
}

.nav-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  color: var(--tint);
}

.nav-btn svg { width: 24px; height: 24px; }

.nav-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--tint);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
}

.nav-compact-title {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-weight: 600;
  font-size: 17px;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}

.nav.scrolled .nav-compact-title { opacity: 1; }

.nav-large-title-wrap {
  padding: 4px 16px 12px;
  transition: opacity 0.15s ease;
}

.nav.scrolled .nav-large-title-wrap { opacity: 0; }

/* ---------- Toolbar: search, segmented control, filter chips ---------- */
.toolbar {
  position: sticky;
  top: calc(var(--nav-height) + env(safe-area-inset-top, 0px));
  z-index: 15;
  background: var(--grouped-background);
  padding: 8px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search-field {
  position: relative;
  display: flex;
  align-items: center;
  background: var(--fill-tertiary);
  border-radius: 10px;
  height: 36px;
  padding: 0 8px;
  gap: 6px;
}

.search-field svg { width: 17px; height: 17px; color: var(--secondary-label); flex-shrink: 0; }

.search-field input {
  border: none;
  background: transparent;
  outline: none;
  color: var(--label);
  font-size: 17px;
  flex: 1;
  min-width: 0;
}

.search-field input::placeholder { color: var(--secondary-label); }

.search-clear {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--secondary-label);
  display: none;
}
.search-field.has-text .search-clear { display: flex; align-items: center; justify-content: center; }

.segmented {
  position: relative;
  display: flex;
  background: var(--fill-tertiary);
  border-radius: 9px;
  padding: 2px;
  height: 32px;
}

.segmented-indicator {
  position: absolute;
  top: 2px;
  bottom: 2px;
  left: 2px;
  width: calc((100% - 4px) / 3);
  background: var(--segment-fill);
  border-radius: 7px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15), 0 1px 1px rgba(0, 0, 0, 0.08);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 0;
}

.segmented button {
  position: relative;
  z-index: 1;
  flex: 1;
  font-size: 13px;
  font-weight: 600;
  color: var(--label);
  border-radius: 7px;
}

.segmented button.dim { color: var(--secondary-label); }

.toolbar-row2 {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chip-scroll {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
  padding-bottom: 1px;
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 20px), transparent 100%);
  mask-image: linear-gradient(to right, #000 calc(100% - 20px), transparent 100%);
}
.chip-scroll::-webkit-scrollbar { display: none; }

.filter-chip {
  flex-shrink: 0;
  height: 30px;
  padding: 0 12px;
  border-radius: 15px;
  background: var(--fill-tertiary);
  color: var(--label);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

.filter-chip.active { background: var(--tint); color: #fff; }

.sort-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  padding: 0 10px;
  border-radius: 15px;
  background: var(--fill-tertiary);
  color: var(--tint);
  font-size: 13px;
  font-weight: 600;
}
.sort-btn svg { width: 13px; height: 13px; }

/* ---------- Sort menu (UIMenu-style popover) ---------- */
.menu-popover {
  position: fixed;
  z-index: 30;
  min-width: 220px;
  background: var(--nav-blur-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-radius: 13px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  transform-origin: top right;
  animation: menu-in 0.16s ease-out;
}

@keyframes menu-in {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 11px 16px;
  font-size: 17px;
  color: var(--label);
  border-bottom: 0.5px solid var(--separator);
}
.menu-item:last-child { border-bottom: none; }
.menu-item svg { width: 18px; height: 18px; color: var(--tint); visibility: hidden; }
.menu-item.selected svg { visibility: visible; }

/* ---------- Content ---------- */
main { padding: 4px 16px 48px; }

.shuffle-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 44px;
  border-radius: 12px;
  background: var(--fill-tertiary);
  color: var(--tint);
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
}
.shuffle-btn svg { width: 18px; height: 18px; }

.shuffle-result {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 10px;
  padding: 8px 0 20px;
}
.shuffle-result .detail-poster { width: 150px; height: 225px; margin: 0 0 4px; }
.shuffle-result .form-actions { width: 100%; flex-direction: column; gap: 10px; margin-top: 8px; }
.shuffle-result .ghost-btn { width: 100%; min-height: 44px; }

.empty-state {
  text-align: center;
  color: var(--secondary-label);
  padding: 4rem 1rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px 12px;
}

.card { display: flex; flex-direction: column; gap: 6px; }

.card-poster {
  aspect-ratio: 2 / 3;
  border-radius: 12px;
  background: var(--fill-tertiary) center/cover no-repeat;
  box-shadow: var(--card-shadow);
  position: relative;
  display: flex;
  align-items: flex-end;
  overflow: hidden;
}

.card-poster.has-image::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.55) 0%, transparent 45%);
  pointer-events: none;
}

.card-poster-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tertiary-label);
}
.card-poster-placeholder svg { width: 32px; height: 32px; }

.card-type-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 1;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06px;
  padding: 3px 7px;
  border-radius: 6px;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.card-mila-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 1;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}
.card-mila-badge svg { width: 12px; height: 12px; }
.card-mila-badge.ready { background: var(--green); }
.card-mila-badge.wait { background: var(--orange); }

.card-avatars {
  position: relative;
  z-index: 1;
  display: flex;
  gap: 4px;
  padding: 8px;
}

.avatar {
  position: relative;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
}

.avatar-badge {
  position: absolute;
  bottom: -3px;
  right: -3px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--green);
  border: 1.5px solid var(--system-background);
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-badge svg { width: 8px; height: 8px; color: #fff; }

.card-title { font-size: 15px; font-weight: 600; line-height: 1.25; padding: 0 2px; }
.card-meta { font-size: 12px; color: var(--secondary-label); padding: 0 2px; }

/* ---------- Sheets (bottom-presented modals) ---------- */
.sheet-scrim {
  position: fixed;
  inset: 0;
  background: var(--sheet-scrim);
  z-index: 50;
  opacity: 0;
  transition: opacity 0.3s ease;
}
.sheet-scrim.open { opacity: 1; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 51;
  max-height: 88vh;
  background: var(--secondary-grouped-background);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.38s cubic-bezier(0.32, 0.72, 0, 1);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  /* Closed sheets are still fixed-position, full-size elements sitting in
     the layout (mobile just moves them off-screen via translateY(100%),
     desktop instead fades them via opacity - see media query below). Without
     this, a closed-but-invisible sheet silently eats clicks meant for
     whatever is visually underneath it. */
  pointer-events: none;
}
.sheet.open { transform: translateY(0); pointer-events: auto; }

@media (min-width: 560px) {
  /* Centered dialog instead of a bottom sheet. Stays position:fixed (not
     static) so its z-index still applies and it keeps stacking above
     .sheet-scrim — switching to static here previously dropped it out of
     the positioned stacking order, letting the still-fixed scrim catch
     clicks meant for the sheet underneath it. */
  .sheet {
    left: 50%;
    right: auto;
    bottom: auto;
    top: 50%;
    width: 90%;
    max-width: 480px;
    border-radius: 20px;
    transform: translate(-50%, -50%) scale(0.96);
    opacity: 0;
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.3s ease;
  }
  .sheet.open { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  .sheet-grabber { display: none; }
}

.sheet-grabber {
  width: 36px;
  height: 5px;
  border-radius: 3px;
  background: var(--tertiary-label);
  margin: 8px auto 4px;
  flex-shrink: 0;
}

.sheet-header {
  display: flex;
  align-items: center;
  padding: 4px 16px 12px;
  flex-shrink: 0;
}

.sheet-header .text-headline { flex: 1; text-align: center; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sheet-header-slot { flex: 1; display: flex; min-height: 44px; align-items: center; }
.sheet-header-slot.leading { justify-content: flex-start; }
.sheet-header-slot.trailing { justify-content: flex-end; }

.sheet-header-btn { color: var(--tint); font-size: 17px; padding: 6px 4px; display: flex; align-items: center; }
.sheet-header-btn.bold { font-weight: 600; }

.sheet-body { overflow-y: auto; padding: 0 16px 20px; }

/* ---------- Action sheet (identity picker) ---------- */
.action-sheet-wrap {
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-sheet-group {
  background: var(--secondary-grouped-background);
  border-radius: 14px;
  overflow: hidden;
  text-align: center;
}

.action-sheet-title {
  padding: 14px 16px 12px;
  font-size: 13px;
  color: var(--secondary-label);
  border-bottom: 0.5px solid var(--separator);
}

.action-sheet-btn {
  display: block;
  width: 100%;
  padding: 16px;
  font-size: 20px;
  font-weight: 400;
  color: var(--tint);
  border-bottom: 0.5px solid var(--separator);
}
.action-sheet-btn:last-child { border-bottom: none; }

/* ---------- Grouped list rows (search results, detail) ---------- */
.list-group {
  background: var(--secondary-grouped-background);
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 16px;
}

.list-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  border-bottom: 0.5px solid var(--separator);
  text-align: left;
}
.list-row:last-child { border-bottom: none; }

.list-row-thumb {
  width: 40px;
  height: 60px;
  border-radius: 6px;
  background: var(--fill-tertiary) center/cover no-repeat;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tertiary-label);
}
.list-row-thumb svg { width: 18px; height: 18px; }

.list-row-text { flex: 1; min-width: 0; }
.list-row-title { font-size: 17px; color: var(--label); }
.list-row-sub { font-size: 13px; color: var(--secondary-label); margin-top: 1px; }

.list-row-chevron { color: var(--tertiary-label); flex-shrink: 0; width: 14px; height: 14px; }

.list-row-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--fill-secondary);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: var(--label);
  flex-shrink: 0;
}
.list-row-avatar.watched { background: var(--green); color: #fff; }

.list-row-check { width: 22px; height: 22px; color: var(--tint); flex-shrink: 0; visibility: hidden; }
.list-row-check.checked { visibility: visible; }

.list-row.destructive { color: var(--red); justify-content: center; font-size: 17px; }

.person-row { cursor: pointer; }

.watched-date-wrap {
  position: relative;
  display: inline-block;
  margin-top: 1px;
}
.watched-date-wrap .list-row-sub { margin-top: 0; color: var(--tint); }
.watched-date-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  border: none;
  padding: 0;
  margin: 0;
  width: 100%;
  cursor: pointer;
}

.section-header {
  font-size: 13px;
  color: var(--secondary-label);
  text-transform: uppercase;
  letter-spacing: 0.06px;
  padding: 0 16px 6px;
  margin-top: 4px;
}

.hint-text { font-size: 13px; color: var(--secondary-label); padding: 10px 4px; text-align: center; }

/* ---------- Form fields (manual add) ---------- */
.field-group { margin-bottom: 16px; }
.field-input {
  width: 100%;
  background: var(--fill-tertiary);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 17px;
  color: var(--label);
  border: none;
  outline: none;
}
.field-row { display: flex; gap: 10px; }
.field-row > div { flex: 1; }
.field-label { font-size: 13px; color: var(--secondary-label); margin-bottom: 6px; display: block; }

select.field-input { appearance: none; -webkit-appearance: none; }
textarea.field-input { resize: vertical; min-height: 120px; font-family: inherit; line-height: 1.4; }

.bulk-review-row { cursor: pointer; }
.bulk-review-row input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--tint); flex-shrink: 0; }

.who-pills { display: flex; gap: 8px; flex-wrap: wrap; }
.who-pill {
  padding: 8px 16px;
  border-radius: 17px;
  background: var(--fill-tertiary);
  font-size: 15px;
  font-weight: 600;
  color: var(--label);
}
.who-pill.selected { background: var(--tint); color: #fff; }
.who-pill.mila-pill-ready.selected { background: var(--green); }
.who-pill.mila-pill-wait.selected { background: var(--orange); }

.primary-action {
  width: 100%;
  background: var(--tint);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  padding: 13px;
  border-radius: 12px;
  min-height: 44px;
}
.primary-action:disabled { opacity: 0.4; }

.detail-poster-row { display: flex; gap: 14px; margin-bottom: 16px; }
.detail-poster {
  width: 80px; height: 120px;
  border-radius: 10px;
  background: var(--fill-tertiary) center/cover no-repeat;
  flex-shrink: 0;
  box-shadow: var(--card-shadow);
}
.detail-overview { font-size: 15px; color: var(--secondary-label); line-height: 1.4; }

.hidden { display: none !important; }

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