:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --dock-h: 0px;
  --rail-w: 228px;
  --touch: 44px;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html.ios-web .player-frame #player,
html.mobile-web .player-frame #player {
  object-fit: contain !important;
  transform: none !important;
  -webkit-transform: none !important;
}

html.ios-web .player-frame,
html.mobile-web .player-frame {
  transform: none;
  -webkit-transform: none;
}

html.ios-web .wm-mask,
html.mobile-web .wm-mask {
  display: none !important;
}

body {
  padding-top: var(--safe-top);
}

input[type="search"] {
  font-size: 16px;
}

.dock,
.filter-toggle,
.web-hint {
  display: none;
}

.remote-box {
  margin: 0 24px 10px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #151b24;
  color: #d5dde6;
  font-size: 13px;
  line-height: 1.7;
  word-break: break-all;
}
.remote-box b { color: #3ee08f; }

@media (hover: hover) and (pointer: fine) {
  .poster:hover {
    transform: scale(1.12) translateY(-6px);
    z-index: 8;
  }
  .poster:hover .poster-cover {
    box-shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
  }
  .poster:hover .poster-cover::after { opacity: 1; }
}

@media (hover: none), (pointer: coarse) {
  .poster { transform: none; }
  .poster-cover::after { opacity: 0; }
  .player-frame #player {
    object-fit: contain;
    transform: none;
  }
  .row-scroller {
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }
  .row-scroller .poster { scroll-snap-align: start; }
  .nav-item,
  .tab,
  .hot,
  .ep,
  .search-hit-play,
  .pager button,
  .filter-opts button {
    min-height: var(--touch);
  }
}

/* Tablet: icon rail, more columns, keep search list + hot if wide */
@media (max-width: 1023px) {
  .sidebar {
    width: 76px;
    padding: 16px 8px 12px;
    padding-left: calc(8px + var(--safe-left));
  }
  .brand { padding: 0 0 16px; justify-content: center; }
  .brand-word,
  .sidebar-foot { display: none; }
  .nav-item {
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    text-align: center;
    padding: 10px 4px;
    font-size: 11px;
    min-height: var(--touch);
  }
  .nav-item::before { margin: 0; }
  .topbar { padding: 12px 16px 8px; }
  #pages { padding: 8px 16px 28px; }
  .hots { padding: 0 16px 8px; }
  .hero { height: 240px; }
  .hero-copy { left: 20px; right: 20px; bottom: 18px; }
  .hero-copy h2 { font-size: 26px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 14px 10px; }
  .poster { width: 132px; flex-basis: 132px; }
  .poster-cover { height: 186px; }
  .grid .poster { width: auto; flex-basis: auto; }
  .grid .poster-cover { height: auto; aspect-ratio: 2 / 3; }
  .search-layout { grid-template-columns: minmax(0, 1fr) 200px; gap: 20px; }

  .player-layer {
    padding: 0;
    align-items: stretch;
    background: #07090c;
  }
  .player-sheet {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border: 0;
    border-radius: 0;
    padding: 12px 12px var(--safe-bottom);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  .player-main { grid-template-columns: minmax(0, 1.35fr) minmax(260px, 0.75fr); gap: 14px; }
  .close-x {
    top: calc(8px + var(--safe-top));
    right: 8px;
    z-index: 12;
    width: var(--touch);
    height: var(--touch);
  }
}

@media (max-width: 839px) {
  .search-layout { grid-template-columns: 1fr; }
  .search-hot { padding-top: 8px; }
  .player-main { grid-template-columns: 1fr; }
}

/* Phone: bottom dock, no sidebar, thumb-first player */
@media (max-width: 599px) {
  :root { --dock-h: calc(56px + var(--safe-bottom)); }

  .sidebar { display: none; }
  .dock {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 15;
    height: var(--dock-h);
    padding: 4px 4px var(--safe-bottom);
    background: rgba(8, 11, 16, 0.94);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(16px);
  }
  .dock .nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    margin: 0;
    padding: 6px 0;
    border-radius: 10px;
    font-size: 11px;
    min-width: 0;
    min-height: 48px;
    text-align: center;
  }
  .dock .nav-item.active {
    color: #061014;
  }

  .topbar {
    padding: 8px 12px;
    gap: 8px;
  }
  .search-box { max-width: none; }
  #btnRemote { display: none; }
  #btnSwitchUser {
    height: var(--touch);
    min-width: 52px;
    padding: 0 10px;
    flex-shrink: 0;
    font-size: 13px;
  }
  #btnRefresh {
    width: var(--touch);
    min-width: var(--touch);
    height: var(--touch);
    padding: 0;
    font-size: 0;
    flex-shrink: 0;
    background-color: var(--bg-3);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23eef3f7' d='M17.65 6.35A8 8 0 1 0 19.5 12h-2a6 6 0 1 1-1.76-4.24L13 10h7V3z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 18px;
  }
  .web-hint { display: none !important; }
  .hots,
  .history-row {
    padding: 0 12px 6px;
    gap: 6px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .hot { flex: 0 0 auto; white-space: nowrap; }
  .dock .nav-item span { line-height: 1.1; }
  #pages {
    padding: 4px 12px calc(20px + var(--dock-h));
  }
  .hero {
    height: min(48vw, 200px);
    border-radius: 14px;
  }
  .hero-copy { left: 14px; right: 14px; bottom: 12px; }
  .hero-copy h2 { font-size: 20px; }
  .hero-copy p { display: none; }
  .hero-dots { right: 12px; bottom: 10px; }
  .row { margin-top: 18px; }
  .row-head h3, .cate-head h3, .block-title { font-size: 17px; }
  .poster { width: 108px; flex-basis: 108px; }
  .poster-cover { height: 152px; border-radius: 10px; }
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px 8px;
    padding: 8px 0 16px;
  }
  .grid .poster { width: auto; flex-basis: auto; }
  .grid .poster-cover { height: auto; aspect-ratio: 2 / 3; }
  .tabs { gap: 6px; overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .tab { flex: 0 0 auto; }

  .filter-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--touch);
    margin: 0 0 8px;
    padding: 0 14px;
    border: 0;
    border-radius: 999px;
    background: var(--bg-3);
    color: #fff;
  }
  .search-filters {
    display: none;
    max-height: 42vh;
    overflow: auto;
    padding-bottom: 8px;
    -webkit-overflow-scrolling: touch;
  }
  .search-filters.open { display: block; }
  .filter-row { font-size: 12px; }
  .filter-opts { flex-wrap: nowrap; overflow-x: auto; }
  .search-hit { gap: 12px; padding: 12px 0; }
  .search-hit-cover { width: 86px; height: 120px; }
  .search-hit-title { font-size: 17px; }
  .search-hit-score { font-size: 18px; }
  .search-hit-meta p:nth-child(n + 3) { display: none; }
  .pager { gap: 10px; padding-bottom: 8px; }
  .toast { bottom: calc(12px + var(--dock-h)); }

  .player-layer {
    padding: 0;
    align-items: stretch;
    background: #07090c;
  }
  .player-sheet {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border: 0;
    border-radius: 0;
    padding: 0 0 var(--safe-bottom);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  .player-main { grid-template-columns: 1fr; gap: 0; }
  .player-frame {
    aspect-ratio: 16 / 9;
    border-radius: 0;
    max-height: 42vh;
  }
  .player-frame #player {
    object-fit: contain;
    transform: none;
  }
  .close-x {
    top: calc(8px + var(--safe-top));
    right: 8px;
    z-index: 12;
    width: var(--touch);
    height: var(--touch);
  }
  .player-side { padding: 12px 14px 20px; }
  #meta h2 { font-size: 18px; }
  .key-hint { display: none; }
  .player-toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .player-toolbar button,
  .player-toolbar select {
    min-height: var(--touch);
    flex: 0 0 auto;
  }
  #episodes { max-height: none; }
  .resume-bar, .next-count { bottom: 12px; }
}

@media (max-width: 320px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 599px) and (orientation: landscape) {
  .dock { display: none; }
  :root { --dock-h: 0px; }
  .player-frame { max-height: 100dvh; height: 100dvh; aspect-ratio: auto; }
  .player-side { display: none; }
  .player-sheet { overflow: hidden; }
}

body.player-open .dock { display: none; }
body.player-open { --dock-h: 0px; }
