/* Trading / Inventory — Phase 1 */
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;500;600;700&family=IBM+Plex+Sans+Thai:wght@400;500;600;700&display=swap');

:root {
  --bg: #e8eef2;
  --bg-accent: #d5e2ea;
  --panel: #f7fafc;
  --ink: #1a2b36;
  --muted: #5c7382;
  --line: #c5d4de;
  --brand: #0f5c4c;
  --brand-2: #147a66;
  --brand-soft: #d8efe8;
  --warn: #9a5b00;
  --danger: #9b2c2c;
  --ok: #1f6b3a;
  --sidebar: #12303a;
  --sidebar-ink: #d7e6ec;
  --radius: 10px;
  --shadow: 0 10px 30px rgba(18, 48, 58, 0.08);
  --font: 'IBM Plex Sans Thai', 'Sarabun', sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 10% -10%, #c9dde8 0%, transparent 55%),
    radial-gradient(700px 380px at 100% 0%, #cfe8df 0%, transparent 50%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-accent) 100%);
  min-height: 100vh;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell {
  display: grid;
  grid-template-columns: 236px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: linear-gradient(180deg, #0e2831 0%, var(--sidebar) 55%, #0b2229 100%);
  color: var(--sidebar-ink);
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-top {
  display: flex;
  align-items: flex-start;
  gap: 6px;
}
.sidebar-top .brand { flex: 1; min-width: 0; }
.sidebar-close {
  display: none;
  border: 0;
  background: rgba(255,255,255,.08);
  color: #d7e6ec;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}
.sidebar-close:hover { background: rgba(255,255,255,.14); }

.brand { padding: 2px 6px 8px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-mark {
  display: inline-block;
  font-size: 11px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #8fd0bf;
  margin-bottom: 2px;
}
.brand strong { display: block; font-size: 15px; color: #fff; line-height: 1.2; }
.brand small { display: block; margin-top: 2px; color: #8aa3ae; font-size: 10.5px; }

.nav {
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 0 0 auto;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--sidebar-ink);
  padding: 5px 8px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 500;
  font-size: 13px;
  line-height: 1.2;
}
.nav-link:hover {
  background: rgba(255,255,255,.06);
  text-decoration: none;
  color: #fff;
}
.nav-link.active {
  background: rgba(20, 122, 102, .35);
  color: #fff;
  box-shadow: inset 3px 0 0 #4ec9ad;
}
.nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #8fd0bf;
}
.nav-link.active .nav-icon { color: #b7f0df; }
.nav-icon svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.nav-label { min-width: 0; }

.sidebar-user {
  margin-top: 2px;
  padding-top: 8px;
  border-top: 1px solid rgba(255,255,255,.08);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sidebar-user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,.04);
}
.sidebar-user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #147a66, #1dbf9f);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.sidebar-user-meta { min-width: 0; }
.user-chip {
  font-size: 13px;
  color: #fff;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-role {
  font-size: 11px;
  color: #8aa3ae;
  margin-top: 1px;
}
.logout-link { color: #9eb8c3; }
.logout-link:hover { color: #ffb4b4; }
.logout-link .nav-icon { color: #9eb8c3; }
.logout-link:hover .nav-icon { color: #ffb4b4; }

.main-wrap { min-width: 0; display: flex; flex-direction: column; min-height: 100vh; }
.mobile-topbar { display: none; }
.sidebar-backdrop { display: none; }

.main { padding: 28px 32px 48px; }
.page-head { margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 28px; font-weight: 700; letter-spacing: -.01em; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.toolbar .left, .toolbar .right { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}

.bank-misc-forms {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
  align-items: start;
}
.bank-misc-forms > .card {
  margin-bottom: 0;
  height: 100%;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.stat {
  background: linear-gradient(180deg, #ffffff 0%, #f3f8fa 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.stat .label { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.stat .value { font-size: 24px; font-weight: 700; color: var(--brand); }

.table-wrap { overflow-x: auto; }
table.data {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
table.data th, table.data td {
  padding: 10px 12px;
  border-bottom: 1px solid #e4edf2;
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}
table.data th {
  background: #eef5f8;
  color: #35505d;
  font-weight: 600;
  white-space: nowrap;
}
table.data tr:last-child td { border-bottom: 0; }
table.data tr:hover td { background: #f5fafb; }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }
table.data .actions { white-space: nowrap; }
table.data .check-col {
  width: 36px;
  text-align: center;
  vertical-align: middle;
}
table.data .check-col input { cursor: pointer; }

.btn {
  display: inline-block;
  border: 1px solid transparent;
  background: var(--brand);
  color: #fff;
  padding: 8px 14px;
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: var(--brand-2); text-decoration: none; color: #fff; }
.btn-secondary {
  background: #fff;
  color: var(--ink);
  border-color: var(--line);
}
.btn-secondary:hover { background: #f0f5f8; color: var(--ink); }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-link {
  background: transparent;
  color: var(--brand);
  border: 0;
  padding: 0;
  font-weight: 600;
  cursor: pointer;
}
.btn-link:hover { text-decoration: underline; }

/* Modal — ใช้ดูรายการ transaction บัญชีธนาคาร */
.app-modal[hidden] { display: none !important; }
.app-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.app-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(14, 40, 49, 0.45);
}
.app-modal-dialog {
  position: relative;
  z-index: 1;
  width: min(960px, 100%);
  max-height: min(86vh, 820px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(18, 48, 58, 0.28);
  overflow: hidden;
}
.app-modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  background: #f7fbfc;
}
.app-modal-body {
  padding: 14px 18px 18px;
  overflow: auto;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
.form-grid .full { grid-column: 1 / -1; }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: #35505d; font-weight: 600; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="search"], select, textarea,
input.flatpickr-alt {
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}
label.field input.flatpickr-alt,
label.field input[type="text"],
label.field input[type="password"],
label.field input[type="number"],
label.field input[type="date"],
label.field input[type="search"],
label.field select,
label.field textarea {
  width: 100%;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus,
input.flatpickr-alt:focus {
  outline: 2px solid rgba(20,122,102,.25);
  border-color: var(--brand-2);
}

/* Live table filter search */
.table-filter {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 12px;
  flex-wrap: wrap;
}
.table-filter__box {
  position: relative;
  flex: 1;
  min-width: 220px;
  max-width: 420px;
}
.table-filter__box input {
  width: 100%;
  padding-left: 36px;
  padding-right: 36px;
  -webkit-appearance: none;
  appearance: none;
}
.table-filter__box input::-webkit-search-decoration,
.table-filter__box input::-webkit-search-cancel-button {
  -webkit-appearance: none;
}
.table-filter__icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--muted);
  pointer-events: none;
}
.table-filter__clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  display: none;
  align-items: center;
  justify-content: center;
}
.table-filter__clear:hover { background: #e8eef2; color: var(--ink); }
.table-filter__box.has-value .table-filter__clear { display: inline-flex; }
.table-filter__meta {
  font-size: 13px;
  color: var(--muted);
}
input:disabled, select:disabled, textarea:disabled,
input.flatpickr-alt:disabled {
  opacity: 0.65;
  cursor: not-allowed;
  background: #eef2f4;
  color: #6a7f89;
}
label.field.is-term-disabled {
  opacity: 0.7;
}

/* Flatpickr — brand theme */
.flatpickr-calendar {
  font-family: var(--font);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(18, 48, 58, 0.16);
  overflow: hidden;
}
.flatpickr-months {
  background: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 100%);
  border-radius: 0;
  padding: 4px 0 2px;
}
.flatpickr-months .flatpickr-month {
  color: #fff;
  fill: #fff;
  height: 42px;
}
.flatpickr-current-month {
  padding-top: 8px;
  font-size: 110%;
  font-weight: 600;
}
.flatpickr-current-month .flatpickr-monthDropdown-months,
.flatpickr-current-month input.cur-year {
  color: #fff;
  font-weight: 600;
  background: transparent;
}
.flatpickr-current-month .flatpickr-monthDropdown-months:hover,
.numInputWrapper:hover {
  background: rgba(255,255,255,.12);
  border-radius: 6px;
}
.flatpickr-months .flatpickr-prev-month,
.flatpickr-months .flatpickr-next-month {
  fill: #fff;
  color: #fff;
  padding: 10px 12px;
}
.flatpickr-months .flatpickr-prev-month:hover svg,
.flatpickr-months .flatpickr-next-month:hover svg {
  fill: #d8efe8;
}
.flatpickr-weekdays {
  background: var(--brand-soft);
  padding: 4px 0;
}
span.flatpickr-weekday {
  color: var(--brand);
  font-weight: 700;
  font-size: 12px;
}
.flatpickr-day {
  border-radius: 8px;
  font-weight: 500;
  max-width: 38px;
  height: 38px;
  line-height: 38px;
}
.flatpickr-day:hover,
.flatpickr-day:focus {
  background: var(--brand-soft);
  border-color: var(--brand-soft);
  color: var(--brand);
}
.flatpickr-day.today {
  border-color: var(--brand-2);
  color: var(--brand);
  font-weight: 700;
}
.flatpickr-day.today:hover {
  background: var(--brand-soft);
  color: var(--brand);
}
.flatpickr-day.selected,
.flatpickr-day.selected:hover,
.flatpickr-day.selected:focus {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  box-shadow: 0 4px 10px rgba(15, 92, 76, 0.28);
}
.flatpickr-day.flatpickr-disabled,
.flatpickr-day.prevMonthDay,
.flatpickr-day.nextMonthDay {
  color: #9aabb4;
}

input.flatpickr-alt {
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%230f5c4c' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='4' width='18' height='18' rx='2'/%3E%3Cline x1='16' y1='2' x2='16' y2='6'/%3E%3Cline x1='8' y1='2' x2='8' y2='6'/%3E%3Cline x1='3' y1='10' x2='21' y2='10'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px 16px;
  padding-right: 36px;
}

.flatpickr-footer-actions {
  display: flex;
  gap: 8px;
  padding: 8px 10px 10px;
  border-top: 1px solid var(--line);
  background: #f4f8fa;
}
.flatpickr-today-btn,
.flatpickr-clear-btn {
  flex: 1;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.flatpickr-today-btn {
  background: var(--brand);
  color: #fff;
}
.flatpickr-today-btn:hover {
  background: var(--brand-2);
}
.flatpickr-clear-btn {
  background: #fff;
  color: var(--muted);
  border-color: var(--line);
}
.flatpickr-clear-btn:hover {
  color: var(--danger);
  border-color: #e0b4b4;
}

.alert {
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  border: 1px solid;
}
.alert-success { background: #e8f6ec; border-color: #b7dfc2; color: var(--ok); }
.alert-error { background: #fdecec; border-color: #f0c0c0; color: var(--danger); }
.alert-info { background: var(--brand-soft); border-color: #b9e8be; color: var(--brand); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-draft { background: #eef2f5; color: #526572; }
.badge-posted { background: #dff3e8; color: var(--ok); }
.badge-cancelled { background: #f7e0e0; color: var(--danger); }
.badge-cash { background: #e7f1ff; color: #1d4f91; }
.badge-credit { background: #fff1da; color: var(--warn); }

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(247,250,252,.96);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 28px;
}
.login-card h1 { margin: 0 0 6px; font-size: 26px; }
.login-card p.lead { margin: 0 0 18px; color: var(--muted); }
.login-card .field { margin-bottom: 12px; }
.login-card .btn { width: 100%; margin-top: 8px; }

.muted { color: var(--muted); }
.help { font-size: 13px; color: var(--muted); font-weight: 400; }

.items-table input, .items-table select { width: 100%; }
.items-table .col-qty, .items-table .col-price, .items-table .col-total { width: 120px; }
.items-table .col-action { width: 70px; }
.items-table td:first-child { min-width: 220px; }

.product-search-bar {
  margin-bottom: 12px;
  max-width: 420px;
}
.product-search-wrap {
  position: relative;
  display: block;
}
.product-search-wrap .js-product-search {
  width: 100%;
  padding-right: 34px;
}
.product-search-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  width: 26px;
  height: 26px;
  padding: 0;
  cursor: pointer;
  border-radius: 6px;
  display: none;
}
.product-search-wrap.has-value .product-search-clear { display: inline-flex; align-items: center; justify-content: center; }
.product-search-clear:hover { background: #e8eef2; color: var(--ink); }
.js-product-select option[disabled] { display: none; }

.btn-secondary.active-tab,
a.btn-secondary.active-tab {
  background: var(--brand-soft);
  border-color: var(--brand-2);
  color: var(--brand);
  font-weight: 700;
}

@media (max-width: 960px) {
  .app-shell { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .bank-misc-forms { grid-template-columns: 1fr; }
  .main { padding: 14px 14px 36px; }
  .page-head { display: none; }

  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 30;
    padding: 10px 12px;
    background: rgba(14, 40, 49, 0.96);
    color: #d7e6ec;
    border-bottom: 1px solid rgba(255,255,255,.08);
    backdrop-filter: blur(8px);
  }
  .mobile-topbar-text {
    min-width: 0;
    display: flex;
    flex-direction: column;
    line-height: 1.2;
  }
  .mobile-topbar-text strong {
    font-size: 14px;
    color: #fff;
  }
  .mobile-topbar-text span {
    font-size: 12px;
    color: #8aa3ae;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .menu-toggle {
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 10px;
    background: rgba(255,255,255,.08);
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    flex-shrink: 0;
  }
  .menu-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    background: #fff;
    border-radius: 2px;
    transition: transform .2s ease, opacity .2s ease;
  }
  body.sidebar-open .menu-toggle span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }
  body.sidebar-open .menu-toggle span:nth-child(2) { opacity: 0; }
  body.sidebar-open .menu-toggle span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(8, 18, 22, 0.55);
    z-index: 40;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 86vw);
    z-index: 50;
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 12px 0 40px rgba(0,0,0,.28);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 10px 18px;
  }
  body.sidebar-open .sidebar {
    transform: translateX(0);
  }
  .sidebar-close { display: inline-flex; align-items: center; justify-content: center; }
  .nav { flex-direction: column; flex-wrap: nowrap; gap: 1px; }
  .nav-link { padding: 6px 8px; }
}

@media (min-width: 961px) {
  body.sidebar-open { overflow: auto; }
}

/* Segmented toggle (2 options) */
.mode-toggle {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  max-width: 440px;
  width: 100%;
  background: #e2ebf0;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: inset 0 1px 2px rgba(18, 48, 58, 0.06);
}
.mode-toggle__opt {
  position: relative;
  margin: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 42px;
  padding: 8px 12px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  transition: background .18s ease, color .18s ease, box-shadow .18s ease, transform .12s ease;
  user-select: none;
}
.mode-toggle__opt input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.mode-toggle__label {
  position: relative;
  z-index: 1;
}
.mode-toggle__opt:hover {
  color: var(--ink);
}
.mode-toggle__opt:has(input:checked),
.mode-toggle__opt.is-active {
  background: linear-gradient(180deg, var(--brand-2) 0%, var(--brand) 100%);
  color: #fff;
  box-shadow: 0 4px 12px rgba(15, 92, 76, 0.28);
}
.mode-toggle__opt.is-active:hover {
  color: #fff;
}
.mode-toggle__opt:has(input:focus-visible) {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
.mode-toggle--locked {
  opacity: 0.92;
  cursor: default;
}
.mode-toggle--locked .mode-toggle__opt {
  cursor: default;
}
.mode-toggle--locked .mode-toggle__opt:not(.is-active) {
  opacity: 0.55;
}
.mode-toggle__hint [data-for] {
  display: none;
}
@media (max-width: 520px) {
  .mode-toggle {
    max-width: none;
    grid-template-columns: 1fr;
  }
}
