*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #f4f5f7;
  --surface:     #ffffff;
  --border:      #e2e4e9;
  --text:        #1a1d23;
  --text-muted:  #6b7280;
  --primary:     #4f46e5;
  --primary-h:   #4338ca;
  --danger:      #dc2626;
  --success:     #16a34a;
  --warning:     #d97706;
  --radius:      8px;
  --shadow:      0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-lg:   0 8px 32px rgba(0,0,0,.2);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  overflow-y: scroll;
}

/* ── Navbar ───────────────────────────────────────────── */
.navbar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav-brand {
  font-weight: 700;
  font-size: 17px;
  color: var(--primary);
  margin-right: 16px;
  white-space: nowrap;
}
.nav-links { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav-link {
  padding: 6px 12px;
  border-radius: var(--radius);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 14px;
  transition: background .15s, color .15s;
  white-space: nowrap;
}
.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: #eef2ff; color: var(--primary); font-weight: 500; }
.nav-user { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.nav-username { font-size: 14px; color: var(--text-muted); }

/* ── Main content ─────────────────────────────────────── */
.main-content { max-width: 1200px; margin: 0 auto; padding: 32px 24px; }
.page-header { margin-bottom: 24px; }
.page-header h1 { font-size: 22px; font-weight: 600; }

/* ── Layout grids ─────────────────────────────────────── */
.page-grid-form {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 24px;
  align-items: start;
}

.form-grid-add {
  display: grid;
  grid-template-columns: 2fr 2fr 1fr auto;
  gap: 12px;
  align-items: end;
}

.form-grid-cat {
  display: grid;
  grid-template-columns: 2fr 1fr 3fr auto;
  gap: 12px;
  align-items: end;
}

/* ── Cards ────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
}
.card-sm { margin-bottom: 16px; }
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.card-header h2 { font-size: 16px; font-weight: 600; }
.card-body { padding: 20px; }
.card-body-flush { padding: 0; }

/* ── Stats grid ───────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-value { font-size: 36px; font-weight: 700; color: var(--primary); line-height: 1.2; }
.stat-label { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

/* ── Table ────────────────────────────────────────────── */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  text-align: left;
  padding: 10px 12px;
  font-weight: 500;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--bg); }
.table-col-actions { width: 120px; }

/* ── Buttons ──────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  text-decoration: none;
  transition: background .15s, transform .1s;
  white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-h); }
.btn-outline { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-full { width: 100%; justify-content: center; }
.btn-block { width: 100%; justify-content: center; margin-bottom: 16px; }

/* ── Icon buttons ─────────────────────────────────────── */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
  padding: 0;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
.icon-btn i, .icon-btn svg { width: 15px; height: 15px; pointer-events: none; }
.icon-btn-danger { border-color: #fecaca; color: var(--danger); }
.icon-btn-danger:hover { background: #fee2e2; }
.icon-row { display: flex; gap: 4px; align-items: center; }

/* ── Forms ────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-group-inline { margin: 0; }
label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
}
input, select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,.12);
}
textarea { resize: vertical; min-height: 90px; }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.input-error { border-color: var(--danger) !important; }

/* ── Checkbox & Radio groups ──────────────────────────── */
.checkbox-group { display: flex; gap: 12px; flex-wrap: wrap; }
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
}
.checkbox-label input[type="checkbox"] { flex-shrink: 0; margin: 0; }

.radio-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.radio-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 400;
  font-size: 14px;
  line-height: 1.4;
}
.radio-label input[type="radio"] { flex-shrink: 0; margin: 0; }

/* ── Alerts ───────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  margin-bottom: 16px;
}
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: var(--success); border: 1px solid #bbf7d0; }

/* ── Badges ───────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
}
.badge-green  { background: #dcfce7; color: #15803d; }
.badge-yellow { background: #fef9c3; color: #a16207; }
.badge-gray   { background: #f3f4f6; color: #4b5563; }
.badge-red    { background: #fee2e2; color: #b91c1c; }
.badge-blue   { background: #dbeafe; color: #1d4ed8; }

/* ── Modals ───────────────────────────────────────────── */
[x-cloak] { display: none !important; }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  padding: 32px;
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow-lg);
  max-height: 90vh;
  overflow-y: auto;
}
.modal-box-sm { max-width: 400px; }
.modal-box-lg { max-width: 560px; }
.modal-title { font-size: 18px; font-weight: 600; margin-bottom: 20px; }
.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 24px;
}

/* ── Split/Merge items ────────────────────────────────── */
.split-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
}
.split-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 600;
}
.merge-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}
.merge-row select { flex: 1; }

/* ── Empty state ──────────────────────────────────────── */
.empty-state { padding: 48px; text-align: center; }

/* ── Login page ───────────────────────────────────────── */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-wrap { width: 100%; max-width: 380px; padding: 24px; }
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px;
  box-shadow: var(--shadow);
}
.login-logo {
  font-size: 28px;
  font-weight: 800;
  color: var(--primary);
  text-align: center;
  letter-spacing: -1px;
}
.login-title {
  font-size: 15px;
  font-weight: 400;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 28px;
}

/* ── Utilities ────────────────────────────────────────── */
.text-muted  { color: var(--text-muted); }
.text-center { text-align: center; }
.text-sm     { font-size: 13px; }
.mt-8        { margin-top: 8px; }
.mt-16       { margin-top: 16px; }
.mb-0        { margin-bottom: 0; }
.flex        { display: flex; }
.flex-end    { justify-content: flex-end; }
.gap-8       { gap: 8px; }
.gap-12      { gap: 12px; }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 1024px) {
  .page-grid-form {
    grid-template-columns: 1fr;
  }
  .form-grid-add {
    grid-template-columns: 1fr 1fr;
  }
  .form-grid-add button[type="submit"] {
    grid-column: 1 / -1;
  }
  .form-grid-cat {
    grid-template-columns: 1fr 1fr;
  }
  .form-grid-cat button[type="submit"] {
    grid-column: 1 / -1;
  }
}

@media (max-width: 768px) {
  .main-content { padding: 16px; }
  .navbar { padding: 0 16px; }
  .nav-username { display: none; }
  .nav-links { gap: 2px; }
  .nav-link { padding: 6px 8px; font-size: 13px; }

  .form-grid-add,
  .form-grid-cat {
    grid-template-columns: 1fr;
  }

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

  .table { font-size: 13px; }
  .table th, .table td { padding: 8px; }

  .modal-box { padding: 20px; }

  .checkbox-group { flex-direction: column; gap: 8px; }
}

@media (max-width: 480px) {
  .nav-brand { font-size: 15px; }
  .page-header h1 { font-size: 18px; }
  .stats-grid { grid-template-columns: 1fr; }
  .modal-footer { flex-direction: column; }
  .modal-footer .btn { width: 100%; justify-content: center; }
}

input[type="radio"],
input[type="checkbox"] {
  width: auto;
  padding: 0;
}

/* ── Toast ────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: #fff;
  padding: 10px 20px;
  border-radius: 99px;
  font-size: 14px;
  opacity: 0;
  transition: opacity .3s, transform .3s;
  pointer-events: none;
  z-index: 999;
}
.toast-show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── File links ───────────────────────────────────────── */
.file-link-external {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--primary);
  text-decoration: none;
}
.file-link-external:hover { text-decoration: underline; }
.file-link-external svg { width: 13px; height: 13px; }

.file-link-local {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}
.file-link-local:hover { color: var(--text); }
.file-link-local svg { width: 13px; height: 13px; }

/* ── HTMX animations ──────────────────────────────────── */
.htmx-added {
  opacity: 0;
  transform: translateY(8px);
}
.htmx-settling .htmx-added,
.htmx-added.htmx-settling {
  opacity: 1;
  transform: translateY(0);
  transition: opacity .25s ease, transform .25s ease;
}

@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.animate-in {
  animation: fadeSlideIn .25s ease forwards;
}

/* ── Stage colors ─────────────────────────────────────── */
.stage-АГР { background: #CC0000; color: #fff; }
.stage-АГК { background: #CC6600; color: #fff; }
.stage-ПД  { background: #006B00; color: #fff; }
.stage-РД  { background: #00007F; color: #fff; }

/* ── Section badges ───────────────────────────────────── */
.section-badge {
  padding: 6px 16px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.section-badge:hover {
  opacity: 0.65;
}
