/* =====================================================================
 * mtl.css - หน้าตากลางของทุกเครื่องมือ (Portal / ETL / หน้าอัปโหลด)
 * ---------------------------------------------------------------------
 * โหลดผ่าน pageStart() ใน _layout.php เสมอ
 * CSS เฉพาะหน้าให้แยกไว้ที่ assets/<ชื่อหน้า>.css (โหลดหลังไฟล์นี้)
 *
 * ระบบดีไซน์
 *   - สีพื้นเทาเย็นชุดเดียว + accent ชมพู (--primary) + แดงเฉพาะงานที่ลบ/เขียนทับ
 *   - มุมโค้ง 2 ระดับ: 6px (ปุ่ม/ช่องกรอก/badge) และ 10px (card/modal)
 *   - เงาแทบไม่มี ใช้เส้นขอบแบ่งชั้นแทน
 *   - ฟอนต์ IBM Plex Sans Thai / IBM Plex Mono (fallback เป็นฟอนต์ระบบเมื่อไม่มีเน็ต)
 * ===================================================================== */

:root {
    --bg: #f6f7f9; --card: #ffffff; --line: #e1e4e8; --line-soft: #eceef1;
    --text: #16191d; --muted: #5d6570; --muted-2: #8a919b;
    --primary: #d6007e; --primary-dark: #b0006a; --primary-soft: #fdeaf4;
    --danger: #c42b2b; --danger-dark: #a52222;
    --ok: #1d6f42; --ok-bg: #e8f3ec; --warn: #8a5a00; --warn-bg: #fbf3e0;
    --bad: #b42323; --bad-bg: #fbeaea; --info: #b0006a; --info-bg: #fdeaf4;
    --gray-bg: #f0f2f4; --muted-bg: #f0f2f4;
    --shadow: 0 1px 2px rgba(22, 25, 29, .04);
    --radius: 10px; --radius-sm: 6px;
    --font: "IBM Plex Sans Thai", "IBM Plex Sans", "Segoe UI", Tahoma, system-ui, sans-serif;
    --mono: "IBM Plex Mono", ui-monospace, "Cascadia Code", Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html { scroll-padding-top: 72px; } /* เผื่อ appbar ที่ตรึงด้านบน */
body {
    background: var(--bg);
    color: var(--text);
    font: 14px/1.6 var(--font);
    -webkit-font-smoothing: antialiased;
}
code { font-family: var(--mono); font-size: .88em; background: var(--gray-bg); padding: 1px 5px; border-radius: 4px; color: #3a414a; }
[hidden] { display: none !important; }
a { color: var(--primary); }
.ti { font-size: 1.15em; line-height: 1; vertical-align: -.12em; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.wrap { max-width: 1320px; margin: 0 auto; padding: 32px 24px 80px; }
h1 { color: var(--primary); font-size: 22px; font-weight: 600; margin: 0 0 4px; letter-spacing: -.2px; line-height: 1.3; }
h2 { font-size: 15px; font-weight: 600; margin: 0; }
h3 { font-size: 16px; font-weight: 600; margin: 0 0 12px; }
.sub { margin: 0; color: var(--muted); font-size: 13.5px; max-width: 75ch; }
.muted { color: var(--muted); }

/* ---------- app bar (ทุกหน้า) ---------- */
.appbar { background: rgba(255, 255, 255, .94); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 90; backdrop-filter: saturate(160%) blur(8px); }
.appbar-in { max-width: 1320px; margin: 0 auto; padding: 0 24px; height: 52px; display: flex; align-items: center; gap: 10px; font-size: 13.5px; }
.appbar a { color: inherit; text-decoration: none; }
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--text) !important; font-weight: 600; white-space: nowrap; letter-spacing: -.1px; }
.brand b { color: var(--muted); font-weight: 400; }
.brand-mark { width: 24px; height: 24px; border-radius: var(--radius-sm); background: var(--primary); color: #fff; display: inline-flex; align-items: center; justify-content: center; font: 600 12px/1 var(--mono); }
.crumb-sep { color: var(--line); font-size: 18px; font-weight: 300; }
.crumb { color: var(--text); display: inline-flex; align-items: center; gap: 7px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.crumb .ti { color: var(--muted); }
.appbar-back { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; color: var(--muted) !important; border-radius: var(--radius-sm); white-space: nowrap; }
.appbar-back:hover { background: var(--gray-bg); color: var(--text) !important; }

/* ---------- header ของหน้า ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 24px; flex-wrap: wrap; margin-bottom: 24px; }
.chip {
    display: inline-flex; align-items: center; gap: 8px; background: var(--card);
    border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 7px 12px;
    font-size: 13px; color: var(--muted); white-space: nowrap;
}
.chip b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }

/* ---------- banners ---------- */
.banner { border-radius: var(--radius-sm); padding: 12px 16px 12px 14px; margin-bottom: 16px; border: 1px solid var(--line); border-left-width: 3px; background: var(--card); font-size: 13.5px; }
.banner.err  { border-color: #ebc4c4; border-left-color: var(--bad); background: var(--bad-bg); color: #7a1c1c; }
.banner.ok   { border-color: #c7e0cf; border-left-color: var(--ok); background: var(--ok-bg); color: #164f31; }
.banner.warn { border-color: #ecd9ad; border-left-color: #c48a12; background: var(--warn-bg); color: #6b4600; }
.banner strong { display: block; margin-bottom: 2px; font-weight: 600; }
.banner .meta { color: inherit; opacity: .75; font-size: 12.5px; }
.banner ul { margin: 8px 0 0; padding-left: 20px; }

/* ---------- card ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 20px; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; padding: 14px 20px; border-bottom: 1px solid var(--line-soft); }
.card-head h2 { font-size: 14.5px; margin: 0; display: flex; align-items: center; gap: 8px; }
.card-head .hint { color: var(--muted-2); font-size: 12.5px; font-weight: 400; }
.card-body { padding: 20px; }

/* ---------- buttons ---------- */
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn {
    appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
    font: inherit; font-weight: 500; font-size: 13.5px; line-height: 1.2; padding: 8px 14px; border-radius: var(--radius-sm);
    cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
    transition: background .12s, border-color .12s, color .12s, transform .05s;
}
.btn:hover { background: var(--gray-bg); border-color: #cfd4da; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { background: var(--danger-dark); border-color: var(--danger-dark); }
.btn:disabled, .btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn.is-busy { opacity: .55; pointer-events: none; }
.btn-lg { padding: 10px 18px; font-size: 14px; }

/* ---------- badges ---------- */
.badge { display: inline-block; font-size: 11.5px; font-weight: 500; padding: 1px 8px; border-radius: 4px; white-space: nowrap; border: 1px solid transparent; }
.badge.ok, .badge.green, .badge.insert { background: var(--ok-bg); color: var(--ok); border-color: #cfe5d6; }
.badge.warn, .badge.amber, .badge.update { background: var(--warn-bg); color: var(--warn); border-color: #efdcb0; }
.badge.bad, .badge.red { background: var(--bad-bg); color: var(--bad); border-color: #efcaca; }
.badge.info, .badge.blue { background: var(--info-bg); color: var(--info); border-color: #f6c6df; }
.badge.muted, .badge.gray { background: var(--gray-bg); color: var(--muted); border-color: var(--line-soft); }

/* ---------- stats: แถวตัวเลขคั่นเส้น ไม่แยกกล่อง ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--card); }
.stat { padding: 14px 18px; border-left: 1px solid var(--line-soft); margin-left: -1px; }
.card-head .stats { grid-auto-flow: column; grid-template-columns: none; }
.stat .k { font-size: 12.5px; color: var(--muted); margin-bottom: 2px; }
.stat .v { font-size: 22px; font-weight: 600; letter-spacing: -.3px; font-variant-numeric: tabular-nums; }
.stat.new .v { color: var(--primary); }
.stat.gone .v { color: var(--bad); }
.stat.changed .v, .stat.update .v { color: var(--warn); }
.stat.ok .v, .stat.insert .v { color: var(--ok); }

/* ---------- data table ---------- */
.table-wrap { max-height: 70vh; overflow: auto; border-top: 1px solid var(--line-soft); }
table.grid { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
table.grid thead th {
    position: sticky; top: 0; z-index: 3; background: #fafbfc; color: var(--muted);
    font-weight: 500; text-align: left; padding: 9px 14px; border-bottom: 1px solid var(--line);
    white-space: nowrap; font-size: 12.5px;
}
table.grid tbody td { padding: 8px 14px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
table.grid tbody tr:hover td { background: #f7f9fc; }
table.grid tbody tr:last-child td { border-bottom: 0; }
table.grid .num { text-align: right; font-variant-numeric: tabular-nums; }
.tfoot-note { padding: 10px 20px; font-size: 12.5px; color: var(--muted); background: #fafbfc; border-top: 1px solid var(--line-soft); }

/* ---------- update bar ---------- */
.update-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.update-note { color: var(--muted); font-size: 13.5px; max-width: 720px; }
.update-note b, .update-note strong { color: var(--text); }

/* ---------- upload drop zone ---------- */
#drop {
    border: 1px dashed #c3c9d1; border-radius: var(--radius); padding: 32px 20px; text-align: center;
    cursor: pointer; background: #fbfcfd; transition: border-color .12s, background .12s;
}
#drop:hover, #drop.over, #drop:focus-visible { border-color: var(--primary); background: var(--primary-soft); outline: none; }
#drop .icon { font-size: 28px; line-height: 1; margin-bottom: 10px; color: var(--muted-2); }
#drop:hover .icon, #drop.over .icon { color: var(--primary); }
#drop .main { font-size: 14.5px; font-weight: 500; margin-bottom: 4px; }
#drop .main b { color: var(--primary); font-weight: 600; }
#drop .sub2 { color: var(--muted); font-size: 12.5px; }
#file-input { display: none; }
#file-name {
    display: none; margin-top: 14px; align-items: center; justify-content: center; gap: 8px;
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 6px 12px;
    font-size: 13px; color: var(--text);
}
#file-name.show { display: inline-flex; }
#file-name .size { color: var(--muted-2); }

/* ---------- modal ยืนยัน (ซ่อน/แสดงด้วย attribute hidden) ---------- */
.modal { position: fixed; inset: 0; background: rgba(22, 25, 29, .42); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal .box { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); max-width: 460px; width: 100%; padding: 22px 24px; box-shadow: 0 16px 48px rgba(22, 25, 29, .2); }
.modal h3 { margin: 0 0 10px; font-size: 16px; }
.modal p { margin: 0 0 6px; color: var(--muted); font-size: 13.5px; }
.modal p strong { color: var(--text); font-weight: 600; }
.modal .warn-text { color: var(--bad); font-weight: 500; }
.modal .warnbox { background: var(--warn-bg); border: 1px solid #ecd9ad; color: #6b4600; border-radius: var(--radius-sm); padding: 10px 14px; font-size: 13px; margin: 14px 0 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ---------- loading ---------- */
.overlay { position: fixed; inset: 0; background: rgba(246, 247, 249, .88); z-index: 110; display: none; align-items: center; justify-content: center; }
.overlay.show { display: flex; }
.overlay .box { text-align: center; }
.overlay .t { font-weight: 600; color: var(--text); }
.overlay .s { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.overlay .spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--primary); margin: 0 auto 14px; animation: mtl-spin .8s linear infinite; }
.btn .spinner, .run-state .spinner {
    display: none; vertical-align: -2px; width: 13px; height: 13px;
    border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
    animation: mtl-spin .7s linear infinite;
}
.btn.is-loading .spinner, .run-state .spinner { display: inline-block; }
.btn.is-loading .ti { display: none; }
.run-state .spinner { margin-right: 6px; }
.btn.is-loading { cursor: progress; opacity: .9; }
@keyframes mtl-spin { to { transform: rotate(360deg); } }

/* ---------- ETL log ---------- */
.log { background: #fafbfc; color: #2a3038; font: 12.5px/1.75 var(--mono); padding: 14px 20px; max-height: 70vh; overflow: auto; counter-reset: ln; }
.log .ln { white-space: pre-wrap; word-break: break-word; padding-left: 44px; position: relative; }
.log .ln::before { counter-increment: ln; content: counter(ln); position: absolute; left: 0; width: 30px; text-align: right; color: #b3b9c1; }
.log .ln.ok { color: var(--ok); }
.log .ln.err { color: var(--bad); font-weight: 500; }
.log .ln.warn { color: var(--warn); }
.result { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.result .banner { margin: 0; flex: 1 1 360px; }

/* ---------- portal ---------- */
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 36px 0 12px; }
.section-title h2 { font-size: 15px; color: var(--primary); }
.section-title span { color: var(--muted); font-size: 13px; }

/* เครื่องมืออัปโหลด: แถวใหญ่ คลิกได้ทั้งแถว */
.tool-list { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.tool {
    display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; align-items: center; gap: 16px;
    padding: 16px 20px; color: inherit; text-decoration: none; border-top: 1px solid var(--line-soft);
    transition: background .12s;
}
.tool:first-child { border-top: 0; }
.tool:hover { background: #f7f9fc; }
.tool-icon { width: 40px; height: 40px; border-radius: var(--radius-sm); border: 1px solid var(--line); background: #fafbfc; color: var(--primary); display: inline-flex; align-items: center; justify-content: center; font-size: 20px; }
.tool-title { font-weight: 600; font-size: 14.5px; }
.tool-desc { color: var(--muted); font-size: 13px; margin-top: 1px; }
.tool-meta { margin-top: 6px; }
.tool-meta code { font-size: 11.5px; }
.tool-go { color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.tool:hover .tool-go { color: var(--primary); }

/* งานดึงข้อมูล: ตาราง + ปุ่มรัน */
table.jobs td:first-child { width: 36%; }
.job-name { font-weight: 600; display: inline-flex; align-items: center; gap: 8px; }
.job-name .ti { color: var(--muted); }
.job-desc { color: var(--muted); font-size: 12.5px; margin-top: 1px; }
table.jobs td.act { text-align: right; width: 1%; }
.btn-sm { padding: 5px 11px; font-size: 13px; }

.search { width: 300px; max-width: 100%; font: inherit; padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--card); color: var(--text); }
.search::placeholder { color: var(--muted-2); }
.search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.empty-state { text-align: center; color: var(--muted); padding: 40px 0; }

@media (max-width: 720px) {
    .wrap { padding: 20px 16px 56px; }
    .appbar-in { padding: 0 16px; }
    .appbar-back span { display: none; }
    h1 { font-size: 20px; }
    .card-body { padding: 16px; }
    .stat .v { font-size: 18px; }
    .tool { grid-template-columns: 40px minmax(0, 1fr); }
    .tool-go { display: none; }
    .section-title { flex-direction: column; align-items: flex-start; gap: 2px; }
    table.jobs td:nth-child(2), table.jobs th:nth-child(2) { display: none; }
    .search { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    /* spinner ต้องหมุนเสมอ ไม่ขึ้นกับ Animation effects ของ OS (ถ้าตัดเวลาเหลือ .01ms จะกระพริบรัว) */
    *:not(.spinner), *::before, *::after { transition: none !important; animation-duration: .01ms !important; }
}
