/* V1.3.2.2 — Mobile First + Tablet / safe-area / touch */
:root {
  --touch-min: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --sticky-bar-h: 72px;
  --header-h: 56px;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  height: 100%;
}
html, body {
  max-width: 100%;
  overflow-x: hidden;
}
body {
  min-height: 100vh;
  min-height: 100dvh;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  font-size: 15px;
  line-height: 1.55;
  -webkit-tap-highlight-color: rgba(31, 78, 121, 0.12);
  overscroll-behavior-y: contain;
}

/* Thai line-height */
:lang(th), [lang="th"], html[lang="th"] body {
  line-height: 1.65;
}

header {
  padding-top: calc(12px + var(--safe-top));
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
}
header.compact .sub { display: none; }
header.compact h1 { font-size: .95rem; }

main {
  width: 100%;
  max-width: 960px;
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  padding-bottom: calc(16px + var(--safe-bottom));
}

/* Touch targets */
.btn, .seg, .tab, .lang-btn, .menu-tile, .product-card, .order-card, .search-item {
  min-height: var(--touch-min);
}
.btn, .seg {
  min-height: 48px;
}
input, select, textarea {
  min-height: 48px;
  font-size: 16px; /* iOS: avoid auto-zoom */
  max-width: 100%;
}
select {
  text-overflow: ellipsis;
}
label { font-size: .9rem; color: #333; }

/* Network banner */
.net-banner {
  display: none;
  position: sticky;
  top: 0;
  z-index: 300;
  background: #c00000;
  color: #fff;
  text-align: center;
  padding: 10px 12px;
  font-weight: 700;
  font-size: .9rem;
}
.net-banner.show { display: block; }
.net-banner.ok { background: #006100; }

/* Mobile home hub — 2-col cards */
.home-hub {
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 14px;
}
.hub-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 108px;
  padding: 16px 10px;
  background: var(--card);
  border: 2px solid #d5e0ea;
  border-radius: 14px;
  box-shadow: var(--shadow);
  text-align: center;
  cursor: pointer;
  color: var(--navy);
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.35;
  word-break: break-word;
  -webkit-user-select: none;
  user-select: none;
}
.hub-card:active { border-color: var(--blue); background: #eef5fb; }
.hub-card .hub-icon { font-size: 1.6rem; line-height: 1; }

/* Tabs: compact on phone, full labels */
.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
  overflow: visible;
}
.tab {
  border-radius: 12px;
  white-space: normal;
  text-align: center;
  line-height: 1.25;
  padding: 12px 8px;
  min-width: 0;
  word-break: break-word;
}

/* Form steps */
.form-step-bar {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.form-step-pill {
  flex: 1 0 auto;
  min-width: 72px;
  min-height: 40px;
  border: 1px solid #c5d0dc;
  background: #fff;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  color: #456;
  padding: 8px 10px;
}
.form-step-pill.active {
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}
.form-step-pill.done { border-color: var(--ok); color: var(--ok); }
.form-step { display: none; }
.form-step.active { display: block; }

.sticky-form-actions {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 150;
  background: #fff;
  border-top: 1px solid #dde5ed;
  box-shadow: 0 -4px 16px rgba(0,0,0,.08);
  padding: 10px max(12px, var(--safe-right)) calc(10px + var(--safe-bottom)) max(12px, var(--safe-left));
  gap: 8px;
}
.sticky-form-actions .btn {
  flex: 1;
  margin: 0;
  width: auto;
}
body.has-sticky-form main,
body.has-sticky-form #panel-new {
  padding-bottom: calc(var(--sticky-bar-h) + var(--safe-bottom) + 12px);
}

/* Product grid mobile */
.product-grid {
  grid-template-columns: 1fr;
  gap: 10px;
  max-height: min(50vh, 420px);
}
.product-card {
  align-items: stretch;
  border-width: 2px;
}
.product-card.selected {
  border-color: var(--navy);
  background: #e8f0f8;
  box-shadow: 0 0 0 2px rgba(31,78,121,.2);
}
.product-card img, .product-card .ph {
  width: 80px;
  height: 80px;
  aspect-ratio: 1 / 1;
}
.pc-zh, .pc-th {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.pc-spec {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

/* Order list cards */
.order-list .order-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 10px;
  padding: 14px;
  min-height: var(--touch-min);
  border-left-width: 5px;
  word-break: break-word;
}
.order-card .oc-title { font-weight: 700; color: var(--navy); font-size: 1rem; }
.order-card .oc-cust {
  grid-column: 1 / -1;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}
.order-card .oc-meta { font-size: .8rem; color: #666; }
.order-card .oc-amt { font-weight: 700; text-align: right; white-space: nowrap; }
.status-pill {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: .75rem;
  font-weight: 700;
  background: #e8f0f8;
  color: var(--navy);
}
.status-pill.warn { background: #fdecea; color: var(--warn); }
.status-pill.ok { background: #e6f4ea; color: var(--ok); }
.list-toolbar {
  position: sticky;
  top: calc(var(--header-h) + var(--safe-top));
  z-index: 80;
  background: var(--card);
  padding: 8px 0;
  margin-bottom: 8px;
}
.filter-drawer {
  position: fixed; inset: 0; z-index: 220;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
.filter-drawer[hidden] { display: none !important; }
.filter-sheet {
  background: #fff;
  width: 100%;
  max-width: 520px;
  max-height: 85dvh;
  overflow: auto;
  border-radius: 16px 16px 0 0;
  padding: 16px;
}

/* Order detail sections */
.detail-section {
  background: #f7f9fc;
  border: 1px solid #e5eaf0;
  border-radius: 12px;
  padding: 12px;
  margin: 10px 0;
}
.detail-section h3 {
  font-size: .95rem;
  color: var(--navy);
  margin-bottom: 8px;
}
.detail-layout { display: block; }
.bank-wrap { word-break: break-all; overflow-wrap: anywhere; }
.totals { text-align: right; }
.totals .grand { font-variant-numeric: tabular-nums; }

/* Timeline: always vertical on phone/tablet portrait to avoid overflow */
.tl-track {
  flex-direction: column !important;
  border-left: 3px solid #dde5ed !important;
  margin-left: 8px !important;
  padding-left: 14px !important;
  overflow-x: hidden !important;
}
.tl-step {
  flex: none !important;
  min-width: 0 !important;
  text-align: left !important;
}
.tl-dot {
  position: absolute !important;
  left: -22px !important;
  top: 12px !important;
  margin: 0 !important;
}

/* Print sheet modal */
.print-sheet-actions { display: flex; flex-direction: column; gap: 8px; }
.print-sheet-actions .btn { width: 100%; margin: 0; }

/* Password toggle */
.pwd-wrap { position: relative; }
.pwd-wrap input { padding-right: 52px; }
.pwd-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-60%);
  min-width: 44px;
  min-height: 44px;
  border: none;
  background: transparent;
  color: var(--navy);
  font-size: .8rem;
  font-weight: 700;
  cursor: pointer;
}

/* Login mobile */
.login-box {
  margin: 16px auto 24px;
  width: 100%;
  max-width: 400px;
}
.login-box input {
  min-height: 48px;
  font-size: 16px;
}
.login-box .btn {
  min-height: 48px;
  width: 100%;
}
.lang-switch .lang-btn {
  min-height: 40px;
  min-width: 56px;
  font-size: .9rem;
  font-weight: 700;
}

/* Modal within viewport */
.modal {
  align-items: flex-end;
  padding: max(12px, var(--safe-top)) max(12px, var(--safe-right)) max(12px, var(--safe-bottom)) max(12px, var(--safe-left));
}
.modal-card {
  max-height: min(90dvh, 90vh);
  overflow: auto;
  margin-bottom: 0;
}
@media (min-width: 640px) {
  .modal { align-items: center; }
}

/* ——— Breakpoints ——— */
@media (max-width: 767px) {
  .home-hub { display: grid; }
  .tabs.tabs-compact-mobile { display: none; }
  body.show-tabs-mobile .home-hub { display: none; }
  body.show-tabs-mobile .tabs.tabs-compact-mobile { display: grid; }
  header .role-bar #roleBadge { display: none; }
  .grid-2 { grid-template-columns: 1fr !important; }
  .desktop-only { display: none !important; }
  .lines-cards { display: flex !important; }
  .sticky-form-actions.show { display: flex; }
  body.has-sticky-form .action-row-desktop { display: none; }
  .seg-group { flex-wrap: wrap; }
  .seg { flex: 1 1 calc(50% - 8px); min-width: 120px; }
  .btn { width: 100%; }
  .action-row .btn { width: 100%; }
}

@media (min-width: 768px) and (max-width: 1023px) {
  /* iPad portrait / small tablet */
  .home-hub {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
  }
  .tabs.tabs-compact-mobile { display: none; }
  body.show-tabs-mobile .home-hub { display: none; }
  body.show-tabs-mobile .tabs.tabs-compact-mobile {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
  }
  .product-grid { grid-template-columns: 1fr 1fr; }
  .sticky-form-actions.show { display: flex; }
  body.has-sticky-form .action-row-desktop { display: none; }
  .detail-layout.ipad-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
    align-items: start;
  }
}

@media (min-width: 1024px) {
  .home-hub { display: none !important; }
  .tabs.tabs-compact-mobile {
    display: flex !important;
    grid-template-columns: none;
    overflow-x: auto;
  }
  .tab { flex: 1 0 auto; white-space: nowrap; }
  .product-grid { grid-template-columns: 1fr 1fr 1fr; }
  .form-step { display: block !important; }
  .form-step-bar { display: none !important; }
  .sticky-form-actions { display: none !important; }
  body.has-sticky-form .action-row-desktop { display: flex; }
  .desktop-only { display: table; }
  .lines-cards { display: none; }
  /* Desktop wide: horizontal timeline OK */
  .tl-track {
    flex-direction: row !important;
    border-left: none !important;
    margin-left: 0 !important;
    padding-left: 0 !important;
    overflow-x: auto !important;
    gap: 4px !important;
  }
  .tl-step {
    flex: 1 !important;
    min-width: 72px !important;
    text-align: center !important;
  }
  .tl-dot {
    position: static !important;
    margin: 0 auto 6px !important;
  }
  .detail-layout.ipad-split {
    display: grid;
    grid-template-columns: 1.4fr 1fr;
    gap: 16px;
  }
}

/* Landscape phone / tablet: reduce top blank */
@media (max-height: 500px) and (orientation: landscape) {
  .login-box { margin-top: 8px; padding: 16px; }
  header h1 { font-size: .9rem; }
}

/* Prevent fixed-width clipping */
img, svg, video, canvas, table {
  max-width: 100%;
}
.panel, .card, .login-box, .modal-card {
  max-width: 100%;
  overflow-x: hidden;
}
