:root {
  --bg: #141634;
  --surface: #22254D;
  --surface-2: #2E2F63;
  --surface-pink: #2A2552;
  --text: #F4F1FF;
  --muted: #B9B6E0;
  --lavender: #C7B8FF;
  --peach: #FFC9A8;
  --peach-deep: #F2B08C;
  --on-peach: #2A1C3B;
  --pink: #FF9FC4;
  --on-pink: #3A1027;
  --danger: #F2557A;
  --online: #7EE0B0;
  --radius: 18px;
  --font-display: 'Mali', 'Noto Sans Thai', system-ui, sans-serif;
  --font-body: 'Noto Sans Thai', system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 13px; }
.em { color: var(--text); font-weight: 600; }

/* ---------- ดาวพื้นหลัง ---------- */
.sky { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.sky i {
  position: absolute; width: 3px; height: 3px; border-radius: 50%;
  background: var(--text); opacity: .75; animation: twinkle 4s ease-in-out infinite;
}
.sky i.s { width: 2px; height: 2px; opacity: .55; animation-delay: 1.3s; }
.sky i.l { background: var(--lavender); animation-delay: 2.1s; }
.sky i.p { width: 4px; height: 4px; background: var(--peach); animation-delay: .6s; }
@keyframes twinkle { 50% { opacity: .2; } }

/* ---------- โครงหน้า ---------- */
#app {
  position: relative;
  max-width: 440px;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
}
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 48px; }
.brand { display: flex; align-items: center; gap: 8px; }
.brand-moon { width: 26px; height: 26px; fill: var(--peach); }
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: 22px; }
.topbar-right { display: flex; align-items: center; gap: 4px; }
.online-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted);
  background: var(--surface); border-radius: 999px; padding: 6px 12px;
}
.online-pill::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: var(--online); }

.screen { flex: 1; display: flex; flex-direction: column; gap: 16px; }
.hero {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 10px; padding: 12px 0;
}
.panel { display: flex; flex-direction: column; gap: 12px; }
.title { margin: 0; font-family: var(--font-display); font-weight: 700; font-size: 30px; line-height: 1.3; }
.foot { text-align: center; font-size: 13px; }
.big-time {
  font-family: var(--font-display); font-weight: 700; font-size: 52px; line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

/* ---------- พระจันทร์ (วาดด้วย CSS) ---------- */
.moon-art { position: relative; width: 156px; height: 156px; margin-bottom: 10px; flex-shrink: 0; }
.moon-art i { position: absolute; border-radius: 50%; }
/* แสงเรืองวางทับรอยเว้า จะได้ไม่เห็นขอบวงกลมสีพื้นตัดแสง */
.moon-art .glow { inset: -18px; background: var(--peach); opacity: .12; z-index: 1; animation: breathe 5s ease-in-out infinite; }
.moon-art .crater, .moon-art .cloud { z-index: 2; }
.moon-art .disc { inset: 0; background: var(--peach); }
.moon-art .bite { left: 48px; top: -13px; width: 140px; height: 140px; background: var(--bg); }
.moon-art .crater { background: var(--peach-deep); }
.moon-art .c1 { left: 28px; top: 96px; width: 13px; height: 13px; }
.moon-art .c2 { left: 58px; top: 123px; width: 8px; height: 8px; }
.moon-art .cloud { background: var(--lavender); }
.moon-art .c-a { left: 110px; top: 110px; width: 70px; height: 28px; border-radius: 999px; }
.moon-art .c-b { left: 126px; top: 95px; width: 36px; height: 36px; }
.moon-art.sleepy { filter: saturate(.55) brightness(.75); }
@keyframes breathe { 50% { transform: scale(1.06); opacity: .2; } }

/* ---------- ปุ่ม ---------- */
button {
  font: inherit; color: inherit; border: 0; cursor: pointer;
  min-height: 44px; border-radius: var(--radius); background: var(--surface);
}
button:disabled { opacity: .45; cursor: not-allowed; }
button:focus-visible { outline: 3px solid var(--lavender); outline-offset: 3px; }
svg.stroke { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.btn-primary {
  display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%;
  min-height: 56px; border-radius: 999px; background: var(--peach); color: var(--on-peach);
  font-family: var(--font-display); font-weight: 700; font-size: 18px;
  transition: transform .12s ease;
}
.btn-primary:active:not(:disabled) { transform: scale(.98); }
.btn-primary.big { min-height: 64px; font-size: 20px; }
.btn-primary svg { width: 22px; height: 22px; stroke-width: 2.2; }
.btn-soft { width: 100%; border-radius: 999px; background: var(--lavender); color: var(--on-peach); font-weight: 600; }
.btn-ghost { width: 100%; border-radius: 999px; background: transparent; border: 1.5px solid var(--surface-2); color: var(--text); }
.btn-link { background: none; color: var(--muted); text-decoration: underline; min-height: 40px; }
.btn-list { width: 100%; text-align: left; padding: 0 16px; border-radius: 14px; }
.btn-danger {
  display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%;
  min-height: 60px; border-radius: 999px; background: var(--danger); color: #fff;
  font-family: var(--font-display); font-weight: 700; font-size: 18px;
}
.btn-danger svg { width: 22px; height: 22px; stroke-width: 2.2; }
.btn-danger .hang { transform: rotate(135deg); }
.icon-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; background: transparent; color: var(--text); display: grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- ตัวเลือก ---------- */
.field { width: 100%; text-align: left; }
.label { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
.chips { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.chips.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.chip { min-height: 46px; padding: 0 4px; border-radius: 16px; border: 2px solid transparent; font-size: 14px; }
.chip[aria-checked="true"] { border-color: var(--lavender); background: var(--surface-2); font-weight: 600; }

.rules {
  margin: 0; padding: 12px 16px 12px 34px; border-radius: var(--radius);
  background: var(--surface); color: var(--muted); font-size: 14px; text-align: left;
}
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; cursor: pointer; }
.check input { width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--lavender); flex-shrink: 0; }

.toast { margin: 0; min-height: 0; text-align: center; color: var(--lavender); font-size: 14px; }
.toast:empty { display: none; }

/* ---------- กำลังหา ---------- */
.radar { position: relative; width: 180px; height: 180px; display: grid; place-items: center; margin-bottom: 10px; }
.radar .ring { position: absolute; inset: 40px; border-radius: 50%; border: 2px solid var(--lavender); opacity: 0; animation: ripple 2.4s ease-out infinite; }
.radar .r2 { animation-delay: .8s; }
.radar .r3 { animation-delay: 1.6s; }
.radar-moon { width: 76px; height: 76px; fill: var(--peach); }
@keyframes ripple { 0% { transform: scale(.6); opacity: .8; } 100% { transform: scale(2); opacity: 0; } }
.hint-card { background: var(--surface); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.hint-card p { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- ในสาย ---------- */
/* ปุ่มรายงานอยู่บนแถบบนสุด (แทนที่จำนวนคนออนไลน์ระหว่างโทร) */
#screen-call { position: relative; }
.call-top { position: absolute; top: -48px; right: -6px; }
.call-top .icon-btn { color: var(--muted); }
#call-state:empty { display: none; }
#screen-call .big-time { font-size: 46px; }
.avatar-wrap { position: relative; width: 152px; height: 152px; display: grid; place-items: center; margin: 26px 0 12px; }
.avatar-wrap .halo { position: absolute; border-radius: 50%; border: 2px solid var(--lavender); transition: opacity .15s, transform .15s; }
.avatar-wrap .h1 { inset: -12px; opacity: .3; }
.avatar-wrap .h2 { inset: -28px; opacity: .12; }
.avatar-wrap.speaking .h1 { opacity: .8; transform: scale(1.03); }
.avatar-wrap.speaking .h2 { opacity: .4; transform: scale(1.05); }
.avatar { width: 152px; height: 152px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.avatar svg { width: 70%; height: 70%; }
.partner-name { margin: 0; font-family: var(--font-display); font-weight: 700; font-size: 26px; }
.pill { font-size: 13px; background: var(--surface); border-radius: 999px; padding: 4px 12px; }
.pill:empty { display: none; }
.timer { display: flex; flex-direction: column; align-items: center; gap: 6px; width: 220px; margin-top: 4px; }
.progress { width: 100%; height: 6px; border-radius: 999px; background: var(--surface); overflow: hidden; }
#call-progress { height: 100%; width: 100%; border-radius: 999px; background: var(--lavender); transition: width .5s linear; }
.timer.unlimited #call-progress { background: var(--pink); }

.like-card { display: flex; align-items: center; gap: 12px; background: var(--surface-pink); border-radius: var(--radius); padding: 12px 16px; font-size: 14px; line-height: 1.45; text-align: left; }
.like-card svg { width: 22px; height: 22px; flex-shrink: 0; }
.fill-pink { fill: var(--pink); }

.actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.action { display: flex; flex-direction: column; align-items: center; gap: 8px; background: transparent; font-size: 13px; padding: 4px 0; }
.action-icon { width: 64px; height: 64px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; transition: background .15s, color .15s; }
.action-icon svg { width: 24px; height: 24px; }
.action.like .action-icon { background: var(--pink); color: var(--on-pink); }
.action.like.on .action-icon { background: var(--surface-pink); color: var(--pink); }
.action.like.on .action-icon svg { fill: var(--pink); }
.action.on:not(.like) .action-icon { background: var(--lavender); color: var(--on-peach); }
.action:disabled { opacity: 1; cursor: default; }

/* ---------- แจ้งเตือนด้านบน ---------- */
.banner {
  position: fixed; left: 16px; right: 16px; top: max(16px, env(safe-area-inset-top)); z-index: 5;
  max-width: 408px; margin: 0 auto; padding: 10px 14px; border-radius: 14px;
  background: #3A2D55; color: var(--text); border: 1px solid var(--lavender); text-align: center; font-size: 14px;
}

/* ---------- โหมดเฝ้านอน ---------- */
#sleep-overlay {
  position: fixed; inset: 0; z-index: 10; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  color: #3F4166; user-select: none; touch-action: manipulation;
}
.sleep-moon { width: 40px; height: 40px; fill: #2A2C4A; margin-bottom: 8px; }
.sleep-timer { font-family: var(--font-display); font-size: 40px; font-variant-numeric: tabular-nums; }
.sleep-state { font-size: 14px; }
.sleep-exit { position: absolute; bottom: max(28px, env(safe-area-inset-bottom)); font-size: 13px; }

/* ---------- หน้าต่าง ---------- */
dialog {
  width: min(400px, calc(100% - 32px)); padding: 20px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--surface-2); border-radius: 22px;
}
dialog::backdrop { background: rgba(5, 6, 20, .7); }
dialog form { display: flex; flex-direction: column; gap: 10px; }
dialog h3 { margin: 0 0 4px; font-family: var(--font-display); font-size: 20px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (max-height: 700px) {
  .moon-art { width: 120px; height: 120px; }
  .moon-art .bite { left: 37px; top: -10px; width: 108px; height: 108px; }
  .moon-art .c1 { left: 22px; top: 74px; } .moon-art .c2 { left: 45px; top: 95px; }
  .moon-art .c-a { left: 84px; top: 84px; width: 56px; height: 22px; }
  .moon-art .c-b { left: 96px; top: 72px; width: 28px; height: 28px; }
  .avatar-wrap, .avatar { width: 136px; height: 136px; }
  .title { font-size: 26px; }
}
