/*
 * PRICING TOOL — Smart Pricing Calculator — Version 1.4 three-column UI
 * Layout by viewport width: 3 columns (CURRENT | NEW | RESULT & COMPARISON)
 * from 1360px, 2 columns from 820px (result panel under CURRENT, beside the
 * pricing methods), 1 column below. Result panel is sticky when the window
 * is tall enough. Detailed comparison table full width below the workspace.
 * CURRENT and NEW share grid rows (CSS subgrid), so their purchase-cost
 * sections have equal height and the net-cost rows line up.
 * Navy = CURRENT, green = NEW, slate = RESULT frame.
 */
:root {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --text: #17202b;
  --muted: #5a6574;
  --faint: #8a94a3;
  --border: #dce1e8;
  --line: #e9edf2;

  --cur: #1f3a5f;        /* navy */
  --cur-mid: #4a6890;    /* muted blue */
  --cur-soft: #eaeff6;
  --new: #0f7047;        /* green */
  --new-mid: #2f8f63;
  --new-soft: #e5f3eb;
  --on-solid: #ffffff;
  --res: #3a4656;        /* slate frame for RESULT & COMPARISON */

  --bad: #b42318;
  --bad-soft: #fdecea;
  --up: #0f7047;
  --down: #b42318;
  --focus: #2f6fb3;

  --radius: 10px;
  --radius-sm: 7px;
  --font: "IBM Plex Sans Thai", "Sarabun", "Noto Sans Thai", "Leelawadee UI", Tahoma, system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141a; --surface: #161d25; --surface-2: #1c2530; --text: #e4e9ef; --muted: #9ba7b6;
    --faint: #6f7b8b; --border: #2a3542; --line: #222c38;
    --cur: #9cbbe3; --cur-mid: #7f9fc8; --cur-soft: #1a2738; --new: #52c58f; --new-mid: #3fa877;
    --new-soft: #12301f; --on-solid: #0c1117; --res: #b3bfcc; --bad: #f08a7e; --bad-soft: #3a1d1a;
    --up: #52c58f; --down: #f08a7e; --focus: #7fb0e6;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141a; --surface: #161d25; --surface-2: #1c2530; --text: #e4e9ef; --muted: #9ba7b6;
  --faint: #6f7b8b; --border: #2a3542; --line: #222c38;
  --cur: #9cbbe3; --cur-mid: #7f9fc8; --cur-soft: #1a2738; --new: #52c58f; --new-mid: #3fa877;
  --new-soft: #12301f; --on-solid: #0c1117; --res: #b3bfcc; --bad: #f08a7e; --bad-soft: #3a1d1a;
  --up: #52c58f; --down: #f08a7e; --focus: #7fb0e6;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 14px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }

.wrap {
  max-width: 1760px; margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* column colour context */
#col-cur { --col: var(--cur); --col-mid: var(--cur-mid); --col-soft: var(--cur-soft); }
#col-new { --col: var(--new); --col-mid: var(--new-mid); --col-soft: var(--new-soft); }
#col-res { --col: var(--new); --col-mid: var(--new-mid); --col-soft: var(--new-soft); }

/* ---------- top bar ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding-block: 10px;
}
.brand { display: flex; flex-direction: column; }
.brand-name { font-weight: 700; letter-spacing: .1em; color: var(--cur); font-size: 15px; }
.brand-sub { font-size: 12px; color: var(--muted); }
.ver {
  display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 4px;
  border: 1px solid var(--border); font-size: 11px; color: var(--muted);
}
.topbar-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- comparison summary (inside RESULT & COMPARISON) ---------- */
.sec-summary { background: var(--surface); }
.sum-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.sum-title { font-weight: 600; font-size: 13px; color: var(--text); }
.sum-method { font-size: 12px; color: var(--new); }
.sum-tiles { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
.sum-tile {
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px 10px;
  background: var(--surface-2); min-width: 0;
  display: grid; grid-template-columns: 1fr auto; grid-template-areas: "label diff" "vals vals"; gap: 2px 8px;
  align-items: baseline;
}
.sum-label { grid-area: label; font-size: 12px; color: var(--muted); }
.sum-diff { grid-area: diff; font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sum-diff small { font-weight: 400; color: var(--muted); }
.sum-diff.up { color: var(--up); }
.sum-diff.down { color: var(--down); }
.sum-vals { grid-area: vals; display: flex; gap: 14px; flex-wrap: wrap; font-variant-numeric: tabular-nums; }
.sum-vals i { font-style: normal; font-size: 11px; margin-right: 5px; }
.sum-cur { color: var(--cur); font-size: 13px; }
.sum-cur i { color: var(--cur-mid); }
.sum-new { color: var(--new); font-weight: 700; font-size: 15px; }
.sum-new i { color: var(--new-mid); font-weight: 500; }

/* ---------- layout ---------- */
.layout {
  display: grid; gap: 16px; align-items: start; padding-block: 16px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "cur" "new" "res" "detail";
}
#col-cur { grid-area: cur; }
#col-new { grid-area: new; }
#col-res { grid-area: res; }
.detail { grid-area: detail; }
.column { display: flex; flex-direction: column; gap: 8px; min-width: 0; }

.col-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 0 2px; }
.col-head h1 {
  margin: 0; font-size: 18px; font-weight: 700; color: var(--col);
  display: flex; align-items: center; gap: 8px;
}
.col-tag {
  font-size: 10px; font-weight: 700; letter-spacing: .1em; padding: 2px 7px; border-radius: 4px;
  background: var(--col); color: var(--on-solid);
}
.col-head .btn-link { margin-left: auto; font-size: 13px; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden;
}
.column .panel { border-top: 3px solid var(--col); }
#col-res .panel { border-top-color: var(--res); }
#col-res .col-head h1 { color: var(--res); }
#col-res .col-tag { background: var(--res); }
#col-res .hero-value { font-size: 32px; }
.sec { padding: 12px 14px; }
.sec + .sec, .panel-rest { border-top: 1px solid var(--line); }
.sec-title {
  margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--text);
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
}
.sec-title small { font-weight: 400; color: var(--faint); font-size: 12px; }
.detail .sec-title { padding: 12px 14px 0; }

/* ---------- compact form rows ---------- */
.frow { display: grid; grid-template-columns: 136px minmax(0, 1fr); gap: 4px 10px; align-items: start; }
.frow + .frow { margin-top: 8px; }
.flabel { font-size: 13px; font-weight: 500; padding-top: 6px; line-height: 1.25; }
.flabel small { display: block; font-weight: 400; color: var(--faint); font-size: 11px; }
.fctl { min-width: 0; display: flex; flex-direction: column; gap: 5px; }

.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap input {
  width: 100%; font: inherit; font-size: 16px; /* 16px avoids iOS zoom */
  height: 34px; padding: 4px 28px 4px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); text-align: right;
  font-variant-numeric: tabular-nums;
}
.input-wrap input:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: var(--focus); }
.input-wrap input.invalid { border-color: var(--bad); background: var(--bad-soft); }
.input-wrap input::placeholder { color: var(--faint); }
.input-wrap .suffix { position: absolute; right: 10px; color: var(--muted); pointer-events: none; font-size: 13px; }
.input-sm { width: 120px; flex: none; }

.segmented { display: flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.segmented label { flex: 1; cursor: pointer; min-width: 0; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; text-align: center; padding: 5px 4px; font-size: 12px; color: var(--muted);
  background: var(--surface-2); line-height: 1.3;
}
.segmented input:checked + span { background: var(--col); color: var(--on-solid); font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: -3px; }

/* discounts: [10 %] × + [5 %] × + เพิ่มส่วนลด */
.discount-line { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 2px; }
.discounts { display: contents; }
.discount-row { display: inline-flex; align-items: center; }
.discount-row + .discount-row::before { content: "+"; color: var(--faint); padding: 0 4px 0 2px; }
.input-discount { width: 70px; }
.input-discount input { padding: 4px 22px 4px 6px; }
.input-discount .suffix { right: 7px; }
.icon-btn {
  border: 0; background: none; font-size: 17px; line-height: 1; color: var(--faint);
  cursor: pointer; padding: 4px 5px; border-radius: 6px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--bad); }
.btn-add-discount { padding: 4px 6px; white-space: nowrap; font-size: 13px; }

/* NEW shows its sync status in that spot; CURRENT keeps the hint (also in the tooltip) */
#col-new .compound-hint { display: none; }
.sync { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 2px; font-size: 11px; font-weight: 400; }
.sync-on { color: var(--new); display: inline-flex; align-items: center; gap: 5px; }
.sync-on::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--new); flex: none; }
.sync .sync-btn { font-size: 11px; padding: 0; line-height: 1.25; text-align: left; }

/* net cost summary with expandable breakdown */
.sec-cost { display: flex; flex-direction: column; }
.sec-cost > .frow:last-of-type { margin-bottom: 10px; }
.cost-sum { margin-top: auto; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--col-soft); }
.cost-sum summary {
  list-style: none; cursor: pointer; display: flex; align-items: baseline; gap: 10px;
  padding: 7px 10px; flex-wrap: wrap;
}
.cost-sum summary::-webkit-details-marker { display: none; }
.cost-sum-label { font-weight: 600; font-size: 13px; }
.cost-sum-label small { font-weight: 400; color: var(--muted); margin-left: 4px; }
.cost-sum-value { margin-left: auto; font-weight: 700; font-size: 15px; color: var(--col); font-variant-numeric: tabular-nums; }
.cost-sum-more { font-size: 12px; color: var(--muted); }
.cost-sum-more::after { content: " ▾"; }
.cost-sum[open] .cost-sum-more::after { content: " ▴"; }
.cost-sum summary:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; border-radius: var(--radius-sm); }
.cost-sum .breakdown { margin: 0; padding: 0 10px 8px; }

.breakdown .row {
  display: flex; justify-content: space-between; gap: 12px; padding: 3px 0;
  border-bottom: 1px dashed var(--line); font-size: 13px;
}
.breakdown dt { color: var(--muted); }
.breakdown dd { margin: 0; font-variant-numeric: tabular-nums; white-space: nowrap; }
.breakdown .total { border-bottom: 0; font-weight: 600; }
.breakdown .total dt { color: var(--text); }

.hint { font-size: 12px; color: var(--muted); margin: 0; }
.error { font-size: 12px; color: var(--bad); margin: 0; }
.error:empty { display: none; }
.warn { background: var(--bad-soft); color: var(--bad); padding: 6px 10px; border-radius: var(--radius-sm); font-size: 13px; margin: 8px 0 0; }
.empty { color: var(--muted); margin: 0; font-size: 13px; }
.note { color: var(--muted); margin: 6px 0 0; font-size: 12px; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-size: 13px; cursor: pointer; border-radius: var(--radius-sm); padding: 5px 11px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .chip:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
.btn-link { border: 0; background: none; padding: 2px 0; color: var(--col, var(--cur)); font-weight: 500; }
.btn-link:hover { background: none; text-decoration: underline; }
.chips { display: flex; gap: 4px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 12px; padding: 1px 9px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.chip:hover { border-color: var(--new); color: var(--new); }

/* ---------- pricing methods ----------
   One compact grid per card:  (radio) title | input + buttons | price
   with an optional small helper line beneath. Every input stays visible. */
.methods { display: flex; flex-direction: column; gap: 4px; }
.method {
  display: grid; cursor: pointer; background: var(--surface);
  grid-template-columns: 15px 112px minmax(0, 1fr) minmax(68px, auto);
  grid-template-areas: "radio title ctl price" ". desc desc desc" ". err err err";
  column-gap: 6px; row-gap: 0; align-items: center;
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 4px 10px; min-height: 40px;
}
.method:hover { border-color: var(--new-mid); }
.method:has(> input:checked) {
  border-color: var(--new); background: var(--new-soft);
  box-shadow: inset 0 0 0 1px var(--new);
}
.method > input { grid-area: radio; margin: 0; accent-color: var(--new); width: 15px; height: 15px; }
.method-body { display: contents; }
.method-title { grid-area: title; font-weight: 600; font-size: 13px; line-height: 1.25; }
.method-ctl { grid-area: ctl; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; min-width: 0; }
.method-ctl .input-sm { width: 86px; margin-right: 2px; }
.method-ctl .input-money { width: 120px; }   /* room for amounts up to 999,999.99 */
.method-ctl .input-wrap input { height: 30px; font-size: 15px; }
.method .chip { padding: 1px 5px; }
.method .chips { gap: 3px; }
.method-desc { grid-area: desc; font-size: 11px; line-height: 1.3; color: var(--muted); margin: 1px 0 0; }
/* Same margin has no input: its reference value sits in the input column */
.method:not(:has(.method-ctl)) .method-desc { grid-area: ctl; font-size: 12px; margin: 0; }
.method .error { grid-area: err; }
/* Helper text beside the input when there is room (wraps under it when not) */
.method-ctl .method-desc { flex: 1 1 110px; min-width: 0; margin: 0; line-height: 1.25; }
.method-price {
  grid-area: price; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap;
  font-size: 14px; line-height: 1.15; color: var(--text);
}
.method-price small { display: block; font-weight: 400; font-size: 10px; color: var(--faint); }
.method:has(> input:checked) .method-price { color: var(--new); }

/* ---------- results ---------- */
.sec-result { background: var(--col-soft); }
.hero {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; align-items: baseline;
}
.hero-label { font-size: 12px; font-weight: 500; color: var(--muted); }
.hero-value {
  grid-row: span 2; align-self: center;
  font-size: 28px; font-weight: 700; color: var(--col); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.hero-value span { font-size: 16px; font-weight: 500; }
.hero-sub { display: flex; gap: 4px 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.hero-sub b { font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; margin-left: 3px; }

.kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; margin-top: 6px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px 10px; min-width: 0; }
.kpi-label { font-size: 11px; color: var(--muted); }
.kpi-value { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi.bad .kpi-value { color: var(--bad); }

/* Same border + padding as a KPI card, so the text starts at the Gross Profit KPI's left edge */
.formula {
  margin-top: 6px; padding: 5px 10px; border-radius: var(--radius-sm);
  border: 1px dashed var(--border); background: var(--surface);
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 12px; font-size: 12px; text-align: left;
}
.formula-head { font-weight: 600; }
.formula-expr { font-variant-numeric: tabular-nums; font-size: 13px; overflow-wrap: anywhere; }
.formula-legend { flex-basis: 100%; color: var(--muted); font-size: 11px; }

/* ---------- detailed comparison ---------- */
.table-wrap { overflow-x: auto; padding: 0 14px 10px; }
.compare { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.compare th, .compare td { padding: 5px 6px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.compare th:first-child { text-align: left; font-weight: 500; color: var(--muted); }
.compare thead th { font-size: 11px; color: var(--muted); font-weight: 500; }
.compare thead th:nth-child(2) { color: var(--cur); }
.compare thead th:nth-child(3) { color: var(--new); }
.compare td.up { color: var(--up); }
.compare td.down { color: var(--down); }
.compare small { color: var(--muted); }

.footer { text-align: center; font-size: 11px; color: var(--faint); padding-block: 0 24px; }

/* ---------- responsive ---------- */
/* 2+ columns: CURRENT and NEW share the page grid's rows (subgrid):
   row 1 = column heading, row 2 = purchase cost, row 3+ = the rest.
   Both cost sections get the height of the taller one, so the
   "ต้นทุนสินค้าสุทธิ" rows sit at the same level. */
@media (min-width: 820px) {
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-areas: none; }
  #col-cur, #col-new {
    display: grid; grid-template-rows: subgrid; row-gap: 8px; align-self: stretch;
  }
  #col-cur > .col-head, #col-new > .col-head { grid-row: 1; }
  #col-cur > .panel, #col-new > .panel {
    grid-row: 2 / -1; display: grid; grid-template-rows: subgrid; row-gap: 0;
  }
  .panel > .sec-cost { grid-row: 1; }
  .panel > .panel-rest { grid-row: 2 / -1; }
  /* 2 columns: result panel goes under CURRENT, beside the taller NEW column */
  #col-cur { grid-column: 1; grid-row: 1 / span 3; }
  #col-new { grid-column: 2; grid-row: 1 / span 4; }
  #col-res { grid-column: 1; grid-row: 4; }
  .detail { grid-column: 1 / -1; grid-row: 5; }
}
/* 3 columns: CURRENT | NEW | RESULT & COMPARISON */
@media (min-width: 1360px) {
  .layout { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  #col-cur { grid-column: 1; grid-row: 1 / span 3; }
  #col-new { grid-column: 2; grid-row: 1 / span 3; }
  #col-res { grid-column: 3; grid-row: 1 / span 3; }
  .detail { grid-column: 1 / -1; grid-row: 4; }
}
/* Sticky result panel only where the window is tall enough to show it whole;
   it lives in its own grid cell, so it never covers other content. */
@media (min-width: 820px) and (min-height: 760px) {
  #col-res {
    position: sticky; top: 12px;
    max-height: calc(100vh - 24px); overflow-y: auto; overscroll-behavior: contain;
  }
}
@media (max-width: 560px) {
  body { font-size: 14px; }
  .topbar-inner { padding-block: 8px; }
  .frow { grid-template-columns: minmax(0, 1fr); }
  .flabel { padding-top: 0; }
  .flabel small { display: inline; margin-left: 4px; }
  .flabel .sync { display: inline-flex; margin: 0 0 0 8px; vertical-align: middle; }
  .method {
    grid-template-columns: 15px minmax(0, 1fr) auto;
    grid-template-areas: "radio title price" ". ctl ctl" ". desc desc" ". err err";
    row-gap: 3px; padding: 6px 10px;
  }
  .sec { padding: 10px 12px; }
  .hero-value, #col-res .hero-value { font-size: 26px; }
  .kpi-value { font-size: 15px; }
  .compare { font-size: 12px; }
  .compare th, .compare td { padding: 5px 3px; }
  .compare small { display: block; font-size: 10px; }
  .table-wrap { padding: 0 8px 8px; }
}
@media (prefers-reduced-motion: no-preference) {
  .method, .segmented span { transition: background-color .12s, border-color .12s; }
}
