/* TubeBox — mobile-first styles for iPhone Safari (works on desktop too). */

:root {
  --bg: #0f0f10;
  --surface: #1b1b1e;
  --surface-2: #27272c;
  --line: #2f2f35;
  --text: #f2f2f5;
  --muted: #a1a1aa;
  --faint: #70707a;
  --accent: #0a84ff;
  --good: #30d158;
  --warn: #ff9f0a;
  --bad: #ff453a;
  --tab-h: 56px;
  --top-h: 56px;
  --radius: 12px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #ffffff;
    --surface: #f2f2f7;
    --surface-2: #e6e6eb;
    --line: #dcdce2;
    --text: #111114;
    --muted: #5c5c66;
    --faint: #8e8e96;
    --accent: #007aff;
    --good: #1f9d47;
    --warn: #c76a00;
    --bad: #d70015;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
img { display: block; }
code { font: 12.5px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; word-break: break-all; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
svg .fill { fill: currentColor; stroke: none; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.strong { font-weight: 600; }
.bad { color: var(--bad); }
.good { color: var(--good); }
.pad { padding: 12px 16px; }
.pad-x { padding: 0 16px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  backdrop-filter: saturate(160%) blur(18px);
  border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; letter-spacing: -0.2px; flex: none; }
.brand .logo { width: 28px; height: 28px; stroke: none; }
.search { flex: 1; min-width: 0; }
.search input {
  width: 100%; height: 38px; border: 0; border-radius: 10px;
  background: var(--surface-2); padding: 0 12px;
  font-size: 16px; /* 16px stops iOS from zooming on focus */
  outline: none; -webkit-appearance: none; appearance: none;
}
.search input:focus { box-shadow: 0 0 0 2px var(--accent); }
@media (max-width: 420px) { .brand span { display: none; } }

body[data-route="watch"] .topbar,
body[data-route="login"] .topbar .search,
body[data-route="login"] .tabbar { display: none; }

main {
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px);
  min-height: 100vh;
}
body.has-mini main { padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 90px); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
  display: flex; justify-content: space-around;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  backdrop-filter: saturate(160%) blur(18px);
  border-top: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; font-weight: 600; color: var(--faint);
}
.tabbar a.on { color: var(--text); }
.tabbar svg { width: 24px; height: 24px; }

/* ---------- player ---------- */
.player { background: #000; }
.player.hidden { display: none; }
.player.full {
  position: sticky; top: 0; z-index: 30;
  padding-top: env(safe-area-inset-top);
}
.player .frame {
  position: relative; width: 100%; aspect-ratio: 16 / 9;
  max-height: 62vh; margin: 0 auto; background: #000; overflow: hidden;
}
.player.full .frame { max-width: calc(62vh * 16 / 9); }
.player video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }

.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #fff; }
.overlay img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.overlay::after { content: ""; position: absolute; inset: 0; background: linear-gradient(rgba(0,0,0,.30), rgba(0,0,0,.62)); }
.overlay-body { position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; padding: 16px; max-width: 92%; }
.ov-play {
  width: 70px; height: 70px; border-radius: 50%; border: 0;
  background: rgba(255,255,255,.95); color: #000; display: grid; place-items: center;
  box-shadow: 0 6px 20px rgba(0,0,0,.35);
}
.ov-play svg { width: 32px; height: 32px; margin-left: 4px; stroke: none; }
.ov-msg { font-size: 14px; font-weight: 600; text-shadow: 0 1px 3px rgba(0,0,0,.7); }
.ov-bar { width: 190px; height: 4px; background: rgba(255,255,255,.28); border-radius: 2px; overflow: hidden; }
.ov-bar i { display: block; height: 100%; width: 0; background: #fff; transition: width .5s ease; }
.ov-actions { display: flex; gap: 14px; align-items: center; }
.overlay .link { background: none; border: 0; color: rgba(255,255,255,.85); font-size: 13px; text-decoration: underline; padding: 6px; }
.overlay[data-kind="preparing"] .ov-play,
.overlay[data-kind="preparing"] #ovRetry { display: none; }
.overlay[data-kind="ready"] .spinner,
.overlay[data-kind="ready"] .ov-bar,
.overlay[data-kind="ready"] .ov-actions { display: none; }
.overlay[data-kind="failed"] .spinner,
.overlay[data-kind="failed"] .ov-bar,
.overlay[data-kind="failed"] .ov-play,
.overlay[data-kind="failed"] #ovCancel { display: none; }

.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.25); border-top-color: #fff;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.player .mini-info { display: none; }
.player.mini {
  position: fixed; left: 8px; right: 8px; z-index: 40;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 8px);
  display: flex; align-items: center; height: 64px;
  border-radius: 14px; overflow: hidden;
  background: var(--surface-2);
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
  cursor: pointer;
}
.player.mini .frame { width: 114px; height: 64px; flex: none; aspect-ratio: auto; max-height: none; margin: 0; }
.player.mini .overlay-body { display: none; }
.player.mini .mini-info { display: flex; flex: 1; min-width: 0; height: 100%; align-items: center; position: relative; padding-left: 10px; }
.mini-text { flex: 1; min-width: 0; }
.mini-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; background: color-mix(in srgb, var(--text) 12%, transparent); }
.mini-progress i { display: block; height: 100%; width: 0; background: var(--accent); }
@media (min-width: 760px) { .player.mini { left: auto; width: 440px; right: 16px; } }

.icon-btn {
  width: 42px; height: 42px; flex: none; border: 0; border-radius: 50%;
  background: transparent; display: grid; place-items: center; color: var(--text);
}
.icon-btn:disabled { opacity: .35; }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- chips & buttons ---------- */
.chips {
  display: flex; gap: 8px; padding: 10px 12px; overflow-x: auto; scrollbar-width: none;
  position: sticky; top: calc(env(safe-area-inset-top) + 55px); z-index: 10; background: var(--bg);
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; border-radius: 8px; padding: 7px 12px;
  background: var(--surface-2); font-weight: 600; font-size: 14px; white-space: nowrap;
}
.chip.on { background: var(--text); color: var(--bg); }
.chip.icon { padding: 6px 9px; margin-left: auto; display: grid; place-items: center; }
.chip.icon svg { width: 18px; height: 18px; }
.chip.busy svg { animation: spin 1s linear infinite; }

.btn {
  appearance: none; -webkit-appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 999px; padding: 9px 15px;
  background: var(--surface-2); font-weight: 600; font-size: 14px; white-space: nowrap;
}
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; }

/* ---------- video cards ---------- */
.grid {
  display: grid; gap: 18px 14px; padding: 4px 12px 12px;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
}
.card { min-width: 0; }
.thumb {
  position: relative; display: block; aspect-ratio: 16 / 9; overflow: hidden;
  border-radius: var(--radius); background: var(--surface-2);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.watched .thumb img { opacity: .5; }
.b-dur, .b-state {
  position: absolute; font-size: 12px; font-weight: 600; line-height: 1;
  padding: 4px 6px; border-radius: 5px; color: #fff;
}
.b-dur { right: 6px; bottom: 8px; background: rgba(0,0,0,.8); }
.b-state { left: 6px; top: 6px; display: inline-flex; align-items: center; gap: 3px; background: rgba(0,0,0,.72); }
.b-state svg { width: 12px; height: 12px; stroke-width: 3; }
.b-state.ready { background: var(--good); color: #002b0f; }
.b-state.dl { background: var(--accent); }
.b-state.live { background: var(--bad); }
.b-state.soon { background: rgba(0,0,0,.78); }
.b-state.bad { background: var(--bad); }
.b-bar { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: rgba(255,255,255,.35); }
.b-bar i { display: block; height: 100%; background: var(--bad); }

.meta { display: flex; gap: 10px; padding: 10px 0 0; align-items: flex-start; }
.avatar { position: relative; width: 36px; height: 36px; flex: none; border-radius: 50%; overflow: hidden; background: var(--surface-2); display: grid; place-items: center; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.avatar.lg { width: 72px; height: 72px; }
.initial { width: 100%; height: 100%; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 15px; }
.avatar.lg .initial { font-size: 28px; }
.text { flex: 1; min-width: 0; }
.title {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  font-weight: 600; font-size: 15px; line-height: 1.3;
}
.sub { font-size: 13px; color: var(--muted); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; }
.more {
  width: 32px; height: 32px; flex: none; border: 0; background: none; color: var(--muted);
  display: grid; place-items: center; margin: -4px -6px 0 0; border-radius: 50%;
}
.more svg { width: 20px; height: 20px; }

/* compact rows (library, up next, playlists) */
.list { padding: 0 12px; display: flex; flex-direction: column; gap: 12px; }
.row, .job { display: flex; gap: 12px; align-items: flex-start; }
.row .thumb, .job .thumb { width: 168px; flex: none; border-radius: 10px; }
.row .title, .job .title { font-size: 14px; }
.job .title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-weight: 600; }
.job .bar { height: 4px; border-radius: 2px; background: var(--surface-2); margin-top: 8px; overflow: hidden; }
.job .bar i { display: block; height: 100%; background: var(--accent); transition: width .5s ease; }
.job.failed .sub { color: var(--bad); }
.job .icon-btn { width: 36px; height: 36px; }
@media (max-width: 380px) { .row .thumb, .job .thumb { width: 140px; } }

/* skeletons */
.sk { background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.3s ease-in-out infinite; }
.sk.line { height: 12px; border-radius: 6px; margin: 4px 0 8px; }
.sk.line.short { width: 60%; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* ---------- pages ---------- */
.page-head { padding: 14px 16px 6px; }
.page-head h1 { margin: 0; font-size: 24px; letter-spacing: -0.3px; }
.page-head p { margin: 4px 0 0; }
.section-title { font-size: 18px; margin: 22px 16px 10px; letter-spacing: -0.2px; }
.footnote { text-align: center; color: var(--faint); font-size: 12.5px; padding: 12px 16px 4px; }
.more-btn { display: block; margin: 8px auto 16px; }
.note { margin: 10px 16px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: 13.5px; line-height: 1.45; }
.note.warn { background: color-mix(in srgb, var(--warn) 18%, var(--surface)); }
.banner { margin: 10px 12px; padding: 12px 14px; border-radius: 12px; background: color-mix(in srgb, var(--warn) 20%, var(--surface)); font-size: 13.5px; line-height: 1.45; display: flex; gap: 10px; align-items: flex-start; }
.banner button { margin-left: auto; flex: none; }

.empty { text-align: center; padding: 56px 28px; color: var(--muted); }
.empty h2 { color: var(--text); margin: 14px 0 6px; font-size: 19px; }
.empty p { margin: 0 auto 18px; max-width: 420px; line-height: 1.45; }
.empty-icon { width: 64px; height: 64px; margin: 0 auto; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); }
.empty-icon svg { width: 30px; height: 30px; }
.empty .btn + .btn { margin-left: 8px; }

/* watch page */
.watch { padding: 12px 0 8px; max-width: 1000px; margin: 0 auto; }
.w-title { font-size: 18px; line-height: 1.3; margin: 0 16px; letter-spacing: -0.2px; }
.w-meta { color: var(--muted); font-size: 13px; margin: 6px 16px 0; }
.w-channel { display: flex; align-items: center; gap: 10px; margin: 14px 16px 0; font-weight: 600; }
.actions { display: flex; gap: 8px; overflow-x: auto; padding: 14px 16px 4px; scrollbar-width: none; }
.actions::-webkit-scrollbar { display: none; }
.sb-note { margin: 10px 16px 0; font-size: 13px; color: var(--good); display: flex; gap: 6px; align-items: center; }
.sb-note svg { width: 16px; height: 16px; }
.desc { margin: 14px 16px 0; background: var(--surface); border-radius: 12px; padding: 10px 12px; }
.desc summary { font-weight: 600; cursor: pointer; }
.desc-body { white-space: pre-wrap; word-wrap: break-word; font-size: 14px; line-height: 1.45; margin-top: 8px; color: var(--muted); }
.desc-body a { color: var(--accent); }

/* channel page */
.chan-head { display: flex; align-items: center; gap: 14px; padding: 18px 16px 6px; }
.chan-head h1 { margin: 0; font-size: 22px; letter-spacing: -0.3px; }
.chan-actions { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; padding: 8px 16px 14px; }
.switch-row { display: inline-flex; align-items: center; gap: 10px; font-weight: 600; font-size: 14px; }

/* playlists */
.pl-row { display: flex; gap: 12px; align-items: center; padding: 6px 0; }
.pl-thumb { width: 120px; aspect-ratio: 16 / 9; flex: none; border-radius: 10px; overflow: hidden; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); }
.pl-thumb img { width: 100%; height: 100%; object-fit: cover; }

/* settings */
.panel { background: var(--surface); border-radius: 14px; margin: 14px 12px; overflow: hidden; }
.panel h2 { font-size: 16px; margin: 0; padding: 14px 16px 8px; display: flex; align-items: center; gap: 8px; }
.panel h2 .count { font-size: 13px; color: var(--muted); font-weight: 600; }
.panel > p.muted { margin: 0 0 8px; }
.panel .note { margin: 4px 12px 12px; }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-top: 1px solid var(--line);
}
.set-row > div { min-width: 0; }
.set-row select, .field {
  background: var(--surface-2); border: 0; border-radius: 9px; padding: 8px 10px; font-size: 16px; max-width: 55%;
  -webkit-appearance: none; appearance: none;
}
.field { width: 100%; max-width: none; outline: none; }
.field.num { width: 90px; text-align: right; }
.sub-row { display: flex; align-items: center; gap: 10px; padding: 8px 16px; border-top: 1px solid var(--line); }
.sub-row .avatar { width: 32px; height: 32px; }
.sub-title { flex: 1; min-width: 0; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag { font-size: 11px; font-weight: 600; color: var(--muted); background: var(--surface-2); padding: 2px 6px; border-radius: 5px; margin-left: 6px; vertical-align: 1px; }
.auto { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.sub-list { max-height: 420px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.tips { margin: 0; padding: 4px 16px 14px 34px; line-height: 1.5; font-size: 14px; }
.tips li { margin: 6px 0; }
.ok-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--good); margin-right: 6px; }
.ok-dot.no { background: var(--bad); }

/* iOS-style switch */
.switch {
  -webkit-appearance: none; appearance: none; flex: none;
  width: 51px; height: 31px; border-radius: 31px; background: var(--surface-2);
  position: relative; transition: background .2s; margin: 0; cursor: pointer;
  border: 1px solid var(--line);
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 25px; height: 25px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.3); transition: transform .2s;
}
.switch:checked { background: var(--good); border-color: var(--good); }
.switch:checked::after { transform: translateX(20px); }

/* login */
.login { max-width: 340px; margin: 12vh auto 0; padding: 24px; text-align: center; display: flex; flex-direction: column; gap: 12px; }
.login h1 { margin: 4px 0 0; }
.login .logo-lg svg { width: 64px; height: 64px; stroke: none; margin: 0 auto; }

/* ---------- sheet & toast ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 60; animation: fade .15s ease; }
.sheet {
  position: fixed; left: 8px; right: 8px; z-index: 61;
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  background: var(--surface); border-radius: 16px; padding: 6px; max-width: 520px; margin: 0 auto;
  animation: rise .18s ease;
}
.sheet-title { font-size: 13px; color: var(--muted); padding: 10px 14px 8px; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-btn { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 14px; font-size: 16px; border-radius: 10px; }
.sheet-btn:active { background: var(--surface-2); }
.sheet-btn.danger { color: var(--bad); }
.sheet-btn.cancel { text-align: center; font-weight: 600; margin-top: 4px; background: var(--surface-2); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }

#toast {
  position: fixed; left: 50%; z-index: 70; pointer-events: none;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 18px);
  transform: translate(-50%, 20px); opacity: 0; transition: opacity .2s, transform .2s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 600; max-width: calc(100% - 32px); text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,.3);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
body.has-mini #toast { bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 86px); }

/* ---------- larger screens ---------- */
@media (min-width: 900px) {
  .grid { padding: 8px 24px 24px; gap: 24px 16px; }
  .chips, .page-head { padding-left: 24px; padding-right: 24px; }
  .search { max-width: 640px; margin: 0 auto; }
}

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