/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: 'Sarabun', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg); color: var(--text); line-height: 1.5;
  padding-bottom: 72px; /* space for bottom nav */
  min-height: 100dvh;
}

/* ===== CSS Variables ===== */
:root {
  --primary: #0f766e; --primary-light: #14b8a6; --primary-bg: #f0fdfa;
  --pass: #16a34a; --pass-bg: #dcfce7;
  --fail: #dc2626; --fail-bg: #fee2e2;
  --warn: #d97706; --warn-bg: #fef3c7;
  --na: #6b7280; --na-bg: #f3f4f6;
  --critical: #991b1b;
  --bg: #f1f5f9; --surface: #fff; --border: #e2e8f0;
  --text: #1e293b; --text2: #64748b; --text3: #94a3b8;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --radius: 10px;
  --cat-q: #2563eb; --cat-s: #0f766e; --cat-c: #d97706; --cat-t: #7c3aed;
  --cat-cross: #2563eb; --cat-foh: #0f766e; --cat-atm: #7c3aed; --cat-com: #d97706;
  --cat-bar: #2563eb; --cat-kitchen: #d97706; --cat-recipe: #0f766e; --cat-hygiene: #7c3aed;
}

/* ===== Typography ===== */
h1 { font-size: 1.25rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
a { color: var(--primary); text-decoration: none; }

/* ===== Layout ===== */
.container { max-width: 600px; margin: 0 auto; padding: 0 12px; }
.view { display: none; }
.view.active { display: block; }

/* ===== Header ===== */
.app-header {
  background: var(--primary); color: #fff;
  padding: 14px 16px; display: flex; align-items: center; gap: 10px;
  position: sticky; top: 0; z-index: 50;
}
.app-header h1 { font-size: 1.1rem; font-weight: 600; flex: 1; }
.header-user { font-size: .8rem; opacity: .85; }

/* ===== Bottom Nav ===== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; z-index: 50;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-btn {
  flex: 1; border: none; background: none; padding: 8px 4px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: .7rem; color: var(--text3); cursor: pointer;
  font-family: inherit; transition: color .15s;
}
.nav-btn .ic { font-size: 1.3rem; }
.nav-btn.active { color: var(--primary); font-weight: 600; }
.nav-btn .nav-badge {
  position: absolute; top: 4px; right: calc(50% - 18px);
  background: var(--fail); color: #fff; font-size: .6rem;
  min-width: 16px; height: 16px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 4px;
}
.nav-btn { position: relative; }

/* ===== Cards ===== */
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px;
}
.card-title { font-weight: 600; margin-bottom: 8px; }
.card-muted { color: var(--text2); font-size: .85rem; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 20px; border-radius: 8px; border: none; cursor: pointer;
  font-family: inherit; font-size: .95rem; font-weight: 500;
  transition: opacity .15s, transform .1s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-danger { background: var(--fail); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: .85rem; }
.btn-lg { padding: 14px 24px; font-size: 1.05rem; }

/* ===== Home Audit Type Cards ===== */
.audit-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px 20px; margin-bottom: 16px;
  cursor: pointer; display: flex; align-items: center; gap: 16px;
  border-left: 5px solid var(--primary);
  transition: transform .1s, box-shadow .15s;
}
.audit-card:active { transform: scale(.98); }
.audit-card .ac-icon { font-size: 2rem; }
.audit-card .ac-body h2 { margin-bottom: 4px; }
.audit-card .ac-body p { color: var(--text2); font-size: .85rem; }
.audit-card.taste { border-left-color: var(--cat-t); }
.audit-card.chef { border-left-color: var(--cat-bar); }

/* ===== Branch Picker ===== */
.branch-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 10px; margin: 16px 0;
}
.branch-btn {
  background: var(--surface); border: 2px solid var(--border);
  border-radius: var(--radius); padding: 16px 12px; text-align: center;
  cursor: pointer; font-family: inherit; font-size: 1rem; font-weight: 500;
  transition: border-color .15s, background .15s;
}
.branch-btn:active, .branch-btn.selected { border-color: var(--primary); background: var(--primary-bg); }

/* ===== Category Headers ===== */
.cat-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; margin: 16px 0 8px; border-radius: 8px;
  font-weight: 600; font-size: .95rem; color: #fff;
}
.cat-header.cat-quality { background: var(--cat-q); }
.cat-header.cat-service { background: var(--cat-s); }
.cat-header.cat-cleanliness { background: var(--cat-c); }
.cat-header.cat-taste { background: var(--cat-t); }
.cat-header.cat-cross { background: var(--cat-cross); }
.cat-header.cat-foh { background: var(--cat-foh); }
.cat-header.cat-atm { background: var(--cat-atm); }
.cat-header.cat-com { background: var(--cat-com); }
.cat-header.cat-bar { background: var(--cat-bar); }
.cat-header.cat-kitchen { background: var(--cat-kitchen); }
.cat-header.cat-recipe { background: var(--cat-recipe); }
.cat-header.cat-hygiene { background: var(--cat-hygiene); }

/* ===== Audit Item Cards ===== */
.audit-item {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px; margin-bottom: 10px;
  border-left: 4px solid var(--border);
  transition: border-color .15s;
}
.audit-item.scored-pass { border-left-color: var(--pass); }
.audit-item.scored-fail { border-left-color: var(--fail); }
.audit-item.scored-na { border-left-color: var(--na); }
.audit-item .item-text { font-size: .9rem; margin-bottom: 8px; line-height: 1.4; }
.audit-item .critical-tag {
  display: inline-block; background: var(--fail-bg); color: var(--critical);
  font-size: .7rem; font-weight: 600; padding: 2px 6px; border-radius: 4px;
  margin-left: 6px; vertical-align: middle;
}
.score-btns { display: flex; gap: 8px; }
.score-btn {
  flex: 1; padding: 10px 0; border-radius: 8px; border: 2px solid var(--border);
  background: var(--surface); cursor: pointer; font-family: inherit;
  font-size: .85rem; font-weight: 600; transition: all .15s;
  min-height: 44px;
}
.score-btn:active { transform: scale(.95); }
.score-btn.s-pass { color: var(--pass); }
.score-btn.s-fail { color: var(--fail); }
.score-btn.s-na   { color: var(--na); }
.score-btn.s-pass.active { background: var(--pass); color: #fff; border-color: var(--pass); }
.score-btn.s-fail.active { background: var(--fail); color: #fff; border-color: var(--fail); }
.score-btn.s-na.active   { background: var(--na); color: #fff; border-color: var(--na); }

.fail-detail {
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border);
  display: none;
}
.audit-item.scored-fail .fail-detail { display: block; }
.fail-detail textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 6px;
  padding: 8px; font-family: inherit; font-size: .85rem; resize: vertical;
  min-height: 60px;
}
.fail-detail .photo-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.fail-detail .photo-preview {
  width: 64px; height: 64px; border-radius: 8px; object-fit: cover;
  border: 1px solid var(--border); cursor: pointer;
}

/* ===== Lightbox ===== */
.lightbox-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85); z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out; animation: fadeIn 0.15s ease;
}
.lightbox-img {
  max-width: 92vw; max-height: 90vh; border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5); object-fit: contain;
}

/* ===== Sticky Score Bar ===== */
.score-bar {
  position: sticky; bottom: 60px; left: 0; right: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 10px 16px; display: flex; align-items: center; gap: 12px;
  z-index: 40; box-shadow: 0 -2px 8px rgba(0,0,0,.06);
}
.score-bar .score-counts { flex: 1; display: flex; gap: 12px; font-size: .85rem; font-weight: 500; }
.score-bar .sc-pass { color: var(--pass); }
.score-bar .sc-fail { color: var(--fail); }
.score-bar .sc-left { color: var(--text3); }

/* ===== Menu Item Picker (Taste) ===== */
.menu-search {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 8px; font-family: inherit; font-size: .95rem;
  margin-bottom: 12px;
}
.menu-list { max-height: 50vh; overflow-y: auto; }
.menu-item {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  background: var(--surface); border-radius: var(--radius); margin-bottom: 8px;
  box-shadow: var(--shadow); cursor: pointer;
  transition: background .15s;
}
.menu-item.selected { background: var(--primary-bg); }
.menu-item .mi-check {
  width: 22px; height: 22px; border: 2px solid var(--border);
  border-radius: 6px; flex-shrink: 0; display: flex;
  align-items: center; justify-content: center;
}
.menu-item.selected .mi-check {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.menu-item .mi-name { font-weight: 500; }
.menu-item .mi-cat { font-size: .8rem; color: var(--text2); }

/* ===== Badges ===== */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 12px;
  font-size: .75rem; font-weight: 600; white-space: nowrap;
}
.badge-pass { background: var(--pass-bg); color: var(--pass); }
.badge-fail { background: var(--fail-bg); color: var(--fail); }
.badge-open { background: var(--warn-bg); color: var(--warn); }
.badge-progress { background: #dbeafe; color: #2563eb; }
.badge-done { background: var(--pass-bg); color: var(--pass); }
.badge-verified { background: var(--primary-bg); color: var(--primary); }
.badge-overdue { background: var(--fail-bg); color: var(--critical); }
.badge-default { background: var(--na-bg); color: var(--na); }

/* ===== Result Screen ===== */
.result-banner {
  text-align: center; padding: 32px 20px; border-radius: var(--radius);
  margin-bottom: 20px;
}
.result-banner.is-pass { background: var(--pass-bg); color: var(--pass); }
.result-banner.is-fail { background: var(--fail-bg); color: var(--fail); }
.result-banner .rb-icon { font-size: 3rem; }
.result-banner .rb-text { font-size: 1.3rem; font-weight: 700; margin-top: 8px; }
.result-banner .rb-sub { font-size: .9rem; margin-top: 4px; opacity: .8; }

/* ===== Stats Row ===== */
.stats-row { display: flex; gap: 10px; margin-bottom: 16px; }
.stat-card {
  flex: 1; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px; text-align: center;
}
.stat-card .stat-num { font-size: 1.5rem; font-weight: 700; }
.stat-card .stat-label { font-size: .75rem; color: var(--text2); }
.stat-card.st-pass .stat-num { color: var(--pass); }
.stat-card.st-fail .stat-num { color: var(--fail); }
.stat-card.st-warn .stat-num { color: var(--warn); }

/* ===== List Items ===== */
.list-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 8px; cursor: pointer;
  transition: background .1s;
}
.list-item:active { background: var(--bg); }
.list-item .li-body { flex: 1; min-width: 0; }
.list-item .li-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item .li-sub { font-size: .8rem; color: var(--text2); }
.list-item .li-right { text-align: right; flex-shrink: 0; }

/* ===== Action Cards ===== */
.action-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 10px;
  border-left: 4px solid var(--warn);
}
.action-card.st-done { border-left-color: var(--pass); }
.action-card.st-verified { border-left-color: var(--primary); }
.action-card.st-overdue { border-left-color: var(--fail); }
.action-card .ac-issue { font-weight: 500; margin-bottom: 6px; }
.action-card .ac-meta { font-size: .8rem; color: var(--text2); display: flex; flex-wrap: wrap; gap: 8px; }

/* ===== Forms ===== */
.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: 4px; color: var(--text2); }
.form-input, .form-select, .form-textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-family: inherit; font-size: .95rem;
  background: var(--surface);
}
.form-textarea { resize: vertical; min-height: 70px; }
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,.12);
}

/* ===== Table (admin) ===== */
.data-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.data-table th {
  background: var(--bg); padding: 8px 10px; text-align: left;
  font-weight: 600; white-space: nowrap; position: sticky; top: 0;
}
.data-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); }
.data-table tr:hover td { background: var(--primary-bg); }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 4px; overflow-x: auto; margin-bottom: 16px; padding: 2px 0; }
.tab-btn {
  padding: 8px 16px; border-radius: 20px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-family: inherit;
  font-size: .85rem; white-space: nowrap; transition: all .15s;
}
.tab-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ===== Sub-header / Section Title ===== */
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  margin: 20px 0 12px; padding-bottom: 6px; border-bottom: 2px solid var(--border);
}
.section-title h2 { font-size: 1rem; }
.back-btn {
  background: none; border: none; cursor: pointer; font-family: inherit;
  font-size: .9rem; color: var(--primary); display: flex; align-items: center; gap: 4px;
}

/* ===== Toast ===== */
#toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  padding: 10px 20px; border-radius: 8px; font-size: .85rem; font-weight: 500;
  z-index: 200; opacity: 0; transition: opacity .3s; pointer-events: none;
  background: var(--text); color: #fff;
}
#toast.show { opacity: 1; }
#toast.success { background: var(--pass); }
#toast.error { background: var(--fail); }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px;
}
.modal-box {
  background: var(--surface); border-radius: var(--radius);
  width: 100%; max-width: 500px; max-height: 85vh; overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-close { background: none; border: none; font-size: 1.2rem; cursor: pointer; color: var(--text2); }
.modal-body { padding: 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* ===== Empty State ===== */
.empty-state {
  text-align: center; padding: 40px 20px; color: var(--text3);
}
.empty-state .es-icon { font-size: 2.5rem; margin-bottom: 8px; }
.empty-state p { font-size: .9rem; }

/* ===== Loading ===== */
.loading { text-align: center; padding: 40px; color: var(--text3); }
.spinner {
  width: 32px; height: 32px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%;
  animation: spin .6s linear infinite; margin: 0 auto 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Admin Header ===== */
.admin-header {
  background: var(--primary); color: #fff;
  padding: 16px 24px; display: flex; align-items: center; gap: 12px;
}
.admin-header h1 { font-size: 1.2rem; }
.admin-nav {
  display: flex; gap: 4px; background: var(--surface);
  border-bottom: 1px solid var(--border); padding: 0 16px;
  overflow-x: auto;
}
.admin-nav button {
  padding: 12px 16px; border: none; background: none;
  cursor: pointer; font-family: inherit; font-size: .9rem;
  color: var(--text2); border-bottom: 3px solid transparent;
  white-space: nowrap; transition: all .15s;
}
.admin-nav button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ===== Responsive ===== */
@media (min-width: 768px) {
  body { padding-bottom: 0; }
  .bottom-nav { display: none; }
  .container { max-width: 720px; padding: 0 24px; }
  .branch-grid { grid-template-columns: repeat(3, 1fr); }
  .stats-row { gap: 16px; }
  .score-bar { bottom: 0; border-radius: var(--radius); margin: 12px 0; position: sticky; }
  .admin-body { max-width: 960px; margin: 0 auto; padding: 24px; }
}

@media (min-width: 1024px) {
  .container { max-width: 800px; }
  .admin-body { max-width: 1100px; }
  .branch-grid { grid-template-columns: repeat(5, 1fr); }
}

/* ===== Print ===== */
@media print {
  .bottom-nav, .score-bar, .app-header, .admin-header, .admin-nav,
  .btn, button { display: none !important; }
  body { background: #fff; padding: 0; }
  .card, .audit-item, .action-card { box-shadow: none; border: 1px solid #ddd; }
}

/* ===== Utility ===== */
.mt-0 { margin-top: 0; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.text-center { text-align: center; }
.text-sm { font-size: .85rem; }
.text-muted { color: var(--text2); }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-8 { gap: 8px; }
.hidden { display: none !important; }
