:root {
  --green-900: #14532d;
  --green-700: #15803d;
  --green-600: #16a34a;
  --green-100: #dcfce7;
  --red-600: #dc2626;
  --amber-600: #d97706;
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.08);
  font-size: 16px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Thai", "Sarabun", sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

#app { min-height: 100%; display: flex; flex-direction: column; }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

.header {
  position: sticky; top: 0; z-index: 20;
  background: var(--green-900);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
  display: flex; align-items: center; justify-content: space-between;
}
.header h1 { font-size: 1.05rem; margin: 0; font-weight: 600; }
.header .sub { font-size: 0.75rem; opacity: 0.85; }
.header button.icon-btn {
  background: rgba(255,255,255,0.15); border: none; color: #fff; border-radius: 999px;
  width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; font-size: 1rem;
}
.company-badge {
  background: rgba(255,255,255,0.15); color: #fff; border-radius: 999px;
  padding: 6px 12px; font-size: 0.75rem; white-space: nowrap; max-width: 40vw;
  overflow: hidden; text-overflow: ellipsis; flex-shrink: 0;
}

.main {
  flex: 1;
  padding: 16px 16px calc(84px + env(safe-area-inset-bottom));
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  background: #fff; border-top: 1px solid var(--border);
  display: flex; justify-content: space-around;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--muted); font-size: 0.6rem; padding: 4px 2px; border-radius: 10px; flex: 1;
  min-width: 0;
}
.bottom-nav a.active { color: var(--green-700); font-weight: 600; }
.bottom-nav .nav-icon { font-size: 1.05rem; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px;
}
.card h2 { margin: 0 0 10px; font-size: 1rem; }
.card h3 { margin: 0 0 8px; font-size: 0.9rem; color: var(--muted); font-weight: 600; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

.stat { text-align: center; }
.stat .value { font-size: 1.3rem; font-weight: 700; color: var(--green-900); }
.stat .label { font-size: 0.72rem; color: var(--muted); margin-top: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--green-700); color: #fff; border: none; border-radius: 10px;
  padding: 11px 16px; font-size: 0.9rem; font-weight: 600; cursor: pointer;
  width: 100%;
}
.btn.secondary { background: #fff; color: var(--green-900); border: 1px solid var(--green-700); }
.btn.danger { background: var(--red-600); }
.btn.ghost { background: transparent; color: var(--green-900); width: auto; padding: 8px 10px; }
.btn.small { width: auto; padding: 8px 12px; font-size: 0.82rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row .btn { width: auto; flex: 1; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  font-size: 0.95rem; background: #fff; color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--green-600); border-color: transparent; }
.field .hint { font-size: 0.72rem; color: var(--muted); margin-top: 4px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.list-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item .title { font-weight: 600; font-size: 0.92rem; }
.list-item .sub { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.list-item .amount { font-weight: 700; color: var(--green-900); font-size: 0.92rem; }

.tabs { display: flex; gap: 8px; margin-bottom: 14px; overflow-x: auto; }
.tab-btn {
  border: 1px solid var(--border); background: #fff; padding: 8px 14px; border-radius: 999px;
  font-size: 0.82rem; white-space: nowrap; color: var(--muted); cursor: pointer;
}
.tab-btn.active { background: var(--green-900); color: #fff; border-color: var(--green-900); }

table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
th, td { padding: 8px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; }
tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 0.7rem; font-weight: 600;
}
.badge.draft { background: #fef3c7; color: #92400e; }
.badge.final { background: var(--green-100); color: var(--green-900); }
.badge.active { background: var(--green-100); color: var(--green-900); }
.badge.inactive { background: #fee2e2; color: #991b1b; }

.empty-state { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty-state .emoji { font-size: 2rem; margin-bottom: 8px; }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: #fff; border-radius: 18px 18px 0 0; width: 100%; max-width: 560px;
  max-height: 90vh; overflow-y: auto; padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  animation: slideUp 0.2s ease-out;
}
@keyframes slideUp { from { transform: translateY(30px); opacity:0; } to { transform: translateY(0); opacity:1; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-header h2 { margin: 0; font-size: 1.05rem; }

.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: linear-gradient(160deg, var(--green-900), var(--green-700));
}
.login-card {
  background: #fff; border-radius: 20px; padding: 32px 24px; width: 100%; max-width: 360px;
  box-shadow: 0 20px 40px rgba(0,0,0,0.2);
}
.login-card .logo { font-size: 2.4rem; text-align: center; margin-bottom: 6px; }
.login-card h1 { text-align: center; font-size: 1.2rem; margin: 0 0 4px; }
.login-card p.sub { text-align: center; color: var(--muted); font-size: 0.85rem; margin: 0 0 20px; }

.toast {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 0.85rem;
  z-index: 60; box-shadow: var(--shadow); max-width: 90vw;
}
.toast.error { background: var(--red-600); }

.alert { padding: 10px 12px; border-radius: 10px; font-size: 0.82rem; margin-bottom: 12px; }
.alert.info { background: #eff6ff; color: #1e40af; }
.alert.warn { background: #fffbeb; color: #92400e; }

.payslip-doc { background: #fff; border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.payslip-doc .row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 0.88rem; border-bottom: 1px dashed var(--border); }
.payslip-doc .row.total { font-weight: 700; font-size: 1rem; border-bottom: none; border-top: 2px solid var(--text); margin-top: 6px; padding-top: 10px; }
.payslip-doc h2 { text-align: center; margin: 0 0 2px; }
.payslip-doc .company { text-align: center; color: var(--muted); font-size: 0.8rem; margin-bottom: 16px; }

@media print {
  .header, .bottom-nav, .no-print { display: none !important; }
  .main { padding: 0; max-width: 100%; }
  body { background: #fff; }
}

@media (min-width: 720px) {
  .main { padding-top: 24px; }
  .grid-3 { grid-template-columns: repeat(3, 1fr); }
}
