:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --faint: #9ca3af;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --accent: #1d4ed8;
  --accent-dark: #1e3a8a;
  --accent-soft: #eff4ff;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --ok: #067647;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

a { color: inherit; text-decoration: none; }

button, input { font: inherit; color: inherit; }

.wrap {
  width: 100%;
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Header */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 68px;
  padding-top: 10px;
  padding-bottom: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.brand-text strong { font-size: 15.5px; font-weight: 650; letter-spacing: -0.01em; }
.brand-text small { color: var(--muted); font-size: 12px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 8px 14px;
  border-radius: 9px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.btn:hover { background: #f9fafb; border-color: #b9bec7; }
.btn:active { background: #f3f4f6; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: #1a44bf; border-color: #1a44bf; }
.btn-primary:active { background: #173aa6; }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: #eef0f4; border-color: transparent; }

.btn-danger { color: var(--danger); border-color: #f2c9c4; background: var(--surface); }
.btn-danger:hover { background: var(--danger-soft); border-color: #e9a8a0; }

.btn-sm { min-height: 32px; padding: 5px 10px; font-size: 13px; border-radius: 8px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  padding: 0;
}
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); border-color: #f2c9c4; }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Layout */
main.wrap { padding-top: 26px; padding-bottom: 40px; min-height: calc(100vh - 150px); }
main.wrap:focus { outline: none; }

.app-footer {
  border-top: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 12.5px;
  padding: 16px 0;
}
.app-footer .wrap { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.app-footer .dot { color: var(--line-strong); }

/* Dashboard */
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.page-head h1 {
  font-size: 24px;
  font-weight: 660;
  letter-spacing: -0.02em;
}

.page-head .sub {
  color: var(--muted);
  font-size: 13.5px;
  margin-top: 3px;
}

.dashboard-tools {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 14px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.search-box {
  position: relative;
  flex: 1 1 260px;
  max-width: 420px;
}

.search-box svg {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--faint);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px 8px 36px;
  border-radius: 9px;
  border: 1px solid var(--line-strong);
  background: #fff;
  font-size: 14px;
}
.search-box input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.12); }

.count-label { color: var(--muted); font-size: 13px; padding-right: 4px; white-space: nowrap; }

.bill-list { display: flex; flex-direction: column; gap: 10px; }

.bill {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 18px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.bill:hover { border-color: var(--line-strong); }

.bill-main { min-width: 0; }

.bill-title-row { display: flex; align-items: baseline; gap: 9px; flex-wrap: wrap; }

.bill-number {
  font-size: 12px;
  font-weight: 650;
  color: var(--accent-dark);
  background: var(--accent-soft);
  border-radius: 5px;
  padding: 1px 7px;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.bill-title {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.bill-meta {
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 5px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
}
.bill-meta b { font-weight: 500; color: #4b5563; }

.bill-total {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 16.5px;
  font-weight: 650;
  white-space: nowrap;
}
.bill-total small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); letter-spacing: 0.03em; text-transform: uppercase; }

.bill-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.empty-state {
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 52px 24px;
  text-align: center;
}
.empty-state h2 { font-size: 17px; font-weight: 600; margin-bottom: 6px; }
.empty-state p { color: var(--muted); font-size: 14px; max-width: 430px; margin: 0 auto 18px; }
.empty-state .empty-icon {
  width: 48px; height: 48px; margin: 0 auto 14px;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
  border-radius: 12px;
}

.no-results { padding: 36px 20px; text-align: center; color: var(--muted); font-size: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }

.state-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 24px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}
.state-card h2 { font-size: 17px; font-weight: 600; color: var(--text); margin-bottom: 6px; }
.state-card p { max-width: 440px; margin: 0 auto; }
.state-card .btn { margin-top: 16px; }

/* Editor */
.editor-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.save-state {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  color: var(--muted);
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
}
.save-state .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--ok);
}
.save-state[data-state="saving"] .dot { background: #d97706; }
.save-state[data-state="error"] { color: var(--danger); border-color: #f2c9c4; }
.save-state[data-state="error"] .dot { background: var(--danger); }

.editor-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.editor-head .eyebrow {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.bill-name-input {
  font-size: 24px;
  font-weight: 650;
  letter-spacing: -0.02em;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  padding: 4px 8px;
  margin-left: -8px;
  width: min(520px, 100%);
  min-width: 220px;
}
.bill-name-input::placeholder { color: var(--faint); font-weight: 500; }
.bill-name-input:hover { border-color: var(--line); }
.bill-name-input:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.12); }

.editor-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card + .card { margin-top: 16px; }

.items-card { overflow: hidden; }

.items-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line);
}
.items-head h2 { font-size: 14px; font-weight: 620; }
.items-head .sub { font-size: 12.5px; color: var(--muted); }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table.items {
  width: 100%;
  min-width: 680px;
  border-collapse: collapse;
  table-layout: fixed;
}

table.items th {
  text-align: left;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  background: #fafbfc;
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  white-space: nowrap;
}

table.items td {
  border-bottom: 1px solid #f0f1f3;
  padding: 5px 6px;
  vertical-align: middle;
}

table.items tbody tr:last-child td { border-bottom: none; }
table.items tbody tr:hover td { background: #fcfcfd; }

.col-no { width: 44px; }
.col-qty { width: 96px; }
.col-item { width: auto; }
.col-size { width: 104px; }
.col-rate { width: 116px; }
.col-price { width: 126px; }
.col-del { width: 44px; }

td.col-no { color: var(--faint); font-size: 12.5px; text-align: center; font-variant-numeric: tabular-nums; }

.cell {
  width: 100%;
  min-height: 38px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 7px;
  padding: 7px 9px;
  font-size: 14px;
}
.cell:hover { border-color: var(--line); background: #fff; }
.cell:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.1); }
.cell.num { text-align: right; font-variant-numeric: tabular-nums; }

.price-cell {
  text-align: right;
  padding-right: 12px !important;
  font-variant-numeric: tabular-nums;
  font-weight: 550;
  white-space: nowrap;
}
.price-cell.is-empty { color: var(--faint); font-weight: 400; }

.row-del { text-align: center; }

.items-total {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 22px;
  padding: 14px 18px;
  border-top: 1px solid var(--line);
  background: #fafbfc;
}

.items-total .label { font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.items-total .amount { font-size: 22px; font-weight: 680; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }

.items-footnote { padding: 0 18px 14px; font-size: 12px; color: var(--faint); }

.editor-note {
  margin-top: 14px;
  font-size: 12.5px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 7px;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: #111827;
  color: #fff;
  font-size: 13.5px;
  padding: 9px 16px;
  border-radius: 9px;
  box-shadow: 0 6px 20px rgba(16, 24, 40, 0.18);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 50;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Recycle bin */
.bin {
  margin-top: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.bin-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.bin-toggle:hover { background: #fafbfc; }
.bin-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.bin-title { font-size: 14px; font-weight: 600; }
.bin-count {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 650;
}
.bin-hint { margin-left: auto; color: var(--muted); font-size: 12.5px; }
.bin-caret { width: 14px; color: var(--muted); font-size: 12px; text-align: center; }

.bin-panel { border-top: 1px solid var(--line); }
.bin-empty { padding: 16px; color: var(--muted); font-size: 13px; }

.bin-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f1f3;
}
.bin-item:last-child { border-bottom: 0; }
.bin-main { min-width: 0; }
.bin-title { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; font-size: 14.5px; font-weight: 600; }
.bin-meta { margin-top: 4px; color: var(--muted); font-size: 12.5px; }
.bin-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Version history */
.history-card { overflow: hidden; }
.version-list { display: flex; flex-direction: column; }
.version + .version { border-top: 1px solid #f0f1f3; }

.version-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 11px 16px;
}

.version-main { display: flex; align-items: center; gap: 10px; min-width: 0; }
.version-tag {
  flex: none;
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 11.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.version-text { min-width: 0; }
.version-time { font-size: 13.5px; font-weight: 550; font-variant-numeric: tabular-nums; }
.version-current {
  padding: 1px 6px;
  border: 1px solid #c6e7d3;
  border-radius: 999px;
  background: #f0faf4;
  color: var(--ok);
  font-size: 10.5px;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.version-meta { margin-top: 2px; color: var(--muted); font-size: 12.5px; }
.version-actions { display: flex; gap: 6px; }

.version-preview { padding: 4px 16px 16px; background: #fafbfc; border-top: 1px solid #f0f1f3; }
.version-preview-name { margin: 10px 0 6px; font-size: 13.5px; font-weight: 600; }
.version-table {
  width: 100%;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  border-collapse: collapse;
  background: #fff;
  font-size: 13px;
}
.version-table th {
  padding: 6px 9px;
  background: #f6f7f9;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: left;
}
.version-table td { padding: 6px 9px; border-top: 1px solid #f0f1f3; font-variant-numeric: tabular-nums; }
.version-preview-total { margin-top: 8px; text-align: right; font-size: 13.5px; font-weight: 650; }
.version-empty { margin: 0; padding: 16px; color: var(--muted); font-size: 13px; }

/* Responsive */
@media (max-width: 860px) {
  .bill { grid-template-columns: minmax(0, 1fr) auto; gap: 12px 16px; }
  .bill-actions { grid-column: 1 / -1; justify-content: flex-start; border-top: 1px solid var(--line); padding-top: 10px; }
  .bill-total { align-self: start; }
  .bill-actions .btn { flex: 1 1 auto; }
}

@media (max-width: 640px) {
  body { font-size: 14.5px; }
  .wrap { padding: 0 14px; }
  main.wrap { padding-top: 18px; padding-bottom: 30px; }
  .brand-text small { display: none; }
  .header-inner { min-height: 58px; }
  .header-actions .btn { padding: 7px 11px; font-size: 13px; }
  .page-head h1 { font-size: 21px; }
  .editor-head .eyebrow { margin-bottom: 3px; }
  .bill-name-input { font-size: 20px; }
  .dashboard-tools { padding: 10px; }
  .search-box { max-width: none; }
  .bill { padding: 12px 13px; }
  .bill-total { font-size: 15px; }
  .items-head { padding: 11px 13px; }
  .items-total { padding: 12px 14px; gap: 14px; }
  .items-total .amount { font-size: 19px; }
  .editor-actions { width: 100%; }
  .editor-actions .btn { flex: 1 1 auto; }
  .app-footer .wrap { gap: 4px 7px; }
  .app-footer .dot { display: none; }
  .bin-hint { display: none; }
  .version-actions { width: 100%; justify-content: flex-end; }
}
