:root {
  --ink: #20242a;
  --muted: #68717d;
  --line: #d9dee4;
  --paper: #fbfaf7;
  --panel: #ffffff;
  --accent: #0f766e;
  --accent-dark: #115e59;
  --accent-hot: #e0a100;
  --accent-red: #c2410c;
  --warn: #b45309;
  --soft: #eef6f4;
  --shadow: 0 18px 50px rgba(20, 24, 32, .12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: Arial, Helvetica, sans-serif;
  overflow-x: hidden;
}

button, input, select, textarea {
  font: inherit;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  cursor: pointer;
  font-weight: 700;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  background: #fff;
  color: var(--ink);
}

input::placeholder, textarea::placeholder {
  color: #9aa6b2;
}

label {
  display: grid;
  gap: 7px;
  color: var(--accent-dark);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
}

fieldset {
  border: 1px solid #cbded9;
  border-radius: 8px;
  padding: 18px 14px 14px;
  margin: 0;
  background: linear-gradient(180deg, #fbfefd, #fff);
}

legend {
  padding: 5px 10px;
  border: 1px solid #cbded9;
  border-radius: 8px;
  background: #e9f5f2;
  color: var(--accent-dark);
  font-weight: 800;
  font-size: 13px;
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 8px; }
h2 { margin-bottom: 10px; }
h3 { margin-bottom: 12px; }

.hidden { display: none !important; }
#printArea { display: none; }
.muted { color: var(--muted); }
.error { color: #b91c1c; min-height: 20px; }
.eyebrow {
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  margin-bottom: 6px;
  text-transform: uppercase;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 22px;
  background:
    linear-gradient(rgba(255,255,255,.72), rgba(255,255,255,.82)),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='900' height='620' viewBox='0 0 900 620'%3E%3Crect width='900' height='620' fill='%23efe7db'/%3E%3Cg transform='translate(92 64)'%3E%3Crect x='0' y='0' width='300' height='210' fill='%23865936'/%3E%3Crect x='32' y='32' width='236' height='146' fill='%23f8f6ed'/%3E%3C/g%3E%3Cg transform='translate(450 105)'%3E%3Crect x='0' y='0' width='330' height='240' fill='%230f766e'/%3E%3Crect x='38' y='38' width='254' height='164' fill='%23fafafa'/%3E%3C/g%3E%3Cg transform='translate(250 360)'%3E%3Crect x='0' y='0' width='380' height='180' fill='%23a16207'/%3E%3Crect x='34' y='30' width='312' height='120' fill='%23fffaf0'/%3E%3C/g%3E%3C/svg%3E");
  background-size: cover;
}

.login-panel {
  width: calc(100vw - 44px);
  max-width: 340px;
  background: rgba(255,255,255,.94);
  border: 1px solid rgba(255,255,255,.8);
  border-radius: 8px;
  padding: 26px;
  box-shadow: var(--shadow);
}

.login-panel p {
  overflow-wrap: anywhere;
}

.stack { display: grid; gap: 14px; }
.primary { background: var(--accent); color: #fff; box-shadow: 0 8px 18px rgba(15, 118, 110, .22); }
.primary:hover { background: var(--accent-dark); }
.secondary { background: #d2f2ea; color: var(--accent-dark); border: 1px solid #8ed8ca; }
.ghost { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.quiet { background: #fff6d7; color: #7a5200; border: 1px solid #e7c75b; }
.danger { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }

.app {
  min-height: 100vh;
  padding-bottom: 90px;
  overflow-x: hidden;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.tabs {
  position: sticky;
  top: 83px;
  z-index: 4;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 14px;
  background: rgba(251,250,247,.96);
  border-bottom: 1px solid var(--line);
}

.tab {
  white-space: nowrap;
  background: #fff;
  color: #29313b;
  border: 1px solid var(--line);
}

.tab.active {
  background: var(--accent-dark);
  color: #fff;
}

.view {
  display: none;
  padding: 18px;
}

.active-view { display: block; }

.catalog-head, .section-head, .quote-title, .cart-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}

.search { max-width: 360px; }

.search-matches {
  display: grid;
  gap: 6px;
  max-height: 210px;
  overflow: auto;
  padding: 8px;
  border: 1px solid #b9d8d2;
  border-radius: 8px;
  background: #f8fffd;
}

.search-match {
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border: 1px solid #d5e8e3;
  background: #fff;
  color: var(--ink);
  font-weight: 700;
}

.catalog-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 14px;
}

.molding-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: left;
  padding: 14px 16px;
  color: var(--ink);
}

.molding-card-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.molding-card strong {
  display: block;
  font-size: 18px;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.molding-code {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.molding-open {
  color: var(--accent-dark);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}

.quote-panel {
  position: fixed;
  inset: 0;
  width: 100vw;
  min-height: 100dvh;
  z-index: 10;
  background: rgba(15, 23, 42, .48);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
}

.quote-card {
  width: 100%;
  max-width: 1160px;
  margin: 0 auto;
  background: var(--panel);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.quote-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(280px, .85fr);
  gap: 16px;
}

.quote-form, .cart, .totals, .actions, .inline-form, .config-card {
  display: grid;
  gap: 12px;
}

.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.three { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }

.check {
  display: flex;
  align-items: center;
  gap: 9px;
}

.check input { width: auto; }

.utility-row {
  display: flex;
  align-items: end;
  gap: 12px;
}

.utility { max-width: 180px; }

.calc-preview, .summary-strip {
  background: var(--soft);
  border: 1px solid #cfe5df;
  border-radius: 8px;
  padding: 12px;
}

.cart {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  align-self: start;
  position: sticky;
  top: 125px;
}

.cart-items {
  display: grid;
  gap: 10px;
  min-height: 80px;
}

#addQuoteItemBtn {
  width: 100%;
  border: 1px solid #8ed8ca;
}

.cart-item, .row-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fff;
}

.cart-item {
  display: grid;
  gap: 8px;
}

.cart-item-head, .row-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}

.mini {
  padding: 7px 9px;
  font-size: 12px;
}

.totals dl, #totalBox {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin: 0;
}

.totals dt { color: var(--muted); }
.totals dd { margin: 0; font-weight: 800; }

.totals .total-action {
  grid-column: 1 / -1;
}

.totals .total-action button {
  width: 100%;
}

.actions {
  grid-template-columns: repeat(2, 1fr);
}

.action-choice {
  min-height: 46px;
  padding: 10px;
  border: 1px solid #9eddd2;
  border-radius: 8px;
  background: #eefbf8;
  color: var(--ink);
}

.action-choice input {
  accent-color: var(--accent-red);
}

.save-final {
  grid-column: 1 / -1;
  background: var(--accent-red);
}

.save-final:hover {
  background: #9a3412;
}

.inline-form {
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  margin-bottom: 14px;
}

.import-box {
  border: 1px dashed #9aa6b2;
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 14px;
  background: #fff;
}

.import-box.compact {
  margin-bottom: 0;
  padding: 10px;
}

.table-list {
  display: grid;
  gap: 10px;
}

.row-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
}

.history-status {
  max-width: 220px;
  margin-top: 8px;
}

.settings-grid, .chart-grid {
  display: grid;
  grid-template-columns: minmax(260px, .9fr) minmax(320px, 1.1fr) minmax(280px, 1fr);
  gap: 14px;
  align-items: start;
}

.chart-grid { grid-template-columns: 1fr 1fr; }

.config-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  align-content: start;
  min-width: 0;
}

#materialForm {
  grid-template-columns: minmax(110px, .8fr) minmax(170px, 1.3fr) minmax(120px, .9fr);
  align-items: end;
}

#materialForm h3,
#materialForm button,
#materialConfigList {
  grid-column: 1 / -1;
}

.config-card h3, .cart h2, .view h2 {
  padding-bottom: 8px;
  border-bottom: 2px solid #d8ebe6;
}

.config-list-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #eef1f4;
  align-items: center;
}

.config-list-item.editable-row {
  grid-template-columns: minmax(130px, 1fr) minmax(92px, 120px) 34px;
}

.config-list-item .config-name,
.config-list-item .config-price {
  padding: 8px 9px;
  font-size: 13px;
}

.icon-danger {
  width: 34px;
  height: 34px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  color: #991b1b;
  border-color: #fca5a5;
  background: #fff5f5;
  font-weight: 800;
}

.config-list-item span {
  min-width: 0;
  overflow-wrap: anywhere;
}

#materialConfigList, #moldingConfigList {
  max-height: 420px;
  overflow: auto;
  padding-right: 4px;
}

#materialConfigList h4 {
  position: sticky;
  top: 0;
  margin: 10px 0 6px;
  padding: 7px 8px;
  border-radius: 8px;
  background: #e9f5f2;
  color: var(--accent-dark);
  z-index: 1;
}

.bars {
  display: grid;
  gap: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.bar-row {
  display: grid;
  grid-template-columns: 110px 1fr 80px;
  gap: 10px;
  align-items: center;
}

.bar-track {
  height: 20px;
  background: #edf1f2;
  border-radius: 8px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-hot));
}

@media (max-width: 840px) {
  .topbar { align-items: flex-start; }
  .tabs { top: 82px; }
  .catalog-head, .section-head, .quote-title { display: grid; }
  .search { max-width: none; }
  .quote-layout, .two, .three, .inline-form, .settings-grid, .chart-grid {
    grid-template-columns: 1fr;
  }
  .cart { position: static; }
  #materialForm {
    grid-template-columns: 1fr;
  }
  .row-card {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 14px;
  }
  #historyList .row-card strong {
    display: block;
    font-size: 16px;
    line-height: 1.25;
  }
  #historyList .row-card .muted {
    margin-bottom: 10px;
    line-height: 1.35;
  }
  .history-status {
    max-width: none;
  }
  .history-status select {
    min-height: 44px;
  }
  #historyList .row-actions {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    width: 100%;
  }
  #historyList .row-actions .mini {
    width: 100%;
    min-height: 42px;
  }
  .molding-card {
    padding: 12px;
    gap: 10px;
  }
  .molding-card strong {
    font-size: 16px;
  }
  .molding-open {
    font-size: 12px;
  }
}

@media (min-width: 841px) and (max-width: 1180px) {
  .settings-grid {
    grid-template-columns: 1fr 1fr;
  }
  #systemForm {
    grid-column: 1 / -1;
  }
  #materialConfigList, #moldingConfigList {
    max-height: 360px;
  }
}
