/* =========================================================
   Cost Calculator — editorial palette (matches Buyer History)
   Cream / Deep Navy / Habañero / Aster Blue / Jodhpur Tan
   ========================================================= */

/* Password gate (hidden until JS shows it) */
.gate {
  display: none;
  position: fixed; inset: 0;
  align-items: center; justify-content: center;
  z-index: 100;
  background:
    radial-gradient(900px 520px at 80% -10%, rgba(249, 133, 19, 0.10), transparent 60%),
    radial-gradient(700px 460px at 0% 110%, rgba(34, 51, 130, 0.10), transparent 65%),
    #F4F1EC;
  padding: 24px;
}
.gate-card {
  width: 100%; max-width: 360px;
  background: #FFFFFF;
  border: 1px solid #E2DACC;
  border-radius: 16px;
  padding: 28px 26px 22px;
  box-shadow: 0 1px 2px rgba(17, 17, 68, 0.04), 0 16px 48px rgba(34, 51, 130, 0.10);
  display: flex; flex-direction: column; gap: 12px;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  letter-spacing: -0.01em;
}
.gate-card h2 {
  margin: 0;
  font-size: 19px; font-weight: 700;
  letter-spacing: -0.025em;
  color: #111144;
}
.gate-card p {
  margin: 0;
  font-size: 13px;
  color: #6E7AA0;
}
.gate-card input {
  background: #FAF8F4;
  border: 1px solid #DAD1C8;
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 15px; font-weight: 500;
  font-family: inherit;
  color: #111144;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.gate-card input:focus {
  border-color: #F98513;
  background: #FFFFFF;
  box-shadow: 0 0 0 3px rgba(249, 133, 19, 0.12);
}
.gate-card button {
  background: #F98513;
  color: #FFFFFF;
  border: 0;
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 14px; font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  letter-spacing: -0.005em;
  transition: background 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
  box-shadow: 0 1px 2px rgba(249, 133, 19, 0.35);
}
.gate-card button:hover { background: #FFA64A; }
.gate-card button:active { transform: scale(0.98); }
.gate-error {
  margin: 0;
  font-size: 12.5px;
  color: #B23A2A;
  background: rgba(178, 58, 42, 0.08);
  border: 1px solid rgba(178, 58, 42, 0.25);
  border-radius: 8px;
  padding: 7px 10px;
}
.gate-build {
  margin-top: 4px;
  font-size: 11px;
  color: #98A0BE;
  text-align: center;
}

:root {
  --bg:           #F4F1EC;
  --bg-2:         #ECE7DE;
  --surface:      #FFFFFF;
  --surface-2:    #FAF8F4;
  --surface-3:    #EFEAE2;
  --surface-hi:   #E8E1D6;

  --border:       #E2DACC;
  --border-2:     #DAD1C8;
  --border-3:     #C4B9AC;

  --text:         #111144;
  --text-2:       #223382;
  --text-3:       #6E7AA0;
  --text-4:       #98A0BE;
  --text-on-acc:  #FFFFFF;

  --accent:       #F98513;
  --accent-2:     #FFA64A;
  --accent-soft:  rgba(249, 133, 19, 0.12);
  --accent-mid:   rgba(249, 133, 19, 0.28);

  --cool:         #9BACD8;
  --cool-soft:    rgba(155, 172, 216, 0.22);
  --cool-mid:     rgba(155, 172, 216, 0.45);

  --good:         #4A6B3E;
  --good-soft:    rgba(74, 107, 62, 0.12);
  --warm:         #F98513;
  --warm-soft:    rgba(249, 133, 19, 0.12);
  --bad:          #B23A2A;
  --bad-soft:     rgba(178, 58, 42, 0.12);

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 22px;

  --shadow-1: 0 1px 2px rgba(17, 17, 68, 0.04), 0 8px 24px rgba(34, 51, 130, 0.06);
  --shadow-2: 0 1px 2px rgba(17, 17, 68, 0.05), 0 16px 48px rgba(34, 51, 130, 0.10);

  --ease: cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-feature-settings: "ss01", "cv11";
}

body {
  font-family:
    "Inter", -apple-system, BlinkMacSystemFont, "SF Pro Display",
    "Segoe UI", Roboto, sans-serif;
  font-size: 14.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  letter-spacing: -0.01em;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  background-image:
    radial-gradient(900px 520px at 80% -10%, rgba(249, 133, 19, 0.08), transparent 60%),
    radial-gradient(700px 460px at 0% 110%, rgba(34, 51, 130, 0.08), transparent 65%);
  background-attachment: fixed;
}

.num, b, input[type="number"] {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

/* =========================================================
   Top bar
   ========================================================= */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 14px) 18px 14px;
  background: rgba(244, 241, 236, 0.78);
  backdrop-filter: saturate(160%) blur(20px);
  -webkit-backdrop-filter: saturate(160%) blur(20px);
  border-bottom: 1px solid var(--border);
}
#topbar h1 {
  margin: 0; font-size: 17px; font-weight: 600;
  flex: 1; letter-spacing: -0.025em;
  color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  background: transparent; border: 0; color: var(--text-2);
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; cursor: pointer;
  transition: background 0.15s var(--ease), transform 0.15s var(--ease);
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 18px; height: 18px; stroke-width: 2; }

/* =========================================================
   Layout
   ========================================================= */
main {
  padding: 20px 18px 80px;
  max-width: 1200px;
  margin: 0 auto;
  animation: fadeUp 0.32s var(--ease) both;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* =========================================================
   Global rate chips
   ========================================================= */
.globals {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin-bottom: 14px;
}
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border-2);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--text-3);
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.chip-input { padding-right: 14px; }
.chip-label {
  text-transform: uppercase; letter-spacing: 0.08em;
  font-weight: 700; font-size: 10.5px;
  color: var(--text-3);
}
.chip-prefix, .chip-suffix {
  color: var(--text-3); font-size: 12px;
}
.chip-input input {
  background: transparent; border: 0; outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px; font-weight: 700;
  letter-spacing: -0.01em;
  width: 60px;
  text-align: right;
  padding: 0;
}
.chip-input:has(input:focus) {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.globals-hint {
  font-size: 11.5px; color: var(--text-4);
  margin-left: 4px;
}

/* =========================================================
   Bulk raw-mat prices panel
   ========================================================= */
.bulk-panel {
  margin-bottom: 14px;
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: var(--r-md);
  overflow: hidden;
}
.bulk-summary {
  list-style: none;
  cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  user-select: none;
  transition: background 0.15s var(--ease);
}
.bulk-summary::-webkit-details-marker { display: none; }
.bulk-summary:hover { background: var(--surface-2); }
.bulk-title {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  font-size: 10.5px;
  color: var(--text-3);
}
.bulk-hint {
  color: var(--text-4);
  font-size: 11.5px;
}
.bulk-hint #bulk-family-name {
  color: var(--text-2);
  font-weight: 700;
}
.bulk-chev {
  margin-left: auto;
  width: 7px; height: 7px;
  border-right: 1.5px solid var(--text-4);
  border-bottom: 1.5px solid var(--text-4);
  transform: rotate(45deg);
  transition: transform 0.2s var(--ease), border-color 0.15s var(--ease);
}
.bulk-panel[open] .bulk-chev {
  transform: rotate(-135deg);
  border-color: var(--accent);
}
.bulk-grid {
  padding: 4px 14px 12px;
  border-top: 1px solid var(--border);
}
.bulk-row {
  display: grid;
  grid-template-columns: 1fr 64px 150px auto;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-top: 1px dashed var(--border-2);
}
.bulk-row:first-child { border-top: 0; }
.bulk-row .bulk-lbl {
  color: var(--text-2);
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.bulk-varies {
  font-size: 9.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 6px;
  border-radius: 5px;
  border: 1px solid var(--accent-mid);
}
.bulk-row .bulk-unit {
  color: var(--text-4);
  font-size: 11px;
  text-align: right;
  letter-spacing: -0.005em;
}
.bulk-input-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  border-radius: var(--r-sm);
  padding: 4px 8px;
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.bulk-input-wrap .pre {
  color: var(--text-4);
  font-size: 11px;
}
.bulk-input-wrap input {
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: -0.01em;
  width: 100px;
  text-align: right;
  padding: 0;
  font-variant-numeric: tabular-nums;
}
.bulk-input-wrap:focus-within {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.bulk-apply {
  font-family: inherit;
  background: var(--surface-3);
  border: 1px solid var(--border-2);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-2);
  cursor: pointer;
  letter-spacing: -0.005em;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), color 0.15s var(--ease), transform 0.12s var(--ease);
}
.bulk-apply:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-on-acc);
  box-shadow: 0 1px 2px rgba(249, 133, 19, 0.35);
}
.bulk-apply:active { transform: scale(0.97); }

@media (max-width: 600px) {
  .bulk-row {
    grid-template-columns: 1fr auto;
    gap: 6px 10px;
  }
  .bulk-row .bulk-lbl { grid-column: 1; }
  .bulk-row .bulk-unit { grid-column: 2; }
  .bulk-input-wrap { grid-column: 1; }
  .bulk-input-wrap input { width: 100%; }
  .bulk-apply { grid-column: 2; }
}

/* =========================================================
   Tabs / segmented controls
   ========================================================= */
.filters {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin: 0 0 14px; align-items: center;
  justify-content: space-between;
}
.seg {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border-2);
  border-radius: 999px;
  padding: 3px;
  position: relative;
  box-shadow: 0 1px 0 rgba(17, 17, 68, 0.03);
}
.seg button {
  background: transparent;
  color: var(--text-3);
  border: 0;
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 500;
  font-family: inherit;
  letter-spacing: -0.01em;
  cursor: pointer;
  transition: color 0.15s var(--ease), background 0.15s var(--ease);
  white-space: nowrap;
}
.seg button:hover { color: var(--text-2); }
.seg button.active {
  background: var(--accent);
  color: var(--text-on-acc);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(249, 133, 19, 0.35);
}
.seg-view { display: inline-flex; }
@media (max-width: 600px) { .seg-view { display: none; } }

/* =========================================================
   Cards grid — product cost cards
   ========================================================= */
.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
@media (min-width: 720px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1000px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
}

.cost-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 16px 18px;
  cursor: pointer;
  position: relative;
  transition:
    background 0.15s var(--ease),
    border-color 0.15s var(--ease),
    transform 0.12s var(--ease),
    box-shadow 0.15s var(--ease);
}
.cost-card:hover {
  border-color: var(--border-2);
  box-shadow: var(--shadow-1);
}
.cost-card.open {
  border-color: var(--accent-mid);
  box-shadow: var(--shadow-2);
  cursor: default;
}
.cost-card.open::before {
  content: "";
  position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: linear-gradient(90deg, var(--accent), var(--cool));
  border-radius: var(--r-lg) var(--r-lg) 0 0;
}

.cc-top {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 12px;
}
.cc-name-block { min-width: 0; flex: 1; }
.cc-name {
  font-weight: 700; font-size: 17px;
  letter-spacing: -0.025em;
  color: var(--text);
  display: inline-flex; align-items: center; gap: 8px;
}
.cc-name .euca-tag {
  font-size: 9.5px; font-weight: 700;
  padding: 2px 6px; border-radius: 5px;
  letter-spacing: 0.04em;
  background: var(--cool-soft); color: var(--text-2);
  border: 1px solid var(--cool-mid);
}
.cc-sub {
  color: var(--text-3); font-size: 11.5px;
  margin-top: 4px;
  text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600;
}
.cc-fob {
  text-align: right;
  white-space: nowrap;
}
.cc-fob .price {
  font-size: 22px; font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--text);
  line-height: 1;
}
.cc-fob .label {
  display: block;
  font-size: 9.5px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--accent);
  margin-bottom: 4px;
}
.cc-fob .unit {
  font-size: 11px; color: var(--text-3);
  margin-left: 2px;
  letter-spacing: -0.005em;
  font-weight: 600;
}
.cc-meta {
  display: flex; gap: 14px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed var(--border-2);
  font-size: 12px; color: var(--text-3);
}
.cc-meta b {
  color: var(--text); font-weight: 600;
  margin-left: 4px;
}

/* expand indicator */
.cc-chevron {
  width: 18px; height: 18px;
  border-right: 1.5px solid var(--text-4);
  border-bottom: 1.5px solid var(--text-4);
  transform: rotate(45deg);
  transition: transform 0.2s var(--ease), border-color 0.15s var(--ease);
  position: absolute;
  right: 18px; bottom: 18px;
  opacity: 0.55;
  width: 8px; height: 8px;
}
.cost-card.open .cc-chevron {
  transform: rotate(-135deg);
  border-color: var(--accent);
  opacity: 1;
}

/* =========================================================
   Editor — inline expansion
   ========================================================= */
.editor {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  animation: fadeUp 0.22s var(--ease) both;
}
.editor h4 {
  margin: 0 0 10px;
  font-size: 10.5px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--text-3);
}
.cost-rows {
  display: flex; flex-direction: column;
  gap: 4px;
}
.cost-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 7px 0;
  border-top: 1px dashed var(--border-2);
  font-size: 13px;
}
.cost-row:first-child { border-top: 0; }
.cost-row .lbl {
  color: var(--text-2);
  font-weight: 500;
  letter-spacing: -0.005em;
}
.cost-row .lbl .sub-unit {
  color: var(--text-4); font-size: 11px;
  margin-left: 6px; font-weight: 400;
}
.num-input {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  border-radius: var(--r-sm);
  padding: 4px 8px;
  transition: border-color 0.15s var(--ease), background 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.num-input input {
  background: transparent; border: 0; outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 13px; font-weight: 600;
  letter-spacing: -0.01em;
  width: 64px;
  text-align: right;
  padding: 0;
}
.num-input .pre { color: var(--text-4); font-size: 11px; }
.num-input .post { color: var(--text-4); font-size: 11px; }
.num-input:focus-within {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.cost-row .out {
  color: var(--text);
  font-weight: 700;
  font-size: 13px;
  text-align: right;
  min-width: 80px;
  letter-spacing: -0.01em;
}
.cost-row.qp { background: rgba(155, 172, 216, 0.05); }
.cost-row.qp .out { color: var(--text-2); }
.num-input .post { color: var(--text-4); font-size: 10.5px; letter-spacing: -0.005em; }

/* breakdown summary */
.summary {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  border-radius: var(--r-md);
  font-size: 12.5px;
  color: var(--text-3);
}
.summary .line {
  display: flex; justify-content: space-between;
  padding: 3px 0;
}
.summary .line b {
  color: var(--text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.summary .line.fob {
  margin-top: 6px; padding-top: 8px;
  border-top: 1px solid var(--border-2);
  font-size: 14px;
}
.summary .line.fob b {
  color: var(--accent);
  font-size: 18px;
  letter-spacing: -0.02em;
}

.editor-actions {
  display: flex; justify-content: flex-end; gap: 6px;
  margin-top: 10px;
}
.btn-ghost {
  background: transparent;
  color: var(--text-3);
  border: 1px solid var(--border-2);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 11.5px; font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  letter-spacing: -0.005em;
  transition: color 0.15s var(--ease), background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.btn-ghost:hover { color: var(--text-2); border-color: var(--border-3); background: var(--surface-3); }
.btn-ghost.danger:hover { color: var(--bad); border-color: rgba(178, 58, 42, 0.3); background: var(--bad-soft); }

/* =========================================================
   Table view
   ========================================================= */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: auto;
  box-shadow: var(--shadow-1);
}
.cost-table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
}
.cost-table th, .cost-table td {
  padding: 9px 12px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  white-space: nowrap;
  letter-spacing: -0.005em;
  vertical-align: middle;
  min-width: 92px;
}
.cost-table th:last-child, .cost-table td:last-child {
  border-right: 0;
}
.cost-table th:first-child, .cost-table td:first-child {
  text-align: left;
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 1;
  border-right: 1px solid var(--border-2);
  font-weight: 600; color: var(--text-2);
  min-width: 200px;
}
.cost-table thead th {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.02em;
  position: sticky; top: 0;
  border-bottom: 1px solid var(--border-3);
  text-align: right;
}
.cost-table thead th:first-child {
  background: var(--surface-2);
  z-index: 2;
  text-align: left;
}

/* Component group: thicker separator after the derived (฿/m³) row */
.cost-table tbody tr.row-comp-end td {
  border-bottom: 2px solid var(--border-2);
}
.cost-table tbody tr.row-derived td {
  background: var(--surface-2);
  color: var(--text-2);
  font-weight: 600;
}
.cost-table tbody tr.row-derived td:first-child {
  background: var(--surface-2);
  color: var(--text-2);
}

.cost-table tbody tr:hover td:not(:first-child) { background: var(--surface-2); }
.cost-table tbody tr.row-derived:hover td:not(:first-child) { background: var(--surface-3); }

/* Top summary block: Ex-fac ฿ → Ex-fac $ → FOB $ */
.cost-table tbody tr.row-summary td {
  background: var(--surface-3);
  color: var(--text);
  font-weight: 700;
  font-size: 13px;
}
.cost-table tbody tr.row-summary td:first-child { background: var(--surface-3); }

.cost-table tbody tr.row-fob td {
  background: var(--accent-soft);
  font-weight: 800;
  color: var(--accent);
  font-size: 14.5px;
  border-bottom: 2px solid var(--accent-mid);
}
.cost-table tbody tr.row-fob td:first-child {
  color: var(--accent);
  background: var(--accent-soft);
  letter-spacing: 0.01em;
}

/* Inputs fill cell, right-align consistently with text cells */
.cost-table td input {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: -0.01em;
  width: 100%;
  text-align: right;
  padding: 4px 6px;
  margin: -4px -6px;
  font-variant-numeric: tabular-nums;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.cost-table td input:hover { background: var(--bg-2); border-color: var(--border-2); }
.cost-table td input:focus {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* =========================================================
   Reduced motion
   ========================================================= */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
