:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --text: #1b1b22;
  --muted: #6b6b78;
  --border: #e2e2e8;
  --accent: #5b4bdb;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 4px 12px rgba(0, 0, 0, .06);
  --c-red: #d9363e;
  --c-purple: #8a3fd1;
  --c-green: #2e9e5b;
  --c-blue: #2a8fd6;
  --c-yellow: #d9a514;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121216;
    --surface: #1c1c22;
    --text: #ececf1;
    --muted: #9a9aa8;
    --border: #2e2e38;
    --accent: #8f82ff;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 4px 12px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body.modal-open { overflow: hidden; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
}

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-left { display: flex; align-items: center; gap: 14px; min-width: 0; flex: 1; }
.series-picker { min-width: 0; }
.brand { font-weight: 700; color: var(--muted); white-space: nowrap; }
.topbar-right { color: var(--muted); font-size: 14px; white-space: nowrap; }

.series-picker select {
  font: inherit; font-weight: 600; font-size: 15px;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 34px 7px 12px;
  width: 100%; max-width: 520px;
  text-overflow: ellipsis;
  cursor: pointer;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
.series-picker select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

@media (max-width: 600px) {
  .topbar { gap: 10px; }
  .brand { display: none; }
  .series-picker { flex: 1; }
  /* 16px stops iOS Safari from zooming in when the dropdown is tapped. */
  .series-picker select { font-size: 16px; max-width: none; }
  .topbar-right { font-size: 13px; }
}

/* Grid */
main { max-width: 1400px; margin: 0 auto; padding: 16px; }

.status { color: var(--muted); padding: 48px 0; text-align: center; }
.status:empty { display: none; }

.set-section + .set-section { margin-top: 28px; }
.set-title {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px;
  margin: 0 0 12px; font-size: 16px; font-weight: 700;
}
.set-title small { color: var(--muted); font-weight: 400; font-size: 13px; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
}
@media (max-width: 520px) {
  main { padding: 12px max(10px, env(safe-area-inset-right)) 12px max(10px, env(safe-area-inset-left)); }
  /* minmax(0, …) so long one-line captions can't widen a column past the screen. */
  .card-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .set-section + .set-section { margin-top: 22px; }
  .card-caption { font-size: 11px; margin-top: 4px; }
}

.card-tile {
  all: unset;
  display: block; cursor: pointer; min-width: 0;
  border-radius: 8px;
  transition: transform .12s ease;
}
/* Only lift on real hover; on touch screens :hover sticks after a tap. */
@media (hover: hover) {
  .card-tile:hover { transform: translateY(-3px); }
}
.card-tile:active .card-thumb { opacity: .8; }
.card-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.card-thumb {
  position: relative;
  aspect-ratio: 5 / 7;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.img-fallback {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  padding: 8px; text-align: center;
  font-size: 12px; color: var(--muted);
  border: 1px dashed var(--border); border-radius: 8px;
}
.img-fallback strong { color: var(--text); font-size: 13px; }

.card-caption {
  margin-top: 6px; font-size: 12px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* Footer */
.site-footer {
  max-width: 1400px; margin: 32px auto 0;
  padding: 16px 16px max(16px, env(safe-area-inset-bottom)); text-align: center;
  font-size: 13px; color: var(--muted);
  border-top: 1px solid var(--border);
}

/* Modal */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(10, 10, 14, .7); }

.modal-panel {
  position: relative;
  display: grid; grid-template-columns: minmax(0, 360px) minmax(0, 1fr); gap: 24px;
  width: min(900px, 100%); max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 24px;
  background: var(--surface); border-radius: 14px; box-shadow: var(--shadow);
}

.modal-image img {
  width: 100%; aspect-ratio: 5 / 7; object-fit: cover;
  border-radius: 10px; background: var(--bg); display: block;
}

.modal-close {
  position: absolute; top: 8px; right: 10px;
  font-size: 28px; line-height: 1;
  background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px;
}
.modal-close:hover { color: var(--text); }

.modal-nav {
  position: fixed; top: 50%; transform: translateY(-50%);
  width: 44px; height: 64px;
  font-size: 36px; line-height: 1;
  color: #fff; background: rgba(0, 0, 0, .35);
  border: 0; border-radius: 8px; cursor: pointer;
}
.modal-nav:hover { background: rgba(0, 0, 0, .6); }
.modal-nav.prev { left: 12px; }
.modal-nav.next { right: 12px; }
.modal-nav:disabled { opacity: .25; cursor: default; }

.info-code { color: var(--muted); font-size: 13px; letter-spacing: .02em; }
.info-name { margin: 2px 0 12px; font-size: 22px; line-height: 1.3; padding-right: 28px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.chip {
  font-size: 13px; font-weight: 600;
  padding: 2px 10px; border-radius: 999px;
  background: var(--bg); border: 1px solid var(--border);
}
.chip.color { color: #fff; border-color: transparent; }
.chip.color.red { background: var(--c-red); }
.chip.color.purple { background: var(--c-purple); }
.chip.color.green { background: var(--c-green); }
.chip.color.blue { background: var(--c-blue); }
.chip.color.yellow { background: var(--c-yellow); }

.info-row { margin-bottom: 14px; }
.info-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.info-value { font-size: 15px; white-space: pre-line; overflow-wrap: anywhere; }
.info-empty { color: var(--muted); }

/* Phone: full-screen sheet, pinned close button, prev/next bar at the bottom. */
@media (max-width: 720px) {
  .modal { padding: 0; align-items: stretch; }
  .modal-panel {
    grid-template-columns: 1fr; gap: 16px;
    width: 100%; max-height: none; height: 100%; height: 100dvh; border-radius: 0;
    padding: max(56px, calc(env(safe-area-inset-top) + 48px)) 16px calc(88px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  .modal-image { margin: 0 auto; width: 100%; max-width: 300px; }
  /* Keep part of the card text visible below the image on short screens. */
  .modal-image img { width: auto; max-width: 100%; max-height: 52dvh; margin: 0 auto; }
  .info-name { font-size: 20px; padding-right: 0; }

  .modal-close {
    position: fixed; z-index: 2;
    top: max(10px, env(safe-area-inset-top)); right: 10px;
    width: 40px; height: 40px; padding: 0;
    border-radius: 50%;
    background: var(--bg); color: var(--text);
    box-shadow: var(--shadow);
  }

  .modal-nav {
    top: auto; transform: none;
    bottom: max(14px, env(safe-area-inset-bottom));
    width: calc(50% - 21px); height: 48px;
    font-size: 30px;
    background: rgba(0, 0, 0, .55);
  }
  .modal-nav.prev { left: 14px; }
  .modal-nav.next { right: 14px; }
}
