/* Mobile-first. Breakpoint desktop: 768px. */

:root {
  --bg: #14161a;
  --surface: #1e2128;
  --surface-2: #262a33;
  --line: #333945;
  --text: #e8eaed;
  --muted: #9aa3b0;
  --accent: #f59e0b;
  --ok: #34d399;
  --danger: #f87171;
  --radius: 12px;
  --tabs-h: 56px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --line: #dfe3e8;
    --text: #16181d;
    --muted: #5f6875;
    --accent: #b45309;
  }
}

* { box-sizing: border-box; }

/* Quy tắc [hidden] của trình duyệt là `display: none` KHÔNG có class, nên bất kỳ
   `.foo { display: flex }` nào cũng thắng nó và phần tử vẫn hiện dù đã set hidden.
   Đã dính đúng lỗi này với .price-bar và .btn. Chặn một lần cho toàn bộ app. */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 0 calc(var(--tabs-h) + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1 { font-size: 1.25rem; margin: 0 0 1rem; }
h2 { font-size: .95rem; margin: 0 0 .75rem; color: var(--muted); font-weight: 600; }

main { padding: 1rem; max-width: 1100px; margin: 0 auto; }

.screen { display: none; }
.screen.active { display: block; }

/* --- Tabs ---------------------------------------------------------------- */

.tabs {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  z-index: 10;
}

.tabs a {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: .875rem;
  font-weight: 500;
  border-top: 2px solid transparent;
  margin-top: -1px;
}

.tabs a.active { color: var(--accent); border-top-color: var(--accent); }

/* --- Cards & fields ------------------------------------------------------ */

.card, .result-card, .block {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.field-row { display: flex; flex-wrap: wrap; gap: .75rem; }
.field-row.tight { gap: .4rem; flex-wrap: nowrap; }
.field-row.tight .btn { flex: none; }
.field { display: flex; flex-direction: column; gap: .3rem; flex: 1 1 8rem; margin-bottom: .75rem; }
.field.grow { flex: 3 1 12rem; }
.field.narrow { flex: 0 0 4rem; }
.field > span { font-size: .8rem; color: var(--muted); }
.field small { font-size: .75rem; color: var(--muted); line-height: 1.35; }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .6rem .7rem;
  width: 100%;
  min-width: 0;
}

input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="color"] { padding: .2rem; height: 2.6rem; }

/* Ẩn spinner: cuộn chuột trên spinner hay đổi nhầm giá trị.
   js/ui.js còn chặn cả sự kiện wheel. */
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* --- Buttons ------------------------------------------------------------- */

.btn {
  font: inherit;
  font-weight: 500;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: .6rem 1rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #14161a; border-color: var(--accent); }
.btn.ghost { background: transparent; width: 100%; }
.form-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* --- Kết quả ------------------------------------------------------------- */

/* Giá vốn là con số to nhất: đó là sự thật về công việc, và là mức
   không được phép báo thấp hơn. Giá bán chỉ là gợi ý, chỉnh theo từng khách. */
.price-main .label, .price-second .label { display: block; font-size: .8rem; color: var(--muted); }
.price-big { display: block; font-size: 2.25rem; font-weight: 700; line-height: 1.1; color: var(--accent); }
.price-main .hint { font-size: .75rem; color: var(--muted); }
/* Placeholder không được trông như một con số thật hay một thanh bar. */
.price-big.blank, .price-second strong.blank { color: var(--line); font-weight: 400; }
.price-second { margin-top: .9rem; padding-top: .9rem; border-top: 1px solid var(--line); }
.price-second strong { display: block; font-size: 1.35rem; }
.empty-hint { color: var(--muted); font-size: .85rem; margin: .75rem 0 0; }

.breakdown { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.breakdown ul { list-style: none; margin: 0; padding: 0; }
.breakdown li { margin-bottom: .7rem; }

.bd-head { display: flex; justify-content: space-between; gap: .5rem; font-size: .875rem; }
.bd-head .pct { color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { height: 5px; background: var(--surface-2); border-radius: 3px; margin-top: .3rem; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bd-sub { list-style: none; margin: .4rem 0 0; padding: 0 0 0 .75rem; border-left: 2px solid var(--line); }
.bd-sub li { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-bottom: .25rem; }

.totals { margin-top: 1rem; padding-top: .75rem; border-top: 1px solid var(--line); font-size: .875rem; }
.totals > div { display: flex; justify-content: space-between; margin-bottom: .3rem; }

/* --- Dòng nhựa ----------------------------------------------------------- */

.block-head { display: flex; justify-content: space-between; align-items: baseline; }
.line { display: grid; grid-template-columns: 1fr 5.5rem auto; gap: .5rem; align-items: center; margin-bottom: .5rem; }
.line-cost { grid-column: 2 / 3; text-align: right; font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.line-remove {
  grid-row: 1 / 3;
  grid-column: 3;
  background: none; border: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer; padding: .35rem .5rem;
}
.line-remove:disabled { opacity: .25; cursor: default; }
.line.invalid select { border-color: var(--danger); }

/* --- Danh sách dạng item (nhựa + sản phẩm đã lưu) ------------------------- */
/* Dùng chung .item-list cho cả #material-list và #products-list. Đổi tên từ
   .material-list — copy CSS thành .product-list riêng sẽ lệch nhau dần mỗi
   lần sửa spacing/màu. Một khối, hai danh sách. */

.item-list { list-style: none; margin: 0; padding: 0; }
.item-list li {
  display: flex; align-items: center; gap: .75rem;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: .75rem 1rem; margin-bottom: .5rem;
}
.swatch { width: 1.1rem; height: 1.1rem; border-radius: 50%; border: 1px solid var(--line); flex: none; }
.item-list .info { flex: 1; min-width: 0; }
.item-list .name { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-list .meta { font-size: .8rem; color: var(--muted); }
.item-list .acts { display: flex; gap: .35rem; flex-wrap: wrap; justify-content: flex-end; }
.item-list .acts button {
  background: none; border: 1px solid var(--line); border-radius: 6px;
  color: var(--muted); cursor: pointer; padding: .3rem .55rem; font-size: .8rem;
}

/* Sản phẩm đã lưu: bung breakdown ngay trong item, nên li không còn là 1 hàng
   flex đơn — .row giữ layout cũ (tên/giá bên trái, nút bên phải), detail nằm
   dưới khi mở. */
.item-list li.product-item { display: block; }
.item-list .row { display: flex; align-items: center; gap: .75rem; }

/* --- Khối Lưu (màn Tính giá) ----------------------------------------------- */

.save-block { margin-top: 1rem; padding-top: 1rem; border-top: 1px solid var(--line); }
.save-block .form-actions { margin-bottom: .5rem; }

/* --- Thông báo ----------------------------------------------------------- */

.error-box, .msg, .nudge, .empty-state {
  border-radius: 8px; padding: .75rem; font-size: .875rem; margin-bottom: .75rem;
}
.error-box { background: color-mix(in srgb, var(--danger) 15%, transparent); border: 1px solid var(--danger); }
.msg { background: color-mix(in srgb, var(--ok) 15%, transparent); border: 1px solid var(--ok); }
.msg.bad { background: color-mix(in srgb, var(--danger) 15%, transparent); border-color: var(--danger); }
.nudge { background: color-mix(in srgb, var(--accent) 15%, transparent); border: 1px solid var(--accent); }
.empty-state { background: var(--surface-2); border: 1px dashed var(--line); }
.empty-state p { margin: 0 0 .75rem; color: var(--muted); }

/* --- Thanh giá dính (mobile) --------------------------------------------- */

.price-bar {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  display: flex;
  gap: 1rem;
  padding: .55rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: 0 -6px 16px rgb(0 0 0 / .18);
  z-index: 9;
}

.price-bar > div { display: flex; align-items: baseline; gap: .4rem; }
.price-bar span { font-size: .72rem; color: var(--muted); }
.price-bar strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
/* Ô đầu là giá vốn — nhấn mạnh, khớp với thứ tự ưu tiên ở thẻ kết quả. */
.price-bar > div:first-child strong { color: var(--accent); font-size: 1.2rem; }

/* Mobile: ô nhập lên trước, thẻ kết quả xuống dưới.
   Giá vẫn luôn nhìn thấy nhờ thanh dính ở đáy. */
.layout { display: flex; flex-direction: column; }
.col-input { order: 1; }
.col-result { order: 2; }

.muted { color: var(--muted); font-size: .85rem; }
.version { text-align: center; color: var(--muted); font-size: .75rem; }

/* --- Desktop ------------------------------------------------------------- */

@media (min-width: 768px) {
  body { padding-bottom: 0; padding-top: var(--tabs-h); }
  /* Desktop hai cột: kết quả luôn nằm cạnh ô nhập, không cần thanh dính. */
  .price-bar { display: none !important; }
  .tabs { top: 0; bottom: auto; border-top: none; border-bottom: 1px solid var(--line); padding-bottom: 0; height: var(--tabs-h); }
  .tabs a { border-top: none; border-bottom: 2px solid transparent; }
  .tabs a.active { border-bottom-color: var(--accent); }

  /* Hai cột: nhập trái, kết quả phải. Đảo order để DOM vẫn ưu tiên kết quả trên mobile. */
  .layout { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start; }
  .col-input { order: 1; }
  .col-result { order: 2; position: sticky; top: calc(var(--tabs-h) + 1rem); }
}
