:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #14161a;
  --muted: #646b76;
  --border: #e3e6eb;
  --accent: #2f6bff;
  --accent-text: #ffffff;
  --accent-soft: #e8efff;
  --danger: #c62828;
  --danger-soft: #fdecec;
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --card: #181b21;
    --text: #eceef2;
    --muted: #9aa2ae;
    --border: #2a2f38;
    --accent: #5b8cff;
    --accent-text: #ffffff;
    --accent-soft: #1d2740;
    --danger: #ff6b6b;
    --danger-soft: #3a1c1f;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --card: #181b21; --text: #eceef2; --muted: #9aa2ae; --border: #2a2f38;
  --accent: #5b8cff; --accent-text: #fff; --accent-soft: #1d2740; --danger: #ff6b6b; --danger-soft: #3a1c1f;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Leelawadee UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.wrap {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: grid;
  gap: 16px;
}
.top { display: flex; align-items: center; gap: 12px; padding-top: 8px; }
.top-text { flex: 1; min-width: 0; }
.top h1 { font-size: 1.25rem; margin: 0; line-height: 1.2; }
.top p { margin: 2px 0 0; font-size: .82rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: grid;
  gap: 12px;
}

.input-row { display: flex; gap: 8px; }
input[type="url"] {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* กัน iOS ซูมเข้าเวลาแตะ input */
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
input:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button, .primary, .secondary {
  font: inherit;
  border: 0;
  border-radius: 12px;
  min-height: 48px;
  padding: 0 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  touch-action: manipulation;
}
.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; width: 100%; }
.secondary { background: var(--accent-soft); color: var(--accent); font-weight: 600; width: 100%; }
.ghost { background: transparent; color: var(--accent); border: 1px solid var(--border); }
.wide { width: 100%; }
button:disabled { opacity: .55; cursor: default; }

.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error {
  margin: 0; padding: 12px 14px; border-radius: 12px;
  background: var(--danger-soft); color: var(--danger); font-size: .92rem;
  overflow-wrap: anywhere;
  white-space: pre-line;
}

.media { position: relative; border-radius: 12px; overflow: hidden; background: var(--bg); aspect-ratio: 16 / 9; }
.media img { width: 100%; height: 100%; object-fit: cover; display: block; }
.badge {
  position: absolute; right: 8px; bottom: 8px;
  background: rgba(0, 0, 0, .72); color: #fff; font-size: .78rem; padding: 2px 8px; border-radius: 6px;
}
h2 { font-size: 1.05rem; margin: 0; line-height: 1.35; overflow-wrap: anywhere; }
#meta { margin: -6px 0 0; font-size: .85rem; }

.qualities { border: 0; padding: 0; margin: 0; }
.qualities legend { font-size: .85rem; color: var(--muted); margin-bottom: 8px; padding: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips label { position: relative; }
.chips input { position: absolute; opacity: 0; pointer-events: none; }
.chips span {
  display: inline-flex; align-items: center; min-height: 40px; padding: 0 14px;
  border-radius: 999px; border: 1px solid var(--border); font-size: .92rem; cursor: pointer; user-select: none;
}
.chips input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.progress { display: grid; gap: 6px; }
.bar { height: 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width .3s ease; }
.bar.indeterminate > div { width: 35%; animation: slide 1.1s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }
#stage { margin: 0; font-size: .88rem; }

#done { display: grid; gap: 12px; }
.success { margin: 0; padding: 12px 14px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.actions { display: grid; gap: 8px; }
#ios-hint { margin: 0; }

footer { text-align: center; padding: 4px 0 8px; }

input[type="password"], input.field, select.field {
  font: inherit; font-size: 16px; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); width: 100%;
}
.tunnel { grid-template-columns: auto 1fr; align-items: center; }
.tunnel h2 { margin-bottom: 2px; }
.tunnel p { margin: 0 0 6px; }
.qr { background: #fff; padding: 8px; border-radius: 10px; line-height: 0; }
.qr img { width: 112px; height: 112px; image-rendering: pixelated; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: .85rem; color: var(--accent); overflow-wrap: anywhere; }

@media (min-width: 480px) {
  .actions { grid-template-columns: 1fr 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0s !important; transition: none !important; }
}

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.grid.single { grid-template-columns: 1fr; }
.tile { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--bg); border: 1px solid var(--border); }
.grid.single .tile { aspect-ratio: auto; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.grid.single .tile img { height: auto; max-height: 70vh; object-fit: contain; }
.tile-video { height: 100%; display: grid; place-content: center; text-align: center; font-size: 1.6rem; color: var(--muted); }
.tile-video small { font-size: .75rem; }
.tile-save {
  position: absolute; right: 6px; bottom: 6px; width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; background: rgba(0, 0, 0, .65); color: #fff; text-decoration: none; font-weight: 700;
}
.tile.saved::after {
  content: "✓"; position: absolute; left: 6px; top: 6px; width: 24px; height: 24px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-text); font-size: .8rem;
}



.settings p { margin: 0; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 2px; }
.settings textarea {
  font: 13px/1.4 ui-monospace, Consolas, monospace; width: 100%; resize: vertical;
  padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.settings input[type="file"] { font-size: .9rem; color: var(--muted); max-width: 100%; }
#cookie-status { color: var(--accent); }
.settings select {
  font: inherit; font-size: 16px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); width: 100%;
}
.howto { display: grid; gap: 6px; }
.settings code { font-family: ui-monospace, Consolas, monospace; background: var(--accent-soft); color: var(--accent); padding: 1px 6px; border-radius: 6px; }

.icon-btn {
  min-height: 44px; width: 44px; padding: 0; flex: none; border-radius: 50%;
  background: transparent; color: var(--muted); border: 1px solid var(--border);
}
.icon-btn:hover { color: var(--text); }

dialog.settings {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto;
  margin: auto; color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .45);
}
dialog.settings:not([open]) { display: none; }
dialog.settings::backdrop { background: rgba(0, 0, 0, .55); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dialog-head h2 { font-size: 1.1rem; }
.dialog-head .icon-btn { width: 40px; min-height: 40px; font-size: 1rem; }

.history-list { list-style: none; margin: 0; padding: 0; display: grid; }
.history-item { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--border); }
.history-item:first-child { border-top: 0; padding-top: 0; }
.history-text { flex: 1; min-width: 0; }
.history-text a {
  color: var(--text); text-decoration: none; font-weight: 600; font-size: .92rem;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere;
}
.history-text p { margin: 2px 0 0; }
.history-item.error .history-text p { color: var(--danger); }
.history .dialog-head { justify-content: flex-start; }
.history .dialog-head .icon-btn { font-size: 1.1rem; }
.history-title { flex: 1; }
.small-btn { min-height: 36px; padding: 0 12px; font-size: .85rem; flex: none; }
.history-del { width: 36px; min-height: 36px; font-size: .85rem; }

.stack { display: grid; gap: 10px; }
.stack h3, #admin-users h3 { font-size: .95rem; margin: 6px 0 0; }
.stack p { margin: 0; }
#account-name { margin: -4px 0 0; }
#pw-form, #admin-users { border-top: 1px solid var(--border); padding-top: 12px; }
.ok-text { color: var(--accent); }
.err-text { color: var(--danger); }
#login { gap: 8px; }
#login label { margin-top: 4px; }
#login button { margin-top: 8px; }
