:root {
  --bg: var(--tg-theme-bg-color, #fff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --text: var(--tg-theme-text-color, #000);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #fff);
  --star: #ffb400;
  /* 왼쪽·오른쪽 안전 여백: 기기(노치, 아이폰 듀오 바깥 화면의 옆 막대)와 텔레그램 자체 단추 자리 중 큰 쪽 */
  --pad-l: max(env(safe-area-inset-left, 0px), var(--tg-safe-area-inset-left, 0px) + var(--tg-content-safe-area-inset-left, 0px));
  --pad-r: max(env(safe-area-inset-right, 0px), var(--tg-safe-area-inset-right, 0px) + var(--tg-content-safe-area-inset-right, 0px));
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
/* 어떤 경우에도 화면 전체가 옆으로 밀리지 않게(가로 스크롤 막기). clip은 sticky 머리를 깨지 않는다 */
html { overflow-x: hidden; } /* iOS 15 이하 대비(body에는 두지 않는다: sticky 머리가 깨짐) */
html, body { overflow-x: clip; max-width: 100%; }
body { font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", sans-serif; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
.muted { color: var(--hint); font-size: 13px; }
.center { text-align: center; padding: 16px; }

header#top { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 10px calc(10px + var(--pad-r)) 6px calc(10px + var(--pad-l)); border-bottom: 1px solid var(--bg2); }
input, select, button { font: inherit; color: var(--text); }
/* iOS는 16px보다 작은 입력 칸을 누르면 화면을 확대해 옆으로 넘친다 */
input, select, textarea { font-size: 16px; }
#q { width: 100%; padding: 9px 12px; border: 0; border-radius: 10px; background: var(--bg2); }
.row { display: flex; gap: 6px; margin-top: 6px; }
.row select { flex: 1; min-width: 0; }
select { padding: 7px 8px; border: 0; border-radius: 9px; background: var(--bg2); -webkit-appearance: none; appearance: none; }
.chips { display: flex; gap: 6px; margin-top: 6px; overflow-x: auto; scrollbar-width: none; }
.chips.wrap { flex-wrap: wrap; overflow: visible; }
.chips:empty { display: none; }
.chip { flex: none; padding: 6px 11px; border: 0; border-radius: 999px; background: var(--bg2); white-space: nowrap; }
.chip[aria-pressed="true"], .chip.on { background: var(--btn); color: var(--btn-text); }
.chip .x { margin-left: 4px; opacity: .7; }
/* 줄 바꿈 칩 묶음(태그 시트, 상세 화면)에서는 긴 태그도 화면 폭 안에서 줄을 바꾼다 */
.chips.wrap .chip { white-space: normal; overflow-wrap: anywhere; max-width: 100%; text-align: left; border-radius: 14px; }

main { padding: 8px calc(8px + var(--pad-r)) 40px calc(8px + var(--pad-l)); }
#count { margin: 2px 4px 8px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.card { background: var(--bg2); border-radius: 10px; overflow: hidden; cursor: pointer; border: 0; padding: 0; text-align: left; }
.thumb { position: relative; aspect-ratio: 16 / 9; background: #0003; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .ph { position: absolute; inset: 0; display: grid; place-items: center; font-size: 26px; opacity: .5; }
.badge { position: absolute; bottom: 4px; right: 4px; background: #000a; color: #fff; font-size: 11px; padding: 1px 5px; border-radius: 4px; }
.badge.left { left: 4px; right: auto; }
.card .meta { padding: 6px 8px 8px; }
.card .name { font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-all; }
.card .sub { font-size: 12px; color: var(--hint); margin-top: 2px; }
.card .sub .s { color: var(--star); }

.sheet { position: fixed; inset: 0; z-index: 20; background: #0006; display: flex; align-items: flex-end; }
.sheet-body { width: 100%; max-height: 80vh; overflow: auto; background: var(--bg); border-radius: 14px 14px 0 0; padding: 16px calc(14px + var(--pad-r)) calc(16px + env(safe-area-inset-bottom)) calc(14px + var(--pad-l)); }
.sheet h2, .detail h2 { font-size: 17px; margin: 0 0 6px; word-break: break-all; }
button.primary, button.secondary, .tag-form button { width: 100%; margin-top: 14px; padding: 11px; border: 0; border-radius: 10px; background: var(--btn); color: var(--btn-text); font-weight: 600; }
button.secondary { background: var(--bg2); color: var(--link); }

.detail { position: fixed; inset: 0; z-index: 10; background: var(--bg); overflow-y: auto; overflow-x: hidden; }
#player { background: #000; padding: 0 var(--pad-r) 0 var(--pad-l); }
#player video { width: 100%; max-height: 56vh; display: block; background: #000; }
#player .noplay { aspect-ratio: 16 / 9; position: relative; display: grid; place-items: center; color: #fff; }
#player .noplay img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: .45; }
#player .noplay span { position: relative; padding: 0 16px; text-align: center; }
.detail-body { padding: 12px calc(14px + var(--pad-r)) calc(30px + env(safe-area-inset-bottom)) calc(14px + var(--pad-l)); }
.actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px; margin: 6px 0; }
.stars button { border: 0; background: none; font-size: 30px; padding: 0 2px; color: var(--hint); line-height: 1; }
.stars button.on { color: var(--star); }
.heart { border: 0; background: none; font-size: 30px; padding: 0 4px; }
.tag-form { display: flex; gap: 6px; margin-top: 8px; }
.tag-form input { flex: 1; min-width: 0; padding: 9px 12px; border: 0; border-radius: 10px; background: var(--bg2); }
.tag-form button { width: auto; margin: 0; padding: 9px 14px; }
.info { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 16px 0 0; font-size: 13px; }
.info dt { color: var(--hint); }
.info dd { margin: 0; word-break: break-all; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50; background: #000c; color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 14px; max-width: 90vw; }
.gate { position: fixed; inset: 0; z-index: 60; background: var(--bg); display: grid; place-items: center; text-align: center; padding: 24px; font-size: 16px; }
