/* หน้าตาของเว็บเมล ต่อยอดจาก app.css ที่ใช้ร่วมกับหน้าจัดการ */

.topbar .searchbar {
    display: flex; align-items: center; gap: 9px;
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: 999px; padding: 0 14px; height: 36px;
    flex: 1; max-width: 620px; margin-left: 16px;
}
.topbar .searchbar svg { width: 18px; height: 18px; color: var(--text-3); flex: none; }
.topbar .searchbar input {
    border: 0; background: none; outline: none; flex: 1; min-width: 0;
    font: inherit; font-size: 14px; color: var(--text); padding: 0; height: 100%;
}
.topbar .who .me { display: inline-flex; border-radius: 50%; overflow: hidden; }
.topbar .who .me img { width: 32px; height: 32px; object-fit: cover; display: block; }

.sidenav .btn.compose {
    width: 100%; height: 42px; margin: 2px 0 10px;
    justify-content: flex-start; gap: 10px; padding: 0 12px;
    font-size: 13.5px; white-space: nowrap;
}
body.nav-min .sidenav .btn.compose { justify-content: center; padding: 0; }

.sidenav .badge {
    margin-left: auto; font-size: 11.5px; font-weight: 700;
    background: var(--brand); color: #fff;
    min-width: 20px; height: 19px; padding: 0 6px;
    border-radius: 999px; display: grid; place-items: center;
}
body.nav-min .sidenav .badge {
    position: absolute; top: 3px; right: 3px; margin: 0;
    min-width: 16px; height: 16px; font-size: 10px; padding: 0 4px;
}

/* ---------- รายการจดหมาย ---------- */

.list-head { margin-bottom: 16px; }

.mlist { display: flex; flex-direction: column; }
.mrow {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 16px; border-bottom: 1px solid var(--line-soft);
    color: var(--text); text-decoration: none;
}
.mrow:last-child { border-bottom: 0; }
.mrow:hover { background: var(--surface-2); text-decoration: none; }
.mrow .mtxt { min-width: 0; flex: 1; }
.mrow .mtop { display: flex; align-items: baseline; gap: 10px; }
.mrow .mfrom {
    font-size: 13.5px; color: var(--text-2); font-weight: 550;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}
.mrow .mdate { font-size: 12px; color: var(--text-3); flex: none; }
.mrow .msubj {
    font-size: 14px; margin-top: 1px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.mrow.unseen .mfrom { color: var(--text); font-weight: 700; }
.mrow.unseen .msubj { font-weight: 650; }
.mrow.unseen { background: #fafbfc; }

.pager {
    display: flex; align-items: center; justify-content: center; gap: 14px;
    padding: 14px; border-top: 1px solid var(--line-soft);
    font-size: 13px; color: var(--text-2);
}

/* ---------- อ่านจดหมาย ---------- */

.msg-head { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.msg-head .spacer { flex: 1; }

.mail-view .mv-head { padding: 18px 20px 14px; border-bottom: 1px solid var(--line-soft); }
.mail-view .mv-head h1 { margin: 0 0 13px; font-size: 19px; font-weight: 650; letter-spacing: -.2px; }
.mv-from { display: flex; align-items: center; gap: 12px; }
.mv-from .nm { font-size: 14px; font-weight: 600; }
.mv-from .em { color: var(--text-3); font-weight: 400; font-size: 13px; }
.mv-from .to { font-size: 12.5px; color: var(--text-2); margin-top: 1px; }
.mv-from .dt { font-size: 12.5px; color: var(--text-3); white-space: nowrap; }
.mv-from .spacer { flex: 1; }

.mv-atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 11px 20px; border-bottom: 1px solid var(--line-soft); }
.mv-atts .att {
    display: inline-flex; align-items: center; gap: 8px;
    border: 1px solid var(--line); border-radius: var(--r);
    padding: 7px 12px; background: var(--surface-2);
    font-size: 13px; color: var(--text); text-decoration: none; max-width: 260px;
}
.mv-atts .att:hover { border-color: var(--brand); text-decoration: none; }
.mv-atts .att svg { width: 16px; height: 16px; color: var(--text-2); flex: none; }
.mv-atts .att span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mv-atts .att em { font-style: normal; color: var(--text-3); font-size: 12px; flex: none; }

.mv-body { padding: 6px 20px 18px; }
.mv-text { margin: 0; white-space: pre-wrap; word-wrap: break-word; font: inherit; font-size: 14px; line-height: 1.65; }
.mv-frame { width: 100%; min-height: calc(100vh - 300px); border: 0; background: #fff; }

/* ---------- เขียนจดหมาย ---------- */

.compose-form .cf { margin-bottom: 15px; }
.compose-form .cf label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.compose-form textarea {
    width: 100%; resize: vertical; line-height: 1.65;
    font: inherit; font-size: 14px;
}
.compose-foot {
    display: flex; justify-content: flex-end; gap: 10px;
    padding: 16px 22px; border-top: 1px solid var(--line-soft);
    background: var(--surface-2);
}

/* ---------- ตั้งค่า ---------- */

.prof-row { display: flex; gap: 22px; align-items: flex-start; }
.prof-pic { flex: none; }
.prof-pic img { width: 84px; height: 84px; border-radius: 12px; object-fit: cover; display: block; }
.prof-pic .avatar.lg { width: 84px; height: 84px; border-radius: 12px; font-size: 32px; }
.prof-fields { flex: 1; min-width: 0; }
.prof-fields label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }

.bg-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 12px; }
.bg-opt { cursor: pointer; display: block; }
.bg-opt input { position: absolute; opacity: 0; pointer-events: none; }
.bg-opt .sw {
    display: block; height: 62px; border-radius: var(--r);
    border: 2px solid var(--line);
}
.bg-opt .nm { display: block; font-size: 12.5px; color: var(--text-2); margin-top: 6px; text-align: center; }
.bg-opt:hover .sw { border-color: #c7d2e0; }
.bg-opt.on .sw { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(58,102,200,.18); }
.bg-opt.on .nm { color: var(--brand-dark); font-weight: 600; }

@media (max-width: 860px) {
    .topbar .searchbar { display: none; }
    .mrow { padding: 11px 14px; }
    .mail-view .mv-head { padding: 18px 16px 14px; }
    .mail-view .mv-head h1 { font-size: 18px; }
    .mv-atts, .mv-body { padding-left: 16px; padding-right: 16px; }
    .mv-from { flex-wrap: wrap; }
    .mv-from .dt { width: 100%; }
    .prof-row { flex-direction: column; gap: 16px; }
    .compose-foot .btn { flex: 1; justify-content: center; }
}

/* แถบแจ้งว่ารูปในจดหมายถูกบล็อกไว้ พร้อมปุ่มให้กดดู */
.imgwarn {
    display: flex; align-items: center; gap: 10px;
    margin: 0 20px 12px; padding: 9px 13px;
    background: var(--amber-soft); border: 1px solid #f0dcae;
    border-radius: var(--r); font-size: 13px; color: #7a5b16;
}
.imgwarn svg { width: 17px; height: 17px; flex: none; }
.imgwarn .spacer { flex: 1; }


/* ---------- หน้าอ่านจดหมาย ให้พอดีความสูงจอ เลื่อนในกรอบเดียว ---------- */

body.view-page .content {
    height: calc(100vh - 56px);
    padding-bottom: 16px;
    display: flex; flex-direction: column; overflow: hidden;
}
body.view-page .mail-view {
    flex: 1; min-height: 0; margin-bottom: 0;
    display: flex; flex-direction: column;
}
body.view-page .mv-head, body.view-page .mv-atts { flex: none; }
body.view-page .mv-body { flex: 1; min-height: 0; overflow: auto; padding-bottom: 0; }
body.view-page .mv-frame { height: 100%; min-height: 0; }
body.view-page .mv-body:has(.mv-frame) { overflow: hidden; }

/* ---------- กล่องเขียนจดหมายลอยมุมขวาล่าง ---------- */

.composer {
    position: fixed; right: 22px; bottom: 0; z-index: 120;
    width: 540px; max-width: calc(100vw - 32px);
    height: 552px; max-height: calc(100vh - 70px);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    box-shadow: 0 -2px 16px -4px rgba(16,24,40,.22), 0 12px 28px -8px rgba(16,24,40,.18);
    display: flex; flex-direction: column; overflow: hidden;
}
.composer[hidden] { display: none; }
.composer form { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.composer[data-state="min"] { height: 44px; }
.composer[data-state="min"] .cp-body,
.composer[data-state="min"] .cp-foot { display: none; }

.composer[data-state="max"] {
    right: 3vw; left: 3vw; bottom: 0; width: auto; max-width: none;
    height: calc(100vh - 64px); max-height: none;
}

.cp-head {
    display: flex; align-items: center; gap: 4px;
    height: 44px; padding: 0 8px 0 16px; flex: none;
    background: #35404d; color: #fff; cursor: pointer;
}
.cp-head .cp-title { font-size: 13.5px; font-weight: 600; line-height: 1.7; }
.cp-head .spacer { flex: 1; }
.cp-ico {
    width: 30px; height: 30px; display: grid; place-items: center;
    background: none; border: 0; border-radius: 6px; cursor: pointer;
    color: inherit; flex: none;
}
.cp-ico:hover { background: rgba(255,255,255,.16); }
.cp-ico svg { width: 16px; height: 16px; }

.cp-body { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 4px 16px 0; overflow: auto; }
.cp-line { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line-soft); flex: none; }
.cp-line label { font-size: 12.5px; color: var(--text-3); width: 42px; flex: none; }
.cp-line input {
    flex: 1; min-width: 0; border: 0; outline: none; background: none;
    height: 38px; font: inherit; font-size: 13.5px; padding: 0;
}
.composer textarea {
    flex: 1; min-height: 120px; width: 100%;
    border: 0; outline: none; resize: none; background: none;
    padding: 12px 0; font: inherit; font-size: 14px; line-height: 1.65;
}

.cp-foot {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 14px; flex: none;
    border-top: 1px solid var(--line-soft); background: var(--surface);
}
.cp-foot .spacer { flex: 1; }
.cp-foot .cp-ico { color: var(--text-2); }
.cp-foot .cp-ico:hover { background: var(--surface-2); color: var(--text); }
.cp-foot .attach { cursor: pointer; }
.cp-files { font-size: 12.5px; color: var(--text-2); }

@media (max-width: 860px) {
    .composer,
    .composer[data-state="max"] {
        right: 0; left: 0; bottom: 0; width: auto; max-width: none;
        height: calc(100vh - 56px); max-height: none; border-radius: 0;
    }
    .composer[data-state="min"] { height: 44px; top: auto; }
    body.view-page .content { height: calc(100vh - 56px); }
}

/* ปุ่มเขียนจดหมายอยู่ในแถบเมนู กฎ .sidenav a จึงทับสีขาวของปุ่มจนอ่านไม่ออก
   ต้องระบุให้จำเพาะกว่าและครอบ hover ด้วย */
.sidenav a.btn.compose,
.sidenav a.btn.compose:hover {
    background: var(--brand); color: #fff; font-weight: 600;
}
.sidenav a.btn.compose:hover { background: var(--brand-dark); }
.sidenav a.btn.compose svg { color: #fff; }

/* ---------- โหมดแบ่งครึ่งจอ ---------- */

.panebar { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.panebar .spacer { flex: 1; }

body.split-page .content {
    height: calc(100vh - 56px);
    padding-bottom: 14px;
    display: flex; flex-direction: column; overflow: hidden;
}
.split { display: flex; gap: 14px; flex: 1; min-height: 0; }
.split-list {
    width: 400px; flex: none; min-height: 0;
    display: flex; flex-direction: column; margin-bottom: 0;
}
.split-list .mlist { flex: 1; min-height: 0; overflow: auto; }
.split-msg { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; margin-bottom: 0; }
.split-msg .mv-head, .split-msg .mv-atts, .split-msg .imgwarn { flex: none; }
.split-msg .mv-body { flex: 1; min-height: 0; overflow: hidden; padding-bottom: 0; }
.split-msg .mv-frame { height: 100%; min-height: 0; }
.split-msg .mv-text { overflow: auto; height: 100%; }

.split-empty {
    flex: 1; display: grid; place-items: center;
    color: var(--text-3); font-size: 14px;
    border: 1px dashed var(--line); border-radius: var(--r-lg);
    background: rgba(255,255,255,.5);
}

.split .mrow.on { background: var(--brand-soft); }
.split .mrow .mtop { flex-wrap: wrap; }

@media (max-width: 1100px) {
    .split-list { width: 330px; }
}
@media (max-width: 860px) {
    .split { flex-direction: column; }
    .split-list { width: auto; }
    .split-msg { min-height: 60vh; }
}

/* ป้ายจำนวนร่างที่ยังไม่ได้ส่ง ใช้สีจางกว่าเพราะไม่ใช่ของใหม่ */
.sidenav .badge.soft { background: var(--surface-2); color: var(--text-2); border: 1px solid var(--line); }

/* กำลังโหลดเนื้อหาใหม่ ทำให้จางลงนิดหน่อยจะได้รู้ว่าระบบทำงานอยู่ */
.content.loading { opacity: .55; pointer-events: none; transition: opacity .12s; }
.toast.err { background: var(--red); }

/* กฎ textarea กลางใน app.css มีขอบและมุมโค้ง ทับค่าที่ตั้งไว้ในกล่องเขียนจดหมาย
   ทำให้ขอบล่างโดนตัด ต้องระบุให้จำเพาะกว่าและเผื่อระยะล่างไว้ */
.composer .cp-body { padding: 4px 16px 12px; }
.composer .cp-body textarea {
    border: 0; border-radius: 0; background: none;
    padding: 12px 0; box-shadow: none; min-height: 140px;
}
.composer .cp-body textarea:focus { border: 0; box-shadow: none; outline: none; }
.composer .cp-line input:focus { box-shadow: none; }

/* ในโหมดแบ่งครึ่งจอ ทั้งหน้าต้องไม่เลื่อน ไม่งั้นพอเปิดจดหมายแล้วแถบเลื่อนโผล่
   ความกว้างจะหดลงราว 15px ทำให้ทั้งหน้าดูโดนบีบ */
body.split-page, body.view-page { overflow: hidden; }
body.split-page .layout, body.view-page .layout { min-height: 0; height: calc(100vh - 56px); }
.split > * { min-width: 0; }
.split-msg, .split-list { overflow: hidden; }

@media (max-width: 860px) {
}

/* โหมดแบ่งครึ่งจอไม่ต้องเว้นขอบกว้างเท่าหน้าอื่น รายการควรอยู่ชิดแถบเมนู
   พื้นที่อ่านจะได้กว้างขึ้นและไม่มีแถบว่างคั่นกลางให้เกะกะ */
body.split-page .content { padding: 14px 14px 12px; }
body.split-page .page-head.list-head { margin-bottom: 10px; }
.split { gap: 12px; }

/* ---------- ตรึงโครงไม่ให้ขยับตอนแถบเลื่อนโผล่ ---------- */

/* เอกสารต้องไม่เลื่อนเลยในหน้าที่เป็นโครงตายตัว
   ถ้าปล่อยให้เลื่อนได้ พอเปิดอ่านจดหมายแล้วแถบเลื่อนโผล่
   ความกว้างหน้าจะหายไปราว 15px ทำให้ทั้งหน้าดูโดนบีบ */
html, body { height: 100%; }
body.split-page, body.view-page { overflow: hidden; height: 100%; }

/* กันแถบเมนูไม่ให้ยืดหดตามเนื้อหาข้าง ๆ */
.sidenav { flex: 0 0 226px; }
body.nav-min .sidenav { flex: 0 0 60px; }

/* จองที่ให้แถบเลื่อนไว้ล่วงหน้า พอมันโผล่มาความกว้างจะไม่กระตุก */
.mlist, .split-msg .mv-body, .cp-body { scrollbar-gutter: stable; }

@media (max-width: 860px) {
    .sidenav { flex: none; }
}

/* ---------- ระยะขอบมาตรฐานเดียวทุกหน้า ---------- */

/* เดิมแต่ละมุมมองใช้ระยะไม่เท่ากัน รายการ 24px แบ่งครึ่งจอ 14px อ่านเต็มหน้า 24px
   สลับไปมาแล้วเนื้อหาขยับทุกครั้ง กำหนดให้เท่ากันหมดที่ 14px */
.content,
body.split-page .content,
body.view-page .content { padding: 14px; }

/* หัวเรื่องของทุกมุมมองเว้นเท่ากัน */
.content > .page-head { margin-bottom: 12px; }
body.split-page .page-head.list-head { margin-bottom: 12px; }

/* การ์ดใบสุดท้ายไม่ต้องมีระยะล่างซ้ำกับ padding ของกรอบ */
.content > .card:last-child { margin-bottom: 0; }

@media (max-width: 860px) {
    .content,
    body.split-page .content,
    body.view-page .content { padding: 12px 12px 28px; }
}

/* ปุ่มสลับธีมใส่ไอคอนและข้อความไว้ทั้งสองสถานะ แล้วโชว์ตามธีมปัจจุบัน
   จะได้สลับกลับไปกลับมาได้โดยไม่ต้องโหลดหน้าใหม่ */
/* โชว์ไอคอนกับข้อความชุดเดียวตามธีมปัจจุบัน
   เดิมมีบรรทัดสั่งให้แสดงทั้งสองชุดอยู่ท้ายสุด เลยทับกฎที่ซ่อนไว้ข้างบน */
#themetoggle .ico-dark { display: inline-flex; align-items: center; }
#themetoggle .ico-light { display: none; }
#themetoggle .lb-dark { display: inline; }
#themetoggle .lb-light { display: none; }
:root[data-theme="dark"] #themetoggle .ico-dark { display: none; }
:root[data-theme="dark"] #themetoggle .lb-dark { display: none; }
:root[data-theme="dark"] #themetoggle .ico-light { display: inline-flex; align-items: center; }
:root[data-theme="dark"] #themetoggle .lb-light { display: inline; }

/* ปุ่มกลับไปรายการใช้เฉพาะจอเล็ก จอใหญ่เห็นรายการอยู่ข้าง ๆ อยู่แล้ว */
.backlist { display: none; margin-bottom: 10px; }

@media (max-width: 860px) {
    /* จอเล็กแสดงทีละอย่าง เปิดอ่านแล้วซ่อนรายการ ไม่งั้นต้องเลื่อนยาวมากกว่าจะเจอเนื้อหา */
    .split:has(.split-msg) .split-list { display: none; }
    .split-msg { min-height: calc(100vh - 150px); }
    .backlist { display: inline-flex; }
    .split-msg .mv-from { gap: 8px; }
    .split-msg .mv-from .btn { padding: 0 10px; }
}

/* เบราว์เซอร์ที่ยังไม่รองรับ :has ให้ย่อรายการลงแทนการซ่อน จะได้ยังใช้งานได้ */
@supports not selector(:has(*)) {
    @media (max-width: 860px) {
        .split-list .mlist { max-height: 38vh; overflow: auto; }
    }
}

/* ---------- เลือกหลายฉบับ ---------- */

.bulkbar {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 12px; border-bottom: 1px solid var(--line-soft);
    background: var(--surface-2); flex: none;
}
.bulkbar .spacer { flex: 1; }
.bulkbar .cnt { font-size: 12.5px; color: var(--text-3); }
/* ปุ่มสั่งงานโผล่เมื่อเลือกแล้วเท่านั้น จะได้ไม่รกตอนไม่ได้ใช้ */
.bulkbar .btn, .bulkbar .movesel { display: none; }
.bulkbar.on .btn, .bulkbar.on .movesel { display: inline-flex; }
.bulkbar.on .cnt { color: var(--brand-dark); font-weight: 600; }
.bulkbar .movesel {
    height: 28px; font: inherit; font-size: 12.5px; padding: 0 8px;
    border: 1px solid var(--line); border-radius: var(--r);
    background: var(--surface); color: var(--text);
}

.mrow { position: relative; }
.mpick {
    display: flex; align-items: center; padding: 0 2px 0 4px;
    flex: none; cursor: pointer;
}
.mpick input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--brand); }
.mlink {
    display: flex; align-items: center; gap: 11px;
    flex: 1; min-width: 0; color: inherit; text-decoration: none;
    padding: 2px 0;
}
.mlink:hover { text-decoration: none; }
.mrow.picked { background: var(--brand-soft); }
:root[data-theme="dark"] .mrow.picked { background: #22304a; }

/* ช่องอัปโหลดรูปพื้นหลังของผู้ใช้ */
.bgupload { margin-bottom: 16px; padding-bottom: 14px; border-bottom: 1px solid var(--line-soft); }
.bgupload label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }

/* ตัวอย่างพื้นหลังแบบรูปของฉัน แสดงรูปจริงถ้ามี */
.bg-opt[data-k="custom"] .sw {
    background: var(--surface-2) url('?p=bgimg') center/cover no-repeat;
    display: grid; place-items: center;
}
.bg-opt[data-k="custom"] .sw::after {
    content: "รูป"; font-size: 11px; color: var(--text-3);
    background: rgba(255,255,255,.8); padding: 1px 7px; border-radius: 999px;
}

/* วงไฮไลต์ต้องขยับทันทีที่กด ไม่ใช่รอจนบันทึกเสร็จ
   เดิมใช้คลาสที่เซิร์ฟเวอร์ใส่มาอย่างเดียว กดแล้วไม่มีอะไรตอบสนอง เลยดูเหมือนใช้ไม่ได้ */
.bg-opt:has(input:checked) .sw { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(58,102,200,.18); }
.bg-opt:has(input:checked) .nm { color: var(--brand-dark); font-weight: 600; }

/* ข้อความจำนวนที่เลือกโดนบีบจนตกบรรทัด */
.bulkbar .cnt { white-space: nowrap; }
.bulkbar .movesel { flex: none; max-width: 150px; }
.bulkbar .btn { flex: none; white-space: nowrap; }

/* รายการกล่องปลายทางในหน้าต่างย้ายจดหมาย */
.pickfolders { padding: 8px 10px 0; display: flex; flex-direction: column; }
.pickfolder {
    display: flex; align-items: center; gap: 11px;
    padding: 0 12px; height: 42px; line-height: 1.7;
    background: none; border: 0; border-radius: var(--r);
    font: inherit; font-size: 13.5px; color: var(--text);
    cursor: pointer; text-align: left; width: 100%;
}
.pickfolder:hover { background: var(--surface-2); }
.pickfolder svg { width: 17px; height: 17px; color: var(--text-3); flex: none; }

/* ---------- ให้สีธีมไหลไปทั้งหน้า ไม่ใช่แค่พื้นหลังกลาง ---------- */

/* แถบบนกับแถบเมนูโปร่งแสง พื้นหลังที่เลือกไว้จะเห็นทะลุขึ้นมาเป็นโทนเดียวกันทั้งหน้า */
.topbar { background: rgba(255,255,255,.72); backdrop-filter: saturate(140%) blur(10px); }
.sidenav { background: rgba(255,255,255,.55); backdrop-filter: saturate(140%) blur(8px); }
.card { background: rgba(255,255,255,.88); }
.split-empty { background: rgba(255,255,255,.45); }
.bulkbar, .card > h2 { background: rgba(255,255,255,.5); }

/* โหมดมืดใช้สีทึบตามเดิม ไม่ต้องโปร่ง */
:root[data-theme="dark"] .topbar,
:root[data-theme="dark"] .sidenav,
:root[data-theme="dark"] .card { background: var(--surface); backdrop-filter: none; }
:root[data-theme="dark"] .bulkbar,
:root[data-theme="dark"] .card > h2 { background: var(--surface-2); }
:root[data-theme="dark"] .split-empty { background: rgba(255,255,255,.02); }

/* เนื้อจดหมายยังต้องเป็นพื้นขาวทึบ ไม่งั้นอ่านยาก */
.mail-view .mv-body { background: #fff; }
:root[data-theme="dark"] .mail-view .mv-body { background: var(--surface); }

/* ปุ่มสร้างโฟลเดอร์ในแถบเมนู หน้าตาเหมือนรายการอื่นแต่เป็นปุ่ม */
.newfolder {
    display: flex; align-items: center; gap: 10px;
    height: 34px; padding: 0 10px; width: 100%;
    background: none; border: 0; border-radius: 7px;
    color: var(--text-3); font: inherit; font-size: 13px; cursor: pointer;
    line-height: 1.7;
}
.newfolder:hover { background: #f2f5f9; color: var(--text); }
:root[data-theme="dark"] .newfolder:hover { background: var(--surface-2); }
.newfolder svg { width: 16px; height: 16px; flex: none; }
body.nav-min .newfolder { justify-content: center; padding: 0; }
body.nav-min .newfolder .lb { display: none; }

/* ช่องกรอกในกล่องยืนยัน */
.modal-input { padding: 0 22px 4px; }
.modal-input input { width: 100%; }

/* ฟอร์มเปลี่ยนรหัสผ่าน */
.pwgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.pwgrid label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.pwgrid .pw-wrap input { padding-right: 74px; }
.pwgrid .pw-wrap .pw-btn[data-pw="toggle"] { right: 39px; }
.pwgrid .pw-wrap .pw-btn[data-pw="gen"] { right: 6px; }
.pwgrid > div:first-child .pw-wrap input { padding-right: 40px; }
.pwgrid > div:first-child .pw-wrap .pw-btn[data-pw="toggle"] { right: 6px; }
.pwgrid > div:last-child .pw-wrap input { padding-right: 40px; }
.pwgrid > div:last-child .pw-wrap .pw-btn[data-pw="toggle"] { right: 6px; }

/* ---------- ตัวกรองจดหมาย ---------- */

.rulgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.rulgrid .wide { grid-column: span 2; }
.rulgrid label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.rulgrid select, .rulgrid input { width: 100%; }
.rulstop { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 13px; color: var(--text-2); cursor: pointer; }
.rulstop input { width: 16px; height: 16px; accent-color: var(--brand); }

.rullist { display: flex; flex-direction: column; }
.rulrow {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 18px; border-bottom: 1px solid var(--line-soft);
}
.rulrow:last-child { border-bottom: 0; }
.rulrow.off { opacity: .55; }
.rultxt { flex: 1; min-width: 0; }
.rulname { font-size: 14px; font-weight: 600; }
.ruldesc { font-size: 12.5px; color: var(--text-2); margin-top: 2px; line-height: 1.6; }
.rulrow .tag {
    font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 999px;
    background: var(--surface-2); border: 1px solid var(--line); color: var(--text-3);
}

@media (max-width: 860px) {
    .rulgrid .wide { grid-column: span 1; }
    .rulrow { flex-wrap: wrap; }
    .rultxt { flex: 1 1 100%; }
}

/* ---------- ตัวจัดรูปแบบข้อความ ---------- */

.rte-bar {
    display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
    padding: 6px 0; border-bottom: 1px solid var(--line-soft); flex: none;
}
.rte-bar .spacer { flex: 1; }
.rte-b {
    min-width: 30px; height: 28px; padding: 0 7px;
    background: none; border: 0; border-radius: 6px; cursor: pointer;
    font: inherit; font-size: 13.5px; color: var(--text-2);
    display: inline-flex; align-items: center; justify-content: center;
}
.rte-b:hover { background: var(--surface-2); color: var(--text); }
.rte-b svg { width: 15px; height: 15px; }
.rte-sep { width: 1px; height: 18px; background: var(--line); margin: 0 4px; }
.rte-mode { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-2); cursor: pointer; }
.rte-mode input { width: 15px; height: 15px; accent-color: var(--brand); }

.rte {
    flex: 1; min-height: 140px; overflow: auto;
    padding: 12px 0; font-size: 14px; line-height: 1.65;
    outline: none;
}
.rte:empty::before { content: attr(data-ph); color: var(--text-3); }
.rte a { color: var(--brand); }
.rte ul, .rte ol { padding-left: 22px; margin: 6px 0; }

/* ---------- ตรึงหน้าไม่ให้เลื่อนทุกขนาดจอ ----------
   ก่อนหน้านี้ยกเว้นให้จอเล็กเลื่อนได้ หน้าต่างแคบ ๆ จึงยังยาวลงไปเรื่อย ๆ
   ตอนนี้ตรึงทุกขนาด ให้เลื่อนเฉพาะข้างในรายการหรือเนื้อจดหมายเท่านั้น */
html, body { height: 100%; overflow: hidden; }
body.split-page, body.view-page { overflow: hidden; height: 100%; }
.layout { height: calc(100vh - 56px); min-height: 0; overflow: hidden; }
.content { overflow: auto; }
body.split-page .content, body.view-page .content { overflow: hidden; }

@media (max-width: 860px) {
    html, body,
    body.split-page, body.view-page { overflow: hidden; height: 100%; }
    body.split-page .content, body.view-page .content { height: auto; overflow: hidden; }
    .layout { height: calc(100vh - 56px); }
    .split { flex-direction: column; min-height: 0; }
    .split-list, .split-msg { overflow: hidden; min-height: 0; }
    .split-list .mlist { overflow: auto; }
    /* ไม่ได้เปิดอ่านอะไร รายการกินพื้นที่ทั้งหมด */
    .split:not(:has(.split-msg)) .split-list { flex: 1; }
}

/* ตัวบอกสถานะตอนเลื่อนลงไปโหลดรายการเพิ่ม แทนแถบแบ่งหน้าเดิม */
.listmore { padding: 10px; text-align: center; font-size: 12.5px; color: var(--text-3); flex: none; }
.listmore:empty { padding: 0; }

/* ปุ่มเปิดหน้าต่างเปลี่ยนรหัสผ่าน แทนการ์ดยาว ๆ บนหน้าตั้งค่า */
.pwcard { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 16px; }
.pwcard .hint { margin: 0; }
#pwmodal .modal-box { max-width: 520px; }
#pwmodal .modal-body { padding: 4px 20px 0; }
#pwmodal .pwgrid { display: grid; gap: 12px; }
#pwmodal .modal-foot { display: flex; gap: 8px; justify-content: flex-end; }

/* ต้นฉบับกฎที่ยกมาจากตัวกรองเดิม */
.rulraw { margin-top: 6px; }
.rulraw summary { cursor: pointer; color: var(--brand); font-size: 12.5px; }
.rulraw pre {
    margin: 6px 0 0; padding: 10px 12px; overflow: auto;
    background: var(--surface-2); border: 1px solid var(--line);
    border-radius: 8px; font-size: 12px; line-height: 1.55;
}

/* ลิงก์ในจดหมายข้อความล้วน ต้องเห็นชัดว่ากดได้ */
.mv-text a { color: var(--brand); text-decoration: underline; word-break: break-all; }
.mv-text a:hover { color: var(--brand-dark); }

/* ปุ่มล้างถังขยะวางชิดขวาของหัวรายการ */
.emptybox { display: inline-block; margin-bottom: 10px; }

/* ---------- แถบเครื่องมือเขียนจดหมาย ---------- */
.rte-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.rte-grp { display: inline-flex; align-items: center; gap: 2px; padding-right: 6px; margin-right: 2px;
           border-right: 1px solid var(--line); }
.rte-grp:last-of-type { border-right: 0; }
.rte-sel {
    height: 28px; padding: 0 22px 0 8px; font: inherit; font-size: 12.5px;
    border: 1px solid var(--line); border-radius: 6px;
    background: var(--surface); color: var(--text); max-width: 140px;
}
.rte-sel.sm { max-width: 96px; }
.rte-color { position: relative; display: inline-grid; place-items: center;
             width: 28px; height: 28px; border-radius: 6px; cursor: pointer; }
.rte-color:hover { background: var(--surface-2); }
.rte-color input[type=color] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.rte-color .sw { font-weight: 700; font-size: 13px; line-height: 1;
                 border-bottom: 3px solid currentColor; padding-bottom: 1px; }
.rte-color .sw.hi { color: var(--text); background: #ffe680; border-bottom-color: transparent;
                    padding: 0 3px; border-radius: 3px; }

/* ตารางที่แทรกในตัวจดหมาย ต้องเห็นเส้นตอนกำลังพิมพ์ด้วย */
.rte table { border-collapse: collapse; }
.rte table td { border: 1px solid var(--line); padding: 6px 8px; min-width: 48px; }
.rte blockquote { margin: 8px 0; padding: 4px 0 4px 12px; border-left: 3px solid var(--line); color: var(--text-2); }
.rte img { max-width: 100%; height: auto; }
.rte hr { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }
.mv-body table { border-collapse: collapse; max-width: 100%; }

/* ---------- มือถือ: อ่านจดหมายเต็มหน้าเสมอ ไม่ต้องบีบเป็นสองคอลัมน์ ---------- */
@media (max-width: 820px) {
    .split { display: block; height: 100%; }
    .split-list { height: 100%; max-height: none; }
    .split-list .mlist { max-height: none; overflow: auto; }
    .split-empty { display: none; }
    .split-msg {
        position: fixed; left: 0; right: 0; top: 56px; bottom: 0;
        margin: 0; border-radius: 0; z-index: 40;
        overflow: auto; -webkit-overflow-scrolling: touch;
    }
    /* บนจอเล็กมีมุมมองเดียวอยู่แล้ว ปุ่มสลับมุมมองจึงไม่มีประโยชน์ */
    .list-head .paneswitch { display: none; }
}

/* ---------- จดจำฉันในหน้าเข้าสู่ระบบ ---------- */
.login .remember {
    display: flex; align-items: center; gap: 8px;
    margin: 2px 0 14px; font-size: 13.5px; color: var(--text-2); cursor: pointer;
}
.login .remember input { width: 16px; height: 16px; accent-color: var(--brand); cursor: pointer; }

/* ---------- ชื่อผู้ส่งอยู่บน อีเมลอยู่ล่าง ---------- */
.mv-from .nm { font-weight: 600; line-height: 1.35; }
.mv-from .em { font-size: 12.5px; color: var(--text-3); line-height: 1.35; word-break: break-all; }
.topbar .who .txt { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.25; min-width: 0; }
.topbar .who .txt .nm { font-weight: 600; font-size: 13px; }
.topbar .who .txt .em { font-size: 11.5px; color: var(--text-3); }

/* ---------- ไฟล์แนบ: กดชื่อเพื่อดู กดไอคอนเพื่อโหลด ---------- */
.att { display: inline-flex; align-items: stretch; max-width: 100%; }
.att .att-open {
    display: inline-flex; align-items: center; gap: 8px; min-width: 0;
    padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px;
    background: var(--surface); color: var(--text); font-size: 13px; text-decoration: none;
}
.att .att-open span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .att-open em { font-style: normal; color: var(--text-3); font-size: 12px; flex: none; }
.att .att-open svg { width: 16px; height: 16px; color: var(--text-3); flex: none; }
.att.has-view .att-open { border-radius: 8px 0 0 8px; border-right: 0; }
.att .att-dl {
    display: none; align-items: center; padding: 0 9px;
    border: 1px solid var(--line); border-radius: 0 8px 8px 0;
    background: var(--surface-2); color: var(--text-2);
}
.att.has-view .att-dl { display: inline-flex; }
.att .att-open:hover, .att .att-dl:hover { background: var(--brand-soft); color: var(--brand-dark); }
.att .att-dl svg { width: 16px; height: 16px; }

/* ---------- หน้าต่างดูไฟล์แนบ ---------- */
.modal.wide .modal-box { max-width: min(1000px, 96vw); width: 1000px; }
.attview {
    margin: 4px 20px; border: 1px solid var(--line); border-radius: 10px;
    background: var(--surface-2); overflow: auto;
    height: min(70vh, 720px); display: grid; place-items: center;
}
.attview iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.attview img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }

/* ---------- จอเล็ก ---------- */
@media (max-width: 820px) {
    /* แถบล่างของเบราว์เซอร์มือถือกินพื้นที่จริงไปด้วย
       100vh จึงสูงเกินจอ ปุ่มล่างสุดเลยโดนบัง กดไม่ได้
       ใช้ 100dvh ที่หดตามแถบจริง แล้วเผื่อ safe-area ของเครื่องที่มีขอบมนอีกชั้น */
    html, body { height: 100dvh; }
    .layout { height: calc(100dvh - 56px); }
    .content { padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }
    .split-msg { top: 56px; bottom: 0; height: auto; padding-bottom: env(safe-area-inset-bottom, 0px); }
    .sidenav { padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }

    /* กล่องเขียนจดหมายบนมือถือให้เต็มจอไปเลย ไม่ต้องลอยแล้วโดนแถบล่างบัง */
    .composer {
        left: 0; right: 0; bottom: 0; top: 56px;
        width: auto; max-width: none; height: auto; border-radius: 0;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* แถบเครื่องมือและปุ่มต่าง ๆ ตกบรรทัดได้ ไม่ต้องดันจนล้นจอ */
    .list-head, .mv-head, .bulkbar, .page-head { flex-wrap: wrap; row-gap: 8px; }
    .mv-head .btn, .list-head .btn { flex: none; }
    .att { max-width: 100%; }
    .att .att-open { max-width: calc(100vw - 96px); }

    /* หน้าต่างต่าง ๆ บนจอเล็กให้เกือบเต็มจอ อ่านง่ายกว่ากล่องเล็ก ๆ กลางจอ */
    .modal-box { max-width: 96vw; }
    .modal.wide .modal-box { width: 96vw; }
    .attview { margin: 4px 12px; height: min(64dvh, 560px); }
    .modal-foot { padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)); }
}

@media (max-width: 520px) {
    /* ที่อยู่อีเมลยาว ๆ บนแถบบนกินที่จนปุ่มโดนดันตก เหลือแค่รูปพอ */
    .topbar .who .txt { display: none; }
    .topbar .brand .s { display: none; }
    .rte-bar { row-gap: 4px; }
    .rte-grp { border-right: 0; padding-right: 2px; }
}

/* ================= รอบแก้ตามที่แจ้งมา ================= */

/* ---------- ไฟล์แนบกลับไปเป็นปุ่มก้อนเดียวเหมือนเดิม ---------- */
/* ของเดิมสวยอยู่แล้ว แค่เติมไอคอนดาวน์โหลดไว้ท้ายปุ่ม ไม่แยกเป็นสองก้อน */
.att {
    display: inline-flex; align-items: center; gap: 8px; max-width: 100%;
    padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px;
    background: var(--surface); color: var(--text);
    font-size: 13px; text-decoration: none;
}
.att:hover { background: var(--brand-soft); border-color: var(--brand); color: var(--text); }
.att > svg { width: 16px; height: 16px; color: var(--text-3); flex: none; }
.att > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.att > em { font-style: normal; color: var(--text-3); font-size: 12px; flex: none; }
.att .att-dl {
    display: inline-grid; place-items: center; flex: none;
    width: 24px; height: 24px; margin-left: 2px; border-radius: 6px;
    color: var(--text-3);
}
.att .att-dl:hover { background: var(--brand); color: #fff; }
.att .att-dl svg { width: 15px; height: 15px; }

/* ---------- ตัวอ่าน PDF ---------- */
.attview { background: var(--surface-2); }
.attview iframe { width: 100%; height: 100%; border: 0; background: #fff; display: block; }

/* ---------- เมนูข้าง อ่านง่ายขึ้น ---------- */
/* เดิมตัวอักษรจางไปเมื่อพื้นหลังเป็นสีของธีม อ่านยากโดยเฉพาะโหมดมืด */
.sidenav a { color: var(--text); }
.sidenav a svg { color: var(--text-2); }
.sidenav a:hover { color: var(--text); }
.sidenav a.on { color: var(--brand-dark); font-weight: 600; }
.sidenav a.on svg { color: var(--brand); }
.sidenav .grp { color: var(--text-2); font-weight: 600; }
.sidenav .newfolder, .sidenav .collapse { color: var(--text-2); }
:root[data-theme="dark"] .sidenav a { color: #e8ecf1; }
:root[data-theme="dark"] .sidenav a svg { color: #b9c2cc; }
:root[data-theme="dark"] .sidenav .grp { color: #9aa5b1; }
:root[data-theme="dark"] .sidenav a.on { color: #fff; background: var(--brand-soft); }
:root[data-theme="dark"] .sidenav a.on svg { color: var(--brand-dark); }

/* ป้ายผู้ดูแลในเมนูบัญชี */
.acctmenu .am-admin { display: flex; align-items: center; gap: 8px; }
.acctmenu .am-admin .tag { margin-left: auto; font-size: 10.5px; padding: 1px 7px; }

/* ---------- หัวจดหมาย: ปุ่มไม่ต้องเบียดกันในบรรทัดเดียว ---------- */
.msg-head, .mv-head .row1 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.msg-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }

/* ผู้รับเยอะ ๆ เช่นส่งต่อเป็นกลุ่ม 30 คน ต้องตัดบรรทัดได้และจำกัดความสูง */
.mv-to, .mv-from .to {
    display: block; max-height: 4.6em; overflow: auto;
    word-break: break-word; line-height: 1.5;
}

/* ================= จอเล็ก ================= */
@media (max-width: 820px) {
    /* Safari กับ Firefox บนมือถือวางแถบที่อยู่ไว้ "ด้านล่าง" ทับเนื้อหาจริง
       100dvh ยังไม่พอเพราะบางจังหวะแถบยุบแล้วเด้งกลับ เนื้อหาท้ายสุดจึงโดนบัง
       ทางที่ชัวร์คือให้ตัวที่เลื่อนได้เผื่อที่ว่างท้ายไว้เท่ากับความสูงแถบ */
    :root { --bottom-gap: calc(env(safe-area-inset-bottom, 0px) + 72px); }

    .content { padding-bottom: var(--bottom-gap); }
    .split-list .mlist { padding-bottom: var(--bottom-gap); }
    .split-msg { padding-bottom: var(--bottom-gap); }
    .mv-body { padding-bottom: 8px; }
    .sidenav { padding-bottom: var(--bottom-gap); }
    .modal-foot { padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)); }

    /* กล่องเขียนจดหมายก็ต้องไม่ให้ปุ่มส่งไปอยู่ใต้แถบ */
    .composer { padding-bottom: env(safe-area-inset-bottom, 0px); }
    .composer .cp-foot { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }

    /* ปุ่มในหัวจดหมาย: ให้ปุ่มกลับอยู่ซ้าย ที่เหลือชิดขวาในบรรทัดเดียวกัน
       ถ้าที่ไม่พอจริง ๆ ค่อยตกลงไปเป็นแถวของตัวเองทั้งกลุ่ม ไม่ใช่ตกทีละปุ่ม */
    .msg-head { row-gap: 8px; }
    .msg-actions { flex: 0 0 auto; }
    .msg-head .btn, .mv-head .btn { white-space: nowrap; }

    /* หัวจดหมายที่มีผู้รับเยอะ ๆ ให้ย่อลงอีกบนจอเล็ก */
    .mv-to, .mv-from .to { max-height: 3.2em; }
}

@media (max-width: 520px) {
    /* จอแคบมาก ปุ่มเหลือแต่ไอคอน ข้อความยาว ๆ ตัดออกไม่ให้ดันจนตก */
    .msg-actions .btn .lb-txt { display: none; }
    .att { max-width: 100%; }
    .att > span { max-width: 46vw; }
}

/* ---------- จอเล็ก: อ่านจดหมายให้เต็มหน้าจริง ๆ ---------- */
@media (max-width: 820px) {
    /* เดิมเอา .split-msg ไปลอยทับแบบ fixed ซึ่งมีปัญหาสองอย่าง
       หนึ่ง หัวรายการกับจำนวนฉบับยังอยู่ข้างหลังแล้วโผล่แทรกออกมา
       สอง เมนูโปรไฟล์ที่กดจากแถบบนไปอยู่ใต้ตัวที่ลอยทับ เลยกดไม่ได้
       เปลี่ยนเป็นซ่อนฝั่งรายการไปเลยตอนเปิดอ่าน แล้วให้เนื้อจดหมายไหลตามปกติ */
    body:has(.split-msg) .split-list,
    body:has(.split-msg) .list-head { display: none; }
    body:has(.split-msg) .split { display: block; }

    .split-msg {
        position: static; left: auto; right: auto; top: auto; bottom: auto;
        z-index: auto; height: auto; min-height: calc(100dvh - 96px);
        border-radius: 0; margin: 0;
    }

    /* เมนูโปรไฟล์ต้องอยู่บนสุดเสมอ และไม่ล้นออกนอกจอ */
    .acctmenu {
        position: fixed; top: 56px; right: 8px; left: auto;
        width: min(300px, calc(100vw - 16px)); max-height: calc(100dvh - 72px);
        overflow: auto; z-index: 200;
    }
    .topbar { z-index: 150; }
}

@media (max-width: 520px) {
    /* จอแคบยังพอมีที่ให้ชื่อ แต่อีเมลยาวเกินไปเก็บไว้ในเมนูแทน */
    .acctbtn .txt { display: block; max-width: 34vw; }
    .acctbtn .txt .em { display: none; }
    .acctbtn .txt .nm {
        display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    }
    .topbar .brand .t { display: none; }
}

/* ชื่อไฟล์แนบยาว ๆ ต้องไม่ทะลุขอบหน้าต่าง */
#attmodal .modal-txt { min-width: 0; flex: 1; }
#attmodal #attname {
    word-break: break-all; overflow-wrap: anywhere;
    font-size: 15px; line-height: 1.35; margin: 0;
}
#attmodal #attmime { font-size: 12px; color: var(--text-3); margin: 2px 0 0; }
@media (max-width: 820px) {
    #attmodal #attname { font-size: 13.5px; }
    #attmodal .modal-head { gap: 8px; }
}
