:root {
  --bg: #f3f3f5;
  --surface: #ffffff;
  --surface-2: #ececf0;
  --border: #e1e1e6;
  --text: #1b1b1f;
  --text-2: #5d5d66;
  --text-3: #8b8b94;
  --accent: #d92440;
  --accent-text: #ffffff;
  --success: #0f7b3f;
  --success-bg: #dff6e7;
  --warning: #8a5a00;
  --warning-bg: #fff4ce;
  --error: #c42b1c;
  --error-bg: #fde7e9;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1f;
    --surface: #26262a;
    --surface-2: #333338;
    --border: #38383e;
    --text: #f2f2f4;
    --text-2: #b4b4bc;
    --text-3: #85858e;
    --accent: #ff4d63;
    --accent-text: #ffffff;
    --success: #6ccb8f;
    --success-bg: #1f3a2a;
    --warning: #f2c661;
    --warning-bg: #3d3216;
    --error: #ff9a8f;
    --error-bg: #44201d;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  -webkit-text-size-adjust: 100%;
  /* 한글을 단어 중간에서 줄바꿈하지 않음 */
  word-break: keep-all;
  overflow-wrap: break-word;
}

[hidden] { display: none !important; }

p { margin: 0; }

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

.muted { color: var(--text-2); }
.small { font-size: 13px; }
.strong { font-weight: 600; }

/* ---------- 상단 ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 4px;
}

.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand img { width: 26px; height: 26px; border-radius: 7px; }

.menu { position: relative; }
.menu summary {
  list-style: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-2);
}
.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover, .menu[open] summary { background: var(--surface-2); }
.menu summary svg { fill: currentColor; stroke: none; }

.menu-list {
  position: absolute;
  right: 0;
  top: 40px;
  z-index: 10;
  min-width: 220px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.16);
}
.menu-list button {
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.menu-list button:hover:not(:disabled) { background: var(--surface-2); }
.menu-list button:disabled { color: var(--text-3); cursor: default; }
.menu-list hr { margin: 6px 0; border: 0; border-top: 1px solid var(--border); }
.menu-info { padding: 4px 10px; color: var(--text-3); font-size: 13px; }

/* ---------- 본문 ---------- */
main {
  flex: 1;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 8px 16px 24px;
}

.url-row { display: flex; gap: 8px; }

input[type=url], input[type=password], select {
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;  /* iOS에서 포커스 시 확대 방지 */
}
input[type=url] { flex: 1; }
input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
select:disabled { opacity: 0.5; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { min-width: 128px; border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }

.card {
  margin-top: 14px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* ---------- 미리보기 ---------- */
.preview { min-height: 178px; display: flex; }
.preview > * { flex: 1; }

.preview-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
}
.big-icon { width: 40px; height: 40px; fill: var(--text-3); stroke: none; }
.big-icon.error { width: 32px; height: 32px; fill: var(--error); }
.big-icon .on-surface { fill: var(--surface); }
#previewErrorText { max-width: 560px; overflow-wrap: anywhere; }

/* 썸네일 | 제목 · 정보
           | 해상도 · 음질 칩 */
.preview-body {
  display: grid;
  grid-template-columns: 256px minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  column-gap: 16px;
  align-content: start;
}
.preview-body .thumb { grid-row: span 2; }
#previewLoading .info { grid-row: span 2; }
.chip-rows { grid-column: 2; }

.thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  align-self: start;
  overflow: hidden;
  border-radius: 8px;
  background: var(--surface-2);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.badge {
  position: absolute;
  right: 6px;
  bottom: 6px;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgb(0 0 0 / 0.8);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}

.info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.info h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
#meta { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chips { display: flex; gap: 6px; margin-top: 6px; color: var(--text-2); }
.chips svg { width: 16px; height: 16px; margin-top: 3px; }
.chip-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 2px 8px; border-radius: 4px; background: var(--surface-2); color: var(--text); font-size: 12px; }

.skeleton { background: var(--surface-2); border-radius: 4px; animation: pulse 0.8s ease-in-out infinite alternate; }
.skeleton.line { height: 18px; margin-bottom: 4px; }
.skeleton.thin { height: 12px; margin-top: 4px; }
.w80 { width: 80%; } .w60 { width: 60%; } .w40 { width: 40%; }
@keyframes pulse { to { opacity: 0.4; } }

.loading-text { display: flex; align-items: center; gap: 8px; margin-top: auto; }
.hint { color: var(--text-3); font-size: 12px; }
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 옵션 ---------- */
fieldset { margin: 0; padding: 0; border: 0; min-width: 0; }
fieldset:disabled { opacity: 0.6; }

.segment {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg);
}
.segment label { position: relative; cursor: pointer; }
.segment input { position: absolute; opacity: 0; pointer-events: none; }
.segment span { display: block; padding: 6px 18px; border-radius: 6px; color: var(--text-2); }
.segment input:checked + span { background: var(--accent); color: var(--accent-text); }
.segment input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 12px; color: var(--text-2); }

.toggles { display: flex; flex-wrap: wrap; gap: 12px 28px; margin-top: 18px; }
.toggle { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; }
.toggle input {
  appearance: none;
  position: relative;
  width: 40px;
  height: 22px;
  margin: 0;
  border-radius: 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-2);
  transition: transform 0.15s;
}
.toggle input:checked { background: var(--accent); border-color: var(--accent); }
.toggle input:checked::after { transform: translateX(18px); background: #fff; }
.toggle input:disabled { opacity: 0.4; cursor: default; }
.toggle input:disabled + span { color: var(--text-3); }
.toggle input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 알림 ---------- */
.notice {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}
.notice > div { flex: 1; min-width: 0; }
.notice.success { background: var(--success-bg); border-color: transparent; }
.notice.success #noticeTitle { color: var(--success); }
.notice.warning { background: var(--warning-bg); border-color: transparent; }
.notice.warning #noticeTitle { color: var(--warning); }
.notice.error { background: var(--error-bg); border-color: transparent; }
.notice.error #noticeTitle { color: var(--error); }
#noticeText { margin-top: 2px; white-space: pre-line; overflow-wrap: anywhere; }
.notice .close { border: 0; background: none; color: var(--text-2); font-size: 20px; line-height: 1; cursor: pointer; padding: 0 2px; }

/* ---------- 진행률 / 다운로드 (넓은 화면: 옵션 아래 카드, 휴대폰: 화면 아래 고정) ---------- */
.bottombar {
  display: flex;
  align-items: center;
  gap: 16px;
  width: calc(100% - 32px);
  max-width: 728px;
  margin: 14px auto 24px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.status { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
#status { font-size: 12px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

progress { width: 100%; height: 4px; appearance: none; border: 0; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
progress::-webkit-progress-bar { background: var(--surface-2); }
progress::-webkit-progress-value { background: var(--accent); transition: width 0.3s; }
progress::-moz-progress-bar { background: var(--accent); }

.actions { display: flex; gap: 8px; }

@media (min-width: 601px) {
  main { flex: none; padding-bottom: 0; }
}

/* ---------- 좁은 화면 (휴대폰) ---------- */
@media (max-width: 600px) {
  .preview { min-height: 150px; }
  /* 작은 썸네일 | 제목, 칩은 아래 전체 폭 */
  .preview-body { grid-template-columns: 136px minmax(0, 1fr); column-gap: 12px; }
  .preview-body .thumb { grid-row: auto; }
  .chip-rows { grid-column: 1 / -1; margin-top: 6px; }
  .info h2 { font-size: 15px; -webkit-line-clamp: 3; }
  #meta { white-space: normal; }
  .loading-text { margin-top: 4px; }

  .bottombar {
    position: sticky;
    bottom: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    width: auto;
    max-width: none;
    margin: 0;
    padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px);
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 0;
  }
  .actions .btn { flex: 1; }
  .btn.primary { min-width: 0; }
  /* 알림으로 스크롤할 때 아래 고정 바에 가리지 않게 */
  .notice { scroll-margin-bottom: 130px; }
}

/* ---------- 로그인 ---------- */
.login { flex: 1; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; }
.login .brand { justify-content: center; font-size: 20px; margin-bottom: 8px; }
.login .brand img { width: 40px; height: 40px; border-radius: 10px; }
.login .error { display: none; color: var(--error); font-size: 13px; }
.login .error:target { display: block; }
