:root {
  --green: #2e7d32;
  --green-dark: #1b5e20;
  --green-light: #e8f5e9;
  --yellow: #f9a825;
  --bg: #f4f6f4;
  --card: #ffffff;
  --text: #1f2a1f;
  --muted: #6b756b;
  --border: #d9e0d9;
  --danger: #c62828;
  --subtotal: #fff8e1;
  --field-bg: #ffffff;   /* พื้นช่องกรอก/ปุ่ม */
  --deep: #1b5e20;       /* พื้นเขียวเข้ม (แถบสรุป/footer ตาราง) */
  --radius: 14px;
  --shadow: 0 2px 8px rgba(0,0,0,.06);
}

/* ===== Dark theme ===== */
html[data-theme="dark"] {
  --green: #43a047;
  --green-dark: #8ed694;   /* ใช้เป็นสีอักษรเน้น ต้องสว่างบนพื้นมืด */
  --green-light: #21331f;  /* พื้นอ่อน → เขียวเข้ม */
  --yellow: #ffca28;
  --bg: #10130f;
  --card: #1c211b;
  --text: #e7eee6;
  --muted: #9aa69a;
  --border: #374237;
  --danger: #ef5350;
  --subtotal: #2c2913;
  --field-bg: #252c24;
  --deep: #16401b;
  --shadow: 0 2px 8px rgba(0,0,0,.4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  color-scheme: light;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html[data-theme="dark"] { color-scheme: dark; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Sarabun', -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Thonburi", system-ui, sans-serif;
  font-size: 16px;
}

/* หัวข้อ / ปุ่ม / ตัวเลขเด่น ใช้ Kanit ให้ดูเป็นแบรนด์ */
.app-header h1, .history-toolbar h2, .modal h3,
.step-title, .sg-title, .recent-title, .grade-block-head,
.grade-btn, .active-grade, .big-input, .unit,
.tab, .seg-btn, .btn, .gs-net, .sum-line.net,
.tag.grand, .tag.net, .price-table tfoot td, tfoot td {
  font-family: 'Kanit', 'Sarabun', sans-serif;
}

/* reset ช่องกรอก/ปุ่ม กัน iOS Safari ใส่สไตล์ default (กรอบมน เงาใน สีเทา) */
input, button, textarea, select {
  font-family: inherit;
  font-size: inherit;
  -webkit-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  border-radius: 0;
}
button { touch-action: manipulation; cursor: pointer; }
input { background-clip: padding-box; }
body { padding-bottom: calc(env(safe-area-inset-bottom) + 20px); max-width: 720px; margin: 0 auto; }

.app-header {
  background: linear-gradient(135deg, var(--green), var(--deep));
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 20;
}
.app-title { display: flex; flex-direction: column; gap: 1px; }
.app-header h1 { font-size: 1.15rem; margin: 0; font-weight: 700; line-height: 1.2; }
.app-brand { font-size: .72rem; font-weight: 500; opacity: .85; font-family: 'Kanit','Sarabun',sans-serif; }
.header-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.install-btn {
  background: rgba(255,255,255,.2); color: #fff;
  border: 1px solid rgba(255,255,255,.5); border-radius: 20px;
  padding: 6px 14px; font-size: .85rem; cursor: pointer;
}
/* slider สลับธีม สว่าง/มืด */
.theme-switch { position: relative; display: inline-flex; align-items: center; cursor: pointer; flex: 0 0 auto; }
.theme-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.ts-track {
  position: relative; width: 58px; height: 30px; border-radius: 30px;
  background: rgba(0,0,0,.22); border: 1px solid rgba(255,255,255,.55);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 7px; transition: background .2s;
}
.ts-ico { font-size: .8rem; line-height: 1; z-index: 1; }
.ts-thumb {
  position: absolute; top: 2px; left: 2px; width: 24px; height: 24px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.35);
  transition: transform .2s;
}
.theme-switch input:checked + .ts-track { background: rgba(0,0,0,.45); }
.theme-switch input:checked + .ts-track .ts-thumb { transform: translateX(28px); }
.theme-switch input:focus-visible + .ts-track { outline: 2px solid #fff; }

.tabs { display: flex; background: var(--card); position: sticky; top: 0; z-index: 15; box-shadow: var(--shadow); }
.tab { flex: 1; border: none; background: none; padding: 14px; font-size: 1rem; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 3px solid transparent; }
.tab.active { color: var(--green); border-bottom-color: var(--green); }
.badge { display: inline-block; min-width: 20px; padding: 1px 6px; background: var(--green); color: #fff; border-radius: 10px; font-size: .75rem; }

.tab-panel { display: none; padding: 14px; }
.tab-panel.active { display: block; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; min-width: 0; }
/* iOS date/time ให้สูงเท่าช่องอื่นและชิดซ้าย */
input[type=date], input[type=time] { min-height: 46px; line-height: normal; }
label { font-size: .85rem; color: var(--muted); font-weight: 600; }

input[type=text], input[type=date], input[type=time], input[type=number] {
  border: 1.5px solid var(--border); border-radius: 10px; padding: 12px;
  font-size: 1rem; width: 100%; background: var(--field-bg); color: var(--text);
}
input:focus { outline: none; border-color: var(--green); }

/* ===== step titles ===== */
.step-title { font-weight: 700; font-size: 1rem; margin-bottom: 14px; display: flex; align-items: center; gap: 8px; }
.step-no {
  display: inline-flex; align-items: center; justify-content: center;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--green); color: #fff; font-size: .85rem;
}
.active-grade { color: var(--green); font-weight: 800; }

/* ===== STEP 1 grade picker ===== */
.grade-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.grade-btn {
  min-width: 0;
  position: relative;
  border: 2px solid var(--border);
  background: var(--field-bg);
  color: var(--text);
  border-radius: 12px;
  padding: 16px 10px;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
}
.grade-btn .count { display: block; font-size: .75rem; color: var(--muted); font-weight: 600; margin-top: 2px; }
.grade-btn.active { border-color: var(--green); background: var(--green-light); color: var(--green-dark); }
.grade-btn.active .count { color: var(--green); }
.grade-btn .rm-grade {
  position: absolute; top: 4px; right: 6px;
  border: none; background: none; color: var(--muted);
  font-size: 1rem; cursor: pointer; opacity: .6; line-height: 1;
}
.chip-add {
  border: 1.5px dashed var(--green); background: var(--field-bg); color: var(--green);
  border-radius: 16px; padding: 8px 14px; font-size: .9rem; font-weight: 600; cursor: pointer;
}
.add-grade-form { display: flex; gap: 8px; margin-top: 10px; align-items: center; }
.add-grade-form[hidden] { display: none; }
.add-grade-form input { flex: 1; }

/* ===== STEP 2 big input ===== */
.weigh-card { background: linear-gradient(180deg, var(--card), var(--green-light)); }
.weigh-row { display: flex; gap: 10px; align-items: stretch; width: 100%; }
/* กล่องครอบช่องกรอก + หน่วย ให้กรอบเดียวกัน อยู่ในการ์ดเสมอ */
.big-input-wrap {
  flex: 1 1 auto; min-width: 0;
  display: flex; align-items: center;
  background: var(--field-bg); border: 2px solid var(--green); border-radius: 14px;
  padding: 0 12px;
}
.big-input {
  flex: 1 1 auto; min-width: 0; width: 100%;
  font-size: 2.2rem !important; font-weight: 800;
  text-align: center; padding: 14px 4px !important;
  border: none !important; background: transparent; color: var(--green-dark);
}
.unit { flex: 0 0 auto; font-size: 1.1rem; font-weight: 700; color: var(--muted); }
.add-weight-btn {
  flex: 0 0 auto; width: auto; padding: 8px 22px;
  font-size: 1.1rem; white-space: nowrap;
  display: flex; align-items: center; justify-content: center;
}

/* ช่องแสดงผลล่าสุด (recheck) */
.recent-title { font-weight: 700; color: var(--green-dark); margin-bottom: 10px; font-size: .95rem; }
.recent-list { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.recent-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px; border: 1.5px solid var(--border); border-radius: 12px;
  background: var(--field-bg); font-size: 0.9rem;
}
.recent-item.latest {
  grid-column: span 2;
  border-color: var(--green);
  background: var(--green-light);
  padding: 10px 14px;
}
.recent-item .ri-info { display: flex; align-items: center; gap: 8px; }
.recent-item .ri-meta { display: flex; align-items: center; gap: 8px; }
.recent-item .ri-grade { font-weight: 800; }
.recent-item .ri-w { font-size: 1.05rem; font-weight: 800; color: var(--green-dark); }
.recent-item .ri-time { font-size: .75rem; color: var(--muted); }
.recent-item .ri-badge { font-size: .68rem; background: var(--green); color: #fff; padding: 2px 8px; border-radius: 10px; font-weight: 600; }
.recent-item .del-row { flex: 0 0 auto; padding: 2px; font-size: 1.1rem; color: var(--danger); border: none; background: none; cursor: pointer; line-height: 1; }

/* ===== STEP 3 grade tables ===== */
.grade-block { margin-bottom: 18px; }
.grade-block:last-child { margin-bottom: 0; }
.grade-block-head {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--green); color: #fff; padding: 8px 12px;
  border-radius: 10px 10px 0 0; font-weight: 700;
}
.grade-block-head .gb-total { font-size: .95rem; }
.grade-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  border: 1.5px solid var(--border);
  border-top: none;
  border-radius: 0 0 10px 10px;
}
.gb-table {
  width: 100%;
  min-width: max(100%, calc(var(--cols, 5) * 70px));
  border-collapse: collapse;
  table-layout: fixed;
}
.gb-table th, .gb-table td {
  padding: 10px 4px;
  border: 1px solid var(--border);
  text-align: center;
  font-size: 0.92rem;
  width: calc(100% / var(--cols, 5));
}
.gb-table thead th {
  background: var(--green-light);
  color: var(--green-dark);
  font-size: 0.85rem;
  font-weight: 700;
  border-top: none;
}
.gb-table thead th:first-child { border-left: none; }
.gb-table thead th:last-child { border-right: none; }
.gb-table tbody td:first-child { border-left: none; }
.gb-table tbody td:last-child { border-right: none; }
.gb-table tr.sub td {
  background: var(--subtotal);
  font-weight: 700;
  color: var(--green-dark);
  border-top: 2px solid var(--yellow);
  border-bottom: none;
}
.gb-table tr.sub td:first-child { border-left: none; }
.gb-table tr.sub td:last-child { border-right: none; }
.gb-table td.weigh-cell.clickable {
  cursor: pointer;
  font-weight: 700;
  transition: background-color 0.15s;
}
.gb-table td.weigh-cell.clickable:hover {
  background-color: #ffebee;
  color: var(--danger);
}
html[data-theme="dark"] .gb-table td.weigh-cell.clickable:hover {
  background-color: #3e2723;
  color: var(--danger);
}
.del-row { border: none; background: none; color: var(--danger); font-size: 1.05rem; cursor: pointer; padding: 2px 6px; }
.empty-block { text-align: center; color: var(--muted); padding: 24px 10px; font-size: .9rem; }

/* ===== STEP 4 price table ===== */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.price-table { width: 100%; border-collapse: collapse; }
.price-table th, .price-table td { padding: 10px 8px; text-align: center; font-size: .92rem; border-bottom: 1px solid var(--border); }
.price-table thead th { background: var(--green); color: #fff; font-weight: 700; }
.price-table td.gname { text-align: left; font-weight: 700; }
.price-table input.price {
  width: 84px; text-align: center; padding: 8px 4px; font-size: 1rem;
  border: 1.5px solid var(--border); border-radius: 8px;
}
.price-table input.price:focus { outline: none; border-color: var(--green); background: var(--green-light); }
.price-table td.money { font-weight: 700; color: var(--green-dark); white-space: nowrap; }
.price-table tfoot td { background: var(--deep); color: #fff; font-weight: 800; }
.price-table tfoot td.r { text-align: right; }

/* ===== STEP 5 expenses ===== */
.expense-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.expense-row { display: flex; gap: 8px; align-items: center; }
/* ช่องค่าใช้จ่าย — สไตล์เดียวกับช่อง input อื่นในแอป */
.expense-row input {
  border: 1.5px solid var(--border); border-radius: 10px; padding: 12px;
  font-size: 1rem; background: var(--field-bg); color: var(--text);
}
.expense-row input:focus { outline: none; border-color: var(--green); }
.expense-row input.exp-name { flex: 1; min-width: 0; }
.exp-amt-wrap { position: relative; width: 130px; flex: none; }
.exp-amt-wrap input.exp-amt { width: 100%; text-align: right; padding-right: 30px; }
.exp-unit { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-weight: 700; pointer-events: none; }
.expense-row .del-row { flex: none; }

/* ===== summary ===== */
.summary-card { background: var(--deep); color: #fff; }
.sum-line { display: flex; justify-content: space-between; padding: 6px 0; font-size: 1.05rem; }
.sum-line.minus { color: #ffcdd2; }
.sum-line.net { border-top: 2px solid rgba(255,255,255,.3); margin-top: 6px; padding-top: 12px; font-size: 1.4rem; font-weight: 800; }

/* ===== action bar ===== */
.action-bar { display: flex; gap: 12px; }
.btn { flex: 1; padding: 16px; border-radius: 12px; font-size: 1.05rem; font-weight: 700; border: none; cursor: pointer; }
.btn-sm { padding: 8px 14px; font-size: .85rem; flex: none; }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:active { background: var(--deep); }
.btn-ghost { background: var(--card); color: var(--muted); border: 1.5px solid var(--border); }

/* ===== history ===== */
.history-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.history-toolbar h2 { margin: 0; font-size: 1.1rem; }
/* segmented view toggle */
.seg { display: flex; background: var(--green-light); border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.seg-btn { flex: 1; border: none; background: none; padding: 10px; font-size: .9rem; font-weight: 700; color: var(--green-dark); border-radius: 9px; cursor: pointer; }
.seg-btn.active { background: var(--green); color: #fff; box-shadow: var(--shadow); }

/* grand summary banner */
.grand-summary { margin-bottom: 12px; }
.grand-summary:empty { display: none; }
.gs-banner { background: var(--deep); color: #fff; border-radius: 12px; padding: 12px 16px; display: flex; justify-content: space-between; align-items: center; }
.gs-banner .gs-label { font-size: .85rem; opacity: .85; }
.gs-banner .gs-net { font-size: 1.3rem; font-weight: 800; }
.gs-banner .gs-sub { font-size: .78rem; opacity: .8; margin-top: 2px; }

/* grouped summary cards (day / orchard) */
.sum-group { border: 1.5px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; }
.sg-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.sg-title { font-weight: 800; font-size: 1.05rem; }
.sg-sub { font-size: .82rem; color: var(--muted); margin-top: 4px; }
.sg-money { display: flex; gap: 14px; margin-top: 8px; flex-wrap: wrap; font-size: .85rem; }
.sg-money b { color: var(--green-dark); }
.sg-money .neg { color: var(--danger); }

.history-list { display: flex; flex-direction: column; gap: 10px; }
.history-item { border: 1.5px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.history-item, .grade-block, .sum-group {
  content-visibility: auto;
  contain-intrinsic-size: 160px;
}
.history-item .hi-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.history-item .hi-orchard { font-weight: 700; font-size: 1.02rem; }
.history-item .hi-time { font-size: .8rem; color: var(--muted); }
.history-item .hi-totals { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.tag { background: var(--green-light); color: var(--green-dark); border-radius: 8px; padding: 3px 8px; font-size: .8rem; font-weight: 600; }
.tag.grand { background: var(--green); color: #fff; }
.tag.net { background: var(--yellow); color: #4e342e; }
.hi-actions { display: flex; gap: 8px; margin-top: 10px; }
.hi-actions button { border: none; background: none; font-size: .85rem; font-weight: 600; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.hi-del { color: var(--danger); }
.hi-view { color: var(--green); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* ===== แบนเนอร์โฆษณา (carousel) ===== */
.ad-banner { margin-top: 16px; }
.ad-banner[hidden] { display: none; }
.ad-track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  border-radius: 14px;
  scrollbar-width: none;
  box-shadow: var(--shadow);
}
.ad-track::-webkit-scrollbar { display: none; }
.ad-slide {
  flex: 0 0 100%;
  scroll-snap-align: start;
  min-height: 120px;
  border-radius: 14px;
  display: flex;
  align-items: flex-end;
  padding: 16px 18px;
  color: #fff;
  text-decoration: none;
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}
.ad-slide::after { content: ''; position: absolute; inset: 0; background: linear-gradient(180deg, rgba(0,0,0,0) 40%, rgba(0,0,0,.45)); }
.ad-content { position: relative; z-index: 1; }
.ad-title { font-family: 'Kanit','Sarabun',sans-serif; font-weight: 700; font-size: 1.2rem; line-height: 1.25; }
.ad-sub { font-size: .85rem; opacity: .95; margin-top: 3px; }
.ad-tag { position: absolute; top: 10px; left: 12px; z-index: 1; background: rgba(0,0,0,.35); color: #fff; font-size: .65rem; padding: 2px 8px; border-radius: 10px; }
.ad-dots { display: flex; justify-content: center; gap: 6px; margin-top: 10px; }
.ad-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--border); cursor: pointer; transition: width .2s, background .2s; }
.ad-dot.active { background: var(--green); width: 18px; border-radius: 4px; }

/* ===== footer เวอร์ชัน ===== */
.app-footer {
  text-align: center;
  color: var(--muted);
  font-size: .78rem;
  padding: 16px 12px calc(env(safe-area-inset-bottom) + 16px);
}

/* ===== toast ===== */
.toast { position: fixed; bottom: calc(env(safe-area-inset-bottom) + 20px); left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 12px 20px; border-radius: 24px; font-size: .9rem; z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,.25); animation: pop .2s ease; }
@keyframes pop { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ===== modal ===== */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: flex-end; justify-content: center; z-index: 90; }
.modal { background: var(--card); width: 100%; max-width: 720px; border-radius: 18px 18px 0 0; padding: 18px; max-height: 85vh; overflow-y: auto; }
.modal h3 { margin: 0 0 4px; }
.modal .close-modal { width: 100%; margin-top: 14px; padding: 14px; border: none; background: var(--green); color: #fff; border-radius: 12px; font-weight: 700; font-size: 1rem; cursor: pointer; }
.ios-steps { padding-left: 20px; margin: 12px 0; line-height: 1.7; }
.ios-steps li { margin-bottom: 8px; }
.ios-steps b { color: var(--green-dark); }
.share-ico { display: inline-block; vertical-align: -2px; }
.modal .m-sum { margin-top: 12px; }
.modal .m-sum .sum-line { color: var(--text); }
.modal .m-sum .sum-line.net { color: var(--green-dark); border-top-color: var(--border); }

/* ===== SEO Landing Section ===== */
.seo-landing-section {
  margin: 16px 14px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  padding: 20px;
  line-height: 1.6;
}
.seo-hero {
  margin-bottom: 14px;
}
.seo-title {
  font-size: 1.1rem;
  color: var(--green-dark);
  margin: 0 0 10px 0;
  font-weight: 700;
  line-height: 1.3;
}
.seo-lead {
  font-size: 0.9rem;
  color: var(--text);
  opacity: 0.85;
  margin: 0;
}
.seo-divider {
  border: none;
  border-top: 1.5px solid var(--border);
  margin: 18px 0;
}
.seo-grid {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.seo-col h2, .seo-faq h2 {
  font-family: 'Kanit', 'Sarabun', sans-serif;
  font-size: 1rem;
  color: var(--green-dark);
  margin: 0 0 10px 0;
  font-weight: 700;
}
.seo-col p {
  font-size: 0.88rem;
  color: var(--text);
  opacity: 0.85;
  margin: 0;
}
.seo-features {
  margin: 0;
  padding-left: 20px;
  font-size: 0.88rem;
}
.seo-features li {
  margin-bottom: 6px;
  color: var(--text);
  opacity: 0.9;
}
.seo-features li strong {
  color: var(--green-dark);
}
.seo-faq {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.faq-item {
  border-bottom: 1px dashed var(--border);
  padding-bottom: 8px;
}
.faq-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}
.faq-item h3 {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
  margin: 0 0 6px 0;
}
.faq-item p {
  font-size: 0.88rem;
  color: var(--text);
  opacity: 0.85;
  margin: 0 0 8px 0;
}

@media (min-width: 576px) {
  .seo-grid {
    flex-direction: row;
    gap: 24px;
  }
  .seo-col {
    flex: 1;
  }
}
