:root {
  --green-900: #1b3a1f;
  --green-700: #2e7d32;
  --green-600: #388e3c;
  --green-100: #e8f5e9;
  --gold-500: #f0a500;
  --gold-100: #fff4de;
  --gray-50: #f7f8f6;
  --gray-100: #eef1ee;
  --gray-300: #d6dcd6;
  --gray-500: #8a938a;
  --gray-700: #4a524a;
  --gray-900: #262b26;
  --red-600: #c0392b;
  --red-100: #fdecea;
  --blue-600: #2563eb;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(27, 58, 31, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: "Noto Sans Thai", "Sarabun", system-ui, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  /* Safety net for phone screens: dynamic content across this app
     (provider org names, UUID-ish demo claims, long combined titles like
     "org name — service label") can be long unbroken strings with no
     natural break point. Without this, a single long token can force
     horizontal scroll on a narrow viewport even though every layout below
     is otherwise responsive. */
  overflow-wrap: break-word;
}

a { color: inherit; }

/* ---------- Login page ---------- */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--green-900) 0%, var(--green-700) 55%, var(--gold-500) 150%);
  padding: 24px;
}

.login-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0,0,0,0.18);
  width: 100%;
  max-width: 420px;
  padding: 36px 32px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}

.login-brand .leaf { font-size: 28px; }

.login-brand h1 {
  font-size: 22px;
  margin: 0;
  color: var(--green-900);
}

.login-sub {
  color: var(--gray-700);
  font-size: 14px;
  margin: 4px 0 24px 0;
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 6px;
}

.field input, .field select, .field textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--gray-300);
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
  background: var(--gray-50);
}

.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--green-600);
  background: #fff;
}

.field .hint {
  font-size: 12px;
  color: var(--gray-500);
  margin-top: 5px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 9px;
  padding: 11px 18px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.05s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary { background: var(--green-700); color: #fff; width: 100%; }
.btn-primary:hover:not(:disabled) { background: var(--green-900); }

.btn-ghost {
  background: var(--gray-100);
  color: var(--gray-900);
}
.btn-ghost:hover:not(:disabled) { background: var(--gray-300); }

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--gray-500);
  font-size: 12px;
  margin: 22px 0 14px 0;
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--gray-300);
}

.demo-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.demo-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--gold-100);
  border: 1px solid #f0d59a;
  border-radius: 9px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
}
.demo-btn:hover { background: #ffe9bd; }
.demo-btn .name { font-weight: 600; color: var(--green-900); }
.demo-btn .claim { font-size: 12px; color: var(--gray-500); font-family: monospace; }

.error-box {
  background: var(--red-100);
  color: var(--red-600);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 16px;
  display: none;
}
.error-box.show { display: block; }

/* ---------- Dashboard shell ---------- */
.app-header {
  background: var(--green-900);
  color: #fff;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
}

.app-header .who {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 14px;
}

.app-header .who .name { font-weight: 600; }

/* Every dashboard-style page (15 of them) shares this exact header —
   .brand plus a row of .who links/buttons that can run to 6-8 items on
   the busiest pages (see frontend/dashboard.html). On a phone-width
   viewport that row has nowhere near enough space and would otherwise
   overflow off-screen or squash unreadably. Below ~860px it wraps onto
   as many lines as needed instead, and the header stops being sticky so
   a multi-row wrapped header doesn't permanently occupy a large chunk of
   a small screen while scrolling. */
@media (max-width: 860px) {
  .app-header {
    position: static;
    flex-wrap: wrap;
    padding: 12px 16px;
    row-gap: 10px;
  }
  .app-header .brand {
    flex: 1 1 100%;
    font-size: 15px;
  }
  .app-header .who {
    flex-wrap: wrap;
    width: 100%;
    gap: 8px;
    font-size: 13px;
  }
  .app-header .who .name {
    flex: 1 1 100%;
  }
}

.logout-btn {
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  border-radius: 7px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.logout-btn:hover { background: rgba(255,255,255,0.22); }

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 20px 60px 20px;
}

.section-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--green-900);
  margin: 30px 0 12px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-title:first-child { margin-top: 0; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}

.stat-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.stat-card .label {
  font-size: 12.5px;
  color: var(--gray-500);
  margin-bottom: 6px;
}
.stat-card .value {
  font-size: 24px;
  font-weight: 700;
  color: var(--green-900);
}
.stat-card .sub {
  font-size: 12px;
  color: var(--gray-700);
  margin-top: 4px;
}

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge.tier-A { background: #e6f4ea; color: #1b5e20; }
.badge.tier-B { background: #eef6e0; color: #558b2f; }
.badge.tier-C { background: var(--gold-100); color: #a06a00; }
.badge.tier-D { background: var(--red-100); color: var(--red-600); }

.badge.status-active, .badge.status-approved, .badge.status-converted, .badge.status-completed { background: #e6f4ea; color: #1b5e20; }
.badge.status-pending, .badge.status-manual_review, .badge.status-draft, .badge.status-pending_signature { background: var(--gold-100); color: #a06a00; }
.badge.status-declined, .badge.status-terminated, .badge.status-breached { background: var(--red-100); color: var(--red-600); }

.badge.sev-critical { background: var(--red-100); color: var(--red-600); }
.badge.sev-warning { background: var(--gold-100); color: #a06a00; }
.badge.sev-info { background: #e8f0fe; color: var(--blue-600); }

.card-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.item-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.item-card .row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.item-card .title { font-weight: 700; font-size: 14.5px; }
.item-card .detail-line {
  font-size: 13px;
  color: var(--gray-700);
  margin-top: 3px;
}
.item-card .muted { color: var(--gray-500); font-size: 12px; }

/* Cover photo on a browse-card (e.g. marketplace.html's ตลาดปัจจัยการผลิต) —
   pulled from marketplace.product_photo's oldest row per listing. Full-bleed
   at the top of the card, same object-fit/rounding convention as the
   Machinery Portal's .photo-card img gallery thumbnails below. */
.item-card .product-cover-photo {
  display: block;
  width: calc(100% + 36px);
  height: 150px;
  margin: -16px -18px 12px;
  object-fit: cover;
  background: var(--gray-300);
  border-radius: var(--radius) var(--radius) 0 0;
}

.empty-state {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  text-align: center;
  color: var(--gray-500);
  font-size: 14px;
}

.panel {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.form-grid .field.full { grid-column: 1 / -1; }

.factor-bar-track {
  background: var(--gray-100);
  border-radius: 6px;
  height: 8px;
  overflow: hidden;
  margin-top: 4px;
}
.factor-bar-fill {
  height: 100%;
  background: var(--green-600);
}
.factor-row { margin-bottom: 12px; }
.factor-row .factor-label {
  display: flex;
  justify-content: space-between;
  font-size: 12.5px;
  color: var(--gray-700);
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--green-900);
  color: #fff;
  padding: 12px 20px;
  border-radius: 9px;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 50;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--red-600); }

.loading-line {
  color: var(--gray-500);
  font-size: 13px;
  padding: 10px 0;
}

.two-col {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 16px;
}

@media (max-width: 760px) {
  .form-grid { grid-template-columns: 1fr; }
  .two-col { grid-template-columns: 1fr; }
}

/* ---------- Lender Portal: review-queue action controls ---------- */
.btn-sm {
  width: auto;
  padding: 8px 16px;
  font-size: 13px;
}

.btn-approve { background: var(--green-700); color: #fff; }
.btn-approve:hover:not(:disabled) { background: var(--green-900); }

.btn-decline { background: var(--red-600); color: #fff; }
.btn-decline:hover:not(:disabled) { opacity: 0.85; }

.action-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.action-row input[type="number"],
.action-row input[type="text"] {
  flex: 1;
  min-width: 140px;
  padding: 8px 10px;
  border: 1px solid var(--gray-300);
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  margin-bottom: 14px;
}

/* ---------- Machinery/Drying-Yard Portal: photo gallery ---------- */
.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
}

.photo-card {
  position: relative;
  border: 1px solid var(--gray-300);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
}

.photo-card img {
  width: 100%;
  height: 130px;
  object-fit: cover;
  display: block;
  background: var(--gray-300);
}

.photo-card .photo-meta {
  padding: 8px 10px;
}

.photo-card .photo-caption {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--gray-700);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.photo-card .photo-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.photo-card .photo-remove:hover { background: var(--red-600); }

@media (max-width: 760px) {
  .photo-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
}
.filter-bar .field { margin-bottom: 0; }

/* ---------- Small phone screens ---------- */
@media (max-width: 480px) {
  .login-page { padding: 16px; }
  .login-card { padding: 28px 20px; }
  .container { padding: 16px 14px 48px 14px; }
  .section-title { font-size: 15px; margin: 24px 0 10px 0; }
  .stat-card { padding: 14px; }
  .stat-card .value { font-size: 20px; }
  .panel { padding: 16px; }
}
