:root {
  --brand:       #1e40af;
  --brand-dark:  #1e3a8a;
  --accent:      #f59e0b;
  --bg:          #f4f6fb;
  --card:        #ffffff;
  --text:        #0f172a;
  --muted:       #64748b;
  --border:      #e2e8f0;
  --danger:      #dc2626;
  --success:     #16a34a;
}

* { box-sizing: border-box; }

html, body { touch-action: manipulation; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-size: 16px;
}

header.brand {
  background: white;
  padding: 8px 16px;
  border-bottom: 3px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  position: relative;
  min-height: 52px;
}
header.brand .logo { height: 36px; width: auto; max-width: 160px; object-fit: contain; }
header.brand .subtitle { font-size: 16px; font-weight: 600; color: var(--text); margin: 0; }
header.brand .user-bar {
  position: absolute;
  top: 50%;
  right: 12px;
  transform: translateY(-50%);
  font-size: 12px;
  color: var(--muted);
  display: flex;
  gap: 10px;
  align-items: center;
}
header.brand .user-bar a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
header.brand .back {
  position: absolute;
  top: 50%;
  left: 12px;
  transform: translateY(-50%);
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
}
@media (max-width: 520px) {
  header.brand .user-bar span { display: none; }
  header.brand .subtitle { font-size: 14px; }
  header.brand .logo { height: 30px; }
}

main {
  flex: 1;
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 16px 32px;
  width: 100%;
}

footer.copyright {
  text-align: center;
  padding: 14px 16px 16px;
  color: var(--text);
  font-size: 13px;
  background: var(--bg);
}
footer.copyright span { font-weight: 600; }

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 20px;
  border: 1px solid var(--border);
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 18px; }
.card h3 { margin: 0 0 12px; font-size: 16px; }

.tile-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 480px) { .tile-grid { grid-template-columns: 1fr; } }

.tile {
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 36px 20px;
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  transition: transform 0.1s, border-color 0.1s;
  display: block;
}
.tile:active { transform: scale(0.98); border-color: var(--accent); }
.tile.primary { background: var(--accent); color: white; border-color: var(--accent); }
.tile .icon { font-size: 44px; display: block; margin-bottom: 8px; }
.tile small { display: block; font-size: 13px; font-weight: 400; opacity: 0.7; margin-top: 6px; }

button, .btn {
  padding: 14px 24px;
  font-size: 17px;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary   { background: var(--accent);  color: white; }
.btn-secondary { background: var(--border);  color: var(--text); }
.btn-danger    { background: var(--danger);  color: white; }
.btn-success   { background: var(--success); color: white; }
.btn-block     { width: 100%; }
.btn-sm        { padding: 8px 14px; font-size: 14px; }
button:active, .btn:active { transform: scale(0.98); }
button:disabled, .btn:disabled { opacity: 0.5; cursor: not-allowed; }

input, select, textarea {
  width: 100%;
  padding: 12px 14px;
  font-size: 17px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: white;
  font-family: inherit;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
input[readonly] { background: #f8fafc; color: var(--muted); }
textarea { resize: vertical; min-height: 80px; }
label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.form-grid > * { min-width: 0; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }

input[type="date"],
input[type="time"],
input[type="datetime-local"] {
  max-width: 100%;
  min-width: 0;
  display: block;
}

.send-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
}
.send-row:last-child { border-bottom: none; }
.send-row .who {
  flex: 1 1 100%;
  min-width: 0;
  color: var(--muted);
  font-size: 14px;
  word-break: break-all;
}
.send-row .when {
  color: var(--muted);
  font-size: 12px;
  margin-left: auto;
}

.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 2px solid var(--border);
  margin-bottom: 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.tab {
  background: transparent;
  border: none;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  border-radius: 0;
  white-space: nowrap;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 8px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { background: #f8fafc; font-weight: 600; }
.table-scroll { overflow-x: auto; }

.items-list { display: flex; flex-direction: column; gap: 8px; }

.item-row {
  position: relative;
  display: grid;
  gap: 8px 12px;
  padding: 12px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 2fr) minmax(0, 0.6fr) minmax(0, 0.9fr) minmax(0, 1.1fr) minmax(0, 1fr) auto;
  grid-template-areas: "pn desc qty price discount total rm";
  align-items: end;
}
.item-row .cell { min-width: 0; }
.item-row .col-pn       { grid-area: pn; }
.item-row .col-desc     { grid-area: desc; }
.item-row .col-qty      { grid-area: qty; }
.item-row .col-price    { grid-area: price; }
.item-row .col-discount { grid-area: discount; }
.item-row .col-total    { grid-area: total; text-align: right; }
.item-row .discount-pair {
  display: grid;
  grid-template-columns: 52px 1fr;
  gap: 4px;
}
.item-row .discount-pair select {
  padding: 10px 4px;
  font-size: 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: white;
  text-align: center;
}
.item-row .discount-pair input { text-align: right; }
.item-row label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  margin-bottom: 4px;
}
.item-row input {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  font-size: 15px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: white;
  font-family: inherit;
  color: var(--text);
}
.item-row input:focus { outline: none; border-color: var(--accent); }
.item-row .qty, .item-row .price { text-align: right; }
.item-row .total { font-weight: 700; font-size: 16px; padding: 10px 0; }
.item-row .rm {
  grid-area: rm;
  align-self: end;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  background: var(--danger);
  color: white;
  border: none;
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  margin-bottom: 4px;
}
.item-row .rm:active { transform: scale(0.95); }

@media (max-width: 640px) {
  .item-row {
    background: #f8fafc;
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "pn       pn"
      "desc     desc"
      "qty      price"
      "discount discount"
      "total    total";
    padding: 14px 14px 12px;
    padding-right: 52px;
  }
  .item-row input { font-size: 16px; padding: 12px 14px; }
  .item-row .col-total {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding-top: 10px;
    border-top: 1px solid var(--border);
    margin-top: 4px;
  }
  .item-row .col-total label { margin-bottom: 0; }
  .item-row .total { font-size: 22px; padding: 0; }
  .item-row .rm {
    position: absolute;
    top: 10px;
    right: 10px;
    margin-bottom: 0;
  }
}

@media (max-width: 640px) {
  #histTable, #histTable thead, #histTable tbody,
  #histTable tr, #histTable th, #histTable td { display: block; }
  #histTable thead { display: none; }

  #histTable tbody tr {
    background: white;
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    margin-bottom: 10px;
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "ordno    status"
      "date     date"
      "client   client"
      "well     well"
      "sp       total"
      "actions  actions";
    gap: 4px 8px;
  }
  #histTable td { padding: 0; border: none; }
  #histTable tbody td:nth-child(1) { grid-area: ordno; font-weight: 700; font-size: 16px; }
  #histTable tbody td:nth-child(2) { grid-area: date; color: var(--muted); font-size: 13px; }
  #histTable tbody td:nth-child(3) { grid-area: client; font-weight: 600; padding-top: 6px; }
  #histTable tbody td:nth-child(4) { grid-area: well; color: var(--muted); font-size: 14px; }
  #histTable tbody td:nth-child(5) {
    grid-area: sp;
    color: var(--muted);
    font-size: 13px;
    padding-top: 6px;
    align-self: end;
  }
  #histTable tbody td:nth-child(6) {
    grid-area: total;
    text-align: right;
    font-weight: 700;
    font-size: 18px;
    padding-top: 6px;
    align-self: end;
  }
  #histTable tbody td:nth-child(7) { grid-area: status; text-align: right; }
  #histTable tbody td:nth-child(8) {
    grid-area: actions;
    display: flex;
    gap: 8px;
    padding-top: 10px;
    margin-top: 6px;
    border-top: 1px solid var(--border);
    flex-wrap: wrap;
  }
  #histTable tbody td:nth-child(8) .btn {
    flex: 1 1 0;
    min-width: 80px;
    padding: 10px 8px;
    font-size: 14px;
    text-align: center;
  }
  #histTable tbody tr.empty-row {
    display: block;
    background: transparent;
    border: none;
    padding: 20px 0;
    margin: 0;
  }
  #histTable tbody tr.empty-row td { text-align: center; }
}

.autocomplete { position: relative; }
.autocomplete-list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  background: white;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
  max-height: 260px; overflow-y: auto;
  z-index: 50; display: none;
}
.autocomplete-list.open { display: block; }
.autocomplete-item { padding: 10px 12px; font-size: 15px; cursor: pointer; border-bottom: 1px solid var(--border); }
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover, .autocomplete-item.active { background: #fff7ed; }
.autocomplete-item .small { display: block; font-size: 12px; color: var(--muted); }
.autocomplete-empty { padding: 12px; color: var(--muted); font-size: 14px; text-align: center; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: white;
  padding: 12px 20px; border-radius: 10px;
  font-size: 15px; opacity: 0;
  transition: opacity 0.2s;
  z-index: 1000;
  max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15,23,42,0.55);
  z-index: 200;
  display: none;
  align-items: center; justify-content: center;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: white;
  border-radius: 16px;
  padding: 20px;
  max-width: 460px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.modal h3 { margin: 0 0 12px; font-size: 18px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--border);
  color: var(--text);
}
.badge.sent { background: #dcfce7; color: #166534; }
.badge.draft { background: #fef3c7; color: #92400e; }
.badge.admin { background: #fee2e2; color: #991b1b; }
.badge.manager { background: #dbeafe; color: #1e40af; }
.badge.salesperson { background: #e0e7ff; color: #3730a3; }
.badge.rush        { background: #fee2e2; color: #991b1b; }

.rush-label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: #fff7ed;
  border: 1.5px solid #fed7aa;
  border-radius: 10px;
  cursor: pointer;
  font-size: 16px;
  text-transform: none;
  letter-spacing: normal;
  color: var(--text);
  margin-bottom: 0;
}
.rush-label input[type=checkbox] {
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

.tpl-item {
  position: relative;
  display: grid;
  gap: 10px;
  padding: 14px;
  padding-right: 50px;
  margin-bottom: 10px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.tpl-item .field { min-width: 0; }
.tpl-item .field-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  margin-bottom: 4px;
}
.tpl-item input, .tpl-item select {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  font-size: 15px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: white;
}
.tpl-item .pair-qty-price {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.tpl-item .pair-discount {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 4px;
}
.tpl-item .qty, .tpl-item .price, .tpl-item .disc-value { text-align: right; }
.tpl-item .rm-item {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  font-size: 18px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.muted { color: var(--muted); }
.right { text-align: right; }
.center { text-align: center; }
.totals-row { font-size: 18px; font-weight: 600; padding-top: 12px; border-top: 2px solid var(--border); margin-top: 8px; display: flex; justify-content: space-between; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 32px 16px;
  font-size: 15px;
}

.spinner {
  display: inline-block;
  width: 18px; height: 18px;
  border: 3px solid #e2e8f0;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.action-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}
.action-bar button { padding: 18px 12px; font-size: 16px; }
@media (max-width: 480px) { .action-bar { grid-template-columns: 1fr; } }
