:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #6b7785;
  --border: #dde3ea;
  --primary: #06c755; /* LINE green */
  --primary-dark: #05a647;
  --danger: #e53935;
  --warn: #fb8c00;
  --radius: 12px;
  --chat-bg: #8ca6cc;
  font-family: 'Sarabun', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }
h1 { font-size: 1.25rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .5rem; }
code { background: #eef1f5; padding: 1px 6px; border-radius: 4px; font-size: .9em; word-break: break-all; }
[hidden] { display: none !important; }

.topbar { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; justify-content: space-between; padding: 14px 20px; background: var(--card); border-bottom: 1px solid var(--border); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: .8rem; padding: 2px 10px; border-radius: 99px; background: #eef1f5; color: var(--muted); }
.chip.ok { background: #e3f7ea; color: #137a3a; }
.chip.bad { background: #fde7e7; color: #b42318; }

.tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--card); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab { border: 0; background: none; padding: 12px 14px; font: inherit; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; white-space: nowrap; }
.tab.active { color: var(--text); font-weight: 600; border-bottom-color: var(--primary); }
.count:not(:empty) { background: var(--warn); color: #fff; border-radius: 99px; padding: 0 7px; font-size: .75rem; margin-left: 4px; }

main { max-width: 1180px; margin: 0 auto; padding: 20px 16px 60px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.compose-grid { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 20px; align-items: start; }
@media (max-width: 900px) { .compose-grid { grid-template-columns: minmax(0, 1fr); } }

.field { margin-bottom: 16px; }
.field > label { display: block; font-weight: 600; margin-bottom: 6px; }
.req { color: var(--danger); }
.muted { color: var(--muted); font-weight: 400; font-size: .85rem; }
.hint { color: var(--muted); font-size: .82rem; margin: 6px 0 0; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.grow { flex: 1 1 200px; }

input, textarea, select { width: 100%; font: inherit; color: inherit; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
input:focus, textarea:focus { outline: 2px solid #b7e9c9; border-color: var(--primary); }
textarea { resize: vertical; min-height: 160px; border-top-left-radius: 0; border-top-right-radius: 0; }

.btn { font: inherit; border: 1px solid var(--border); background: #fff; padding: 9px 18px; border-radius: 8px; cursor: pointer; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.primary:disabled { opacity: .6; cursor: wait; }
.btn.ghost:hover { background: #f1f4f7; }
.btn.small { padding: 5px 12px; font-size: .88rem; }
.btn.danger { color: var(--danger); }
.actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

.group-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.group-chip { display: flex; align-items: center; gap: 6px; border: 1px solid var(--border); padding: 6px 12px; border-radius: 99px; cursor: pointer; user-select: none; }
.group-chip input { width: auto; margin: 0; }
.group-chip:has(input:checked) { border-color: var(--primary); background: #e9f9ef; }
.group-chip img { width: 22px; height: 22px; border-radius: 50%; }
.empty { color: var(--muted); font-size: .9rem; }

.swatches { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--border); cursor: pointer; padding: 0; }
.swatch.active { box-shadow: 0 0 0 2px var(--text); }
.swatches input[type=color] { width: 32px; height: 32px; padding: 0; border: 0; background: none; cursor: pointer; }

.toolbar { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; padding: 6px; border: 1px solid var(--border); border-bottom: 0; border-radius: 8px 8px 0 0; background: #f8fafc; }
.toolbar button { font: inherit; border: 1px solid transparent; background: none; padding: 4px 10px; border-radius: 6px; cursor: pointer; min-width: 34px; }
.toolbar button:hover { background: #e9edf2; border-color: var(--border); }
.toolbar .sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.pop-wrap { position: relative; }
.popover { position: absolute; top: calc(100% + 6px); left: 0; z-index: 20; background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.12); }
.popover.swatches { width: 220px; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; width: 300px; max-width: 80vw; }
.emoji-grid button { font-size: 1.25rem; padding: 4px; min-width: 0; }

.link-rows { display: grid; gap: 8px; margin-bottom: 8px; }
.link-row { display: grid; grid-template-columns: 1fr 2fr auto; gap: 8px; }
@media (max-width: 560px) { .link-row { grid-template-columns: 1fr auto; } .link-row input[name=url] { grid-column: 1 / -1; grid-row: 2; } }

.dropzone { display: block; border: 2px dashed var(--border); border-radius: 10px; padding: 18px; text-align: center; color: var(--muted); cursor: pointer; }
.dropzone.over { border-color: var(--primary); background: #f0fbf4; }
.dropzone input { display: none; }
.file-list { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 6px; }
.file-list li { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; }
.file-list img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.file-list .icon { width: 40px; text-align: center; font-size: 1.4rem; }
.file-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-list .size { color: var(--muted); font-size: .8rem; }
.file-list button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 1.1rem; }

.when { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; }
.radio { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.radio input { width: auto; }
.when input[type=datetime-local] { width: auto; }

/* ----- preview (จำลองหน้าแชท LINE) ----- */
.preview-col { position: sticky; top: 16px; }
.preview-label { font-weight: 600; margin-bottom: 8px; color: var(--muted); }
.chat { background: var(--chat-bg); border-radius: var(--radius); padding: 16px 12px; min-height: 300px; max-height: calc(100vh - 80px); overflow-y: auto; display: grid; gap: 10px; align-content: start; }
.bubble { background: #fff; border-radius: 16px; overflow: hidden; box-shadow: 0 1px 2px rgba(0,0,0,.1); }
.bubble-header { padding: 14px 16px; }
.bubble-badge { font-size: .75rem; font-weight: 700; opacity: .8; }
.bubble-title { font-size: 1.2rem; font-weight: 700; word-break: break-word; }
.bubble-body { padding: 14px 16px; color: #333; word-break: break-word; }
.bubble-body .line { min-height: 1.4em; }
.bubble-body a { color: inherit; }
.bubble-sender { border-top: 1px solid #eee; margin-top: 12px; padding-top: 8px; text-align: right; color: #888; font-size: .88rem; }
.bubble-footer { padding: 4px 12px 12px; display: grid; gap: 8px; }
.fbtn { display: block; text-align: center; border-radius: 8px; padding: 8px; font-size: .9rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fbtn.secondary { background: #eef0f3; color: #111; }
.chat-image { max-width: 70%; border-radius: 14px; display: block; }

/* ----- mission list ----- */
.filters { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.pill { font: inherit; border: 1px solid var(--border); background: #fff; padding: 4px 14px; border-radius: 99px; cursor: pointer; }
.pill.active { background: var(--text); color: #fff; border-color: var(--text); }
.mission-list, .group-list { display: grid; gap: 10px; margin-top: 14px; }
.mission { background: var(--card); border: 1px solid var(--border); border-left: 5px solid var(--theme, #ccc); border-radius: var(--radius); padding: 14px 16px; }
.mission-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between; }
.mission-title { font-weight: 700; font-size: 1.02rem; }
.mission-meta { color: var(--muted); font-size: .85rem; margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px 14px; }
.mission-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.deliveries { margin: 8px 0 0; padding: 0; list-style: none; font-size: .85rem; }
.deliveries .err { color: var(--danger); }
.status { font-size: .78rem; font-weight: 600; padding: 2px 10px; border-radius: 99px; white-space: nowrap; }
.status.scheduled { background: #fff3e0; color: #b45309; }
.status.sending { background: #e3f2fd; color: #1565c0; }
.status.sent { background: #e3f7ea; color: #137a3a; }
.status.partial, .status.failed { background: #fde7e7; color: #b42318; }
.status.cancelled { background: #eef1f5; color: var(--muted); }

.group-item { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; }
.group-item img, .group-item .avatar { width: 40px; height: 40px; border-radius: 50%; background: #e6ebf1; display: grid; place-items: center; }
.group-item .info { flex: 1 1 200px; min-width: 0; }
.group-item .gid { color: var(--muted); font-size: .78rem; word-break: break-all; }
.howto { margin: 0 0 16px; padding-left: 20px; }
.howto li { margin-bottom: 4px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1f2933; color: #fff; padding: 10px 18px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.2); z-index: 50; max-width: calc(100vw - 32px); }
.toast.error { background: var(--danger); }

/* ----- login / บัญชี ----- */
.topbar-right { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.user-menu { display: flex; align-items: center; gap: 8px; }
.me { font-size: .9rem; color: var(--muted); }
.card.narrow { max-width: 480px; margin-bottom: 20px; }
.role-select { width: auto; }
.btn.block { width: 100%; }
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; text-align: left; }
.login-card h1 { margin-bottom: 2px; }
.login-logo { font-size: 2.4rem; }
.login-error { background: #fde7e7; color: #b42318; padding: 8px 12px; border-radius: 8px; margin: 0 0 12px; font-size: .9rem; }

/* ----- ข้อความธรรมดา (format = text) ----- */
.editor.text-mode .flex-only { display: none; }
.text-bubble { background: #fff; border-radius: 16px; padding: 10px 14px; white-space: pre-wrap; word-break: break-word; max-width: 92%; box-shadow: 0 1px 2px rgba(0,0,0,.1); font-size: .95rem; }

/* ----- ปฏิทิน ----- */
.cal-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.cal-nav { display: flex; align-items: center; gap: 6px; }
.cal-nav h2 { margin: 0 6px; min-width: 150px; text-align: center; }
.cal-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.cal-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
@media (max-width: 900px) { .cal-layout { grid-template-columns: minmax(0, 1fr); } }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); background: var(--border); gap: 1px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.cal-head { background: #f8fafc; text-align: center; font-weight: 600; font-size: .85rem; padding: 6px 0; }
.cal-head.weekend { color: var(--danger); }
.cal-cell { background: var(--card); min-height: 96px; padding: 4px 5px; border: 0; font: inherit; text-align: left; cursor: pointer; display: flex; flex-direction: column; gap: 2px; position: relative; min-width: 0; }
.cal-cell.empty { background: #fafbfc; cursor: default; }
.cal-cell:not(.empty):hover { background: #f3f8ff; }
.cal-cell.selected { outline: 2px solid var(--primary); outline-offset: -2px; z-index: 1; }
.cal-num { font-size: .85rem; font-weight: 600; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; }
.cal-cell.today .cal-num { background: var(--primary); color: #fff; }
.cal-events { display: grid; gap: 2px; min-width: 0; }
.cal-ev { font-size: .72rem; line-height: 1.3; background: #e3f2fd; color: #0d47a1; border-radius: 4px; padding: 1px 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-ev.postponed { background: #fff3e0; color: #b45309; }
.cal-ev.cancelled { background: #eef1f5; color: var(--muted); text-decoration: line-through; }
.cal-more { font-size: .7rem; color: var(--muted); }
.cal-count { display: none; }
@media (max-width: 600px) {
  .cal-cell { min-height: 52px; align-items: center; }
  .cal-events { display: none; }
  .cal-count { display: block; font-size: .7rem; background: var(--primary); color: #fff; border-radius: 99px; padding: 0 6px; }
  .cal-nav h2 { min-width: 0; font-size: 1rem; }
}
.cal-day { position: sticky; top: 16px; }
.cal-day-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.cal-day-head h3 { margin: 0; font-size: 1rem; }
.day-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.day-item { display: grid; grid-template-columns: 78px 1fr; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-left: 4px solid var(--primary); border-radius: 8px; cursor: pointer; }
.day-item:hover { background: #f8fafc; }
.day-item.postponed { border-left-color: var(--warn); }
.day-item.cancelled { border-left-color: #b0b8c1; opacity: .7; }
.day-item.cancelled .day-title { text-decoration: line-through; }
.day-time { font-weight: 600; font-size: .88rem; color: #137a3a; }
.day-title { white-space: pre-wrap; word-break: break-word; }
.day-meta { color: var(--muted); font-size: .85rem; }

/* ----- dialog ----- */
.dialog { border: 0; border-radius: var(--radius); padding: 20px; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 50px rgba(0,0,0,.25); color: var(--text); }
.dialog.wide { width: min(760px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(15, 23, 42, .45); }
.dialog h2 { margin-bottom: 14px; }
.dialog .actions .grow { flex: 1; }
.dialog .chat { max-height: 360px; }
.dialog textarea#import-text { min-height: 180px; border-radius: 8px; }
.import-table { max-height: 300px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; margin: 8px 0; }
.import-row { display: grid; grid-template-columns: 90px 100px 1fr; gap: 8px; padding: 6px 10px; border-bottom: 1px solid #eef1f5; font-size: .88rem; }
.import-row:last-child { border-bottom: 0; }
.import-date { color: var(--muted); }
.import-time { font-weight: 600; color: #137a3a; }
.import-title { white-space: pre-wrap; word-break: break-word; }
@media (max-width: 560px) { .import-row { grid-template-columns: 1fr; gap: 0; } }
.replace-opt { margin-top: 8px; align-items: flex-start; }

/* ----- ส่งอัตโนมัติ ----- */
.digest-time { font-size: 1.5rem; font-weight: 700; color: var(--primary-dark); min-width: 84px; }
.digest-time small { font-size: .8rem; margin-left: 2px; color: var(--muted); }
.digest-item.disabled { opacity: .6; }
#digest-list { margin-top: 14px; }

/* ----- dialog แบบเต็ม (ภารกิจ / ประกาศ) ----- */
.dialog.full { width: min(1100px, calc(100vw - 24px)); padding: 0; }
.dialog.full form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 16px 20px 8px; }
.dialog-head h2 { margin: 0; }
.dialog-grid { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 20px; padding: 8px 20px; overflow-y: auto; }
.dialog-grid .preview-col { position: sticky; top: 0; align-self: start; }
.dialog-grid .chat { max-height: calc(100vh - 220px); }
@media (max-width: 860px) {
  .dialog-grid { grid-template-columns: minmax(0, 1fr); }
  .dialog-grid .preview-col { position: static; }
}
.dialog-foot { margin: 0; padding: 12px 20px; border-top: 1px solid var(--border); background: #fafbfc; }
.send-box { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px 2px; background: #f8fbff; margin-bottom: 12px; }
.sends { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: .9rem; }
.sends li { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.day-badges { font-size: .8rem; }

/* ----- การ์ดตารางภารกิจ (พรีวิว) ----- */
.sched-body { padding: 0 14px; }
.sched-row { display: grid; grid-template-columns: 60px 1fr; gap: 10px; padding: 10px 0; border-top: 1px solid #eee; }
.sched-row:first-child { border-top: 0; }
.sched-time { text-align: center; display: flex; flex-direction: column; align-items: center; color: #1E88E5; }
.sched-icon { font-size: 1.6rem; line-height: 1.2; }
.sched-time small { color: #8a8f98; font-size: .7rem; }
.sched-time .label { font-size: .78rem; }
.sched-detail { font-size: .9rem; display: flex; flex-direction: column; justify-content: center; gap: 2px; word-break: break-word; }
.sched-detail small { color: #8a8f98; font-size: .78rem; }
.sched-detail .warn { color: #e65100; font-weight: 700; }
.sched-foot { text-align: right; color: #8a8f98; font-size: .72rem; padding: 6px 14px 10px; }
.sched-detail .file { color: #1E88E5; text-decoration: underline; }

/* ไฟล์แนบในรายการของวัน */
.day-files { display: flex; flex-direction: column; gap: 2px; margin-top: 4px; }
.day-files > a { color: #1E88E5; font-size: .85rem; text-decoration: underline; word-break: break-word; }
.day-thumbs { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 2px; }
.day-thumbs img { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }

/* ----- ส่งอัตโนมัติ: ประเภทภารกิจที่เหลือทั้งเดือน ----- */
#digest-form.scope-month .day-only { display: none; }
.rest-day { display: flex; justify-content: space-between; align-items: center; gap: 8px; border-radius: 8px; padding: 6px 10px; margin: 12px 0 2px; font-size: .88rem; }
.rest-day:first-child { margin-top: 10px; }
.rest-day span { font-size: .78rem; white-space: nowrap; }
.rest-day + .sched-row { border-top: 0; }
.sched-cal { margin: 4px 14px 0; background: #eef0f3; border-radius: 8px; text-align: center; padding: 7px; font-weight: 600; font-size: .85rem; }
#digest-form:not(.scope-month) .month-only { display: none; }
.cal-preview { width: 100%; border-radius: 12px; display: block; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
