:root {
  --bg: #f4eee4;
  --bg-elev: #fffdf8;
  --ink: #2a2218;
  --muted: #6f6254;
  --line: #e4d8c8;
  --accent: #b85c38;
  --accent-soft: #f3e0d4;
  --chip: #efe6d8;
  --chip-on: #b85c38;
  --chip-on-ink: #fffdf8;
  --shadow: 0 8px 28px rgba(42, 34, 24, 0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --bar: 56px;
  --radius: 16px;
}
[data-theme="dark"] {
  --bg: #171410;
  --bg-elev: #241f19;
  --ink: #f3eadc;
  --muted: #b5a894;
  --line: #3a332b;
  --accent: #e08a5a;
  --accent-soft: #3a2a22;
  --chip: #2c271f;
  --chip-on: #e08a5a;
  --chip-on-ink: #1a1612;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html { height: 100%; }
body {
  font-family: "Sarabun", "Noto Sans Thai", "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100%;
  line-height: 1.5;
  overflow-x: hidden;
}
img { max-width: 100%; display: block; }
button, input { font: inherit; color: inherit; }
h1, h2, h3 { font-weight: 650; letter-spacing: -0.02em; margin: 0; }

.app {
  padding-top: calc(var(--safe-top) + 8px);
  padding-bottom: calc(var(--bar) + var(--safe-bot) + 16px);
}

/* Header */
.top {
  padding: 8px 16px 4px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; flex-direction: column; gap: 0; }
.brand h1 { font-size: 1.25rem; }
.brand .sub { font-size: 0.75rem; color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: var(--chip); color: var(--ink); cursor: pointer;
  display: grid; place-items: center;
}
.search {
  margin-top: 10px;
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 12px;
  padding: 10px 12px;
  outline: none;
}
.search:focus { border-color: var(--accent); }

/* Chips */
.filters { padding: 8px 0 4px; }
.chips {
  display: flex; gap: 8px; overflow-x: auto;
  padding: 4px 16px 8px;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  border: 0;
  background: var(--chip);
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.88rem;
  cursor: pointer;
  white-space: nowrap;
}
.chip[aria-pressed="true"] {
  background: var(--chip-on);
  color: var(--chip-on-ink);
}
.filt-label {
  font-size: 0.72rem;
  color: var(--muted);
  padding: 0 16px;
  text-transform: none;
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 8px 16px 24px;
  align-items: start;
}
.card {
  background: var(--bg-elev);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  cursor: pointer;
  text-align: left;
  padding: 0;
  width: 100%;
  color: inherit;
  position: relative;
}
.card .thumb-wrap {
  aspect-ratio: 3/4;
  background: #d9cbb8;
  overflow: hidden;
  position: relative;
}
.card img { width: 100%; height: 100%; object-fit: cover; }
.card .meta { padding: 8px 10px 10px; }
.card .title { font-size: 0.9rem; font-weight: 650; line-height: 1.3; }
.card .who { font-size: 0.72rem; color: var(--muted); margin-top: 2px; }
.fav {
  position: absolute; top: 6px; right: 6px;
  width: 34px; height: 34px; border: 0; border-radius: 50%;
  background: rgba(20,16,12,0.45); color: #fff; cursor: pointer;
  display: grid; place-items: center; z-index: 2;
  backdrop-filter: blur(6px);
}
.fav.on { background: var(--accent); }
.ai-badge {
  position: absolute; left: 6px; bottom: 6px;
  font-size: 0.62rem; letter-spacing: 0.02em;
  background: rgba(20,16,12,0.5); color: #f6f0e6;
  padding: 2px 6px; border-radius: 6px;
}
.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
}

/* Bottom nav */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--bar) + var(--safe-bot));
  padding-bottom: var(--safe-bot);
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  z-index: 30;
}
.nav button {
  border: 0; background: none; cursor: pointer;
  color: var(--muted); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  font-size: 0.7rem;
}
.nav button svg { width: 22px; height: 22px; }
.nav button[aria-current="page"] { color: var(--accent); font-weight: 650; }

/* Views */
.view { display: none; }
.view.on { display: block; }

/* Tips */
.tips { padding: 8px 16px 32px; }
.tips .lead { color: var(--muted); margin: 0 0 16px; font-size: 0.95rem; }
.acc {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 14px;
  margin-bottom: 8px;
  overflow: hidden;
}
.acc summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  font-weight: 650;
}
.acc summary::-webkit-details-marker { display: none; }
.acc .body {
  padding: 0 14px 14px;
  white-space: pre-wrap;
  font-size: 0.92rem;
  color: var(--ink);
}
.acc + .acc { }

/* Detail sheet */
.sheet-bg {
  position: fixed; inset: 0; background: rgba(20,16,12,0.5);
  z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.sheet-bg.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  max-height: min(92vh, 860px);
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  z-index: 41;
  transform: translateY(110%);
  transition: transform .28s ease;
  overflow: auto;
  padding-bottom: calc(16px + var(--safe-bot));
}
.sheet.on { transform: translateY(0); }
.sheet .hero {
  position: relative;
  background: #1a1612;
  cursor: zoom-in;
}
.sheet .hero img { width: 100%; max-height: 52vh; object-fit: contain; margin: 0 auto; }
.sheet .ai-note {
  font-size: 0.72rem; color: var(--muted);
  padding: 6px 16px 0;
}
.sheet .pad { padding: 8px 16px 16px; }
.sheet h2 { font-size: 1.2rem; margin-bottom: 6px; }
.howto, .block { margin: 0 0 12px; font-size: 0.95rem; }
.block h3 { font-size: 0.78rem; color: var(--accent); margin-bottom: 4px; text-transform: none; }
.cam-row { margin: 0 0 6px; font-size: 0.9rem; }
.cam-row b { font-weight: 650; }
.sheet-top {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 8px 0 16px;
}
.grab {
  width: 40px; height: 4px; background: var(--line); border-radius: 4px;
  margin: 8px auto 0;
}
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag {
  font-size: 0.72rem; background: var(--chip);
  border-radius: 999px; padding: 3px 8px; color: var(--muted);
}

/* Lightbox */
.lb {
  position: fixed; inset: 0; background: #0d0b09;
  z-index: 50; display: none; color: #f6f0e6;
  touch-action: pan-y;
}
.lb.on { display: flex; flex-direction: column; }
.lb-bar {
  display: flex; justify-content: space-between; align-items: center;
  padding: calc(8px + var(--safe-top)) 8px 8px;
}
.lb-bar span { font-size: 0.9rem; padding: 0 8px; flex: 1; }
.lb-stage {
  flex: 1; display: flex; align-items: center; justify-content: center;
  overflow: hidden; position: relative;
}
.lb-stage img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  user-select: none; -webkit-user-drag: none;
}
.lb-hint { text-align: center; font-size: 0.72rem; color: #b5a894; padding: 8px 12px calc(10px + var(--safe-bot)); }

.offline-pill {
  display: none;
  font-size: 0.72rem;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 2px 8px;
  margin-left: 8px;
}
.offline-pill.on { display: inline-block; }

@media (min-width: 720px) {
  .grid { grid-template-columns: 1fr 1fr 1fr; max-width: 980px; margin: 0 auto; }
  .top, .filters, .tips, .app { max-width: 980px; margin-left: auto; margin-right: auto; }
  .nav { max-width: 980px; left: 50%; transform: translateX(-50%); border-radius: 16px 16px 0 0; }
}
