:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #132033;
  --muted: #6b778c;
  --blue: #005bff;
  --blue-dark: #0044c7;
  --line: #e4e8f0;
  --green: #0aad55;
  --red: #dd2f2f;
  --yellow: #b98200;
  --shadow: 0 10px 30px rgba(15, 23, 42, .08);
  --radius: 18px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: Arial, Helvetica, sans-serif; background: var(--bg); color: var(--text); }
a { color: inherit; text-decoration: none; }
h1 { margin: 0 0 10px; font-size: 30px; }
h2 { margin: 0 0 14px; font-size: 20px; }
.muted { color: var(--muted); }
.app-shell { min-height: 100vh; display: flex; }
.sidebar { width: 270px; min-height: 100vh; background: #0f172a; color: #fff; padding: 22px 18px; position: fixed; left: 0; top: 0; bottom: 0; }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 28px; }
.brand-mark { width: 44px; height: 44px; border-radius: 14px; background: var(--blue); display: grid; place-items: center; font-weight: 700; }
.brand-title { font-weight: 700; font-size: 18px; }
.brand-subtitle { font-size: 12px; color: #bac4d6; }
.menu { display: grid; gap: 6px; }
.menu-link { padding: 12px 13px; border-radius: 12px; color: #dbe4f0; }
.menu-link:hover, .menu-link.active { background: rgba(255,255,255,.12); color: #fff; }
.main { margin-left: 270px; flex: 1; min-width: 0; }
.main-login { margin-left: 0; display: grid; place-items: center; min-height: 100vh; }
.topbar { height: 70px; background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; padding: 0 28px; position: sticky; top: 0; z-index: 10; }
.topbar-title { font-weight: 700; }
.content { padding: 30px; }
.user-panel { display: flex; gap: 12px; align-items: center; }
.username { font-weight: 600; }
.mobile-menu-button { display: none; border: none; background: var(--blue); color: #fff; border-radius: 9px; padding: 8px 10px; margin-right: 10px; }
.panel, .login-card, .stat-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel { padding: 22px; margin-top: 22px; }
.panel.narrow { max-width: 760px; }
.login-card { width: min(420px, calc(100vw - 32px)); padding: 32px; }
.cards-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 16px; margin-top: 24px; }
.stat-card { padding: 20px; }
.stat-value { font-size: 30px; font-weight: 700; }
.stat-label { margin-top: 8px; font-weight: 600; }
.stat-hint { margin-top: 5px; color: var(--muted); font-size: 13px; }
.steps { display: grid; gap: 10px; }
.step { padding: 12px 14px; border: 1px solid var(--line); border-radius: 13px; background: #f9fbff; }
.step span { display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 50%; background: #e8eef9; margin-right: 8px; font-weight: 700; }
.step.done span { background: var(--green); color: #fff; }
.input { width: 100%; border: 1px solid #cdd5e3; border-radius: 12px; padding: 12px 14px; font-size: 15px; background: #fff; }
label { display: block; margin: 14px 0 6px; font-weight: 600; }
.btn { border: none; border-radius: 12px; padding: 11px 16px; cursor: pointer; font-weight: 700; display: inline-flex; justify-content: center; align-items: center; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-light { background: #eef2f8; color: var(--text); }
.btn-wide { width: 100%; margin-top: 18px; }
.inline-form { display: inline; }
.inline-fields { display: grid; grid-template-columns: 1fr 1fr 180px auto; gap: 10px; align-items: center; }
.form-grid { display: grid; gap: 6px; }
.alert { padding: 13px 15px; border-radius: 12px; margin: 12px 0; }
.alert-danger { background: #fff1f1; color: var(--red); border: 1px solid #ffd1d1; }
.alert-success { background: #eefbf4; color: #087a3d; border: 1px solid #c5f0d7; }
.role-badge, .badge { display: inline-flex; align-items: center; padding: 5px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; background: #eef2f8; color: #34435c; }
.role-admin { background: #e8f0ff; color: #0044c7; }
.role-manager { background: #eaf9f1; color: #087a3d; }
.role-viewer { background: #f6f1e8; color: #8a5a00; }
.page-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.clean-list { margin: 0; padding-left: 20px; line-height: 1.9; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 700; background: #f9fbff; }
@media (max-width: 980px) {
  .sidebar { transform: translateX(-100%); transition: .2s transform; z-index: 20; }
  body.menu-open .sidebar { transform: translateX(0); }
  .main { margin-left: 0; }
  .mobile-menu-button { display: inline-block; }
  .cards-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .inline-fields { grid-template-columns: 1fr; }
}
@media (max-width: 620px) {
  .content { padding: 18px; }
  .topbar { padding: 0 16px; }
  .cards-grid { grid-template-columns: 1fr; }
  .user-panel .username { display: none; }
  h1 { font-size: 24px; }
  .page-heading { display: block; }
  .panel { padding: 16px; }
}

/* Stage 5.8b: unified stable table headers
   All working tables use the same safe model:
   - the table wrapper scrolls vertically and horizontally;
   - TH sticks to the top of that wrapper, not to the whole page;
   - no row can appear above the header. */
:root {
  --table-header-bg: #f9fbff;
  --table-max-height: min(680px, calc(100vh - 210px));
}

.table-wrap,
.calc-table-wrap,
.preview-table-wrap,
.task-wrap,
.history-table-wrap,
.table-scroll {
  max-width: 100% !important;
  max-height: var(--table-max-height) !important;
  overflow: auto !important;
  position: relative !important;
  border: 1px solid var(--line) !important;
  border-radius: 12px !important;
  background: #fff !important;
}

.table-wrap > table,
.calc-table-wrap > table,
.preview-table-wrap > table,
.task-wrap > table,
.history-table-wrap > table,
.table-scroll > table {
  width: 100% !important;
  min-width: 980px;
  border-collapse: separate !important;
  border-spacing: 0 !important;
}

.table-wrap > table thead,
.calc-table-wrap > table thead,
.preview-table-wrap > table thead,
.task-wrap > table thead,
.history-table-wrap > table thead,
.table-scroll > table thead {
  position: static !important;
  top: auto !important;
  z-index: auto !important;
  transform: none !important;
}

.table-wrap > table thead th,
.calc-table-wrap > table thead th,
.preview-table-wrap > table thead th,
.task-wrap > table thead th,
.history-table-wrap > table thead th,
.table-scroll > table thead th,
.calc-table thead th,
.preview-table thead th,
.task-table thead th,
.history-table thead th {
  position: sticky !important;
  top: 0 !important;
  z-index: 20 !important;
  background: var(--table-header-bg) !important;
  color: var(--muted) !important;
  box-shadow: inset 0 -1px 0 var(--line), 0 3px 8px rgba(15, 23, 42, .06) !important;
}

.table-wrap > table tbody td,
.calc-table-wrap > table tbody td,
.preview-table-wrap > table tbody td,
.task-wrap > table tbody td,
.history-table-wrap > table tbody td,
.table-scroll > table tbody td {
  background: #fff;
}

.table-wrap > table thead th:first-child,
.calc-table-wrap > table thead th:first-child,
.preview-table-wrap > table thead th:first-child,
.task-wrap > table thead th:first-child,
.history-table-wrap > table thead th:first-child,
.table-scroll > table thead th:first-child {
  border-top-left-radius: 12px;
}

.table-wrap > table thead th:last-child,
.calc-table-wrap > table thead th:last-child,
.preview-table-wrap > table thead th:last-child,
.task-wrap > table thead th:last-child,
.history-table-wrap > table thead th:last-child,
.table-scroll > table thead th:last-child {
  border-top-right-radius: 12px;
}

/* Reset previous page-sticky values that made headers float under the site topbar. */
[style*="margin-top:var(--table-sticky-top)"],
[style*="margin-top: var(--table-sticky-top)"] {
  margin-top: 0 !important;
}

@media (max-width: 760px) {
  .table-wrap,
  .calc-table-wrap,
  .preview-table-wrap,
  .task-wrap,
  .history-table-wrap,
  .table-scroll {
    max-height: none !important;
  }
  .table-wrap > table thead th,
  .calc-table-wrap > table thead th,
  .preview-table-wrap > table thead th,
  .task-wrap > table thead th,
  .history-table-wrap > table thead th,
  .table-scroll > table thead th,
  .calc-table thead th,
  .preview-table thead th,
  .task-table thead th,
  .history-table thead th {
    position: static !important;
    top: auto !important;
    z-index: auto !important;
  }
}

/* Stage 5.9b: action log */
.action-log-filter {
  display: grid;
  grid-template-columns: 170px 150px minmax(220px, 1fr) 150px 150px 110px 120px auto;
  gap: 10px;
  align-items: end;
}
.action-log-filter label { margin-top: 0; }
.checkbox-line { display: inline-flex; align-items: center; gap: 7px; font-weight: 700; }
.action-log-filter-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.action-log-table-wrap { max-height: min(720px, calc(100vh - 260px)) !important; }
.action-log-apply-wrap { max-height: 520px !important; }
.action-log-table th:nth-child(7), .action-log-table td:nth-child(7) { min-width: 380px; }
.nowrap { white-space: nowrap; }
.small-text { font-size: 12px; margin-top: 4px; }
.details-cell { line-height: 1.35; }
.badge.status-ok { background: #eaf9f1; color: #087a3d; }
.badge.status-bad { background: #fff1f1; color: #bb1d1d; }
.badge.status-warn { background: #fff7df; color: #8a5a00; }
.badge.status-info { background: #eef2f8; color: #34435c; }
@media (max-width: 1180px) {
  .action-log-filter { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .action-log-filter-actions { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  .action-log-filter { grid-template-columns: 1fr; }
  .action-log-cards { grid-template-columns: 1fr 1fr; }
}

/* Stage 6.0: admin settings */
.btn-danger {
  background: #dc2626;
  color: #fff;
  border-color: #dc2626;
}
.btn-danger:hover { filter: brightness(.96); }
code {
  background: #f3f6fb;
  border: 1px solid #e5eaf2;
  border-radius: 6px;
  padding: 2px 5px;
}

/* Stage 6.1: price snapshots */
.btn-danger {
  background: #dc2626;
  color: #fff;
  border-color: #dc2626;
}
.btn-danger:hover { filter: brightness(.96); }
.badge.status-info { background: #eef2f8; color: #34435c; }
.badge.status-warn { background: #fff7df; color: #8a5a00; }
.badge.status-bad { background: #fff1f1; color: #bb1d1d; }
code {
  background: #f3f6fb;
  border: 1px solid #e5eaf2;
  border-radius: 6px;
  padding: 2px 5px;
}

.alert-warning { background: #fff7df; color: #8a5a00; border: 1px solid #f3d999; }
