/* ตรวจคำ — mobile first (320px ขึ้นไป), สีและฟอนต์เดียวกับแอปเก็บเสียงอีสาน */
@font-face { font-family: 'LINE Seed Sans TH'; src: url('fonts/LINESeedSansTH_W_Rg.woff2') format('woff2'); font-weight: 400; font-display: swap; }
@font-face { font-family: 'LINE Seed Sans TH'; src: url('fonts/LINESeedSansTH_W_Bd.woff2') format('woff2'); font-weight: 700; font-display: swap; }
@font-face { font-family: 'LINE Seed Sans TH'; src: url('fonts/LINESeedSansTH_W_XBd.woff2') format('woff2'); font-weight: 800; font-display: swap; }

:root {
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --fill: #E9E9EE;
  --label: #000000;
  --secondary: #6C6C70;
  --separator: #C6C6C8;
  --green: #34C759;
  --red: #E1261C;
  --earth: #C8781E; /* ดินลูกรังอีสาน */
  --earth-press: #A9620F;
  --focus: #0A84FF;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --pad: clamp(12px, 4.2vw, 20px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1C1C1E;
    --fill: #2C2C2E;
    --label: #FFFFFF;
    --secondary: #98989F;
    --separator: #38383A;
    --green: #30D158;
    --red: #FF453A;
    --earth: #E39A3B;
    --earth-press: #C27F25;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--label);
  font: 400 16px/1.4 'LINE Seed Sans TH', system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
a { color: inherit; text-decoration: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
svg { display: block; }

/* ---------- ปุ่ม ---------- */
.btn {
  min-height: 48px; padding: 0 16px; border-radius: 14px;
  background: var(--card); color: var(--label);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-size: 16px; font-weight: 700; white-space: nowrap;
  transition: transform .12s ease, background-color .15s ease, opacity .15s ease;
}
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--earth); color: #fff; }
.btn.primary:active { background: var(--earth-press); }
.btn.block { display: flex; width: 100%; }
.btn.danger { color: var(--red); }
.btn.ghost { background: none; color: var(--secondary); }
.btn:disabled { opacity: .45; pointer-events: none; }
kbd { display: none; }
@media (hover: hover) and (pointer: fine) {
  kbd {
    display: inline-block; font: 600 11px/1 system-ui, sans-serif; padding: 3px 5px; border-radius: 5px;
    background: color-mix(in srgb, currentColor 14%, transparent);
  }
}

/* ---------- จอหลัก: สูงเท่าส่วนที่มองเห็นจริง (คีย์บอร์ดเปิดก็ไม่บัง) ---------- */
.screen {
  position: fixed; left: 0; right: 0; top: 0;
  height: var(--app-h, 100dvh);
  transform: translateY(var(--app-y, 0px));
}

/* ---------- เข้าสู่ระบบ ---------- */
.login {
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--safe-t) + 24px) calc(var(--safe-r) + var(--pad)) calc(var(--safe-b) + 24px) calc(var(--safe-l) + var(--pad));
  overflow-y: auto;
}
.login-box { width: 100%; max-width: 380px; text-align: center; }
.login-icon { width: 88px; height: 88px; border-radius: 22px; margin: 0 auto 14px; display: block; box-shadow: 0 6px 24px rgba(0,0,0,.12); }
.login h1 { font-size: 30px; font-weight: 800; letter-spacing: -.3px; }
.login .sub { color: var(--secondary); margin: 4px 0 28px; }
.field-label { display: block; text-align: left; font-size: 14px; color: var(--secondary); margin: 0 0 6px 4px; }
.name-input {
  width: 100%; height: 56px; border-radius: 14px; border: 1px solid var(--separator); background: var(--card);
  text-align: center; font: 700 20px/1 'LINE Seed Sans TH', system-ui, sans-serif;
}
.name-input::placeholder { color: var(--separator); font-weight: 400; }
.name-input:focus { outline: 2px solid var(--earth); outline-offset: -1px; border-color: transparent; }
.field-label .opt { color: var(--separator); }
.code-input {
  width: 100%; height: 56px; border-radius: 14px; border: 1px solid var(--separator); background: var(--card);
  text-align: center; font: 700 24px/1 ui-monospace, 'SF Mono', Menlo, monospace; letter-spacing: 3px; text-transform: uppercase;
}
.code-input::placeholder { color: var(--separator); letter-spacing: 3px; }
.code-input:focus { outline: 2px solid var(--earth); outline-offset: -1px; border-color: transparent; }
.login-err { min-height: 22px; color: var(--red); font-size: 14px; margin: 8px 0 6px; }
.hint { color: var(--secondary); font-size: 13px; margin-top: 18px; text-wrap: balance; }

/* ---------- หน้าตรวจ ---------- */
.app { display: flex; flex-direction: column; }
.topbar {
  flex: none; display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + var(--pad) - 6px) 6px calc(var(--safe-l) + var(--pad) - 6px);
  min-height: calc(var(--safe-t) + 56px);
}
.icon-btn, .avatar-btn {
  width: 44px; height: 44px; flex: none; border-radius: 50%;
  display: grid; place-items: center; transition: transform .12s ease, opacity .15s;
}
.icon-btn:active, .avatar-btn:active { transform: scale(.9); }
.icon-btn svg { width: 26px; height: 26px; fill: none; stroke: var(--earth); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: .3; pointer-events: none; }
.avatar-btn span {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: color-mix(in srgb, var(--earth) 18%, var(--card)); color: var(--earth); font-weight: 800; font-size: 16px;
}
.title { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; gap: 8px; }
.title h1 { font-size: 20px; font-weight: 800; letter-spacing: -.2px; white-space: nowrap; }
.mode-chip {
  font-size: 12.5px; font-weight: 700; padding: 3px 9px; border-radius: 999px; white-space: nowrap;
  background: color-mix(in srgb, var(--earth) 16%, transparent); color: var(--earth);
}
.mode-chip.train { background: color-mix(in srgb, var(--green) 16%, transparent); color: var(--green); }
.goal { flex: none; height: 3px; margin: 0 calc(var(--safe-r) + var(--pad)) 0 calc(var(--safe-l) + var(--pad)); background: var(--fill); border-radius: 2px; overflow: hidden; }
.goal-fill { height: 100%; width: 0; background: var(--earth); border-radius: 2px; transition: width .4s ease; }

.content {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 12px calc(var(--safe-r) + var(--pad)) 16px calc(var(--safe-l) + var(--pad));
}
.content > * { max-width: 640px; margin-left: auto; margin-right: auto; }

.card { background: var(--card); border-radius: 18px; padding: 14px var(--pad) 16px; }
.item { transition: transform .22s cubic-bezier(.2,.8,.2,1), opacity .22s ease; will-change: transform, opacity; }
.item.out { transform: translateX(-28px); opacity: 0; }
.item.out-back { transform: translateX(28px); opacity: 0; }
.item.in { transform: translateX(28px); opacity: 0; transition: none; }
.item.in-back { transform: translateX(-28px); opacity: 0; transition: none; }

.meta { color: var(--secondary); font-size: 13px; line-height: 1.35; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-all; }
.meta .flag { color: var(--red); }

/* เครื่องเล่น */
.player { display: flex; align-items: center; gap: 12px; margin: 12px 0 14px; }
.play {
  width: 60px; height: 60px; flex: none; border-radius: 50%; background: var(--earth); color: #fff;
  display: grid; place-items: center; position: relative;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--earth) 40%, transparent);
  transition: transform .12s ease, background-color .15s;
}
.play:active { transform: scale(.92); background: var(--earth-press); }
.play svg { width: 28px; height: 28px; fill: currentColor; grid-area: 1 / 1; }
.play .i-pause, .play.playing .i-play { display: none; }
.play.playing .i-pause { display: block; }
.play .spinner {
  grid-area: 1 / 1; width: 30px; height: 30px; border-radius: 50%; display: none;
  border: 3px solid rgba(255,255,255,.35); border-top-color: #fff; animation: spin .8s linear infinite;
}
.play.loading svg { display: none !important; }
.play.loading .spinner { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

.track-wrap { flex: 1; min-width: 0; padding: 10px 0 0; cursor: pointer; touch-action: none; }
.track { position: relative; height: 6px; border-radius: 3px; background: var(--fill); }
.track-fill { height: 100%; width: 0; border-radius: 3px; background: var(--earth); }
.knob {
  position: absolute; top: 50%; left: 0; width: 16px; height: 16px; margin: -8px 0 0 -8px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.3); transition: transform .12s;
}
.track-wrap.drag .knob { transform: scale(1.3); }
.times { display: flex; justify-content: space-between; margin-top: 6px; color: var(--secondary); font: 500 12px/1 ui-monospace, 'SF Mono', Menlo, monospace; }
.speed {
  flex: none; min-width: 48px; height: 36px; padding: 0 8px; border-radius: 10px; background: var(--fill);
  font-weight: 700; font-size: 14px; color: var(--label); transition: transform .12s;
}
.speed:active { transform: scale(.92); }
.speed.slow { background: color-mix(in srgb, var(--earth) 18%, transparent); color: var(--earth); }

/* ข้อความ */
.text-wrap { position: relative; }
textarea {
  display: block; width: 100%; min-height: 120px; max-height: 45vh; resize: none;
  font: 400 20px/1.55 'LINE Seed Sans TH', system-ui, sans-serif; color: var(--label);
  background: var(--bg); border: 1px solid transparent; border-radius: 14px; padding: 12px 14px;
  -webkit-user-select: text; user-select: text;
}
textarea:focus { outline: 2px solid var(--earth); outline-offset: -1px; }
.reset {
  margin-top: 4px; min-height: 40px; font-size: 14px; font-weight: 700; color: var(--earth); padding: 0 2px;
}
.rule { color: var(--secondary); font-size: 13px; line-height: 1.45; margin-top: 10px; }
.rule b { color: var(--label); }

.empty { text-align: center; padding: 32px var(--pad); }
.empty-title { font-size: 18px; font-weight: 800; }
.empty-sub { color: var(--secondary); font-size: 14px; margin: 6px 0 18px; }

.mystats { text-align: center; color: var(--secondary); font-size: 13px; margin-top: 12px; }

/* แถบปุ่มล่าง — ใต้นิ้วโป้ง */
.actionbar {
  flex: none; display: flex; gap: 8px;
  padding: 10px calc(var(--safe-r) + var(--pad)) calc(var(--safe-b) + 10px) calc(var(--safe-l) + var(--pad));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(16px); backdrop-filter: saturate(1.6) blur(16px);
  border-top: .5px solid var(--separator);
}
.actionbar .act { height: 54px; min-width: 0; padding: 0 10px; font-size: 16px; }
.actionbar .skip { flex: 1; color: var(--secondary); }
.actionbar .replay { flex: 1.15; }
.actionbar .replay svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.actionbar .save { flex: 1.7; font-size: 17px; }
.actionbar .save.changed { background: var(--green); }
@media (min-width: 700px) {
  .actionbar { justify-content: center; }
  .actionbar .act { flex: 0 1 auto; min-width: 150px; }
  .actionbar .save { min-width: 220px; }
}

/* คีย์บอร์ดเปิด: เก็บของที่ไม่จำเป็นให้เห็นข้อความกับปุ่ม */
body.kb .rule, body.kb .mystats, body.kb .goal { display: none; }
body.kb .content { padding-top: 6px; }
body.kb .player { margin: 8px 0 10px; }
body.kb textarea { min-height: 88px; }
body.kb .actionbar { padding-bottom: 8px; }

/* จอเล็กมาก (iPhone SE 1 / Android 320px) */
@media (max-width: 359px) {
  .title h1 { font-size: 18px; }
  .mode-chip { font-size: 11.5px; padding: 2px 7px; }
  .play { width: 52px; height: 52px; }
  .play svg { width: 24px; height: 24px; }
  .speed { min-width: 42px; }
  .player { gap: 10px; }
  textarea { font-size: 18px; }
  .actionbar { gap: 6px; }
  .actionbar .act { padding: 0 6px; font-size: 15px; }
  .actionbar .replay span { display: none; }
  .actionbar .replay { flex: .8; }
}
@media (max-height: 600px) { textarea { min-height: 96px; } }

/* ---------- แผ่นบัญชี (bottom sheet) ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; transition: opacity .2s ease; z-index: 10; }
.sheet-backdrop.show { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-width: 560px; margin: 0 auto;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 8px calc(var(--safe-r) + var(--pad)) calc(var(--safe-b) + 14px) calc(var(--safe-l) + var(--pad));
  transform: translateY(100%); transition: transform .26s cubic-bezier(.2,.8,.2,1);
  display: flex; flex-direction: column; gap: 10px;
}
.sheet.show { transform: translateY(0); }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--separator); margin: 0 auto 6px; }
.sheet-name { font-size: 20px; font-weight: 800; text-align: center; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat-row div { background: var(--card); border-radius: 14px; padding: 12px 6px; text-align: center; }
.stat-row b { display: block; font-size: 22px; font-weight: 800; color: var(--earth); }
.stat-row span { font-size: 13px; color: var(--secondary); }
.sheet-note { color: var(--secondary); font-size: 13px; text-align: center; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; z-index: 20; top: calc(var(--safe-t) + 64px);
  transform: translate(-50%, -8px); max-width: calc(100% - 32px);
  background: var(--label); color: var(--bg); font-size: 14px; font-weight: 700;
  padding: 8px 14px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease; text-align: center;
}
.toast.show { opacity: .92; transform: translate(-50%, 0); }
.toast.err { background: var(--red); color: #fff; }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0s !important; animation-duration: 0s !important; }
}
