@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;500;600;700;800&display=swap');

:root {
    --bg:     #fff4f1;
    --bg2:    #ffe7e2;
    --card:   #ffffff;
    --line:   #ffd3ca;
    --txt:    #4a2b29;
    --mut:    #a37d77;
    --pri:    #d92027;
    --pri-d:  #b3171d;
    --accent: #ffb703;
    --ok:     #2fa84f;
    --warn:   #f59e0b;
    --err:    #e5484d;
    --shadow: 0 8px 22px rgba(217,32,39,.10);
}
* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: "Noto Sans Thai", -apple-system, "Segoe UI", sans-serif;
    background:
        radial-gradient(circle at 15% 12%, rgba(255,183,3,.10), transparent 38%),
        radial-gradient(circle at 85% 8%, rgba(217,32,39,.10), transparent 40%),
        var(--bg);
    color: var(--txt);
    line-height: 1.6;
}
h1, h2, .result-msg, .stat .num { font-weight: 800; letter-spacing: -.01em; }

.wrap { max-width: 1040px; margin: 0 auto; padding: 22px 18px 64px; }
.wrap.narrow { max-width: 560px; }

/* ---------- brand header ---------- */
header { text-align: center; margin-bottom: 26px; }
.brand-banner {
    width: 100%; display: block; border-radius: 22px;
    box-shadow: var(--shadow);
    border: 3px solid #fff;
}
.brand-logo {
    width: 108px; height: 108px; border-radius: 50%;
    border: 5px solid #fff; box-shadow: var(--shadow);
    margin-top: -54px; position: relative; background: #fff;
    object-fit: cover;
}
header h1 {
    margin: 12px 0 2px; font-size: 2rem; color: var(--pri);
    text-shadow: 2px 2px 0 #ffe0c2;
}
.sub { color: var(--mut); margin: 0; font-size: 1rem; }

/* ---------- mobile-app layout (หน้าลูกค้า) ---------- */
body.app {
    background:
        radial-gradient(circle at 50% -8%, rgba(217,32,39,.18), transparent 55%),
        var(--bg);
    min-height: 100vh;
    min-height: 100dvh;
}
.phone {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100vh;
    min-height: 100dvh;
    background: var(--bg);
    box-shadow: 0 0 40px rgba(217,32,39,.10);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.app-head {
    position: relative;
    text-align: center;
    padding-bottom: 8px;
    background: linear-gradient(180deg, #fff, var(--bg));
}
.app-head .brand-banner {
    width: 100%; display: block; border-radius: 0 0 26px 26px;
    border: 0; border-bottom: 4px solid #fff; box-shadow: 0 6px 16px rgba(217,32,39,.18);
}
.app-head .brand-logo {
    width: 96px; height: 96px; margin-top: -48px;
    border: 5px solid #fff; box-shadow: var(--shadow);
}
.app-head h1 { margin: 10px 16px 2px; font-size: 1.6rem; color: var(--pri); text-shadow: 2px 2px 0 #ffe0c2; }
.app-head .sub { margin: 0 16px; }
.app-body {
    flex: 1;
    padding: 18px 16px calc(20px + env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 0;
}
.app-body .alert, .app-body .result-card { margin-bottom: 18px; }
.app-body footer { margin-top: auto; padding-top: 24px; }

/* ---------- cards ---------- */
.card {
    background: var(--card);
    border: 2px solid var(--line);
    border-radius: 20px;
    padding: 24px;
    margin-bottom: 22px;
    box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 16px; font-size: 1.4rem; color: var(--pri); }

/* ---------- email field ---------- */
.field { display: block; margin-bottom: 8px; font-size: 1.02rem; font-weight: 700; color: var(--txt); }
.upload-card input[type="email"] {
    width: 100%; padding: 14px 16px; border-radius: 13px; border: 2px solid var(--line);
    background: #fffafa; color: var(--txt); font-size: 1.05rem; font-family: inherit; margin-top: 7px;
}
.upload-card input[type="email"]:focus { outline: none; border-color: var(--pri); box-shadow: 0 0 0 4px rgba(217,32,39,.12); }
.field-note {
    display: flex; align-items: center; gap: 8px;
    background: #fff7e6; border: 2px dashed var(--accent); color: #9a6a00;
    padding: 10px 14px; border-radius: 13px; margin: 4px 0 18px; font-size: .92rem; font-weight: 600;
}

/* ---------- dropzone ---------- */
.dropzone {
    display: block;
    border: 3px dashed var(--line);
    border-radius: 18px;
    padding: 34px 18px;
    text-align: center;
    cursor: pointer;
    background: #fffafa;
    transition: border-color .2s, background .2s, transform .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--pri); background: #fff0ee; transform: translateY(-2px); }
.dz-inner { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.dz-icon { font-size: 2.6rem; }
.dz-text { font-weight: 700; font-size: 1.05rem; }
.dz-hint { color: var(--mut); font-size: .88rem; }
#preview { max-height: 300px; max-width: 100%; border-radius: 14px; margin-top: 12px; box-shadow: var(--shadow); }

/* ---------- button ---------- */
.btn {
    width: 100%;
    margin-top: 18px;
    padding: 15px;
    border: 0;
    border-radius: 16px;
    background: linear-gradient(135deg, var(--pri), var(--pri-d));
    color: #fff;
    font-weight: 800;
    font-size: 1.25rem;
    cursor: pointer;
    box-shadow: 0 6px 0 var(--pri-d), var(--shadow);
    transition: transform .12s, box-shadow .12s;
}
.btn:hover { transform: translateY(2px); box-shadow: 0 4px 0 var(--pri-d), var(--shadow); }
.btn:active { transform: translateY(6px); box-shadow: 0 0 0 var(--pri-d); }
.btn:disabled { opacity: .6; cursor: progress; }

/* ---------- alerts ---------- */
.alert { padding: 14px 17px; border-radius: 14px; margin-bottom: 18px; font-weight: 600; border: 2px solid; }
.alert.success { background: #e9f9ee; color: #1f7a39; border-color: #aee5be; }
.alert.warn    { background: #fff7e6; color: #9a6a00; border-color: #ffd98a; }
.alert.error   { background: #fdeaea; color: #b3171d; border-color: #f5b5b5; }

/* ---------- result card (หน้าลูกค้า) ---------- */
.result-card {
    display: flex; align-items: center; gap: 18px;
    padding: 24px 24px; border-radius: 20px; margin-bottom: 22px;
    border: 3px solid; box-shadow: var(--shadow);
}
.result-card .result-icon { font-size: 2.8rem; line-height: 1; }
.result-card .result-msg { font-size: 1.4rem; }
.result-card.success { background: #eafaef; border-color: #8fdca6; color: #1f7a39; }
.result-card.warn    { background: #fff6e0; border-color: #ffce6b; color: #9a6a00; }
.result-card.error   { background: #fde9e9; border-color: #f3a3a3; color: #b3171d; }

/* ---------- quota card ---------- */
.quota-grid { display: flex; flex-wrap: wrap; gap: 18px; align-items: stretch; }
.quota-main {
    flex: 1 1 240px; background: linear-gradient(135deg, #fff0ee, #ffe2dd);
    border: 2px solid var(--line); border-radius: 16px; padding: 18px 22px;
    display: flex; flex-direction: column; gap: 2px;
}
.quota-main.low { background: linear-gradient(135deg, #fff3e0, #ffe6c2); border-color: #ffd98a; }
.quota-main .ql { color: var(--mut); font-size: .9rem; font-weight: 600; }
.quota-main .qv { font-weight: 800; font-size: 2.6rem; color: var(--pri); line-height: 1.1; }
.quota-main.low .qv { color: #d97706; }
.quota-main .qsub { color: var(--mut); font-size: .85rem; }
.quota-mini { flex: 1 1 240px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-content: center; }
.quota-mini > div {
    background: #fff8f6; border: 2px solid var(--line); border-radius: 13px; padding: 12px 14px;
    display: flex; flex-direction: column; gap: 3px;
}
.quota-mini .ql { color: var(--mut); font-size: .82rem; font-weight: 600; }
.quota-mini .qv2 { font-weight: 700; font-size: 1.15rem; color: var(--txt); }
.quota-bar { margin-top: 16px; height: 14px; background: #ffe2dd; border-radius: 20px; overflow: hidden; }
.quota-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--pri)); border-radius: 20px; }
.quota-pct { text-align: right; color: var(--mut); font-size: .82rem; margin-top: 5px; font-weight: 600; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; margin-bottom: 22px; }
.stat {
    background: var(--card); border: 2px solid var(--line);
    border-radius: 16px; padding: 16px; text-align: center; box-shadow: var(--shadow);
}
.stat .num { display: block; font-size: 1.7rem; color: var(--pri); }
.stat .lbl { color: var(--mut); font-size: .85rem; font-weight: 600; }
.stat.ok .num { color: var(--ok); }
.stat.err .num { color: var(--err); }

/* ---------- search bar ---------- */
.search-bar { display: flex; gap: 8px; margin: 0 0 14px; flex-wrap: wrap; }
.search-bar input[type="search"] {
    flex: 1 1 220px; min-width: 0; padding: 11px 15px; border-radius: 12px;
    border: 2px solid var(--line); background: #fffafa; color: var(--txt);
    font-size: 1rem; font-family: inherit;
}
.search-bar input[type="search"]:focus { outline: none; border-color: var(--pri); box-shadow: 0 0 0 4px rgba(217,32,39,.12); }
.search-bar .mini-btn { padding: 11px 18px; font-size: .95rem; text-decoration: none; display: inline-flex; align-items: center; }
.search-info { color: var(--mut); margin: -4px 0 14px; font-size: .9rem; }

/* ---------- filter tabs ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 16px; }
.tab {
    display: inline-flex; align-items: center; gap: 7px;
    padding: 8px 15px; border-radius: 22px; border: 2px solid var(--line);
    background: #fff; color: var(--txt); text-decoration: none; font-weight: 700; font-size: .92rem;
    transition: background .15s, color .15s, border-color .15s;
}
.tab:hover { border-color: var(--pri); }
.tab.active { background: var(--pri); border-color: var(--pri); color: #fff; }
.tab-count {
    min-width: 22px; text-align: center; padding: 1px 7px; border-radius: 12px;
    background: var(--bg2); color: var(--txt); font-size: .8rem;
}
.tab-count.err { background: #fdeaea; color: #b3171d; }
.tab-count.warn { background: #fff3e0; color: #9a6a00; }
.tab.active .tab-count { background: rgba(255,255,255,.28); color: #fff; }
.tab-empty { text-align: center; color: var(--mut); padding: 26px; font-size: 1.05rem; }

/* ---------- table ---------- */
.table-scroll { overflow-x: auto; border-radius: 14px; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 11px 13px; text-align: left; border-bottom: 2px solid var(--bg2); white-space: nowrap; }
th { color: #fff; background: var(--pri); font-weight: 600; }
th:first-child { border-top-left-radius: 12px; }
th:last-child { border-top-right-radius: 12px; }
tbody tr:nth-child(odd) { background: #fff8f6; }
td small { color: var(--mut); }
td small.en { font-style: italic; font-size: .78rem; }
td.amt { font-variant-numeric: tabular-nums; font-weight: 700; }
.amt-void { text-decoration: line-through; color: var(--mut); font-weight: 400; }
.amt-odd { color: #d97706; font-weight: 700; white-space: nowrap; }
td.ref { font-family: monospace; font-size: .78rem; color: var(--mut); }
.empty { text-align: center; color: var(--mut); padding: 28px; }
.group-head td {
    background: #ffe7e2 !important; color: var(--pri); font-weight: 800;
    border-top: 2px solid var(--pri); white-space: normal;
}

.badge { padding: 4px 12px; border-radius: 20px; font-size: .8rem; font-weight: 700; background: #f0e3e0; color: var(--mut); }
.badge.ok   { background: #e9f9ee; color: #1f7a39; }
.badge.warn { background: #fff7e6; color: #9a6a00; }
.badge.err  { background: #fdeaea; color: #b3171d; }

/* ---------- JSON viewer ---------- */
.json-box { margin-bottom: 18px; background: #fff8f6; border: 2px solid var(--line); border-radius: 14px; overflow: hidden; }
.json-box summary { cursor: pointer; padding: 12px 16px; font-weight: 700; color: var(--pri); user-select: none; }
.json-box pre, .json-row pre {
    margin: 0; padding: 14px 16px; background: #2b1d1c;
    font-family: "SF Mono", Menlo, monospace; font-size: .82rem; line-height: 1.5;
    color: #ffd7b0; white-space: pre-wrap; word-break: break-word;
    max-height: 420px; overflow: auto;
}
.json-btn {
    border: 2px solid var(--line); background: #fff; color: var(--pri);
    border-radius: 9px; padding: 3px 10px; cursor: pointer; font-family: monospace; font-weight: 700;
}
.json-btn:hover, .json-btn.active { background: var(--pri); color: #fff; }
.json-row td { white-space: normal; background: #fff8f6 !important; }

/* ---------- mini buttons + review panel ---------- */
.mini-btn {
    border: 2px solid var(--line); background: #fff; color: var(--pri);
    border-radius: 9px; padding: 5px 11px; cursor: pointer; font-family: inherit; font-weight: 700; font-size: .85rem;
}
.mini-btn:hover, .mini-btn.active { background: var(--pri); color: #fff; }
.mini-btn.solid { background: var(--pri); color: #fff; border-color: var(--pri); }
.mini-btn.solid:hover, .mini-btn.solid.active { background: var(--pri-d); border-color: var(--pri-d); }
.mini-btn.big { padding: 12px 22px; font-size: 1.05rem; border-radius: 12px; }
/* ป้ายสถานะ/ลิงก์ที่กดได้ */
button.badge.open-modal { cursor: pointer; font-family: inherit; font-size: .8rem; border: 0; }
button.badge.open-modal:hover { background: var(--err); color: #fff; }
.link-btn { border: 0; background: none; color: var(--pri); cursor: pointer; font-family: inherit; font-weight: 700; font-size: .9rem; padding: 2px 4px; }
.link-btn:hover { text-decoration: underline; }
.hint-line { background: #fff7e6; border: 2px dashed var(--accent); color: #9a6a00; padding: 10px 14px; border-radius: 12px; margin: 0 0 14px; font-weight: 600; font-size: .9rem; }
.hint-line .badge { font-size: .75rem; }
.mini-btn.ok { border-color: #8fdca6; color: #1f7a39; }
.mini-btn.ok:hover { background: var(--ok); color: #fff; }
.mini-btn.ghost { border-color: #ffce6b; color: #9a6a00; }
.mini-btn.ghost:hover { background: var(--warn); color: #fff; }
.muted { color: var(--mut); }

/* ---------- popup ดูสลิป ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-bg { position: absolute; inset: 0; background: rgba(40,15,15,.55); backdrop-filter: blur(2px); }
.modal-card {
    position: relative; z-index: 1; background: var(--card); border-radius: 20px;
    width: 100%; max-width: 440px; max-height: 90vh; max-height: 90dvh; overflow-y: auto;
    padding: 22px; box-shadow: 0 20px 60px rgba(40,15,15,.4); border: 2px solid var(--line);
}
.modal-x {
    position: absolute; top: 12px; right: 12px; width: 36px; height: 36px; border-radius: 50%;
    border: 0; background: var(--bg2); color: var(--txt); font-size: 1.1rem; cursor: pointer; font-weight: 700;
}
.modal-x:hover { background: var(--pri); color: #fff; }
.modal-title { font-weight: 800; color: var(--pri); margin: 0 38px 14px 0; font-size: 1.5rem; }
.modal-imgwrap { text-align: center; margin-bottom: 16px; }
.modal-imgwrap img { max-width: 100%; max-height: 56vh; border-radius: 14px; border: 2px solid var(--line); box-shadow: var(--shadow); }
.modal-imgwrap .no-img { padding: 40px; color: var(--mut); background: #fff8f6; border: 2px dashed var(--line); border-radius: 14px; }
.modal-info { margin: 0 0 16px; }
.modal-info > div { display: flex; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--bg2); }
.modal-info dt { flex: 0 0 88px; color: var(--mut); font-weight: 700; margin: 0; }
.modal-info dd { margin: 0; flex: 1; word-break: break-word; }
#m-ref { font-family: monospace; font-size: .85rem; word-break: break-all; }
#m-form { display: flex; flex-direction: column; gap: 12px; }
#m-form[hidden] { display: none; }
.m-amt { display: flex; flex-direction: column; gap: 6px; font-weight: 700; }
.m-amt input {
    padding: 13px 15px; border-radius: 12px; border: 2px solid var(--line);
    background: #fffafa; color: var(--txt); font-size: 1.05rem; font-family: inherit;
}
.m-amt input:focus { outline: none; border-color: var(--pri); box-shadow: 0 0 0 4px rgba(217,32,39,.12); }
#m-form .mini-btn.big { width: 100%; }

/* ---------- login ---------- */
.login-form { display: flex; flex-direction: column; gap: 15px; }
.login-form label { display: flex; flex-direction: column; gap: 7px; font-size: .95rem; font-weight: 600; color: var(--txt); }
.login-form input {
    padding: 13px 15px; border-radius: 13px; border: 2px solid var(--line);
    background: #fffafa; color: var(--txt); font-size: 1.02rem; font-family: inherit;
}
.login-form input:focus { outline: none; border-color: var(--pri); box-shadow: 0 0 0 4px rgba(217,32,39,.12); }

.admin-link { color: var(--pri); text-decoration: none; font-weight: 600; }
.admin-link:hover { text-decoration: underline; }

footer { text-align: center; color: var(--mut); font-size: .85rem; margin-top: 30px; }

/* ---------- มือถือ ---------- */
@media (max-width: 720px) {
    .wrap { padding: 16px 12px 48px; }
    .card { padding: 18px; border-radius: 16px; }
    header h1 { font-size: 1.5rem; }
    .brand-logo { width: 88px; height: 88px; margin-top: -44px; }

    .stats { grid-template-columns: repeat(2, 1fr); gap: 9px; }
    .stat { padding: 13px; border-radius: 13px; }
    .stat .num { font-size: 1.4rem; }

    /* โควต้า: เรียงเป็นแนวตั้ง */
    .quota-grid { flex-direction: column; gap: 12px; }
    .quota-mini { grid-template-columns: 1fr 1fr; }
    .quota-main .qv { font-size: 2.2rem; }

    /* ตาราง → การ์ดเรียงต่อกัน (label: ค่า) */
    .table-scroll { overflow: visible; }
    table, tbody, tr, td { display: block; width: 100%; }
    thead { display: none; }
    table { font-size: .92rem; }

    tbody tr {
        border: 2px solid var(--line); border-radius: 16px;
        margin-bottom: 14px; padding: 4px 2px; background: #fff !important;
        box-shadow: var(--shadow);
    }
    /* แถว JSON อยู่นอกการ์ด ไม่ใส่กรอบ */
    tbody tr.json-row {
        border: 0; box-shadow: none; margin: -10px 0 14px; padding: 0; background: transparent !important;
    }
    tr[hidden] { display: none !important; }

    tbody tr:not(.json-row) td {
        display: block; text-align: left; white-space: normal;
        padding: 9px 16px; border-bottom: 1px solid var(--bg2);
        overflow-wrap: anywhere; word-break: break-word;
    }
    tbody tr:not(.json-row) td:last-child { border-bottom: 0; }
    tbody tr:not(.json-row) td::before {
        content: attr(data-label); display: block;
        font-weight: 700; color: var(--mut); font-size: .8rem; margin-bottom: 3px;
    }
    /* ซ่อนช่องที่ไม่จำเป็นบนมือถือ ให้การ์ดกระชัด (ดูเต็มได้ใน popup) */
    td[data-label="#"], td[data-label="ผู้โอน"] { display: none !important; }
    /* ชื่อ/ธนาคารในการ์ดให้อ่านง่าย */
    tbody tr td small { display: inline; }
    tbody tr td .en { display: inline-block; margin-top: 2px; }
    .json-row td { display: block; padding: 0; border: 0; }
    .json-row td::before { content: none; }
    /* หัวกลุ่มอีเมล: เป็นแถบ ไม่ใช่การ์ด */
    tbody tr.group-head { border: 0 !important; box-shadow: none; margin: 12px 0 4px; padding: 0; background: transparent !important; }
    .group-head td { display: block; border: 0; border-radius: 10px; padding: 10px 14px; }
    .group-head td::before { content: none; }

    /* ผลลัพธ์ลูกค้า */
    .result-card { gap: 12px; padding: 18px; }
    .result-card .result-icon { font-size: 2.2rem; }
    .result-card .result-msg { font-size: 1.1rem; }
    .btn { font-size: 1.15rem; }
}

@media (max-width: 420px) {
    .stats { grid-template-columns: 1fr 1fr; }
    .quota-mini { grid-template-columns: 1fr; }
    header h1 { font-size: 1.3rem; }
}
