:root {
  --bg: #f6f7f9;
  --fg: #1d2129;
  --muted: #6b7280;
  --card: #ffffff;
  --border: #d9dde3;
  --accent: #2563eb;
  --error: #b91c1c;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --run: #1d4ed8;
  --run-bg: #dbeafe;
  --bad-bg: #fee2e2;
  --soft: #eef0f3;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --fg: #e6e8eb;
    --muted: #9aa1ab;
    --card: #1e2127;
    --border: #343941;
    --accent: #60a5fa;
    --error: #f87171;
    --warn: #fcd34d;
    --warn-bg: #3a2f12;
    --ok: #4ade80;
    --ok-bg: #12321f;
    --run: #93c5fd;
    --run-bg: #172a4a;
    --bad-bg: #3b1717;
    --soft: #272b32;
  }
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
}

h1 { font-size: 1.4rem; margin: 0; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}

/* --- 로그인: PIN 키패드 --------------------------------------------------------- */

.pin-screen {
  width: 100%;
  max-width: 420px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 72px 24px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.pin-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
}
.pin-logo {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  background: var(--fg);
  color: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
}
.pin-title { margin: 6px 0 0; font-size: 1.75rem; font-weight: 800; letter-spacing: -0.5px; }
.pin-guide { margin: 0; font-size: 1.1rem; color: var(--muted); }

.pin-form {
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.pin-fallback {
  width: 100%;
  margin-top: 32px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.pin-form.has-js .pin-fallback { display: none; }

.pin-dots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-top: 40px;
  min-height: 20px;
}
.pin-dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2.5px solid var(--muted);
}
.pin-dot.is-filled { border-color: var(--fg); background: var(--fg); }
.pin-dots.is-shaking { animation: pin-shake 0.4s ease-in-out; }
@keyframes pin-shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-10px); }
  40%, 80% { transform: translateX(10px); }
}
@media (prefers-reduced-motion: reduce) {
  .pin-dots.is-shaking { animation: none; }
}

.pin-message {
  margin: 18px 0 0;
  min-height: 24px;
  font-weight: 500;
  color: var(--error);
  text-align: center;
}

.pin-keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  width: 100%;
  margin-top: auto;
  padding-top: 28px;
}
.pin-key {
  height: 76px;
  padding: 0;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--fg);
  font-size: 1.9rem;
  font-weight: 500;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.pin-key:active { background: var(--soft); }
.pin-key-action {
  border: none;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pin-key:disabled { cursor: default; opacity: 0.5; }
.pin-key:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.pin-hint { margin: 18px 0 0; font-size: 0.95rem; color: var(--muted); text-align: center; }

/* PC: 키패드를 화면 아래에 붙이지 않고 가운데 묶음으로 */
@media (min-width: 768px) and (min-height: 700px) {
  .pin-screen { justify-content: center; padding-top: 32px; }
  .pin-form { flex: 0; }
  .pin-keypad { margin-top: 28px; }
}

input {
  font: inherit;
  font-size: 1.3rem;
  letter-spacing: 0.2em;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}

button {
  font: inherit;
  padding: 10px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button.secondary {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
}

.error { color: var(--error); margin: 0; }
.muted { color: var(--muted); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.topbar form { margin: 0; }

.main {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
}

/* --- 메인 화면 ------------------------------------------------------------------- */

h2 { font-size: 1.05rem; margin: 0; }
.small { font-size: 0.85rem; }
button.small { padding: 6px 12px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.danger { color: var(--error); border-color: var(--error); }

.main { display: flex; flex-direction: column; gap: 16px; }
.main .card { padding: 16px; }

select,
.form input[type="url"] {
  font: inherit;
  font-size: 1rem;
  letter-spacing: normal;
  width: 100%;
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}

.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label, .field legend { font-size: 0.9rem; color: var(--muted); }
.grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) {
  .grid { grid-template-columns: 1fr 1fr; }
}
.choice {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px 10px;
  margin: 0;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 8px 20px;
}
.choice legend { padding: 0 4px; }
.choice label { display: flex; align-items: center; gap: 6px; }

.hint { margin: 0; font-size: 0.88rem; color: var(--muted); }
.hint.warn {
  color: var(--warn);
  background: var(--warn-bg);
  border-radius: 8px;
  padding: 8px 10px;
}
.warn { color: var(--warn); }

/* 작업 카드 */
.jobs { display: flex; flex-direction: column; gap: 16px; }
.jobs:empty { display: none; }
.job { display: flex; flex-direction: column; gap: 10px; }
.job-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.job-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.job-title h2 {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.job-head-right { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.kind {
  flex-shrink: 0;
  font-size: 0.75rem;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--soft);
  color: var(--muted);
}

.badge {
  display: inline-block;
  white-space: nowrap;
  font-size: 0.78rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--soft);
  color: var(--muted);
}
.badge.s-done { background: var(--ok-bg); color: var(--ok); }
.badge.s-running, .badge.s-expanding, .badge.s-downloading, .badge.s-normalizing {
  background: var(--run-bg); color: var(--run);
}
.badge.s-partial { background: var(--warn-bg); color: var(--warn); }
.badge.s-failed { background: var(--bad-bg); color: var(--error); }

.job-progress { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; }
.bar { height: 8px; border-radius: 4px; background: var(--soft); overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); transition: width 0.4s ease; }

.stats {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 12px;
  margin: 0;
  font-size: 0.9rem;
}
.stats dt { color: var(--muted); }
.stats dd { margin: 0; min-width: 0; word-break: break-word; }

.player { display: flex; flex-direction: column; gap: 6px; }
.player audio { width: 100%; }
.player-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.now-playing { font-size: 0.85rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1 1 0; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented label { position: relative; padding: 4px 12px; font-size: 0.85rem; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--accent); color: #fff; }
.segmented label:has(input:disabled) { opacity: 0.4; cursor: not-allowed; }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }
a.button {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  font-size: 0.95rem;
}
.actions button { padding: 8px 14px; font-size: 0.95rem; }

button.icon, a.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 34px;
  padding: 0 6px;
  border-radius: 6px;
  background: transparent;
  color: var(--fg);
  border: 1px solid transparent;
  text-decoration: none;
  font-size: 1rem;
}
button.icon:hover, a.icon:hover { border-color: var(--border); }

/* 플레이리스트 항목 표 */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table.items { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.items th {
  text-align: left;
  font-weight: 500;
  color: var(--muted);
  font-size: 0.8rem;
  border-bottom: 1px solid var(--border);
  padding: 4px;
}
table.items td { padding: 6px 4px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.items tr:last-child td { border-bottom: 0; }
table.items tr.playing td { background: var(--soft); }
.c-sel { width: 28px; }
.c-idx { width: 28px; color: var(--muted); text-align: right; }
.c-title { min-width: 0; max-width: 0; width: 100%; }
.c-title .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-title .meta { font-size: 0.8rem; color: var(--muted); word-break: break-word; }
.c-title .meta.error { color: var(--error); }
.c-status { white-space: nowrap; }
.c-act { white-space: nowrap; text-align: right; }
@media (max-width: 420px) {
  table.items { font-size: 0.85rem; }
  .c-idx { width: 22px; }
  button.icon, a.icon { min-width: 30px; height: 30px; }
}

/* 변환 적용 */
.apply { border-top: 1px solid var(--border); padding-top: 8px; }
.apply summary { cursor: pointer; font-weight: 500; padding: 4px 0; }
.apply-form { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.apply-form .row { display: flex; flex-wrap: wrap; gap: 8px 12px; }
.apply-form label.inline { display: flex; align-items: center; gap: 6px; font-size: 0.9rem; }
.apply-form select { width: auto; }
.estimate { margin: 0; font-size: 0.9rem; }
.apply-form button { align-self: flex-start; }

/* 최근 작업 */
.recent { display: flex; flex-direction: column; gap: 8px; }
.recent-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.recent-list { list-style: none; margin: 0; padding: 0; }
.recent-list li + li { border-top: 1px solid var(--border); }
.recent-item {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "title badge" "sub badge";
  align-items: center;
  gap: 0 8px;
  padding: 8px 2px;
  background: transparent;
  color: var(--fg);
  border-radius: 0;
  text-align: left;
}
.recent-item:hover { background: var(--soft); }
.r-title { grid-area: title; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.r-sub { grid-area: sub; font-size: 0.8rem; }
.recent-item .badge { grid-area: badge; }

/* yt-dlp 업데이트 안내 */
.notice {
  background: var(--warn-bg);
  color: var(--warn);
  border-color: var(--warn);
}
.notice p {
  margin: 0.35rem 0 0;
}
/* 스크립트가 없을 때는 키패드용 표시(점·자동 확인 안내)를 숨긴다 */
.pin-form:not(.has-js) .pin-dots,
.pin-form:not(.has-js) .pin-hint { display: none; }
