:root {
  --deep: #102A33; --panel: #17383F; --line: #2A5058;
  --text: #E8F1EF; --muted: #93B3AE; --accent: #F2B544; --danger: #E0604E; --online: #7FD69B;
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", "Tahoma", system-ui, sans-serif;
  color: var(--text); background: var(--deep); box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }
html, body { height: 100%; margin: 0; }
body { background: var(--deep); }
button, input { font: inherit; }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.link { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0; font-size: .8rem; }
.link:hover { color: var(--text); text-decoration: underline; }
.link.danger { color: #C9847A; }
.link.danger:hover { color: var(--danger); }

/* ---------- หน้าเข้าสู่ระบบ: ซ้ายเป็นรายงาน ขวาเป็นฟอร์ม ---------- */
body.portal {
  --navy: #1B3A6B; --navy-deep: #14294C; --gold: #F5C842; --paper: #F4F6FA;
  min-height: 100%; margin: 0; display: grid; grid-template-columns: 1.35fr 1fr;
  background: var(--paper); color: #24303F;
}
.portal-left { display: grid; place-items: center; padding: 28px;
  background: var(--navy-deep); }
.portal-left img { max-width: 100%; max-height: calc(100vh - 56px);
  border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.35); }

.noimg { max-width: 360px; padding: 20px 22px; border-radius: 10px; line-height: 1.8;
  background: rgba(255,255,255,.08); border: 1px dashed rgba(255,255,255,.35); color: #D7E2F2; }
.noimg strong { display: block; color: #F5C842; margin-bottom: 6px; }
.noimg code { background: rgba(0,0,0,.25); padding: 2px 6px; border-radius: 4px; }
.cover .noimg { background: #E8EDF5; border-color: #B6C4D8; color: #46566B; }
.cover .noimg strong { color: #1B3A6B; }
.cover .noimg code { background: #D3DCE9; }

.portal-right { display: grid; place-items: center; padding: 32px 28px; }
.signin { width: 100%; max-width: 340px; }
.signin h1 { font-size: 1.85rem; font-weight: 600; color: var(--navy);
  margin: 0 0 6px; line-height: 1.3; }
.signin h1::after { content: ""; display: block; width: 52px; height: 4px;
  background: var(--gold); border-radius: 2px; margin-top: 12px; }
.signin .sub { color: #5B6B80; margin: 14px 0 26px; font-size: .95rem; }
.signin label { display: block; font-size: .88rem; color: #46566B; margin-bottom: 6px; }
.signin input { width: 100%; padding: 12px 14px; margin-bottom: 18px;
  background: #fff; color: #24303F; border: 1px solid #C9D3E0; border-radius: 8px; }
.signin input:focus { border-color: var(--navy); outline: 2px solid rgba(27,58,107,.25); outline-offset: 0; }
.signin .btn { width: 100%; min-height: 46px; padding: 12px; border: 0; border-radius: 8px;
  background: var(--navy); color: #fff; font-weight: 600; cursor: pointer; }
.signin .btn:hover { background: var(--navy-deep); }
.signin .error { color: #C0392B; min-height: 1.4em; margin-top: 12px; font-size: .9rem; line-height: 1.5; }
.signin .notice { color: #9A6B00; min-height: 1.4em; margin-top: 12px; font-size: .9rem; line-height: 1.5; }
.signin .foot { margin-top: 28px; padding-top: 16px; border-top: 1px solid #DCE3ED;
  color: #7A8799; font-size: .78rem; line-height: 1.7; }

@media (max-width: 900px) {
  body.portal { grid-template-columns: 1fr; }
  .portal-left { padding: 16px; }
  .portal-left img { max-height: 42vh; }
  .portal-right { padding: 28px 20px 40px; }
}

/* ---------- โครงหน้า: แชทเป็นหลัก วิดีโออยู่แถบข้าง ---------- */
.app { height: 100%; display: grid; grid-template-columns: 240px 1fr; }

.callpanel { display: flex; flex-direction: column; gap: 12px; padding: 12px;
  background: var(--panel); border-right: 1px solid var(--line); min-width: 0; }
.callbar { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.videos { display: flex; flex-direction: column; gap: 8px; }
.vbox { position: relative; aspect-ratio: 4/3; border-radius: 10px; overflow: hidden;
  background: #0A1D23; border: 1px solid var(--line); }
.vbox.small { aspect-ratio: 16/10; }
.vbox video { width: 100%; height: 100%; object-fit: cover; display: block; }
.vbox.small video { transform: scaleX(-1); }
.vlabel { position: absolute; left: 6px; bottom: 6px; font-size: .7rem;
  background: rgba(10,29,35,.75); padding: 2px 7px; border-radius: 6px; }
.vstatus { position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: .85rem; text-align: center; padding: 8px; }

.callinfo { display: flex; align-items: flex-start; gap: 8px; font-size: .85rem;
  color: var(--muted); line-height: 1.45; min-width: 0; }
.callinfo .dot { margin-top: 5px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); flex: none; }
.dot.on { background: var(--online); }

.controls { display: flex; gap: 8px; justify-content: center; }
.ctrl { min-width: 44px; height: 44px; border-radius: 22px; border: 0; cursor: pointer;
  background: var(--deep); color: var(--text); display: flex; align-items: center;
  justify-content: center; gap: 6px; padding: 0 12px; font-size: .9rem; }
.ctrl svg { width: 19px; height: 19px; flex: none; }
.ctrl.off { background: var(--text); color: var(--deep); }
.ctrl.call { background: var(--accent); color: #2B1E05; font-weight: 600; }
.ctrl.call:disabled { opacity: .4; cursor: not-allowed; }
.ctrl.hangup { background: var(--danger); color: #fff; font-weight: 600; }
.ctrl.hangup:hover { background: #C94B3A; }

/* ---------- ปุ่มซ่อนหน้าจอและออกจากระบบ ---------- */
.side { display: flex; flex-direction: column; gap: 8px; }
.hidebtn, .logout { display: flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 44px; padding: 10px 14px; border-radius: 10px; cursor: pointer;
  font-size: .92rem; text-decoration: none; border: 0; }
.hidebtn { background: var(--accent); color: #2B1E05; font-weight: 600; }
.hidebtn:hover { background: #E0A62F; }
.logout { background: var(--deep); border: 1px solid var(--line); color: var(--text); }
.logout:hover { background: var(--danger); border-color: var(--danger); color: #fff; }
.hidebtn svg, .logout svg { width: 17px; height: 17px; flex: none; }
.autohide { display: flex; align-items: center; gap: 7px; font-size: .78rem;
  color: var(--muted); cursor: pointer; }
.autohide input { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.who { margin-top: auto; font-size: .8rem; color: var(--muted); }

/* ---------- สายเรียกเข้า ---------- */
.incoming { position: fixed; inset: 0; z-index: 9000; display: grid; place-items: center;
  background: rgba(10,29,35,.78); padding: 24px; }
.incoming-card { width: 100%; max-width: 320px; text-align: center; padding: 28px 24px;
  border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
  box-shadow: 0 18px 50px rgba(0,0,0,.45); }
.incoming-ring { width: 64px; height: 64px; margin: 0 auto 16px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: #2B1E05;
  animation: ringpulse 1.4s ease-in-out infinite; }
.incoming-ring svg { width: 30px; height: 30px; }
@keyframes ringpulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(242,181,68,.45); }
  50%      { box-shadow: 0 0 0 14px rgba(242,181,68,0); }
}
@media (prefers-reduced-motion: reduce) { .incoming-ring { animation: none; } }
.incoming-title { font-size: 1.3rem; font-weight: 600; }
.incoming-sub { color: var(--muted); font-size: .9rem; margin-top: 6px; }
.incoming-actions { display: flex; gap: 12px; margin-top: 24px; }
.incoming-actions .ctrl { flex: 1; height: 50px; border-radius: 12px; font-size: .95rem; }

/* ---------- หน้าจอบัง ---------- */
.cover { position: fixed; inset: 0; z-index: 9999; background: #F4F6FA;
  display: flex; flex-direction: column; color: #24303F; }
body.covered { overflow: hidden; }
.cover-bar { display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; background: #1B3A6B; color: #fff;
  padding: 14px 22px; padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
.cover-title { font-size: 1.05rem; font-weight: 600; }
.cover-title::before { content: ""; display: inline-block; width: 8px; height: 18px;
  background: #F5C842; border-radius: 2px; margin-right: 10px; vertical-align: -3px; }
.cover-date { font-size: .85rem; color: #C3D1E8; }
.cover-body { flex: 1; display: grid; place-items: center; padding: 20px; overflow: auto; }
.cover-body img { max-width: 100%; max-height: 100%; border-radius: 8px;
  box-shadow: 0 6px 28px rgba(20,41,76,.18); }
.cover-foot { padding: 12px 22px; background: #E8EDF5; border-top: 1px solid #D3DCE9;
  color: #5B6B80; font-size: .8rem; }

/* ---------- แชท ---------- */
.chat { display: flex; flex-direction: column; min-height: 0; }
.chat-head { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.chat-head h2 { font-size: 1rem; font-weight: 500; margin: 0; }

.idlewarn { padding: 10px 16px; background: #6B4E12; color: #FFF3D6;
  font-size: .88rem; line-height: 1.5; border-bottom: 1px solid #8A6516; }

#messages { flex: 1; overflow-y: auto; padding: 16px; display: flex;
  flex-direction: column; gap: 12px; }
.row { display: flex; }
.row.mine { justify-content: flex-end; }
.msg { max-width: min(70%, 520px); padding: 8px 12px; border-radius: 12px;
  line-height: 1.5; overflow-wrap: anywhere; background: var(--panel); }
.row.mine .msg { background: var(--accent); color: #2B1E05; border-bottom-right-radius: 4px; }
.row.theirs .msg { border-bottom-left-radius: 4px; }
.meta { font-size: .75rem; color: var(--muted); display: block; margin-bottom: 2px; }
.row.mine .meta { color: #6B4E12; }

.quote { border-left: 3px solid currentColor; padding: 4px 8px; margin: 4px 0 6px;
  font-size: .85rem; opacity: .75; cursor: pointer; border-radius: 0 6px 6px 0;
  background: rgba(0,0,0,.12); }
.quote-name { display: block; font-weight: 600; }

.attach-img { display: block; margin-top: 8px; max-width: 100%; max-height: 260px;
  border-radius: 8px; background: var(--deep); }
.attach-file { display: inline-block; margin-top: 8px; padding: 8px 12px; border-radius: 8px;
  background: rgba(0,0,0,.18); color: inherit; text-decoration: none; font-size: .85rem; }
.attach-file:hover { text-decoration: underline; }

.acts { display: flex; gap: 12px; margin-top: 6px; }
.row.mine .acts .link { color: #6B4E12; }
.row.mine .acts .link:hover { color: #2B1E05; }
.sys { align-self: center; font-size: .8rem; color: var(--muted); text-align: center; }

/* แถบตอบกลับและไฟล์ที่เลือก */
.replybar, .filebar { display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 8px 16px; border-top: 1px solid var(--line);
  background: var(--panel); font-size: .85rem; }
.replybar { border-left: 3px solid var(--accent); }
.replybar-name { display: block; color: var(--accent); font-size: .78rem; }
.replybar-text { color: var(--muted); }
.filebar span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* แผงอิโมจิ */
.emojibar { border-top: 1px solid var(--line); background: var(--panel); }
.emoji-tabs { display: flex; gap: 4px; padding: 8px 12px 0; flex-wrap: wrap; }
.emoji-tab { background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 5px 10px; border-radius: 7px; font-size: .85rem; }
.emoji-tab.on { background: var(--deep); color: var(--text); }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  gap: 2px; padding: 8px 12px 12px; max-height: 190px; overflow-y: auto; }
.emoji { background: none; border: 0; cursor: pointer; font-size: 1.35rem;
  line-height: 1; padding: 6px 0; border-radius: 7px; }
.emoji:hover { background: var(--deep); }

.composer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.composer input[type=text] { flex: 1; min-width: 0; margin: 0; padding: 12px 14px;
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; }
.composer button[type=submit] { padding: 0 18px; border: 0; border-radius: 10px;
  background: var(--accent); color: #2B1E05; font-weight: 600; cursor: pointer; }
.composer button:disabled { opacity: .6; cursor: wait; }
.attach { width: 44px; flex: none; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); color: var(--muted); cursor: pointer;
  display: grid; place-items: center; font-size: 1.15rem; }
.attach svg { width: 20px; height: 20px; }
.attach:hover { color: var(--text); }

/* ---------- มือถือ: แชทเต็มจอ วิดีโอโผล่เฉพาะตอนโทร ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }

  .callpanel { gap: 8px; padding: 8px 10px;
    border-right: 0; border-bottom: 1px solid var(--line); }

  /* ไม่ได้โทรอยู่ ไม่ต้องโชว์กรอบวิดีโอดำๆ ให้เปลืองที่ */
  .videos { display: none; }
  body.calling .videos { display: block; position: relative; }
  body.calling .vbox { aspect-ratio: 16/10; max-height: 38vh; }
  body.calling .vbox.small { position: absolute; right: 8px; bottom: 8px; z-index: 2;
    width: 86px; aspect-ratio: 3/4; border-color: rgba(255,255,255,.35); }

  .callbar { flex-direction: row; align-items: center; gap: 8px; }
  .callinfo { flex: 1; min-width: 0; font-size: .78rem; align-items: center; }
  .callinfo span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .callinfo .dot { margin: 0; }

  .controls { gap: 6px; }
  .ctrl { padding: 0; }                            /* มือถือใช้ไอคอนล้วน */
  .side { flex-direction: row; gap: 6px; }
  .hidebtn, .logout { min-width: 44px; padding: 0; }
  .wide-only { display: none; }
  .autohide { font-size: .72rem; }
  .who { display: none; }

  .chat { border-left: 0; }
  .chat-head { padding: 10px 14px; }
  #messages { padding: 12px; gap: 10px; }
  .msg { max-width: 88%; }
  .composer { padding: 10px 12px; }
  .composer input[type=text] { font-size: 16px; }  /* กัน iOS ซูมเองตอนแตะช่องพิมพ์ */
  .composer button[type=submit] { min-height: 44px; }
  .emoji-grid { max-height: 150px; }
}
