/* ============================================================================
   styles.css — ธีม SaaS โทนสว่าง
   ========================================================================== */
:root {
  --bg:        #f5f7fb;
  --surface:   #ffffff;
  --border:    #e7ebf2;
  --text:      #0f172a;
  --muted:     #64748b;
  --primary:   #2563eb;
  --primary-d: #1d4ed8;
  --ok:        #10b981;
  --warn:      #f59e0b;
  --danger:    #ef4444;
  --radius:    14px;
  --shadow:    0 1px 2px rgba(16,24,40,.04), 0 4px 16px rgba(16,24,40,.06);
  --shadow-lg: 0 10px 30px rgba(16,24,40,.12);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Inter', 'Noto Sans Thai', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }

/* ── ปุ่ม ─────────────────────────────────────────────────────────────── */
.btn-primary {
  width: 100%; padding: 12px 16px; border: 0; border-radius: 10px;
  background: var(--primary); color: #fff; font-weight: 600; font-size: 15px;
  cursor: pointer; transition: background .15s, transform .05s;
}
.btn-primary:hover { background: var(--primary-d); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.btn-ghost {
  padding: 8px 14px; border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); color: var(--text); font-size: 13px; font-weight: 500; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.btn-ghost:hover { border-color: #cbd5e1; background: #f8fafc; }

/* ── หน้า Login ───────────────────────────────────────────────────────── */
.login-screen {
  min-height: 100%; display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(1200px 600px at 100% -10%, #dbeafe 0%, transparent 55%),
    radial-gradient(900px 500px at -10% 110%, #e0f2fe 0%, transparent 50%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow-lg);
  padding: 34px 30px; display: flex; flex-direction: column; gap: 16px;
}
.login-brand { text-align: center; margin-bottom: 6px; }
.login-logo, .brand-logo {
  display: inline-grid; place-items: center;
  background: linear-gradient(135deg, #3b82f6, #2563eb); color: #fff;
  font-weight: 700; border-radius: 12px;
}
.login-logo { width: 52px; height: 52px; font-size: 20px; margin-bottom: 12px; }
.login-brand h1 { font-size: 20px; margin: 0 0 4px; }
.login-brand p { margin: 0; color: var(--muted); font-size: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--muted); }
.field input, .field select {
  padding: 11px 13px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 15px; color: var(--text); background: #fff; outline: none; transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.login-error {
  margin: 0; padding: 10px 12px; border-radius: 9px; font-size: 13px;
  background: #fef2f2; color: var(--danger); border: 1px solid #fecaca;
}
.login-foot { text-align: center; font-size: 12px; color: var(--muted); margin: 4px 0 0; }

/* ── Loading ──────────────────────────────────────────────────────────── */
.loading { position: fixed; inset: 0; display: grid; place-items: center; gap: 14px; background: rgba(245,247,251,.85); z-index: 50; backdrop-filter: blur(2px); }
.loading p { color: var(--muted); font-size: 14px; }
.spinner { width: 38px; height: 38px; border: 3px solid #dbeafe; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── โครง App ─────────────────────────────────────────────────────────── */
.app { min-height: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 24px; background: rgba(255,255,255,.85); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 40px; height: 40px; font-size: 15px; }
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-chip { font-size: 13px; font-weight: 600; color: var(--text); background: #eff6ff; border: 1px solid #dbeafe; padding: 6px 12px; border-radius: 20px; }

.tabs { display: flex; gap: 4px; padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 65px; z-index: 19; }
.tab { border: 0; background: none; padding: 14px 16px; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.content { padding: 24px; max-width: 1320px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }

/* ── KPI cards ────────────────────────────────────────────────────────── */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 14px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 6px;
}
.kpi .kpi-label { font-size: 13px; color: var(--muted); font-weight: 500; }
.kpi .kpi-value { font-size: 26px; font-weight: 700; letter-spacing: -.5px; }
.kpi .kpi-foot { font-size: 12px; color: var(--muted); }
.kpi.accent { background: linear-gradient(135deg, #2563eb, #1d4ed8); border: 0; color: #fff; }
.kpi.accent .kpi-label, .kpi.accent .kpi-foot { color: rgba(255,255,255,.85); }

/* progress bar เล็กใน KPI */
.kpi-bar { height: 6px; border-radius: 4px; background: #eef2f7; overflow: hidden; margin-top: 4px; }
.kpi-bar > i { display: block; height: 100%; border-radius: 4px; background: var(--primary); }
.kpi.accent .kpi-bar { background: rgba(255,255,255,.25); }
.kpi.accent .kpi-bar > i { background: #fff; }

/* ── การ์ด/กราฟ ──────────────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
.chart-box { position: relative; height: 320px; }
.chart-box.chart-tall { height: 380px; }

/* ── ตาราง ────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border-radius: 10px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table th { position: sticky; top: 0; background: #f8fafc; color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .3px; z-index: 1; }
.data-table td.r, .data-table th.r { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tbody tr:hover { background: #f8fafc; }
.data-table td.wrap { white-space: normal; max-width: 340px; }
.data-table .total-row td { font-weight: 700; background: #f1f5f9; border-top: 2px solid var(--border); }

/* แถวรวมตาม RO (คลิกเพื่อเปิดหน้าต่างรายละเอียด) */
.data-table tr.ro-row { cursor: pointer; }
.data-table tr.ro-row:hover { background: #f1f5f9; }
.data-table td.ro-name { font-weight: 600; color: var(--primary); }
.data-table tr.ro-row:hover td.ro-name { text-decoration: underline; }
.data-table td.ro-name .line-count {
  display: inline-block; min-width: 18px; text-align: center; margin-left: 6px;
  background: #eff6ff; color: #1d4ed8; border-radius: 10px; font-size: 11px; font-weight: 600; padding: 0 6px;
}
/* แถวรายการย่อยที่กางออก */
.data-table tr.line-row > td { padding: 0; background: #f8fafc; }
.line-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.line-table th, .line-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.line-table th { background: #eef2f7; color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }
.line-table td.wrap { white-space: normal; max-width: 420px; }
.line-table td.r, .line-table th.r { text-align: right; font-variant-numeric: tabular-nums; }
.line-table tr:last-child td { border-bottom: 0; }

/* หัวคอลัมน์ที่กดเรียงได้ */
.data-table th.sortable { cursor: pointer; user-select: none; }
.data-table th.sortable:hover { color: var(--primary); }
.data-table th .sort-ar { font-size: 10px; opacity: .7; }

/* toggle เล็ก (มูลค่า/จำนวน RO) */
.toggle { display: inline-flex; background: #f1f5f9; border-radius: 9px; padding: 3px; gap: 2px; }
.toggle button { border: 0; background: none; padding: 5px 12px; border-radius: 7px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; }
.toggle button.active { background: #fff; color: var(--primary); box-shadow: var(--shadow); }

/* ── Modal ────────────────────────────────────────────────────────────── */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; padding: 24px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15,23,42,.45); backdrop-filter: blur(2px); }
.modal-card {
  position: relative; background: var(--surface); border-radius: 16px; box-shadow: var(--shadow-lg);
  width: 100%; max-width: 860px; max-height: 86vh; display: flex; flex-direction: column; overflow: hidden;
  animation: modal-in .15s ease-out;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(.99); } to { opacity: 1; transform: none; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 22px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-x { border: 0; background: #f1f5f9; width: 32px; height: 32px; border-radius: 8px; font-size: 14px; cursor: pointer; color: var(--muted); }
.modal-x:hover { background: #e2e8f0; color: var(--text); }
.modal-meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px 18px; padding: 16px 22px; border-bottom: 1px solid var(--border); }
.meta-item { display: flex; flex-direction: column; gap: 3px; }
.meta-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.meta-value { font-size: 14px; word-break: break-word; }
#ro-modal-body { overflow: auto; padding: 6px 22px 22px; }

/* แถบความคืบหน้าในเซลล์ (% ใช้ไป) */
.cellbar { position: relative; min-width: 90px; }
.cellbar > i { position: absolute; left: 0; top: 0; bottom: 0; background: rgba(37,99,235,.12); border-radius: 4px; z-index: 0; }
.cellbar > span { position: relative; z-index: 1; }

/* badge สถานะ */
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge.ok { background: #ecfdf5; color: #047857; }
.badge.warn { background: #fffbeb; color: #b45309; }
.badge.info { background: #eff6ff; color: #1d4ed8; }
.badge.muted { background: #f1f5f9; color: #475569; }

/* ── error banner ─────────────────────────────────────────────────────── */
.error-banner {
  background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca;
  border-radius: var(--radius); padding: 14px 18px; font-size: 14px;
  display: flex; gap: 10px; align-items: flex-start; line-height: 1.5;
  box-shadow: var(--shadow);
}
.error-banner::before { content: '⚠️'; font-size: 16px; }
.error-banner code { background: rgba(185,28,28,.08); padding: 1px 5px; border-radius: 4px; font-size: 12px; word-break: break-all; }

/* ── toolbar หมวด ─────────────────────────────────────────────────────── */
.cat-toolbar { display: flex; gap: 14px; align-items: end; flex-wrap: wrap; }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
.field.inline select { min-width: 280px; }
.search { flex: 1; min-width: 220px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 10px; font-size: 14px; outline: none; }
.search:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
