/* ===== มือถือ (จอแคบ ≤ 640px): จัดหน้าให้กระชับ ใช้นิ้วกดง่าย · จอคอมไม่โดนไฟล์นี้ =====
   1) แถบบนบรรทัดเดียว: โลโก้ · ชื่อเรื่อง · ☰ เมนู · โปรไฟล์ — ปุ่มอื่นอยู่ในเมนู (10-app.js ใส่ .nav-c/.m-open)
   2) หัวเรื่อง (ชื่อเรื่อง/บันทึก/แท็บ) เหลือ 2 แถวเตี้ย ๆ แท็บเลื่อนซ้าย-ขวา
   3) ปุ่มลอย (แชท GPT · เสียงแจ้ง · วิทยุ · หิมะ) รวมเป็นปุ่มเดียว (58-mobile-fab-js.js)
   4) ปุ่มเล็กขยายให้กดง่าย (สูง ≥ 40px) */
@media (max-width:640px){
  /* ---------- 1) แถบบน ---------- */
  html body .topbar,html body[data-pg] .topbar{flex-wrap:nowrap;gap:8px;padding:6px 10px;min-height:52px;align-items:center}
  html body .topbar .logo{flex:none;order:0}
  html body .topbar .logo img{height:34px;max-width:42vw}
  html body .topbar #crumb{order:1;flex:1 1 auto;min-width:0;gap:4px;overflow:hidden}
  html body .topbar #crumb b{display:block;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding:10px 2px;line-height:20px}
  html body .topbar>.sp{order:1;flex:1 1 0;min-width:0}
  html body .topbar .mnavw{display:contents!important}
  html body .topbar .navtog{order:2;flex:none;min-height:40px;padding:8px 12px}
  html body .topbar .profb{order:3;flex:none;width:40px;height:40px}
  /* ปุ่มที่ย้ายเข้าเมนู: ซ่อนตอนเมนูปิด (ตอนเปิดไม่บังคับ display เพื่อให้ค่าซ่อนจากแอดมินยังมีผล) */
  html body .topbar:not(.m-open) :is(#upgradeb,#homeb,#wstats,#hq,#langb,#tmenu){display:none!important}
  /* เมนูเปิด: ขยายแถบบนลงมาเป็นแผงเต็มความกว้าง */
  html body .topbar.m-open{flex-wrap:wrap;row-gap:10px;max-height:100vh;max-height:100dvh;overflow-y:auto;overscroll-behavior:contain;padding-bottom:14px;box-shadow:0 18px 40px rgba(0,0,0,.6)}
  html body .topbar.nav-c .mnav{position:static;order:10;flex:1 1 100%;width:100%;margin:0;padding:6px;box-shadow:none}
  html body .topbar.nav-c .mnav button{min-height:46px}
  html body .topbar.m-open #upgradeb{order:11;flex:1 1 100%;justify-content:center;margin:0;min-height:46px;font-size:16px;border-radius:12px}
  html body .topbar.m-open #homeb{order:12;display:inline-flex;align-items:center;min-height:42px;padding:8px 14px;border:1px solid rgba(255,255,255,.16);border-radius:10px;background:rgba(255,255,255,.05)}
  html body .topbar.m-open #langb{order:12;min-height:42px;min-width:52px}
  html body .topbar.m-open #tmenu{order:12}
  html body .topbar.m-open #tmenu>summary{min-height:42px;display:inline-flex;align-items:center}
  html body .topbar.m-open #tmenu .gpop{right:auto;left:0;max-width:calc(100vw - 20px)}
  html body .topbar.m-open #hq{order:13;flex:1 1 100%;width:100%;max-width:none;min-height:44px;font-size:16px}
  html body .topbar.m-open #wstats{order:14;margin:0}

  /* ---------- 2) หัวเรื่อง (sticky ใต้แถบบน) ---------- */
  html body .subbar,html body[data-pg] .subbar{margin:0 -4% 14px;padding:6px 4% 0}
  html body .subbar .top{display:flex;align-items:center;flex-wrap:nowrap;gap:6px;margin-bottom:4px}
  html body[data-pg] .subbar #pname,html body[data-pg] .subbar.stuck #pname{flex:1 1 auto;min-width:0;width:0;font-size:20px;line-height:1.3;padding:6px 0}
  html body .subbar .top>.sp{display:none}
  html body .subbar .top :is(#opjson,#save,#xdoc){flex:none;min-height:40px;padding:8px 12px;font-size:14px;white-space:nowrap}
  /* "เปิดไฟล์ข้อมูล" ยาวไป → ไอคอนโฟลเดอร์ (ชื่อเต็มยังอยู่ใน title/aria) */
  html body .subbar .top #opjson{font-size:0;width:42px;padding:0;display:inline-grid;place-items:center}
  html body .subbar .top #opjson::before{content:"📂";font-size:18px;line-height:1}
  html body .subbar .tabs,html body[data-pg] .subbar .tabs,html body.stproj .subbar .tabs{flex-wrap:nowrap;justify-content:flex-start;overflow-x:auto;overflow-y:hidden;-webkit-overflow-scrolling:touch;scrollbar-width:none;gap:2px;margin:0 -4%;padding:0 4%}
  html body .subbar .tabs::-webkit-scrollbar{display:none}
  html body .subbar .tabs a,html body[data-pg] .subbar .tabs a,html body[data-pg] .subbar.stuck .tabs a{flex:none;white-space:nowrap;padding:10px 14px;font-size:15px;min-height:44px;display:inline-flex;align-items:center}

  /* ---------- 3) ปุ่มลอยรวมเป็นปุ่มเดียว ---------- */
  #mfab{position:fixed;left:12px;bottom:calc(12px + env(safe-area-inset-bottom,0px));z-index:9002;width:46px;height:46px;padding:0;border-radius:50%;border:1px solid rgba(255,255,255,.22);background:linear-gradient(180deg,#3a3935,#1b1a18);color:#f2eee4;font-size:20px;line-height:1;display:grid;place-items:center;cursor:pointer;box-shadow:0 6px 16px rgba(0,0,0,.6),inset 0 1px 0 rgba(255,255,255,.2);transition:transform .2s,background .2s}
  #mfab svg{width:22px;height:22px;transition:transform .25s}
  html.mfab-open #mfab{background:linear-gradient(180deg,#ff5a4d,#c3170d);border-color:rgba(255,160,150,.6)}
  html.mfab-open #mfab svg{transform:rotate(45deg)}
  html.mfab-on #aiSndBtn,html.mfab-on #rdoBtn{left:12px!important;width:46px!important;height:46px!important;opacity:1!important;transition:transform .25s cubic-bezier(.3,1.4,.5,1),opacity .18s}
  html.mfab-on #aiSndBtn{bottom:calc(68px + env(safe-area-inset-bottom,0px))!important}
  html.mfab-on #rdoBtn{bottom:calc(122px + env(safe-area-inset-bottom,0px))!important}
  html.mfab-on #gptt{top:auto!important;right:auto!important;left:12px;bottom:calc(230px + env(safe-area-inset-bottom,0px));transform:none;writing-mode:horizontal-tb;min-height:46px;padding:0 16px;border-right:1px solid rgba(255,255,255,.14);border-radius:999px;display:inline-flex;align-items:center;gap:6px;transition:transform .25s cubic-bezier(.3,1.4,.5,1),opacity .18s}
  html.mfab-on:not(.mfab-open) :is(#aiSndBtn,#rdoBtn,#gptt){opacity:0!important;pointer-events:none!important;transform:translateY(24px) scale(.6)!important}

  /* ---------- 4) ปุ่มเล็ก → กดง่ายขึ้น (ขยายพื้นที่แตะ ไม่เปลี่ยนหน้าตามาก) ---------- */
  html body .hdl{width:36px;height:36px;font-size:15px}
  html body .hdl::after{content:"";position:absolute;inset:-6px;border-radius:50%}
  html body .aibtn{min-height:40px}
  html body summary{min-height:40px;box-sizing:border-box;padding-top:8px;padding-bottom:8px}
  html body :is(.aiepr,.aiepw,.aivepc){min-height:40px;padding-top:9px;padding-bottom:9px}
  html body .aihsw::after{content:"";position:absolute;inset:-10px -6px}
  html body #aiNLrg{height:32px}
  html body .topbar #crumb b,html body .topbar #sidep{min-height:40px}

  /* ---------- 5) ข้อความไม่ล้นกรอบ ---------- */
  /* หน้าสร้างปก: “สัดส่วนภาพ” กับ “ภาษาพรอมต์” วางซ้อนกันแทนวางคู่ ปุ่มจะได้กว้างพอ */
  #p-ai-cvr#p-ai-cvr#p-ai-cvr#p-ai-cvr#p-ai-cvr#p-ai-cvr .cvr-opts{grid-template-columns:1fr;gap:12px}
  /* หน้าบทหนัง: แถว “ประเภท” เคยตั้งกว้างขั้นต่ำ 300px → ล้นจอแคบ */
  html body #p-ai-script .aitop .aigsec{min-width:0!important;flex:1 1 100%!important;max-width:none}
  html body #p-ai-script .aitop .aigsec .aigsel{min-width:0;flex:1 1 auto}
  /* ช่องวางลิงก์ (แบบไฟล์หรือลิงก์): คอลัมน์ยืดตามจอ ไม่ใช่ตามความยาวข้อความ */
  html body #p-ai-script #aiNews .ainlinks{grid-template-columns:minmax(0,1fr)!important;min-width:0}
  html body #p-ai-script #aiNews .ainlinks .ainlk{min-width:0;max-width:100%}
  html body #p-ai-script #aiNews .ainlinks .ainlk input{min-width:0;width:100%}
  /* ชื่อยาวในช่องพิมพ์ (ชื่อเรื่อง ชื่อตัวละคร) → ตัดท้ายเป็น … แทนการโดนตัดกลางตัวอักษร (ตอนกดแก้ยังเห็นเต็ม) */
  html body :is(input[type=text],input[type=search],input:not([type])){text-overflow:ellipsis}
}
@media (max-width:640px) and (prefers-reduced-motion:reduce){
  #mfab,#mfab svg,html.mfab-on :is(#aiSndBtn,#rdoBtn,#gptt){transition:none!important}
}

/* เมนูโปรไฟล์ › ตั้งค่า (js/07-app.js ใส่ .tm-pop): โชว์กล่องตั้งค่าใต้ปุ่มโปรไฟล์ แม้แถบบนจะซ่อนปุ่มตั้งค่าไว้ */
@media (max-width:640px){
  html body .topbar:not(.m-open) #tmenu.tm-pop[open]{display:block!important}
  html body .topbar:not(.m-open) #tmenu.tm-pop>summary{position:absolute;width:1px;height:1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);border:0}
  html body .topbar:not(.m-open) #tmenu.tm-pop .gpop{right:0;left:auto;max-width:calc(100vw - 16px);max-height:calc(100dvh - 90px);overflow-y:auto}
}
