:root {
  --bg: #f4f4f6;
  --card: #ffffff;
  --text: #17171a;
  --muted: #6b6b75;
  --line: #e3e3e8;
  --accent: #207ab7;
  --accent-text: #ffffff;
  --accent-soft: #e2eff8;
  --danger: #d93d3d;
  --ok: #1f9d55;
  --radius: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --card: #1d1d21;
    --text: #f1f1f4;
    --muted: #9a9aa6;
    --line: #2e2e35;
    --accent: #4ba3dc;
    --accent-soft: #15304a;
    --danger: #ff6b6b;
    --ok: #4cd38a;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  /* Notch, status bar and home indicator in standalone mode. */
  padding:
    calc(env(safe-area-inset-top) + 12px)
    calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 24px)
    calc(env(safe-area-inset-left) + 16px);
}

.view { max-width: 560px; margin: 0 auto; }
[hidden] { display: none !important; }

h1 { font-size: 28px; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 16px; margin: 20px 0 4px; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 8px 0 0; min-height: 0; }
.error:empty { display: none; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 4px 0 14px;
}

/* 16px+ on every field, otherwise iOS Safari zooms in on focus. */
input, button, select, textarea { font: inherit; font-size: 17px; color: inherit; }

input[type="search"], input[type="text"], input[type="password"], input:not([type]) {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  -webkit-appearance: none;
  appearance: none;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; }

label { display: block; font-size: 14px; color: var(--muted); margin-top: 12px; }
label input { margin-top: 6px; color: var(--text); }

button {
  min-height: 44px;
  border: 0;
  border-radius: 12px;
  padding: 0 18px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button:disabled { opacity: 0.55; }
.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.secondary { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.danger { background: transparent; color: var(--danger); width: 100%; margin-top: 24px; }
.small { min-height: 36px; padding: 0 12px; font-size: 15px; }
.link-btn { background: none; color: var(--accent); padding: 0 4px; min-height: 36px; font-size: 16px; }
.icon-btn {
  background: var(--card);
  color: var(--muted);
  width: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
}

.row { display: flex; gap: 10px; margin-top: 10px; }
.row button { flex: 1; }

/* Login */
.login { margin-top: 10dvh; text-align: center; padding: 24px 18px; }
.login label { text-align: left; }
.login .primary { width: 100%; margin-top: 18px; }
.login-icon { border-radius: 16px; margin-bottom: 8px; }

/* Search results and playlist preview */
.panel h2 { margin-bottom: 4px; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.list { list-style: none; margin: 0; padding: 0; }

.result {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px 0;
  background: none;
  text-align: left;
  border-radius: 0;
  border-top: 1px solid var(--line);
  min-height: 64px;
}
.list li:first-child .result { border-top: 0; }
.result .add { color: var(--accent); font-weight: 600; font-size: 15px; }

.thumb {
  width: 52px;
  height: 52px;
  flex: none;
  border-radius: 8px;
  object-fit: cover;
  background: var(--line);
}
.meta { flex: 1; min-width: 0; }
.title, .sub, .status {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title { font-weight: 600; }
.sub { color: var(--muted); font-size: 14px; }

.check {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0;
  padding: 10px 0;
  border-top: 1px solid var(--line);
  color: var(--text);
  font-size: 16px;
  min-height: 48px;
}
.list li:first-child .check { border-top: 0; }
.check input { width: 22px; height: 22px; flex: none; margin: 0; accent-color: var(--accent); }
.playlist-list { max-height: 50dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; margin: 4px 0 8px; }
.option { border-top: 0; color: var(--muted); font-size: 15px; }
.panel .primary { width: 100%; margin-top: 8px; }

/* History */
.history-head { display: flex; justify-content: space-between; align-items: center; margin: 22px 2px 8px; }
.jobs { list-style: none; margin: 0; padding: 0; }
.job {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.job .status { font-size: 14px; color: var(--muted); }
.job.done .status, .job.duplicate .status { color: var(--ok); }
.job.error .status { color: var(--danger); white-space: normal; }
.progress { height: 4px; border-radius: 2px; background: var(--line); margin-top: 6px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--accent); width: 0; transition: width 0.4s; }
.empty { text-align: center; padding: 18px 8px; }

/* Settings sheet */
.sheet {
  border: 0;
  border-radius: 18px;
  background: var(--card);
  color: var(--text);
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - env(safe-area-inset-top) - 40px);
  padding: 16px 18px calc(env(safe-area-inset-bottom) + 18px);
  overflow-y: auto;
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sheet .secondary { width: 100%; margin-top: 8px; }
.steps { padding-left: 20px; color: var(--muted); font-size: 15px; }
.steps li { margin: 6px 0; }
code { font-size: 14px; background: var(--bg); padding: 1px 5px; border-radius: 5px; word-break: break-all; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 15px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
