:root {
  color-scheme: light dark;
  --accent-text: #c8102e;
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1a1d23;
  --muted: #667085;
  --line: #e4e7ec;
  --accent: #c8102e;
  --accent-ink: #ffffff;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 12px rgba(16,24,40,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --card: #1b1e24;
    --text: #eef0f3;
    --muted: #9aa3b2;
    --line: #2c313a;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --accent-text: #ff6b7d;
  }
}
body:has(dialog[open]) { overflow: hidden; }
:is(.btn, .x, .cats button, input, select, textarea, summary):focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Noto Sans Thai", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px; }

.top { background: var(--card); border-bottom: 1px solid var(--line); padding: 20px 0 10px; position: sticky; top: 0; z-index: 5; }
.top h1 { margin: 0; font-size: 1.4rem; }
.sub { margin: 2px 0 12px; color: var(--muted); font-size: .95rem; }
#search {
  width: 100%; padding: 11px 14px; font: inherit; font-size: 1rem;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}
#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.cats { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 4px; scrollbar-width: none; }
.cats button {
  flex: none; font: inherit; font-size: .9rem; padding: 6px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--card); color: var(--text); cursor: pointer;
}
.cats button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.grid {
  display: grid; gap: 16px; padding: 20px 0 48px;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden;
  cursor: pointer; box-shadow: var(--shadow); transition: transform .15s, box-shadow .15s;
  text-align: left; padding: 0; font: inherit; color: inherit;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(16,24,40,.14); }
.card:focus-visible { outline: 2px solid var(--accent); }
.thumb, .ph { aspect-ratio: 4 / 5; width: 100%; display: block; }
.thumb { object-fit: cover; object-position: center top; background: var(--line); }
.ph { display: grid; place-items: center; padding: 16px; text-align: center; color: #fff; font-weight: 700; font-size: 1.1rem; }
.card-body { padding: 10px 12px 14px; }
.card-title { display: block; margin: 2px 0 0; font-size: 1rem; font-weight: 700; }
.tag { display: inline-block; font-size: .75rem; color: var(--accent-text); font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

dialog {
  border: none; padding: 0; border-radius: 16px; background: var(--card); color: var(--text);
  width: min(1040px, calc(100vw - 32px)); max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow: hidden;
  overscroll-behavior: contain;
}
dialog::backdrop { background: rgba(0,0,0,.55); }
.close-row { position: absolute; right: 10px; top: 10px; margin: 0; z-index: 2; }
.x { border: none; background: var(--card); color: var(--text); width: 36px; height: 36px; border-radius: 50%; font-size: 1.1rem; cursor: pointer; box-shadow: var(--shadow); }
.modal-body { display: grid; grid-template-columns: 1fr 1.1fr; max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); }
.m-img { background: var(--bg); display: grid; place-items: center; }
.m-img img { width: 100%; height: 100%; object-fit: contain; max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); display: block; }
.m-img .ph { height: 100%; }
.m-info { padding: 24px; overflow-y: auto; }
.m-info h2 { margin: 2px 0 6px; font-size: 1.3rem; padding-right: 36px; }
.desc { color: var(--muted); margin: 0 0 16px; }
.fields { display: grid; gap: 12px; margin-bottom: 6px; }
.fields label { font-size: .88rem; font-weight: 600; display: block; margin-bottom: 4px; }
.fields .req { color: var(--accent-text); }
.fields .hint { font-weight: 400; color: var(--muted); font-size: .8rem; }
.fields input, .fields select, .fields textarea, .preview textarea {
  width: 100%; font: inherit; padding: 9px 11px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.fields input.missing, .fields textarea.missing { border-color: var(--accent); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.preview { margin-top: 16px; }
.preview summary { cursor: pointer; color: var(--muted); font-size: .9rem; }
.preview textarea { margin-top: 8px; font-size: .82rem; resize: vertical; }
.actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; }
.btn {
  font: inherit; font-weight: 600; padding: 11px 18px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); flex: 1; }
.tip { font-size: .88rem; color: var(--muted); margin: 12px 0 0; background: var(--bg); padding: 10px 12px; border-radius: 8px; }
.tip:empty { display: none; }

.toast {
  position: fixed; inset: auto; left: 50%; bottom: 24px; top: auto; right: auto; margin: 0; border: 0;
  width: max-content; max-width: calc(100vw - 32px); overflow: visible;
  transform: translateX(-50%) translateY(20px);
  background: #1a1d23; color: #fff; padding: 10px 18px; border-radius: 10px; opacity: 0;
  transition: .2s; pointer-events: none; z-index: 100;
}.toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 720px) {
  .top { padding: 8px 0 4px; }
  .top h1 { font-size: 1.1rem; }
  .sub { display: none; }
  #search { padding: 8px 12px; }
  .cats { padding: 8px 0 4px; }
  /* dialog itself never scrolls, so the close button stays put; .modal-body scrolls */
  dialog { width: calc(100vw - 16px); max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); overflow: hidden; }
  .modal-body { grid-template-columns: 1fr; max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
  .m-img img { max-height: 38vh; max-height: 38dvh; }
  .m-img .ph { aspect-ratio: 16 / 9; }
  .m-info { padding: 18px; overflow: visible; }
  .preview textarea, .fields input, .fields select, .fields textarea { font-size: 16px; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .row2 { grid-template-columns: 1fr; }
}
