/* ============================================================
   Sales App — Clean & Minimal UI
   หลักการ: เรียบง่าย โหลดไว (system font + pure CSS)
   สวยงาม เป็นระเบียบ
   ============================================================ */
:root {
  --primary: #1d6fe0;
  --primary-dark: #155bb5;
  --primary-soft: #e8f1fd;
  --danger: #d64545;
  --danger-dark: #b73232;
  --danger-soft: #fdeeee;
  --success: #1e9e6a;
  --success-soft: #e6f6ef;
  --ink: #1a2433;
  --ink-soft: #5b6b7f;
  --ink-faint: #93a1b3;
  --line: #e6ebf1;
  --bg: #f5f7fa;
  --card: #ffffff;
  --radius: 10px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(26, 36, 51, 0.06), 0 2px 8px rgba(26, 36, 51, 0.05);
  --shadow-lg: 0 12px 40px rgba(26, 36, 51, 0.18);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai",
               "Sarabun", "Leelawadee UI", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.container { max-width: 1080px; margin: 0 auto; padding: 16px 16px 64px; }

h1 { font-size: 1.45rem; font-weight: 700; letter-spacing: -0.01em; margin: 0.5em 0 0.7em; }
h2 { font-size: 1.12rem; font-weight: 700; margin: 1.4em 0 0.6em; color: var(--ink); }

/* ---------- Topbar ---------- */
.topbar {
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 20px;
  position: sticky; top: 0; z-index: 10;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08);
}
.topbar .brand {
  color: #fff; text-decoration: none; font-weight: 800;
  font-size: 1.08rem; letter-spacing: -0.01em; margin-right: auto;
}
.topbar .nav { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; }
.topbar .nav a {
  color: #b9c6d6; text-decoration: none; font-size: 0.93rem;
  padding: 7px 11px; border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
}
.topbar .nav a:hover { color: #fff; background: rgba(255, 255, 255, 0.1); }
.topbar .nav a.muted { color: #7d8ea3; }

/* ---------- Flash messages ---------- */
.flash {
  background: var(--success-soft);
  border: 1px solid #bfe6d2;
  color: #146c46;
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 14px 0;
  font-size: 0.95rem;
}
.flash p { margin: 4px 0; }

/* ---------- Cards & stats ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 16px; }
@media (min-width: 700px) { .cards { grid-template-columns: repeat(4, 1fr); } }

.stat {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat .label { font-size: 0.82rem; color: var(--ink-soft); font-weight: 500; }
.stat .value { font-size: 1.4rem; font-weight: 800; letter-spacing: -0.02em; margin-top: 4px; }
.stat .value.good { color: var(--success); }
.stat .value.bad { color: var(--danger); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  border: 1px solid #cbd5e1;
  background: #fff;
  color: var(--ink);
  border-radius: var(--radius-sm);
  padding: 9px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  line-height: 1.4;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s, transform 0.05s;
  user-select: none;
}
.btn:hover { background: #f1f5f9; border-color: #aebdcd; }
.btn:active { transform: translateY(1px); }

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  box-shadow: 0 1px 2px rgba(29, 111, 224, 0.35);
}
.btn.primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn.danger { background: #fff; border-color: #efb4b4; color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn.danger.solid {
  background: var(--danger); border-color: var(--danger); color: #fff;
  box-shadow: 0 1px 2px rgba(214, 69, 69, 0.35);
}
.btn.danger.solid:hover { background: var(--danger-dark); border-color: var(--danger-dark); }

.btn.small { padding: 5px 12px; font-size: 0.86rem; border-radius: 7px; }
.btn.block { display: block; width: 100%; margin-top: 14px; padding: 12px 18px; }

/* ---------- Modal ---------- */
.modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(26, 36, 51, 0.55);
  backdrop-filter: blur(2px);
  z-index: 1000; align-items: center; justify-content: center; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: #fff; border-radius: 14px; max-width: 430px; width: 100%;
  padding: 26px; box-shadow: var(--shadow-lg);
  animation: pop 0.18s ease-out;
}
@keyframes pop { from { transform: scale(0.96) translateY(6px); opacity: 0; } }
.modal-box h2 { margin: 0 0 12px; color: var(--danger); font-size: 1.2rem; }
.modal-box p { margin: 0 0 8px; line-height: 1.65; color: var(--ink-soft); }
.modal-box .warn-date { font-weight: 700; color: var(--ink); }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions .btn { flex: 1; margin-top: 0; }

/* ---------- Forms ---------- */
.form label {
  display: block; margin-bottom: 12px;
  font-weight: 600; font-size: 0.92rem; color: var(--ink);
}
.form input, .form select, .form textarea {
  display: block; width: 100%; margin-top: 6px; padding: 10px 12px;
  font-size: 16px; /* ป้องกัน iOS auto-zoom */
  font-family: inherit; color: var(--ink);
  border: 1px solid #cbd5e1; border-radius: var(--radius-sm); background: #fff;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.form input:focus, .form select:focus, .form textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.inline-form { display: inline; }
.inline-form label { font-size: 0.9rem; font-weight: 500; margin-right: 8px; }
.inline-form input {
  font-size: 15px; font-family: inherit; padding: 7px 10px;
  border: 1px solid #cbd5e1; border-radius: var(--radius-sm); margin: 0 4px;
}
.inline-form input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; background: #fff; font-size: 0.93rem; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th {
  background: #f1f5f9; color: var(--ink-soft);
  font-weight: 700; font-size: 0.82rem; letter-spacing: 0.02em;
  position: sticky; top: 0;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #fafcfe; }
tfoot td { font-weight: 700; background: #f8fafc; }
tr.inactive td { color: var(--ink-faint); }

/* ---------- Month nav / search ---------- */
.month-nav { display: flex; align-items: center; gap: 8px; margin: 12px 0 16px; flex-wrap: wrap; }
.month-nav .btn { padding: 8px 14px; font-size: 0.9rem; }
.month-nav input[type="month"], .month-nav input[type="date"] {
  font-size: 15px; font-family: inherit; padding: 8px 10px;
  border: 1px solid #cbd5e1; border-radius: var(--radius-sm); background: #fff; color: var(--ink);
}

.search {
  width: 100%; font-size: 16px; font-family: inherit; padding: 10px 14px; margin: 4px 0 12px;
  border: 1px solid #cbd5e1; border-radius: var(--radius);
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2393a1b3' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M21 21l-4.3-4.3'/%3E%3C/svg%3E") no-repeat 12px center;
  padding-left: 38px; transition: border-color 0.15s, box-shadow 0.15s;
}
.search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

/* ---------- Entry table ---------- */
.entry-table input[type="text"], .entry-table input[type="number"] {
  width: 92px; font-size: 16px; font-family: inherit; padding: 8px 8px;
  border: 1px solid #cbd5e1; border-radius: 7px; text-align: right; color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s; background: #fff;
}
.entry-table input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.entry-table input.note { width: 150px; text-align: left; }
.entry-table td.ro, .entry-table th.ro { color: var(--ink-faint); font-size: 0.84rem; }
.profit-pos { color: var(--success); font-weight: 700; }
.profit-neg { color: var(--danger); font-weight: 700; }

/* ---------- P&L ---------- */
.pnl { width: 100%; max-width: 580px; }
.pnl td:first-child { text-align: left; }
.pnl td { text-align: right; }
.pnl tr.sub td:first-child { padding-left: 26px; color: var(--ink-soft); }
.pnl tr.total td { font-size: 1.12rem; font-weight: 800; border-top: 2px solid var(--ink); }

/* ---------- Login ---------- */
.login-wrap { max-width: 400px; margin: 9vh auto 0; }
.login-wrap h1 { text-align: center; letter-spacing: -0.02em; }
.login-wrap .card { padding: 28px; }

/* ---------- Misc ---------- */
.badge {
  display: inline-block; padding: 3px 12px; border-radius: 999px;
  font-size: 0.78rem; font-weight: 700;
}
.badge.on { background: var(--success-soft); color: #146c46; }
.badge.off { background: #eef1f5; color: var(--ink-soft); }
.by { font-size: 11px; color: var(--ink-faint); margin-top: 3px; }

/* ลิงก์ทั่วไป */
a { color: var(--primary); }
a:hover { color: var(--primary-dark); }
