:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #9aa3ad;
  --border-dark: #30343b;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #0f4c81;
  --danger: #b42318;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Tahoma, sans-serif;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  background: #0f172a;
  color: white;
  align-items: center;
}

.topbar h1 { margin: 0 0 4px; font-size: 22px; }
.topbar p { margin: 0; color: #cbd5e1; }
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; }

button {
  border: 1px solid #cbd5e1;
  background: white;
  color: #111827;
  border-radius: 8px;
  padding: 9px 12px;
  font-weight: 700;
  cursor: pointer;
}
button:hover { filter: brightness(0.96); }
button.primary { background: var(--accent); color: white; border-color: var(--accent); }
button.danger { color: white; background: var(--danger); border-color: var(--danger); }
button.mini-danger { color: white; background: var(--danger); border-color: var(--danger); }

.layout {
  display: grid;
  grid-template-columns: 370px 1fr;
  gap: 16px;
  padding: 16px;
}

.panel {
  display: grid;
  gap: 16px;
  align-content: start;
}

.panel section {
  background: var(--panel);
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
}

.panel h2 { margin: 0 0 10px; font-size: 16px; }
label { display: block; font-size: 13px; font-weight: 700; margin-bottom: 10px; color: #374151; }
input {
  margin-top: 4px;
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 14px;
}
summary {
  cursor: pointer;
  font-weight: 700;
  color: #0f4c81;
  margin: 8px 0 10px;
}
.hint { color: var(--muted); font-size: 12px; line-height: 1.45; }
.button-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.header-editor { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.product-tools { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; margin-bottom: 10px; }

.master-list-wrap {
  max-height: 430px;
  overflow: auto;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
}
.master-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.master-table th,
.master-table td {
  border-bottom: 1px solid #e5e7eb;
  padding: 5px;
  vertical-align: middle;
}
.master-table th { background: #f8fafc; position: sticky; top: 0; z-index: 1; }
.master-table input {
  margin: 0;
  border-radius: 6px;
  padding: 6px 7px;
  font-size: 12px;
}
.master-table td:nth-child(1) { width: 95px; }
.master-table td:nth-child(3) { width: 70px; }
.master-table td:nth-child(4) { width: 96px; }
.master-actions { display: flex; flex-wrap: wrap; gap: 4px; }
.master-actions button { padding: 5px 7px; border-radius: 6px; font-size: 11px; }
.hint-cell { text-align: center; color: var(--muted); padding: 10px !important; }

.sheet-wrap {
  overflow: auto;
  padding-bottom: 24px;
}

.a4 {
  width: 210mm;
  min-height: 297mm;
  margin: 0 auto;
  background: white;
  padding: 11mm 8mm 10mm;
  box-shadow: 0 4px 18px rgba(15, 23, 42, 0.12);
}

.sheet-heading {
  position: relative;
  text-align: center;
  min-height: 24px;
  margin-bottom: 4px;
}

.sheet-heading h2 {
  margin: 0;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.hand-note {
  position: absolute;
  left: 8px;
  top: -2px;
  color: #1d4ed8;
  font-family: 'Brush Script MT', cursive;
  font-size: 18px;
  transform: rotate(-4deg);
  opacity: 0.8;
}

.sheet-date {
  position: absolute;
  right: 8px;
  top: 2px;
  font-size: 12px;
}

.table-scroll { overflow: visible; }
.stock-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-family: 'Times New Roman', serif;
  font-size: 11px;
  color: #111827;
}
.stock-table th,
.stock-table td {
  border: 1px solid #333;
  padding: 2px 3px;
  height: 20px;
  line-height: 1.05;
  vertical-align: middle;
}
.stock-table th {
  text-align: center;
  font-weight: 700;
  background: #f8fafc;
}
.stock-table td:nth-child(1),
.stock-table td:nth-child(3),
.stock-table td:nth-child(n+4):nth-child(-n+10) {
  text-align: center;
}
.stock-table .item-name { font-weight: 700; }
.stock-table input {
  border: 0;
  border-radius: 0;
  padding: 0;
  margin: 0;
  width: 100%;
  min-height: 16px;
  text-align: center;
  font-family: 'Times New Roman', serif;
  font-size: 11px;
  background: transparent;
}
.stock-table input.remark-input { text-align: left; }
.stock-table input:focus { outline: 1px solid var(--accent); background: #eff6ff; }

.code-col { width: 80px; }
.item-col { width: 285px; }
.price-col { width: 54px; }
.remark-col { width: 92px; }
.stock-table th:nth-child(4),
.stock-table th:nth-child(5),
.stock-table th:nth-child(6),
.stock-table th:nth-child(7),
.stock-table th:nth-child(8),
.stock-table th:nth-child(9),
.stock-table th:nth-child(10) { width: 45px; }

.signature-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  margin-top: 12px;
  font-family: 'Times New Roman', serif;
  font-size: 14px;
}
.signature-row div { border-bottom: 1px dotted #555; padding-bottom: 4px; }

@media (max-width: 980px) {
  .topbar { align-items: stretch; flex-direction: column; }
  .layout { grid-template-columns: 1fr; }
  .panel { grid-template-columns: 1fr; }
  .a4 { transform-origin: top left; width: 100%; min-height: auto; padding: 10px; }
  .sheet-heading { text-align: left; padding-top: 34px; }
  .hand-note { top: 0; }
  .sheet-date { top: 0; right: 0; }
  .table-scroll { overflow-x: auto; }
  .stock-table { min-width: 920px; }
}

@page { size: A4 portrait; margin: 8mm; }
@media print {
  body { background: white; }
  .no-print { display: none !important; }
  .layout { display: block; padding: 0; }
  .sheet-wrap { overflow: visible; padding: 0; }
  .a4 { width: auto; min-height: auto; margin: 0; padding: 0; box-shadow: none; }
  .stock-table th, .stock-table td { height: 18px; padding: 1px 2px; }
  .stock-table input { font-size: 10.5px; }
}

.hidden { display: none !important; }
.button-grid { grid-template-columns: 1fr 1fr; }
.button-grid .wide { grid-column: 1 / -1; }
.history-panel {
  margin-top: 10px;
  border: 1px solid #e5e7eb;
  background: #f8fafc;
  border-radius: 12px;
  padding: 10px;
}
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
}
.history-head p { margin: 3px 0 0; }
.history-list {
  display: grid;
  gap: 8px;
  max-height: 300px;
  overflow: auto;
}
.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid #e5e7eb;
  background: #fff;
  border-radius: 10px;
  padding: 9px;
}
.history-meta { display: grid; gap: 2px; font-size: 12px; }
.history-meta strong { font-size: 13px; color: #111827; }
.history-meta span { color: #374151; }
.history-meta small { color: var(--muted); }
.history-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.history-actions button { padding: 6px 9px; font-size: 12px; }
.history-empty {
  color: var(--muted);
  background: #fff;
  border: 1px dashed #cbd5e1;
  border-radius: 10px;
  padding: 10px;
  text-align: center;
  font-size: 13px;
}

@media (max-width: 520px) {
  .button-grid { grid-template-columns: 1fr; }
  .history-head { flex-direction: column; }
  .history-item { grid-template-columns: 1fr; }
}
