/* ============================================================
   Paperflow — Main Stylesheet
   Font: Sarabun (Thai)
   ============================================================ */

:root {
  --primary:     #2563eb;
  --primary-dk:  #1d4ed8;
  --primary-lt:  #dbeafe;
  --success:     #10b981;
  --warning:     #f59e0b;
  --danger:      #ef4444;
  --text:        #1e293b;
  --text-muted:  #64748b;
  --bg:          #f1f5f9;
  --card:        #ffffff;
  --border:      #e2e8f0;
  --sidebar-w:   230px;
  --radius:      10px;
  --shadow:      0 1px 4px rgba(0,0,0,.08);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: 'Sarabun', sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.6;
}

/* ── App Layout ─────────────────────────────────────────── */
.app-layout { display: flex; min-height: 100vh; }

/* ── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: #0f172a;
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 16px 16px;
  border-bottom: 1px solid #1e293b;
}
.brand-icon { font-size: 1.4rem; }
.brand-name { font-size: 1.15rem; font-weight: 700; color: #f8fafc; letter-spacing: .5px; }

.nav-menu { list-style: none; padding: 8px 0; flex: 1; }
.nav-section {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #475569;
  padding: 14px 16px 4px;
  font-weight: 600;
}
.nav-item a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  color: #94a3b8;
  text-decoration: none;
  border-radius: 6px;
  margin: 1px 8px;
  font-size: .92rem;
  transition: background .15s, color .15s;
}
.nav-item a:hover { background: #1e293b; color: #f1f5f9; }
.nav-item.active a { background: var(--primary); color: #fff; }
.nav-icon { font-size: 1rem; width: 18px; text-align: center; }

.sidebar-footer {
  padding: 14px 16px;
  border-top: 1px solid #1e293b;
  font-size: .82rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.logout-link { color: #64748b; text-decoration: none; }
.logout-link:hover { color: var(--danger); }

/* ── Main Content ───────────────────────────────────────── */
.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 14px 28px;
  position: sticky; top: 0; z-index: 50;
}
.topbar-title { font-size: 1.1rem; font-weight: 600; }

.content-area { padding: 24px 28px; }

/* ── Cards ──────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.card-header h3 { font-size: 1rem; font-weight: 600; }
.card-hint { font-size: .8rem; color: var(--text-muted); }

/* ── Buttons ────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 7px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dk); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; border: none; }
.btn-sm { padding: 5px 11px; font-size: .82rem; }
.btn-block { width: 100%; justify-content: center; padding: 11px; }
.btn-remove {
  background: none; border: none; cursor: pointer;
  color: #94a3b8; font-size: 1rem; padding: 4px;
}
.btn-remove:hover { color: var(--danger); }

/* ── Forms ──────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: .87rem; font-weight: 500; margin-bottom: 5px; color: var(--text); }
.form-control {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-family: inherit;
  font-size: .9rem;
  background: #fff;
  transition: border .15s, box-shadow .15s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-lt); }
.form-control[readonly] { background: var(--bg); color: var(--text-muted); }
.form-control-sm { padding: 5px 9px; font-size: .85rem; border: 1px solid var(--border); border-radius: 6px; font-family: inherit; }
textarea.form-control { resize: vertical; }
.form-hint { font-size: .78rem; color: var(--primary); text-decoration: none; margin-top: 3px; display: inline-block; }
.req { color: var(--danger); }

.form-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 8px;
}
.form-group.inline { display: flex; align-items: center; gap: 10px; }
.form-group.inline label { margin: 0; white-space: nowrap; }

/* Contract editor */
.contract-editor { font-family: inherit; font-size: .9rem; line-height: 1.8; }

/* ── Tables ─────────────────────────────────────────────── */
.table-wrapper { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .88rem;
}
.data-table thead { background: var(--bg); }
.data-table th, .data-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); }
.data-table th { font-weight: 600; font-size: .8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .3px; }
.data-table tbody tr:hover { background: #f8fafc; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table code { background: #f1f5f9; padding: 2px 7px; border-radius: 4px; font-size: .82rem; }
.text-right { text-align: right !important; }
.text-center { text-align: center !important; }
.actions { display: flex; gap: 6px; white-space: nowrap; }

/* ── Items Table (in doc form) ──────────────────────────── */
.items-table-wrap { overflow-x: auto; margin-bottom: 0; }
.items-table { width: 100%; border-collapse: collapse; }
.items-table th, .items-table td { padding: 6px 8px; border: 1px solid var(--border); }
.items-table th { background: var(--bg); font-size: .8rem; font-weight: 600; color: var(--text-muted); }
.items-table .form-control { border: none; border-radius: 0; padding: 5px; box-shadow: none; font-size: .88rem; }
.items-table .form-control:focus { background: var(--primary-lt); }

/* ── Totals ─────────────────────────────────────────────── */
.totals-section { display: flex; justify-content: space-between; align-items: flex-start; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); flex-wrap: wrap; gap: 12px; }
.totals-table { border-collapse: collapse; font-size: .9rem; }
.totals-table td { padding: 4px 10px; }
.totals-table td:last-child { text-align: right; font-weight: 500; min-width: 120px; }
.totals-table .total-row { font-weight: 700; font-size: 1rem; border-top: 2px solid var(--text); }
.totals-table .total-row td { padding-top: 8px; }

/* ── Badges ─────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 20px;
  font-size: .76rem;
  font-weight: 600;
}
.badge-draft  { background: #f1f5f9; color: #64748b; }
.badge-sent   { background: #dbeafe; color: #1d4ed8; }
.badge-paid   { background: #dcfce7; color: #15803d; }
.badge-signed { background: #fef3c7; color: #92400e; }

/* ── Dashboard ──────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 28px; }
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow);
}
.stat-icon { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.4rem; flex-shrink: 0; }
.stat-value { font-size: 1.5rem; font-weight: 700; line-height: 1; }
.stat-label { font-size: .82rem; color: var(--text-muted); margin-top: 3px; }

.section-header { margin: 24px 0 12px; }
.section-header h2 { font-size: 1rem; font-weight: 600; color: var(--text-muted); }

.quick-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 28px; }
.quick-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  font-size: .9rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  transition: all .15s;
}
.quick-btn:hover { border-color: var(--primary); color: var(--primary); box-shadow: 0 2px 8px rgba(37,99,235,.12); transform: translateY(-1px); }

/* ── Toolbar ────────────────────────────────────────────── */
.toolbar { display: flex; justify-content: flex-end; margin-bottom: 16px; gap: 10px; }

/* ── Alerts ─────────────────────────────────────────────── */
.alert {
  padding: 11px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: .9rem;
  border: 1px solid;
}
.alert a { text-decoration: underline; }
.alert-success { background: #f0fdf4; border-color: #86efac; color: #166534; }
.alert-error   { background: #fef2f2; border-color: #fca5a5; color: #991b1b; }
.alert-info    { background: #eff6ff; border-color: #93c5fd; color: #1e40af; }

/* ── Empty State ────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
}
.empty-icon { font-size: 3rem; margin-bottom: 12px; }
.empty-state p { margin-bottom: 16px; }

/* ── Danger Zone ────────────────────────────────────────── */
.danger-zone {
  margin-top: 20px;
  padding: 16px;
  border: 1px solid #fca5a5;
  border-radius: var(--radius);
  background: #fff5f5;
}

/* ── Doc Form Layout ────────────────────────────────────── */
.doc-form-layout { max-width: 900px; }

/* ── Settings Layout ────────────────────────────────────── */
.settings-layout { max-width: 760px; }

/* ── Clickable Table Rows ───────────────────────────────── */
.clickable-row { cursor: pointer; transition: background .1s; }
.clickable-row:hover { background: #eff6ff !important; }

/* ── Image Preview (settings) ───────────────────────────── */
.img-preview-box { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; }
.img-preview { max-height: 100px; max-width: 280px; object-fit: contain; border: 1px solid var(--border); border-radius: 6px; padding: 6px; background: #f8fafc; }
.img-preview-sig { max-height: 70px; max-width: 220px; }
.img-placeholder { color: var(--text-muted); font-size: .88rem; padding: 16px; background: var(--bg); border: 1px dashed var(--border); border-radius: 6px; text-align: center; }

/* ── Login Page ─────────────────────────────────────────── */
body.login-page { background: var(--bg); display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-container { width: 100%; max-width: 420px; padding: 20px; }
.login-card { background: var(--card); border: 1px solid var(--border); border-radius: 16px; padding: 40px 36px; box-shadow: 0 4px 20px rgba(0,0,0,.08); }
.login-brand { text-align: center; margin-bottom: 28px; }
.login-logo { font-size: 2.5rem; display: block; margin-bottom: 8px; }
.login-brand h1 { font-size: 1.5rem; font-weight: 700; }
.login-brand p { color: var(--text-muted); font-size: .9rem; margin-top: 4px; }

/* ============================================================
   PRINT STYLES
   ============================================================ */
body.print-page {
  background: #fff;
  font-size: 13px;
  color: #000;
}

.print-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  gap: 8px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
}
.print-toolbar-left { display: flex; gap: 8px; flex-wrap: wrap; }

.doc-paper {
  max-width: 210mm;
  margin: 20px auto;
  padding: 20mm 18mm;
  background: #fff;
  font-size: 13px;
  line-height: 1.6;
}

/* Document Header */
.doc-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  gap: 20px;
}
.doc-logo { max-height: 70px; max-width: 200px; object-fit: contain; display: block; margin-bottom: 8px; }
.doc-company-name { font-size: 1.1rem; font-weight: 700; margin-bottom: 4px; }
.doc-company-info { font-size: .85rem; color: #444; line-height: 1.5; }

.doc-title-block { text-align: right; flex-shrink: 0; }
.doc-type-label {
  font-size: 1.3rem;
  font-weight: 700;
  color: #1e40af;
  margin-bottom: 8px;
  border-bottom: 2px solid #1e40af;
  padding-bottom: 4px;
}
.doc-meta-table { margin-left: auto; font-size: .87rem; border-collapse: collapse; }
.doc-meta-table td { padding: 2px 6px; }
.doc-meta-table td:first-child { color: #666; }
.doc-meta-table td:last-child { font-weight: 500; }

.doc-divider { border: none; border-top: 1px solid #ddd; margin: 12px 0; }

/* Parties */
.doc-parties { display: flex; gap: 30px; margin-bottom: 16px; }
.doc-party { flex: 1; }
.party-label { font-size: .78rem; color: #666; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 2px; }
.party-name { font-weight: 600; font-size: 1rem; }
.party-company { font-weight: 400; font-size: .9rem; }
.party-address { font-size: .85rem; color: #444; }

/* Items Table (print) */
.doc-items-table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: .88rem;
}
.doc-items-table th {
  background: #bfdbfe;
  color: #1e3a8a;
  padding: 5px 10px;
  text-align: left;
  font-weight: 600;
}
.doc-items-table td { padding: 5px 10px; border-bottom: 1px solid #e8e8e8; }
.doc-items-table tbody tr:nth-child(even) { background: #f0f7ff; }
.doc-items-table tfoot td { border-top: 1px solid #ccc; font-size: .87rem; padding-top: 6px; }
.grand-total-row td { background: #dbeafe; font-size: 1rem !important; font-weight: 600; border-top: 2px solid #3b82f6 !important; }
.col-no    { width: 4%; }
.col-desc  { width: 52%; }
.col-qty   { width: 7%; }
.col-unit  { width: 8%; }
.col-price { width: 14%; text-align: right; }
.col-amount{ width: 15%; text-align: right; }

/* Bank Info */
.doc-bank-info {
  background: #f0f9ff;
  border: 1px solid #bae6fd;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: .85rem;
  margin: 12px 0;
}
.doc-bank-grid {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 8px;
}
.doc-bank-card {
  background: #fff;
  border: 1px solid #bae6fd;
  border-radius: 6px;
  padding: 8px 12px;
  min-width: 180px;
  flex: 1;
}
.doc-bank-card-title { font-weight: 600; font-size: .85rem; margin-bottom: 4px; color: #0369a1; }
.doc-bank-card-row   { font-size: .82rem; color: #334155; line-height: 1.5; }

/* Notes */
.doc-notes { font-size: .85rem; color: #444; margin: 8px 0; line-height: 1.45; }

/* Contract body */
.contract-body { font-size: .9rem; line-height: 1.7; padding: 8px 0; }
.contract-body p { margin: 0 0 1.25rem 0; }

/* Signature */
.doc-signature {
  display: flex;
  justify-content: space-around;
  margin-top: 24px;
  gap: 20px;
}
.sig-box { text-align: center; flex: 1; }
.sig-line {
  border-bottom: 1px dotted #999;
  height: 60px;
  margin-bottom: 6px;
}
.sig-img-wrap {
  height: 60px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  margin-bottom: 0;
}
.sig-img {
  max-height: 56px;
  max-width: 180px;
  object-fit: contain;
}
.sig-label { font-size: .82rem; color: #555; border-top: 1px solid #aaa; padding-top: 4px; margin-top: 6px; }
.sig-name  { font-size: .85rem; font-weight: 500; }
.sig-date  { font-size: .8rem; color: #777; margin-top: 4px; }

/* Receipt block */
.receipt-amount-block { margin: 16px 0; }

/* ── Bank account checkboxes (quotation form) ── */
.bank-checkboxes { display: flex; flex-direction: column; gap: 8px; margin-bottom: 4px; }
.bank-check-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.bank-check-item:hover { background: #f0f7ff; border-color: var(--primary); }
.bank-check-item input[type=checkbox] { width: 17px; height: 17px; flex-shrink: 0; accent-color: var(--primary); cursor: pointer; }
.bank-check-bank   { font-weight: 600; font-size: .9rem; }
.bank-check-detail { font-size: .82rem; color: var(--text-muted); margin-top: 2px; }

/* ── Bank accounts list (settings) ── */
.bank-accounts-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.bank-account-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.bank-account-info { display: flex; flex-direction: column; gap: 2px; }
.bank-account-bank   { font-weight: 600; font-size: .9rem; }
.bank-account-num    { font-size: .85rem; color: var(--text); font-family: monospace; }
.bank-account-holder { font-size: .82rem; color: var(--text-muted); }

/* ── @media print ───────────────────────────────────────── */
@media print {
  .no-print { display: none !important; }
  body.print-page { background: #fff; }
  .doc-paper {
    margin: 0;
    padding: 8mm 12mm;
    max-width: none;
    box-shadow: none;
  }
  .print-toolbar { display: none !important; }
  a { color: inherit !important; text-decoration: none !important; }
  @page { margin: 14mm 8mm 6mm; size: A4; }

  /* Layout table สำหรับ tfoot signature repeat */
  .doc-layout-table {
    width: 100%;
    border-collapse: collapse;
  }
  .doc-layout-table > tbody > tr > td,
  .doc-layout-table > tfoot > tr > td {
    padding: 0;
    border: none;
    vertical-align: top;
  }

  /* Signature — อยู่ใน tfoot จึง repeat ทุกหน้าอัตโนมัติ */
  .doc-signature {
    border-top: 1px solid #ddd;
    padding: 6px 0 4px;
    margin-top: 0;
  }

  /* บังคับพิมพ์สีพื้นหลังตาราง */
  .doc-items-table th {
    background: #bfdbfe !important;
    color: #1e3a8a !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .doc-items-table tbody tr:nth-child(even) {
    background: #f0f7ff !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .grand-total-row td {
    background: #dbeafe !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

@media (max-width: 768px) {
  :root { --sidebar-w: 0px; }
  .sidebar { display: none; }
  .main-content { margin-left: 0; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .form-grid-2, .form-grid-3 { grid-template-columns: 1fr; }
  .content-area { padding: 16px; }
}
