* { box-sizing: border-box; }
*, *::before, *::after { max-width: 100vw; }
html, body { height: 100%; overflow-x: hidden; width: 100%; max-width: 100vw; }
img, canvas, svg, video, iframe { max-width: 100%; }
img, svg, video, iframe { height: auto; }
body {
    font-family: 'Segoe UI', 'Sarabun', 'Tahoma', sans-serif;
    margin: 0; padding: 0;
    background: #f4f6f9;
    color: #2c3e50;
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}
/* main content wrapper (div.container ที่ห่อเนื้อหาระหว่าง header กับ footer) */
body > .container { flex: 1 0 auto; }
/* footer อยู่ล่างสุดเสมอ (แต่ไม่มี whitespace เกินหลังจากนั้น) */
body > footer { flex-shrink: 0; margin-top: auto !important; margin-bottom: 0; }
html, body { overflow-x: hidden; }
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
@media (max-width: 768px) {
    .container { padding: 12px; }
}
@media (max-width: 480px) {
    .container { padding: 10px; }
}
/* หน้า check.php / validate_jhcis.php ใช้ container กว้างเต็มจอ */
body.wide > .container { max-width: none; padding-left: 32px; padding-right: 32px; }

/* Header — ตรึงด้านบน (fixed) ทุกหน้า */
body > header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    box-shadow: 0 2px 12px rgba(0,0,0,0.15);
}
/* padding-top ของ body ถูกกำหนดโดย JS ใน header.php (syncHeaderOffset) */
/* fallback สำหรับกรณี JS ปิด */
body { padding-top: 132px; }
@media (max-width: 768px) {
    body { padding-top: 120px; }
}

/* Header */
header {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    color: #fff;
    padding: 12px 0;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
header h1 { margin: 0; font-size: clamp(1rem, 3.2vw, 1.35rem); line-height: 1.25; }
header .sub { opacity: 0.85; font-size: clamp(0.7rem, 2.2vw, 0.85rem); margin-top: 2px; line-height: 1.3; }
@media (max-width: 768px) {
    header { padding: 8px 0; }
    header .container { padding: 0 14px; }
    header h1 { font-size: 1rem; }
    header .sub { font-size: 0.72rem; }
}
header .visitor-badge {
    position: absolute;
    top: 0;
    right: 20px;
    background: rgba(255,255,255,0.15);
    color: #fff;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 0.85rem;
    border: 1px solid rgba(255,255,255,0.25);
    backdrop-filter: blur(4px);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: help;
    white-space: nowrap;
}
/* iPad และมือถือ — badge เลื่อนไปใต้ title */
@media (max-width: 1024px) {
    header .container { padding-right: 16px; }
    header .visitor-badge {
        position: static;
        margin-top: 10px;
        display: inline-flex;
    }
}

/* Navigation */
nav { background: #fff; border-bottom: 1px solid #e1e5eb; }
nav .container {
    display: flex;
    gap: 4px;
    padding: 0 20px;
    overflow-x: auto;                              /* เลื่อนแนวนอนได้บนมือถือ */
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}
nav .container::-webkit-scrollbar { height: 3px; }
nav .container::-webkit-scrollbar-thumb { background: #d5dae0; border-radius: 3px; }
nav a {
    padding: 14px 18px;
    color: #34495e;
    text-decoration: none;
    border-bottom: 3px solid transparent;
    font-weight: 500;
    transition: 0.15s;
    white-space: nowrap;                           /* กันบรรทัดแตก */
    flex-shrink: 0;
}
nav a:hover { background: #f8f9fb; color: #1e3c72; }
nav a.active { border-bottom-color: #2a5298; color: #1e3c72; }
@media (max-width: 768px) {
    nav .container { padding: 0 12px; }
    nav a { padding: 12px 14px; font-size: 0.9rem; }
}

/* Cards */
.card {
    background: #fff;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.card h2 { margin-top: 0; color: #1e3c72; border-bottom: 2px solid #ecf0f5; padding-bottom: 10px; font-size: clamp(1rem, 3.5vw, 1.4rem); }
@media (max-width: 768px) {
    .card { padding: 14px; margin-bottom: 12px; border-radius: 6px; }
    .card h2 { padding-bottom: 8px; margin-bottom: 8px; }
}

/* Stats grid */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat {
    background: #fff;
    padding: 18px;
    border-radius: 8px;
    border-left: 4px solid #2a5298;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.stat .num { font-size: 1.8rem; font-weight: 700; color: #1e3c72; }
.stat .label { color: #7f8c8d; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.5px; }
.stat.error { border-left-color: #e74c3c; }
.stat.error .num { color: #c0392b; }
.stat.ok { border-left-color: #27ae60; }
.stat.ok .num { color: #27ae60; }
.stat.warn { border-left-color: #f39c12; }
.stat.warn .num { color: #d68910; }
/* Stat cards ปรับขนาดบนมือถือ */
@media (max-width: 480px) {
    .stats { grid-template-columns: repeat(2, 1fr) !important; gap: 8px; }
    .stat { padding: 12px 10px; }
    .stat .num { font-size: 1.15rem; }
    .stat .label { font-size: 0.7rem; }
}

/* Grid 2-column ทุกที่ที่ใช้ inline style ให้ stack บนมือถือ */
@media (max-width: 768px) {
    [style*="grid-template-columns:1fr 2fr"],
    [style*="grid-template-columns: 1fr 2fr"],
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns:repeat(4,1fr)"],
    [style*="grid-template-columns: repeat(4,1fr)"],
    [style*="grid-template-columns:repeat(4, 1fr)"] {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }
}

/* ========== Responsive helpers สำหรับทุกหน้า ========== */

/* Chart canvas — ทุกจอ (บังคับให้พอดี parent) */
canvas {
    max-width: 100% !important;
    width: 100% !important;
    display: block !important;
    box-sizing: border-box;
}
/* parent ของ canvas (มี height inline) — บังคับความกว้าง */
.card > div[style*="position:relative"] {
    max-width: 100% !important;
    width: 100% !important;
    overflow: hidden !important;
    box-sizing: border-box;
}

/* ป้องกันข้อความยาว (path, PID, code) ล้นออก */
code, .muted, td, .card, p, div, span {
    word-break: break-word;
    overflow-wrap: break-word;
}
code { word-break: break-all; }                  /* code สามารถแยกกลางคำได้ */

/* Chart containers — ปรับความสูงเมื่อจอเล็ก */
@media (max-width: 1024px) {
    /* iPad ก็ต้องยืดหด — สลับ 2 การ์ดกราฟจากข้างกันเป็นซ้อนกัน */
    [style*="grid-template-columns:1fr 2fr"],
    [style*="grid-template-columns: 1fr 2fr"] {
        grid-template-columns: 1fr !important;
    }
}
@media (max-width: 768px) {
    .card canvas,
    #chartDonut, #chartBar {
        max-height: 240px !important;
        width: 100% !important;
    }
    .card > div[style*="height:280px"],
    .card > div[style*="height: 280px"] {
        height: 220px !important;
    }
    /* pre code blocks (raw file view) */
    pre[style*="max-height:400px"],
    pre[style*="max-height: 400px"] { max-height: 260px !important; font-size: 0.75rem !important; }
    pre[style*="max-height:300px"],
    pre[style*="max-height: 300px"] { max-height: 220px !important; font-size: 0.75rem !important; }
}
@media (max-width: 480px) {
    .card canvas,
    #chartDonut, #chartBar { max-height: 200px !important; }
    .card > div[style*="height:280px"],
    .card > div[style*="height: 280px"] { height: 180px !important; }
}

/* Filter bar — stack แนวตั้งบนมือถือ */
.filter-bar { display: flex; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
@media (max-width: 768px) {
    .filter-bar { flex-direction: column; gap: 8px; }
    .filter-bar > * { width: 100%; min-width: 0 !important; max-width: none !important; flex: none !important; }
    .filter-bar > div { flex-direction: row !important; flex-wrap: wrap; }
}

/* Transaction grid (2 คอลัมน์บน desktop → 1 คอลัมน์บน tablet/mobile) */
#txn-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}
@media (max-width: 1024px) {
    #txn-grid { grid-template-columns: 1fr !important; }
}

/* Transaction card ภายใน — ป้องกันล้น */
.txn-card { max-width: 100%; overflow: hidden; word-wrap: break-word; }
.txn-card > div { max-width: 100%; word-wrap: break-word; }

/* Summary-grid (การ์ดสรุปตัวเลข) */
@media (max-width: 768px) {
    .summary-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 8px !important; }
}
@media (max-width: 360px) {
    .summary-grid { grid-template-columns: 1fr !important; }
}

/* Buttons — เต็ม row บนจอเล็ก */
@media (max-width: 480px) {
    .btn { padding: 10px 14px; font-size: 0.9rem; }
}

/* Input/Select — ขยายเต็ม + tap target ใหญ่ขึ้นบนมือถือ */
@media (max-width: 768px) {
    input[type="text"], input[type="search"], input[type="file"], select, textarea {
        padding: 12px 14px;
        font-size: 16px;                            /* กัน iOS ซูมอัตโนมัติ */
    }
    input[type="file"] { font-size: 14px; }
}

/* Code badge (T/R/S/H/C/W) — ลดขนาดบนมือถือ */
@media (max-width: 480px) {
    .code { padding: 2px 8px; font-size: 0.78rem; }
}

/* Stat card ตัวเลขใหญ่ */
.stat .num { font-size: 1.8rem; }
@media (max-width: 768px) {
    .stat .num { font-size: 1.35rem; }
    .stat .label { font-size: 0.75rem; }
}

/* pre (raw text) — ป้องกันล้น */
pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    word-wrap: normal;
}

/* Visitor badge บนมือถือ */
@media (max-width: 480px) {
    header .visitor-badge {
        font-size: 0.72rem !important;
        padding: 4px 10px !important;
    }
    header .visitor-badge b { font-size: 0.78rem; }
}

/* details/summary card */
@media (max-width: 480px) {
    details.card summary { font-size: 0.9rem; }
}

.summary-grid .stat { padding: 12px 14px; min-width: 0; }
.summary-grid .stat .num { font-size: 1.35rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.summary-grid .stat .label { font-size: 0.78rem; }

.filter-btn { background: #ecf0f5; color: #34495e; padding: 6px 14px; font-size: 0.85rem; }
.filter-btn:hover { background: #d5dde5; }
.filter-btn.active { background: #2a5298; color: #fff; }
.filter-btn.success.active { background: #27ae60; }
.filter-btn.danger.active { background: #e74c3c; }
.filter-btn.warn.active { background: #f39c12; }

/* Tables */
table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #ecf0f5; vertical-align: top; }
th { background: #f8f9fb; color: #34495e; font-weight: 600; font-size: 0.9rem; }
tr:hover td { background: #fafbfd; }

/* ตาราง — เลื่อนแนวนอนบนมือถือ (แต่ .card ไม่ scroll เอง เพื่อไม่ให้ card อื่นล้น) */
@media (max-width: 768px) {
    .card { max-width: 100%; overflow: hidden; word-wrap: break-word; }
    .card > table,
    .card > div > table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }
    th, td { padding: 8px 10px; font-size: 0.85rem; }
    th { font-size: 0.8rem; }
    /* Card ที่มี pre/code จะ overflow-x auto ด้วย */
    .card > pre, .card > details > pre { max-width: 100%; overflow-x: auto; }
}

/* Error code badges */
.code {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 4px;
    font-weight: 700;
    font-family: 'Consolas', monospace;
    font-size: 0.85rem;
}
.code.T { background: #fde2e2; color: #c0392b; }
.code.R { background: #fff3cd; color: #856404; }
.code.S { background: #d1ecf1; color: #0c5460; }
.code.H { background: #e2d5f0; color: #5b2c87; }
.code.C { background: #d4edda; color: #155724; }
.code.W { background: #ffeaa7; color: #b8860b; }

/* Forms */
input[type="text"], input[type="search"], select, textarea {
    padding: 10px 12px;
    border: 1px solid #d5dae0;
    border-radius: 6px;
    font-size: 0.95rem;
    width: 100%;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: none; border-color: #2a5298;
    box-shadow: 0 0 0 3px rgba(42,82,152,0.1);
}
.btn {
    display: inline-block;
    padding: 10px 18px;
    background: #2a5298;
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-decoration: none;
    font-size: 0.95rem;
    font-weight: 500;
}
.btn:hover { background: #1e3c72; }
.btn.secondary { background: #95a5a6; }
.btn.secondary:hover { background: #7f8c8d; }
.btn.danger { background: #e74c3c; }
.btn.success { background: #27ae60; }

/* Filter bar */
.filter-bar { display: flex; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.filter-bar > * { flex: 1; min-width: 180px; }

/* File list */
.file-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border: 1px solid #ecf0f5;
    border-radius: 6px;
    margin-bottom: 8px;
    background: #fafbfd;
}
.file-item .name { font-weight: 500; color: #1e3c72; }
.file-item .meta { color: #7f8c8d; font-size: 0.85rem; }

/* Detail box */
.detail-box {
    background: #f8f9fb;
    border-left: 3px solid #2a5298;
    padding: 12px 16px;
    margin: 8px 0;
    border-radius: 4px;
}
.detail-box .desc { color: #555; margin: 6px 0; }
.detail-box .fix { color: #1e3c72; }
.detail-box .fix b { color: #c0392b; }

.empty { text-align: center; padding: 40px; color: #95a5a6; }
.muted { color: #7f8c8d; font-size: 0.85rem; }

/* ==================== Footer (responsive ทุกแพลตฟอร์ม) ==================== */
.site-footer {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    color: #ecf0f5;
    margin-top: 40px;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom, 0px)) 16px;
    text-align: center;
    /* iOS smooth scroll + คำอ่านไทยแยกคำ */
    word-break: break-word;
    overflow-wrap: break-word;
}
.site-footer .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0;
}
.site-footer .footer-line-1 {
    font-size: clamp(0.85rem, 2.5vw, 0.95rem);
    font-weight: 600;
    line-height: 1.5;
}
.site-footer .footer-line-2 {
    font-size: clamp(0.8rem, 2.3vw, 0.9rem);
    margin-top: 6px;
    opacity: 0.9;
    line-height: 1.5;
}
.site-footer .footer-line-3 {
    font-size: clamp(0.7rem, 2vw, 0.78rem);
    margin-top: 6px;
    opacity: 0.6;
    line-height: 1.6;
}
.site-footer .footer-sep {
    display: inline-block;
    margin: 0 6px;
    opacity: 0.5;
}

/* จอเล็กมาก (iPhone SE, จอเก่า) — บีบตัวอักษรและตัวคั่น */
@media (max-width: 480px) {
    .site-footer { padding: 16px 12px calc(16px + env(safe-area-inset-bottom, 0px)) 12px; }
    .site-footer .footer-sep { margin: 0 4px; }
}
/* iPad / แท็บเล็ต */
@media (min-width: 481px) and (max-width: 1024px) {
    .site-footer { padding: 18px 20px calc(18px + env(safe-area-inset-bottom, 0px)) 20px; }
}
