:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --border: #e3e6eb;
  --accent: #2f5bea;
  --accent-text: #ffffff;
  --danger: #c8322b;
  --ok: #1e8a4c;
  --warn: #b26b00;
  --sidebar: #141a26;
  --sidebar-text: #c9d1de;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
body { margin: 0; display: flex; min-height: 100vh; background: var(--bg); }

.sidebar {
  width: 210px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: flex-end; gap: 8px; padding: 14px 18px 12px; text-decoration: none; }
.brand img { display: block; width: 96px; height: 96px; }
.brand .tag { margin-bottom: 14px; color: #c9d1de; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; background: #2a3346; padding: 2px 6px; border-radius: 4px; margin-left: 4px; vertical-align: middle; }
.sidebar nav { display: flex; flex-direction: column; padding: 0 10px; gap: 2px; }
.sidebar nav a { color: var(--sidebar-text); text-decoration: none; padding: 9px 10px; border-radius: 6px; }
.sidebar nav a:hover { background: #1f2738; }
.sidebar nav a.active { background: #2a3346; color: #fff; }
.sidebar-foot { margin-top: auto; padding: 16px 18px; font-size: 12px; line-height: 1.6; }
.lang-switch { display: inline-flex; border: 1px solid #2a3346; border-radius: 6px; overflow: hidden; margin-bottom: 10px; }
.lang-switch button { border: 0; background: none; color: var(--sidebar-text); font: inherit; font-size: 12px; padding: 4px 12px; cursor: pointer; }
.lang-switch button:hover { background: #1f2738; }
.lang-switch button.active { background: #2a3346; color: #fff; font-weight: 600; }

main { flex: 1; min-width: 0; padding: 28px 32px 60px; max-width: 1280px; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.crumb { color: var(--muted); text-decoration: none; font-size: 13px; }
.crumb:hover { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px; }
.card.flush { padding: 0; overflow: hidden; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--panel);
  color: var(--text); padding: 7px 13px; border-radius: 6px; font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: #c5cad3; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

label { display: block; font-weight: 600; font-size: 12px; margin-bottom: 5px; }
.field { margin-bottom: 14px; }
.field .help { font-size: 12px; color: var(--muted); margin-top: 4px; }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=number], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit;
  background: var(--panel); color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
textarea { resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 16px; }
.inline-check { display: flex; align-items: center; gap: 8px; font-weight: 400; font-size: 14px; }

table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data th { font-size: 12px; color: var(--muted); font-weight: 600; background: color-mix(in srgb, var(--bg) 60%, var(--panel)); }
table.data tr:last-child td { border-bottom: 0; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: color-mix(in srgb, var(--accent) 5%, var(--panel)); }
table.data td.actions { text-align: right; white-space: nowrap; }
.empty { padding: 36px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: #eef0f4; color: #475467; }
.badge.sending, .badge.queued { background: #e5edff; color: #2f5bea; }
.badge.done { background: #e3f5ea; color: var(--ok); }
.badge.failed { background: #fde8e7; color: var(--danger); }
.badge.interrupted, .badge.cancelled { background: #fff2dc; color: var(--warn); }

.progress { height: 8px; background: #eef0f4; border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width .4s; }
.progress > div.fail { background: var(--danger); }
.stats { display: flex; gap: 28px; margin: 14px 0; }
.stats .num { font-size: 22px; font-weight: 700; }

.log { font-family: ui-monospace, Consolas, monospace; font-size: 12px; max-height: 360px; overflow: auto; }
.log div { padding: 4px 0; border-bottom: 1px dashed var(--border); }
.log .error { color: var(--danger); }

.banner { padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; font-size: 13px; }
.banner.warn { background: #fff5e0; color: #7a4a00; border: 1px solid #f3dca8; }
.banner.info { background: #eaf0ff; color: #1f3c9c; border: 1px solid #cad8ff; }

/* template editor */
.editor-layout { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 18px; align-items: start; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { font-family: ui-monospace, Consolas, monospace; font-size: 11px; padding: 3px 8px; border-radius: 4px; border: 1px dashed #b8c2d6; background: #f3f6fb; color: #34405a; cursor: pointer; }
.chip:hover { border-style: solid; border-color: var(--accent); }
.ck-editor__editable_inline { min-height: 420px; }
.ck.ck-editor { color: #222; }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 20, 30, .45); display: flex; align-items: flex-start; justify-content: center; padding: 60px 16px; z-index: 100; overflow: auto; }
.modal { background: var(--panel); border-radius: 10px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; flex: 1; }
.modal-close { border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.preview-frame { width: 100%; height: 60vh; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.preview-subject { font-weight: 600; margin-bottom: 10px; }

.kv-row { display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 8px; margin-bottom: 8px; }

#toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: #1c2330; color: #fff; padding: 10px 14px; border-radius: 6px; box-shadow: var(--shadow); max-width: 380px; font-size: 13px; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

.pager { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--border); }
.toolbar { display: flex; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); align-items: center; flex-wrap: wrap; }
.toolbar input[type=search] { max-width: 280px; }


/* compose page */
.compose-btn {
  display: inline-flex; align-items: center; gap: 10px; margin: 0 12px 16px; padding: 12px 20px 12px 16px;
  border-radius: 16px; background: #c2e7ff; color: #001d35; font-weight: 600; font-size: 14px; text-decoration: none;
  align-self: flex-start; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); transition: box-shadow .15s;
}
.compose-btn:hover { box-shadow: 0 2px 6px rgba(0, 0, 0, .35); }
.compose-btn.active { outline: 2px solid #7cc4f8; outline-offset: 2px; }

.compose-card { padding: 0; }
.compose-section { padding: 16px 18px 10px; border-bottom: 1px solid var(--border); }
.compose-products { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.product-btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--text); padding: 7px 16px; border-radius: 999px;
  font: inherit; cursor: pointer;
}
.product-btn:hover { border-color: var(--accent); }
.product-btn.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.compose-from { font-size: 12px; color: var(--muted); min-height: 16px; }
.compose-row { display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border); min-height: 44px; padding: 0 18px; }
.compose-row[hidden] { display: none; }
.compose-label { color: var(--muted); font-size: 13px; width: 52px; flex-shrink: 0; }
.addr { flex: 1; display: flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 6px 0; cursor: text; min-width: 0; }
.addr input { flex: 1; min-width: 160px; border: 0; outline: 0; padding: 4px 0; font: inherit; background: transparent; color: var(--text); }
.addr-chip { display: inline-flex; align-items: center; gap: 4px; background: #e8eef9; color: #1c2c4a; border-radius: 999px; padding: 2px 4px 2px 10px; font-size: 13px; max-width: 100%; }
.addr-chip.invalid { background: #fde8e7; color: var(--danger); outline: 1px dashed var(--danger); }
.addr-chip button { border: 0; background: none; cursor: pointer; color: inherit; opacity: .6; font-size: 14px; line-height: 1; padding: 0 4px; }
.addr-chip button:hover { opacity: 1; }
.cc-toggles button { border: 0; background: none; cursor: pointer; color: var(--muted); font: inherit; font-size: 13px; padding: 2px 4px; }
.cc-toggles button:hover { color: var(--text); text-decoration: underline; }
.cc-toggles button[hidden] { display: none; }
input.bare { flex: 1; border: 0; outline: 0; padding: 10px 0; font: inherit; background: transparent; color: var(--text); }
.compose-editor { padding: 14px 18px 4px; }
.compose-editor .ck-editor__editable_inline { min-height: 360px; }
.compose-foot { display: flex; align-items: center; gap: 12px; padding: 12px 18px 16px; flex-wrap: wrap; }
.compose-foot .btn.primary { border-radius: 18px; padding: 8px 24px; font-weight: 600; }
.compose-foot .spacer { flex: 1; }


/* logs */
.nav-group { margin: 14px 10px 4px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: #7d8799; }
.nowrap { white-space: nowrap; }
table.logs td { vertical-align: top; }
.log-details td { background: color-mix(in srgb, var(--bg) 70%, var(--panel)); }
.log-details pre { margin: 0; font-size: 12px; white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow: auto; }
.badge.level-warn { background: #fff2dc; color: var(--warn); }
.badge.level-error { background: #fde8e7; color: var(--danger); }
.badge.level-info { background: #e5edff; color: #2f5bea; }
.toolbar select { width: auto; }


/* attachments */
.attach-box { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
.attach-list { display: contents; }
.attach-chip { display: inline-flex; align-items: center; gap: 6px; max-width: 320px; padding: 4px 6px 4px 10px; border: 1px solid var(--border); border-radius: 6px; background: color-mix(in srgb, var(--bg) 60%, var(--panel)); font-size: 13px; }
.attach-chip a { color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip a:hover { text-decoration: underline; }
.attach-chip button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 15px; line-height: 1; padding: 0 4px; }
.attach-chip button:hover { color: var(--danger); }
.attach-chip.uploading { color: var(--muted); font-style: italic; padding-right: 10px; }
.attach-btn { cursor: pointer; }
.compose-attach { padding: 0 18px; }
.drop-hover { outline: 2px dashed var(--accent); outline-offset: -4px; }


/* signed-in user */
.user-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.user-name { color: #fff; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout-btn { margin-left: auto; border: 1px solid #2a3346; background: none; color: var(--sidebar-text); font: inherit; font-size: 12px; padding: 3px 10px; border-radius: 6px; cursor: pointer; }
.logout-btn:hover { background: #1f2738; color: #fff; }

/* login page */
body.login-page { background: var(--sidebar); align-items: center; justify-content: center; padding: 24px 16px; }
.login-wrap { width: 100%; max-width: 380px; display: flex; flex-direction: column; align-items: center; gap: 20px; }
.login-logo { width: 128px; height: 128px; }
.login-card { width: 100%; background: var(--panel); border-radius: 12px; padding: 28px 26px 24px; box-shadow: 0 20px 50px rgba(0, 0, 0, .35); }
.login-card h1 { font-size: 20px; }
.login-sub { margin: 4px 0 20px; font-size: 13px; }
.login-card input { padding: 10px 12px; }
.login-submit { width: 100%; justify-content: center; padding: 10px 14px; font-weight: 600; margin-top: 4px; }
.login-page .lang-switch { margin: 0; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131b; --panel: #171c26; --text: #e4e8ef; --muted: #93a0b4; --border: #2a3140;
    --accent: #5b82ff; --sidebar: #0b0e14;
  }
  .badge { background: #232a38; color: #c1c9d6; }
  .badge.sending, .badge.queued { background: #1d2a4d; color: #9fb6ff; }
  .badge.done { background: #16321f; color: #6fd495; }
  .badge.failed { background: #3a1a19; color: #ff8a84; }
  .badge.interrupted, .badge.cancelled, .badge.level-warn { background: #3a2a10; color: #f0b660; }
  .badge.level-error { background: #3a1a19; color: #ff8a84; }
  .badge.level-info { background: #1d2a4d; color: #9fb6ff; }
  .progress { background: #232a38; }
  .chip { background: #1d2331; border-color: #3a4459; color: #c1c9d6; }
  .banner.warn { background: #33270f; color: #f5d08f; border-color: #54401a; }
  .banner.info { background: #17213d; color: #a9bfff; border-color: #2a3a66; }
  .toast { background: #2a3346; }
  .compose-btn { background: #004a77; color: #c2e7ff; }
  .addr-chip { background: #243150; color: #cfdcff; }
  .addr-chip.invalid { background: #3a1a19; color: #ff8a84; }
}

@media (max-width: 900px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .brand img { width: 56px; height: 56px; }
  .brand .tag { margin-bottom: 6px; }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; padding-bottom: 10px; }
  .sidebar-foot { padding: 0 12px 10px; }
  #status-foot { display: none; }
  .compose-btn { margin: 0 12px 10px; }

  main { padding: 20px 16px; }
  .editor-layout, .grid-2, .grid-3 { grid-template-columns: 1fr; }
}
