:root {
  --bg: #ffffff;
  --bg-soft: #f2f2f2;
  --bg-hover: #e5e5e5;
  --text: #0f0f0f;
  --text-sub: #606060;
  --border: #d9d9d9;
  --accent: #10b981;
  --skeleton: #e8e8e8;
  --topbar-h: 56px;
  --sidebar-w: 220px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0f0f;
    --bg-soft: #222222;
    --bg-hover: #333333;
    --text: #f1f1f1;
    --text-sub: #aaaaaa;
    --border: #303030;
    --skeleton: #262626;
  }
}

:root[data-theme="dark"] {
  --bg: #0f0f0f;
  --bg-soft: #222222;
  --bg-hover: #333333;
  --text: #f1f1f1;
  --text-sub: #aaaaaa;
  --border: #303030;
  --skeleton: #262626;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  font-size: 14px;
}

a { color: inherit; text-decoration: none; }

svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

/* 상단 바 */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h); z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 0 16px; background: var(--bg);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; }
.icon-btn:hover { background: var(--bg-hover); }
.logo { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 19px; letter-spacing: -0.5px; }
.logo-mark { width: 30px; height: 22px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; }
.logo-mark svg { width: 16px; height: 16px; fill: #fff; stroke: none; }

.search { flex: 1; max-width: 640px; display: flex; }
.search-box { position: relative; flex: 1; }
.search input {
  width: 100%; height: 40px; padding: 0 16px; font-size: 16px;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-right: 0; border-radius: 20px 0 0 20px; outline: none;
}
.search input:focus { border-color: var(--accent); }
.search-btn { width: 64px; height: 40px; border: 1px solid var(--border); border-radius: 0 20px 20px 0; background: var(--bg-soft); display: grid; place-items: center; }
.search-btn svg { width: 20px; height: 20px; }
.suggestions {
  position: absolute; top: 44px; left: 0; right: 0; margin: 0; padding: 8px 0; list-style: none;
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 4px 24px rgba(0, 0, 0, .15);
}
.suggestions li { padding: 6px 16px; cursor: pointer; font-size: 15px; }
.suggestions li:hover, .suggestions li.active { background: var(--bg-hover); }

/* 사이드바 */
.sidebar {
  position: fixed; top: var(--topbar-h); left: 0; bottom: 0; width: var(--sidebar-w);
  padding: 12px; overflow-y: auto; background: var(--bg); z-index: 15;
}
.sidebar a { display: flex; align-items: center; gap: 20px; padding: 10px 12px; border-radius: 10px; }
.sidebar a:hover { background: var(--bg-hover); }
.sidebar a.active { background: var(--bg-soft); font-weight: 600; }
body.sidebar-collapsed .sidebar { display: none; }

.content { padding: calc(var(--topbar-h) + 8px) 24px 40px calc(var(--sidebar-w) + 24px); min-height: 100vh; }
body.sidebar-collapsed .content { padding-left: 24px; }

/* 카테고리 칩 */
.chips { position: sticky; top: var(--topbar-h); z-index: 10; background: var(--bg); display: flex; gap: 12px; padding: 12px 0; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex-shrink: 0; padding: 6px 12px; border-radius: 8px; background: var(--bg-soft); font-weight: 500; white-space: nowrap; }
.chip:hover { background: var(--bg-hover); }
.chip.active { background: var(--text); color: var(--bg); }

/* 영상 카드 그리드 */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 40px 16px; padding-top: 12px; }
.card { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.thumb { position: relative; aspect-ratio: 16 / 9; border-radius: 12px; overflow: hidden; background: var(--skeleton); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform .2s; }
.card:hover .thumb img { transform: scale(1.03); }
.duration { position: absolute; right: 6px; bottom: 6px; padding: 1px 5px; border-radius: 4px; background: rgba(0, 0, 0, .8); color: #fff; font-size: 12px; font-weight: 500; }
.duration:empty { display: none; }
.card-body { display: flex; gap: 12px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; flex-shrink: 0; background: var(--skeleton); }
.avatar[hidden] { display: none !important; }
.card-text { display: flex; flex-direction: column; min-width: 0; }
.card-title { margin: 0 0 4px; font-size: 16px; font-weight: 600; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-channel, .card-meta { color: var(--text-sub); font-size: 14px; line-height: 1.5; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-channel:hover { color: var(--text); }

/* 가로형 카드 (관련 영상, 검색 결과) */
.list { display: flex; flex-direction: column; gap: 12px; }
.list .card { flex-direction: row; gap: 10px; }
.list .thumb { width: 168px; flex-shrink: 0; border-radius: 8px; }
.list .avatar { display: none; }
.list .card-title { font-size: 14px; }
.list .card-channel, .list .card-meta { font-size: 12px; }

.search-results { max-width: 1100px; padding-top: 16px; gap: 16px; }
.search-results .thumb { width: min(360px, 45%); border-radius: 12px; }
.search-results .card { gap: 16px; }
.search-results .card-title { font-size: 18px; }
.search-results .avatar { display: block; width: 24px; height: 24px; }
.search-results .card-body { flex-direction: column; gap: 8px; }

/* 시청 페이지 */
.watch { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 24px; padding-top: 16px; max-width: 1700px; }
.player { position: relative; aspect-ratio: 16 / 9; border-radius: 12px; overflow: hidden; background: #000; }
.player iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.watch-title { margin: 12px 0 8px; font-size: 20px; font-weight: 700; line-height: 1.4; }
.owner { display: flex; align-items: center; gap: 12px; }
.watch-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.popup-btn { height: 36px; padding: 0 16px; border-radius: 18px; background: var(--bg-soft); font-weight: 600; }
.popup-btn:hover { background: var(--bg-hover); }
.owner img { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; background: var(--skeleton); }
.owner-name { font-weight: 600; font-size: 16px; }
.owner-subs { color: var(--text-sub); font-size: 12px; }
.description { margin-top: 12px; padding: 12px; border-radius: 12px; background: var(--bg-soft); white-space: pre-wrap; word-break: break-word; line-height: 1.6; max-height: 120px; overflow: hidden; cursor: pointer; }
.description.open { max-height: none; cursor: auto; }
.description-meta { font-weight: 600; margin-bottom: 4px; }
.description a { color: #3ea6ff; }

/* 채널 페이지 */
.channel-banner { aspect-ratio: 6 / 1; border-radius: 16px; background: var(--bg-soft) center / cover no-repeat; margin-top: 8px; }
.channel-head { display: flex; align-items: center; gap: 20px; padding: 20px 0; }
.channel-head img { width: 120px; height: 120px; border-radius: 50%; object-fit: cover; background: var(--skeleton); }
.channel-head h1 { margin: 0 0 6px; font-size: 32px; }
.channel-head p { margin: 0; color: var(--text-sub); max-width: 700px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.section-title { font-size: 20px; margin: 16px 0 0; }

/* 로딩/오류 */
.skeleton .thumb, .skeleton .avatar, .skeleton .line { background: var(--skeleton); animation: pulse 1.2s ease-in-out infinite; }
.skeleton .line { height: 14px; border-radius: 4px; margin-bottom: 8px; }
@keyframes pulse { 50% { opacity: .5; } }
.message { padding: 80px 16px; text-align: center; color: var(--text-sub); font-size: 16px; }
.message button { margin-top: 16px; padding: 8px 18px; border-radius: 18px; background: var(--bg-soft); font-weight: 500; }

@media (max-width: 1100px) {
  .watch { grid-template-columns: 1fr; }
}

/* 단축키 설정 창 */
.settings {
  max-height: calc(100vh - 32px); overflow-y: auto;
  width: min(380px, calc(100vw - 32px)); padding: 24px; border: 0; border-radius: 16px;
  background: var(--bg); color: var(--text); box-shadow: 0 8px 40px rgba(0, 0, 0, .3);
}
.settings::backdrop { background: rgba(0, 0, 0, .5); }
.settings h2 { margin: 0 0 6px; font-size: 18px; }
.settings p { margin: 0 0 16px; color: var(--text-sub); line-height: 1.5; }
.settings .hint { margin: 16px 0 0; font-size: 12px; }
.settings .section-gap { margin-top: 24px; }
.shortcut-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.settings kbd {
  min-width: 40px; padding: 4px 10px; border: 1px solid var(--border); border-bottom-width: 3px; border-radius: 8px;
  background: var(--bg-soft); font: 600 15px/1.4 ui-monospace, monospace; text-align: center;
}
.settings button { width: 100%; height: 40px; border-radius: 20px; background: var(--bg-soft); font-weight: 600; }
.settings button:hover { background: var(--bg-hover); }
.settings button.primary { background: var(--accent); color: #fff; }
.settings button.primary.recording { animation: pulse 1s ease-in-out infinite; }
.settings-actions { display: flex; gap: 8px; margin-top: 8px; }
.settings .close { margin-top: 16px; background: none; border: 1px solid var(--border); }

.account-section { padding-bottom: 20px; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
.account-section h2 { margin-bottom: 12px; }
.settings-link[hidden] { display: none; }
.settings-link {
  display: flex; align-items: center; justify-content: center; height: 40px; margin-bottom: 8px;
  border-radius: 20px; background: var(--accent); color: #fff; font-weight: 600;
}

/* 로그인 / 가입 화면 */
body.auth-mode .sidebar, body.auth-mode .search, body.auth-mode .search-open-btn { display: none !important; }
body.auth-mode .content { padding-left: 16px; padding-right: 16px; }
.auth { display: flex; justify-content: center; padding: 48px 0; }
.auth-card { width: min(380px, 100%); padding: 28px 24px; border: 1px solid var(--border); border-radius: 16px; }
.auth-title { margin: 0 0 20px; font-size: 22px; text-align: center; }
.auth-tabs { display: flex; gap: 4px; padding: 4px; margin-bottom: 20px; border-radius: 12px; background: var(--bg-soft); }
.auth-tab { flex: 1; height: 36px; border-radius: 9px; font-weight: 600; color: var(--text-sub); }
.auth-tab.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; }
.field input {
  height: 44px; padding: 0 14px; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.auth-hint { margin: 0; font-size: 12px; color: var(--text-sub); line-height: 1.5; }
.auth-message { margin: 0; min-height: 1em; font-size: 13px; color: #e5484d; line-height: 1.5; }
.auth-message:empty { display: none; }
.auth-message.ok { color: var(--accent); font-weight: 600; }
.auth-submit { height: 44px; border-radius: 22px; background: var(--accent); color: #fff; font-weight: 700; font-size: 15px; }
.auth-submit:disabled { opacity: .6; cursor: default; }

/* 관리자 화면 */
.admin { max-width: 720px; margin: 0 auto; padding: 16px 0 40px; }
.admin-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.admin-head h1 { margin: 0; font-size: 22px; }
.admin-refresh { padding: 8px 16px; border-radius: 18px; background: var(--bg-soft); font-weight: 600; }
.admin-section h2 { margin: 28px 0 10px; font-size: 16px; }
.admin-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.admin-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; }
.admin-row + .admin-row { border-top: 1px solid var(--border); }
.admin-user { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.admin-user strong { font-size: 15px; overflow: hidden; text-overflow: ellipsis; }
.admin-user span { font-size: 12px; color: var(--text-sub); }
.admin-actions { display: flex; gap: 6px; flex-shrink: 0; }
.admin-actions button { padding: 7px 14px; border-radius: 16px; background: var(--bg-soft); font-weight: 600; font-size: 13px; }
.admin-actions button.approve { background: var(--accent); color: #fff; }
.admin-actions button.danger { color: #e5484d; }
.admin-actions button:disabled { opacity: .5; }
.admin-empty { margin: 0; color: var(--text-sub); }

.search-back, .search-open-btn { display: none; }

/* 쇼츠: 세로로 한 개씩 */
body.shorts-mode .content { padding-bottom: 0; }
.shorts {
  height: calc(100dvh - var(--topbar-h) - 8px); overflow-y: auto; scroll-snap-type: y mandatory;
  scrollbar-width: none; outline: none;
}
.shorts::-webkit-scrollbar { display: none; }
.short { height: 100%; scroll-snap-align: start; scroll-snap-stop: always; display: flex; align-items: center; justify-content: center; padding: 8px 0; }
.short-frame { position: relative; height: 100%; aspect-ratio: 9 / 16; max-width: 100%; border-radius: 14px; overflow: hidden; background: #000; }
.short-frame img { width: 100%; height: 100%; object-fit: cover; display: block; }
.short-frame iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; z-index: 2; }
.short-info { position: absolute; left: 0; right: 0; bottom: 0; padding: 40px 14px 16px; color: #fff; background: linear-gradient(transparent, rgba(0, 0, 0, .75)); }
.short-title { font-weight: 600; font-size: 15px; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.short-meta { margin-top: 4px; font-size: 12px; opacity: .85; }
.shorts .message { padding-top: 120px; }

/* 가리기: 아무것도 없는 빈 화면 */
.cover { position: fixed; inset: 0; z-index: 1000; background: var(--bg); cursor: default; }
.cover[hidden] { display: none; }

/* ---------- 휴대폰 ---------- */
@media (max-width: 800px) {
  :root { --topbar-h: 48px; --bottom-h: 56px; }

  .topbar { padding: 0 4px 0 12px; gap: 4px; }
  #menuBtn, .shortcut-section, .popup-btn, body.auth-mode .settings-btn { display: none; }
  .shorts { height: calc(100dvh - var(--topbar-h) - var(--bottom-h) - env(safe-area-inset-bottom)); }
  .short { padding: 0; }
  .short-frame { border-radius: 0; width: 100%; aspect-ratio: auto; }
  body.shorts-mode .content { padding-bottom: calc(var(--bottom-h) + env(safe-area-inset-bottom)); }
  .account-section { padding-bottom: 0; margin-bottom: 0; border-bottom: 0; }
  .admin { padding: 16px 12px 40px; }
  body.auth-mode .content { padding-left: 16px; padding-right: 16px; }
  .logo-text { font-size: 17px; }

  /* 검색: 돋보기를 누르면 상단 바 전체가 검색창이 된다 */
  .search { display: none; }
  .search-open-btn { display: grid; }
  body.search-open .search { display: flex; position: absolute; inset: 0; align-items: center; gap: 4px; padding: 0 8px 0 4px; background: var(--bg); max-width: none; }
  body.search-open .search-back { display: grid; flex-shrink: 0; }
  .search input { height: 36px; border-radius: 18px; border: 0; background: var(--bg-soft); padding: 0 14px; }
  .search-btn { display: none; }
  .suggestions { position: fixed; top: var(--topbar-h); left: 0; right: 0; border-radius: 0; border: 0; border-bottom: 1px solid var(--border); }
  .suggestions li { padding: 12px 16px; }

  /* 사이드바 → 하단 탭바 */
  .sidebar, body.sidebar-collapsed .sidebar {
    display: flex; top: auto; bottom: 0; width: 100%; height: calc(var(--bottom-h) + env(safe-area-inset-bottom));
    padding: 0 0 env(safe-area-inset-bottom); overflow: visible; border-top: 1px solid var(--border); z-index: 20;
  }
  .sidebar a { flex: 1; flex-direction: column; justify-content: center; gap: 2px; padding: 0; border-radius: 0; font-size: 10px; }
  .sidebar a svg { width: 22px; height: 22px; }
  .sidebar a:hover { background: none; }
  .sidebar a.active { background: none; font-weight: 700; }
  .sidebar a.active svg { stroke-width: 2.4; }

  .content, body.sidebar-collapsed .content { padding: var(--topbar-h) 0 calc(var(--bottom-h) + env(safe-area-inset-bottom) + 16px); }

  .chips { padding: 8px 12px; gap: 8px; }
  .chip { padding: 5px 12px; font-size: 13px; }

  /* 영상 카드: 화면 꽉 차는 썸네일 */
  .grid { grid-template-columns: 1fr; gap: 20px; padding-top: 4px; }
  .grid .thumb, .list .thumb, .search-results .thumb { width: 100%; border-radius: 0; }
  .card-body { padding: 0 12px; }
  .card-title { font-size: 15px; }
  .card-channel, .card-meta { font-size: 12px; display: inline; }
  .card-channel:not(:empty) + .card-meta:not(:empty)::before { content: ' · '; }
  .card-text { display: block; }
  .card-title { margin-bottom: 2px; }
  .list, .search-results { gap: 20px; padding-top: 4px; }
  .list .card, .search-results .card { flex-direction: column; gap: 12px; }
  .list .avatar, .search-results .avatar { display: block; width: 36px; height: 36px; }
  .search-results .card-body { flex-direction: row; gap: 12px; }
  .list .card-title, .search-results .card-title { font-size: 15px; }
  .list .card-channel, .list .card-meta { font-size: 12px; }

  /* 시청 화면: 플레이어는 위에 고정, 아래로 정보와 관련 영상 */
  .watch { display: block; padding-top: 0; }
  .watch > div:first-child { display: contents; }
  .player { position: sticky; top: var(--topbar-h); z-index: 12; border-radius: 0; }
  .watch-title { font-size: 17px; margin: 12px 12px 10px; }
  .owner { padding: 0 12px; }
  .owner img { width: 34px; height: 34px; }
  .owner-name { font-size: 14px; }
  .description { margin: 12px 12px 0; max-height: 96px; font-size: 13px; }
  .watch aside { margin-top: 20px; }
  .watch .message { padding: 24px 12px; }

  /* 채널 */
  .channel-banner { border-radius: 0; margin-top: 0; aspect-ratio: 4 / 1; }
  .channel-head { padding: 16px 12px; gap: 14px; }
  .channel-head img { width: 64px; height: 64px; }
  .channel-head h1 { font-size: 20px; margin-bottom: 4px; }
  .channel-head p { font-size: 12px; }
  .section-title { margin: 0 12px 8px; font-size: 16px; }
}

/* 큰 휴대폰 가로 / 태블릿: 2열 */
@media (min-width: 561px) and (max-width: 800px) {
  .grid { grid-template-columns: 1fr 1fr; gap: 24px 12px; padding: 4px 12px 0; }
  .grid .thumb { border-radius: 10px; }
  .grid .card-body { padding: 0; }
}

@media (hover: none) {
  .card:hover .thumb img { transform: none; }
}
