:root {
  --navy: #10233e;
  --navy-light: #16304f;
  --blue: #2f6fed;
  --blue-dark: #1f52c4;
  --green: #1e8e5a;
  --red: #c53030;
  --amber: #b7791f;
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #e2e6ec;
  --text: #1a2233;
  --text-muted: #64748b;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

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

#app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--navy);
  color: #dfe7f5;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
}
.sidebar-brand {
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  margin-bottom: 12px;
}
.sidebar-brand .brand-title { font-size: 16px; font-weight: 700; color: #fff; }
.sidebar-brand .brand-sub { font-size: 12px; color: #9db0cc; margin-top: 2px; }
.sidebar-nav { list-style: none; margin: 0; padding: 0; flex: 1; }
.sidebar-nav li a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  color: #c3d0e6;
  font-size: 13.5px;
  border-left: 3px solid transparent;
}
.sidebar-nav li a:hover { background: rgba(255,255,255,0.06); color: #fff; }
.sidebar-nav li a.active {
  background: var(--navy-light);
  color: #fff;
  border-left-color: var(--blue);
  font-weight: 600;
}
.sidebar-nav .nav-icon { width: 18px; text-align: center; opacity: 0.9; }
.sidebar-footer { padding: 14px 20px 0; border-top: 1px solid rgba(255,255,255,0.08); margin-top: 12px; }
.sidebar-footer button { width: 100%; }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 60px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  flex-shrink: 0;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
.topbar .topbar-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.topbar-tag { font-size: 12.5px; white-space: nowrap; }
.content { flex: 1; padding: 24px; overflow-y: auto; overflow-x: hidden; }

.mobile-menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}
.sidebar-backdrop { display: none; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  background: var(--card);
  color: var(--text);
  line-height: 1.2;
}
.btn:hover { filter: brightness(0.97); }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-outline { background: transparent; color: var(--blue); border-color: var(--blue); }
.btn-danger { background: var(--red); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: #eef1f5; }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-block { width: 100%; justify-content: center; }

/* ---------- Cards / layout helpers ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
}
.card + .card { margin-top: 16px; }
.page-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 12px; flex-wrap: wrap; }
.page-toolbar .filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; gap: 16px; }
.grid > * { min-width: 0; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 1000px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }

.stat-card { display: flex; flex-direction: column; gap: 4px; }
.stat-card .stat-value { font-size: 26px; font-weight: 700; }
.stat-card .stat-label { color: var(--text-muted); font-size: 12.5px; }

.section-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); margin: 0 0 10px; }

/* ---------- Tables ---------- */
table.data-table { width: 100%; min-width: 520px; border-collapse: collapse; }
table.data-table th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
table.data-table td { padding: 12px; border-bottom: 1px solid var(--border); font-size: 13.5px; vertical-align: middle; }
table.data-table tbody tr:hover { background: #f8fafc; }
table.data-table tbody tr.clickable { cursor: pointer; }
.table-wrap { background: var(--card); border: 1px solid var(--border); border-radius: 10px; overflow-x: auto; }
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 700; }
.badge-gray { background: #eef1f5; color: #4b5563; }
.badge-blue { background: #e3ecfd; color: var(--blue-dark); }
.badge-green { background: #e2f5ea; color: var(--green); }
.badge-slate { background: #e6e9ee; color: #52606d; }
.badge-amber { background: #fdf1de; color: var(--amber); }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: #374151; }
.field .hint { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
input[type=text], input[type=email], input[type=tel], input[type=number],
input[type=date], input[type=search], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13.5px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(47,111,237,0.12); }
.input-row { display: flex; gap: 10px; flex-wrap: wrap; }
.input-row > * { flex: 1 1 140px; min-width: 0; }

/* ---------- Modal ---------- */
#modal-root { position: fixed; inset: 0; display: none; z-index: 100; }
#modal-root.open { display: block; }
.modal-overlay { position: fixed; inset: 0; background: rgba(16,23,42,0.45); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; }
.modal-box { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,0.25); }
.modal-header { padding: 18px 22px; border-bottom: 1px solid var(--border); }
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px 22px; max-height: 65vh; overflow-y: auto; }
.modal-footer { padding: 14px 22px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

/* ---------- Toasts ---------- */
#toast-root { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px; opacity: 0; transform: translateY(8px); transition: all 0.2s; box-shadow: 0 8px 20px rgba(0,0,0,0.2); }
.toast.show { opacity: 1; transform: translateY(0); }
.toast-error { background: var(--red); }
.toast-success { background: var(--green); }

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty-state p { margin-bottom: 14px; }

/* ---------- Document preview (КП / Договор / Письмо) ---------- */
.doc-preview {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 40px 46px;
  max-width: 780px;
  margin: 0 auto;
  line-height: 1.55;
}
.doc-warning {
  background: #fdf1de;
  border: 1px solid #f1c98a;
  color: #7a4b0a;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 16px;
  text-align: center;
}
.doc-letterhead { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--navy); padding-bottom: 14px; margin-bottom: 20px; }
.doc-letterhead .doc-company { font-weight: 700; font-size: 16px; }
.doc-letterhead .doc-company-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; line-height: 1.5; }
.doc-letterhead .doc-meta { text-align: right; font-size: 12.5px; color: var(--text-muted); }
.doc-title { text-align: center; font-size: 17px; font-weight: 700; margin: 6px 0 18px; }
.doc-section { margin-bottom: 16px; }
.doc-section h4 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); margin: 0 0 8px; }
.doc-section p { margin: 0 0 8px; white-space: pre-wrap; }
.doc-table-scroll { overflow-x: auto; margin: 10px 0; }
.doc-table { width: 100%; min-width: 480px; border-collapse: collapse; margin: 0; }
.doc-table th, .doc-table td { border: 1px solid #d7dce3; padding: 7px 9px; font-size: 12.5px; }
.doc-table th { background: #f4f6f9; text-align: left; }
.doc-totals { margin-top: 8px; font-size: 13px; }
.doc-totals div { display: flex; justify-content: space-between; padding: 3px 0; }
.doc-totals .grand-total { font-weight: 700; font-size: 15px; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 8px; }
.doc-signature { display: flex; justify-content: space-between; margin-top: 30px; }
.doc-editable { border: 1px dashed #c9d2e0; border-radius: 8px; padding: 10px 12px; background: #fbfcfe; }
.doc-toolbar { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }

.two-col { display: grid; grid-template-columns: 1.15fr 0.85fr; gap: 20px; align-items: start; }
.two-col > * { min-width: 0; }
@media (max-width: 1100px) { .two-col { grid-template-columns: 1fr; } }

#items-table { overflow-x: auto; }
.item-row-grid { display: grid; grid-template-columns: 2fr 0.7fr 0.9fr 0.7fr 0.8fr 1fr 32px; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; min-width: 620px; }
.item-row-grid.header { font-size: 11.5px; text-transform: uppercase; color: var(--text-muted); font-weight: 700; border-bottom: 2px solid var(--border); }
.cell-label { display: none; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tabs button { background: none; border: none; padding: 10px 16px; font-size: 13.5px; font-weight: 600; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; flex-shrink: 0; }
.tabs button.active { color: var(--blue); border-bottom-color: var(--blue); }

.kv-list { display: grid; grid-template-columns: 140px 1fr; row-gap: 8px; column-gap: 10px; font-size: 13.5px; }
.kv-list dt { color: var(--text-muted); }
.kv-list dd { margin: 0; }

.link-list { list-style: none; margin: 0; padding: 0; }
.link-list li { padding: 9px 0; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.link-list li:last-child { border-bottom: none; }
.link-list a.item-link { font-weight: 600; color: var(--blue-dark); }

.no-print {}

@media print {
  .sidebar, .topbar, .no-print { display: none !important; }
  .content { padding: 0; overflow: visible; }
  body { background: #fff; }
  .doc-preview { border: none; box-shadow: none; padding: 0; max-width: 100%; }
}

/* ---------- Mobile ---------- */
@media (max-width: 880px) {
  .mobile-menu-btn { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 150;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 12px 0 30px rgba(0,0,0,0.25);
    width: min(var(--sidebar-w), 82vw);
  }
  #app.sidebar-open .sidebar { transform: translateX(0); }
  #app.sidebar-open .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(10,16,28,0.45);
    z-index: 140;
  }

  .topbar { padding: 0 14px; }
  .topbar-tag { display: none; }
  .content { padding: 14px; }

  .doc-preview { padding: 20px 16px; }
  .doc-letterhead { flex-wrap: wrap; gap: 10px; }
  .doc-letterhead .doc-meta { text-align: left; }
  .doc-signature { flex-wrap: wrap; gap: 16px; }

  .modal-overlay { padding: 16px 10px; align-items: center; }
  .modal-body, .modal-header, .modal-footer { padding-left: 16px; padding-right: 16px; }

  .kv-list { grid-template-columns: 100px 1fr; }

  .page-toolbar .filters { width: 100%; }
  .page-toolbar .filters input[type=search] { min-width: 0 !important; flex: 1 1 auto; }
}

@media (max-width: 480px) {
  .grid-4, .grid-3 { grid-template-columns: 1fr 1fr; }
  .stat-card .stat-value { font-size: 21px; }
  .doc-preview { padding: 16px 12px; }
}

/* ---------- Phones: list tables become cards, bigger touch targets ---------- */
@media (max-width: 700px) {
  body { font-size: 14.5px; }

  .btn { padding: 10px 14px; min-height: 40px; }
  .btn-sm { padding: 8px 12px; min-height: 36px; }
  input[type=text], input[type=email], input[type=tel], input[type=number],
  input[type=date], input[type=search], select, textarea {
    padding: 10px 12px;
    font-size: 15px;
    min-height: 42px;
  }
  textarea { min-height: 90px; }

  .page-toolbar { flex-direction: column; align-items: stretch; }
  .page-toolbar > .btn, .page-toolbar > a.btn { width: 100%; }

  /* Dashboard quick actions, per-record quick actions: full-width, stacked */
  .quick-actions { flex-direction: column; }
  .quick-actions > .btn { width: 100%; }

  /* Table -> stacked card list */
  .table-wrap { overflow-x: visible; background: transparent; border: none; }
  table.data-table { min-width: 0; width: 100%; }
  table.data-table thead { display: none; }
  table.data-table tbody { display: block; }
  table.data-table tr {
    display: block;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 10px;
  }
  table.data-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 5px 0;
    border-bottom: none;
    font-size: 13.5px;
    text-align: right;
  }
  table.data-table td[data-label]::before {
    content: attr(data-label);
    font-size: 10.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--text-muted);
    text-align: left;
    flex-shrink: 0;
  }
  table.data-table td:first-child {
    display: block;
    text-align: left;
    font-size: 14.5px;
    padding-bottom: 8px;
    margin-bottom: 4px;
    border-bottom: 1px solid var(--border);
  }
  table.data-table td:last-child:not(:first-child) {
    padding-top: 8px;
    margin-top: 2px;
    border-top: 1px solid var(--border);
  }

  /* Calculation item rows -> stacked mini-cards */
  #items-table { overflow-x: visible; }
  .item-row-grid.header { display: none; }
  .item-row-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "name name" "unit price" "qty discount" "total remove";
    min-width: 0;
    row-gap: 8px;
    column-gap: 10px;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 10px;
  }
  .cell-name { grid-area: name; font-weight: 600; }
  .cell-unit { grid-area: unit; }
  .cell-price { grid-area: price; text-align: right; }
  .cell-qty { grid-area: qty; }
  .cell-discount { grid-area: discount; }
  .cell-total { grid-area: total; font-weight: 700; align-self: center; }
  .cell-remove { grid-area: remove; display: flex; justify-content: flex-end; align-items: center; }
  .cell-label { display: block; font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.02em; color: var(--text-muted); margin-bottom: 3px; }
}
