/* ส่วนประกอบที่ใช้ร่วมกัน: popup, dropdown (select.js), ปฏิทิน (datepicker.js), เอฟเฟกต์ (fx.js) โหลดหลัง style.css */

/* ---------- popup (กล่องลอย / แผ่นจากด้านล่าง) ---------- */
html.is-locked { overflow: hidden; }
.pop { z-index: 90; color: var(--ink); }
.pop-float {
  position: fixed; max-width: min(560px, calc(100vw - 16px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 22px 60px rgba(3, 6, 25, .35), 0 0 0 1px rgba(255, 255, 255, .02);
  transform-origin: top center; animation: pop-in .18s cubic-bezier(.2, .9, .3, 1.2);
}
.pop-float.is-up { transform-origin: bottom center; animation-name: pop-in-up; }
.pop-float.is-leaving { animation: pop-out .16s ease-in forwards; }
@keyframes pop-in { from { opacity: 0; transform: translateY(-8px) scale(.96); } }
@keyframes pop-in-up { from { opacity: 0; transform: translateY(8px) scale(.96); } }
@keyframes pop-out { to { opacity: 0; transform: translateY(-4px) scale(.97); } }
.pop-backdrop { position: fixed; inset: 0; z-index: 89; background: rgba(4, 6, 22, .55); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); animation: fade-in .2s ease-out; }
.pop-backdrop.is-leaving { animation: fade-out .18s ease-in forwards; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
.pop-sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 82vh; max-height: 82dvh; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-bottom: 0; border-radius: 24px 24px 0 0;
  padding: 6px 12px calc(14px + env(safe-area-inset-bottom)); box-shadow: 0 -20px 60px rgba(3, 6, 25, .35);
  animation: sheet-in .32s cubic-bezier(.2, .9, .25, 1);
}
.pop-sheet.is-leaving { animation: sheet-out .2s ease-in forwards; }
@keyframes sheet-in { from { transform: translateY(100%); } }
@keyframes sheet-out { to { transform: translateY(100%); } }
.pop-head { display: grid; justify-items: center; gap: 8px; padding: 4px 0 10px; position: sticky; top: -6px; background: var(--surface); z-index: 1; }
.pop-head .sheet-grip { margin: 4px 0 0; }
.pop-head b { font-family: var(--font-head); font-weight: 600; font-size: 1.02rem; }

/* ---------- dropdown ---------- */
.sel { position: relative; min-width: 0; }
.sel-native { display: none !important; }
.sel-btn {
  width: 100%; min-height: 46px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; border: 1.5px solid var(--line); border-radius: var(--radius-sm);
  background: var(--bg-2); color: var(--ink); text-align: left; cursor: pointer;
  transition: border-color .15s, box-shadow .15s, background-color .15s;
}
.sel-btn:hover:not(:disabled) { border-color: var(--primary); }
.sel-btn:focus-visible, .sel-btn[aria-expanded="true"] { outline: none; border-color: var(--primary-2); box-shadow: 0 0 0 3px rgba(47, 199, 255, .2); }
.sel-btn:disabled { opacity: .5; cursor: not-allowed; }
.sel-value { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sel-chev { width: 18px; height: 18px; color: var(--muted); transition: transform .2s; }
.sel-btn[aria-expanded="true"] .sel-chev { transform: rotate(180deg); color: var(--primary-2); }
.sel-panel { padding: 6px; outline: none; }
.pop-float.sel-panel { max-height: 320px; overflow: auto; }
.sel-list { display: grid; gap: 2px; }
.sel-opt {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 42px; padding: 9px 12px; border-radius: 11px; cursor: pointer; position: relative; overflow: hidden;
  animation: opt-in .22s ease-out backwards; animation-delay: calc(var(--i, 0) * 18ms);
}
@keyframes opt-in { from { opacity: 0; transform: translateY(-4px); } }
.sel-opt .ic { width: 18px; height: 18px; color: var(--primary-2); opacity: 0; transform: scale(.5); transition: opacity .15s, transform .2s cubic-bezier(.2, .9, .3, 1.4); }
.sel-opt.is-active { background: var(--surface-2); }
.sel-opt[aria-selected="true"] { color: var(--primary-2); font-weight: 600; background: rgba(47, 199, 255, .08); }
.sel-opt[aria-selected="true"] .ic { opacity: 1; transform: none; }
.sel-opt.is-disabled { opacity: .45; cursor: not-allowed; }
.pop-sheet .sel-opt { min-height: 54px; font-size: 1.02rem; padding: 12px 14px; }

/* ---------- ปฏิทิน ---------- */
.date-btn {
  display: inline-flex; align-items: center; gap: 10px; width: 100%; min-height: 46px; padding: 10px 14px;
  border: 1.5px solid var(--line); border-radius: var(--radius-sm); background: var(--bg-2); color: var(--ink); cursor: pointer; text-align: left;
  transition: border-color .15s, box-shadow .15s;
}
.date-btn .ic { color: var(--primary-2); width: 19px; height: 19px; }
.date-btn span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.date-btn:hover { border-color: var(--primary); }
.date-btn:focus-visible, .date-btn[aria-expanded="true"] { outline: none; border-color: var(--primary-2); box-shadow: 0 0 0 3px rgba(47, 199, 255, .2); }
.date-btn.is-set { border-color: rgba(47, 199, 255, .55); }
.dp { display: grid; padding: 12px; outline: none; }
.dp-range.pop-float { grid-template-columns: 150px 300px; gap: 12px; }
.pop-float.dp:not(.dp-range) { width: 312px; }
.dp-presets { display: grid; align-content: start; gap: 4px; padding-right: 12px; border-right: 1px solid var(--line); }
.dp-preset { text-align: left; padding: 9px 12px; border-radius: 10px; border: 0; background: none; color: var(--ink); cursor: pointer; font-weight: 500; }
.dp-preset:hover { background: var(--surface-2); }
.dp-preset.is-active { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; }
.dp-main { display: grid; gap: 8px; min-width: 0; }
.dp-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dp-title { font-family: var(--font-head); font-weight: 600; }
.dp-week, .dp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.dp-week span { text-align: center; font-size: .75rem; color: var(--muted); font-weight: 600; padding: 4px 0; }
.dp-week span:first-child { color: var(--bad); }
.dp-grid.slide-l { animation: dp-l .25s ease-out; }
.dp-grid.slide-r { animation: dp-r .25s ease-out; }
@keyframes dp-l { from { opacity: 0; transform: translateX(18px); } }
@keyframes dp-r { from { opacity: 0; transform: translateX(-18px); } }
.dp-day {
  position: relative; aspect-ratio: 1; min-height: 36px; border: 0; border-radius: 11px; background: none; color: var(--ink);
  font-weight: 500; font-variant-numeric: tabular-nums; cursor: pointer; overflow: hidden; transition: background-color .12s, color .12s, transform .12s;
}
.dp-day:hover:not(:disabled) { background: var(--surface-2); }
.dp-day:active:not(:disabled) { transform: scale(.92); }
.dp-day:disabled { color: var(--muted); opacity: .35; cursor: not-allowed; }
.dp-day.is-today::after { content: ''; position: absolute; left: 50%; bottom: 5px; width: 4px; height: 4px; margin-left: -2px; border-radius: 50%; background: var(--gold); }
.dp-day.in-range { background: rgba(47, 199, 255, .14); border-radius: 0; }
.dp-day.is-start, .dp-day.is-end { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; box-shadow: 0 6px 16px rgba(123, 108, 255, .35); }
.dp-day.is-start.is-today::after, .dp-day.is-end.is-today::after { background: #fff; }
.dp-day:focus-visible { outline: 2px solid var(--primary-2); outline-offset: 1px; }
.dp-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding-top: 8px; border-top: 1px solid var(--line); }
.dp-sum { font-size: .86rem; color: var(--muted); }
.dp-actions { display: flex; gap: 6px; margin-left: auto; }
.pop-sheet.dp { padding: 6px 16px calc(16px + env(safe-area-inset-bottom)); }
.pop-sheet .dp-presets { display: flex; gap: 6px; overflow-x: auto; padding: 0 0 10px; border-right: 0; scrollbar-width: none; }
.pop-sheet .dp-presets::-webkit-scrollbar { display: none; }
.pop-sheet .dp-preset { flex: none; border: 1.5px solid var(--line); border-radius: 999px; padding: 7px 14px; background: var(--bg-2); }
.pop-sheet .dp-day { min-height: 44px; font-size: 1.02rem; }
.pop-sheet .dp-actions .btn { min-height: 44px; }

/* ---------- เอฟเฟกต์ ---------- */
.has-rip { overflow: hidden; isolation: isolate; }
.rip { position: absolute; border-radius: 50%; background: currentColor; opacity: .22; transform: scale(0); pointer-events: none; animation: rip .6s ease-out forwards; z-index: -1; }
.btn-primary .rip { background: #fff; opacity: .35; }
@keyframes rip { to { transform: scale(1); opacity: 0; } }
.fx-canvas { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 120; }
[data-reveal] { opacity: 0; transform: translateY(26px); transition: opacity .7s cubic-bezier(.2, .8, .2, 1), transform .7s cubic-bezier(.2, .8, .2, 1); transition-delay: var(--d, 0ms); }
[data-reveal="left"] { transform: translateX(-36px); }
[data-reveal="right"] { transform: translateX(36px); }
[data-reveal="zoom"] { transform: scale(.92); }
[data-reveal].is-in { opacity: 1; transform: none; }
[data-tilt] {
  transform: perspective(900px) rotateX(var(--rx, 0deg)) rotateY(var(--ry, 0deg));
  transition: transform .35s cubic-bezier(.2, .8, .2, 1); transform-style: preserve-3d; position: relative;
}
[data-tilt].is-tilting { transition: transform .08s linear; }
[data-tilt]::after {
  content: ''; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; opacity: 0; transition: opacity .3s;
  background: radial-gradient(420px circle at var(--mx, 50%) var(--my, 50%), rgba(255, 255, 255, .1), transparent 45%);
}
[data-tilt].is-tilting::after { opacity: 1; }
[data-magnetic] { transform: translate(var(--tx, 0), var(--ty, 0)); transition: transform .25s cubic-bezier(.2, .8, .2, 1); }
.scroll-progress { position: fixed; left: 0; right: 0; top: 0; height: 3px; z-index: 50; transform-origin: 0 50%; transform: scaleX(0); background: linear-gradient(90deg, var(--primary), var(--primary-2), var(--gold)); pointer-events: none; }

@media (prefers-reduced-motion: reduce) {
  [data-reveal] { opacity: 1; transform: none; }
  [data-tilt], [data-magnetic] { transform: none !important; }
}
