:root {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --surface-2: #eef3ef;
  --ink: #1c2520;
  --muted: #66736b;
  --line: #d8e0da;
  --brand: #1f6f50;
  --brand-dark: #15533b;
  --blue: #265a7f;
  --warn: #9a5a15;
  --bad: #9d2d2d;
  --shadow: 0 12px 30px rgba(35, 50, 40, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

.hidden {
  display: none !important;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  min-height: 38px;
  border-radius: 6px;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  border-color: #aab8ae;
}

button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: white;
  font-weight: 700;
}

button.primary:hover {
  background: var(--brand-dark);
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
  background: white;
  color: var(--ink);
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(135deg, #eef5ef 0%, #f7f8f4 48%, #eaf0f5 100%);
}

.login-panel {
  width: min(390px, 100%);
  display: grid;
  gap: 14px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.login-brand {
  color: var(--ink);
  margin-bottom: 6px;
}

.login-brand .brand-mark {
  background: #183529;
  color: white;
}

.login-brand span {
  color: var(--muted);
}

.login-error {
  min-height: 18px;
  margin: 0;
  color: var(--bad);
  font-size: 13px;
  font-weight: 700;
}

.install-banner {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid #b6d2c2;
  padding: 10px 18px;
  background: #eaf5ee;
  color: #123829;
  box-shadow: 0 10px 22px rgba(20, 45, 33, 0.08);
}

.install-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.install-copy strong {
  font-size: 14px;
}

.install-copy span {
  color: #3d5c4d;
  font-size: 13px;
  line-height: 1.3;
}

.install-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.app {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: #183529;
  color: white;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #e2f0df;
  color: #183529;
  font-weight: 900;
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  margin-top: 2px;
  color: #bdd0c6;
  font-size: 12px;
}

.nav {
  display: grid;
  gap: 6px;
}

.nav-item {
  border: 0;
  background: transparent;
  color: #dbe8e0;
  text-align: left;
  border-radius: 6px;
  justify-content: flex-start;
}

.nav-item:hover,
.nav-item.active {
  background: rgba(255, 255, 255, 0.12);
  color: white;
}

.sidebar-foot {
  margin-top: auto;
  padding: 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.09);
  display: grid;
  gap: 3px;
}

.sidebar-foot span {
  color: #bdd0c6;
  font-size: 12px;
}

.sidebar-foot button {
  margin-top: 6px;
  min-height: 32px;
  border-color: rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.08);
  color: white;
}

.main {
  min-width: 0;
  padding: 22px;
}

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

.topbar h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.1;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
}

.today {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--muted);
  white-space: nowrap;
}

.view {
  display: none;
}

.view.active {
  display: grid;
  gap: 16px;
}

.grid {
  display: grid;
  gap: 16px;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 16px;
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel h2,
.panel h3 {
  margin: 0;
}

.panel h2 {
  font-size: 17px;
}

.panel h3 {
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 8px;
}

.toolbar,
.inline-form,
.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
}

.toolbar {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.toolbar label {
  min-width: 140px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.form-grid.compact {
  grid-template-columns: repeat(3, minmax(160px, 1fr));
}

.form-grid.one {
  grid-template-columns: 1fr;
}

.span-2 {
  grid-column: span 2;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
  min-height: 92px;
}

.kpi span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.kpi strong {
  display: block;
  margin-top: 10px;
  font-size: 26px;
  line-height: 1.1;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.db-note {
  margin: 12px 0;
  color: var(--muted);
  font-size: 13px;
}

.db-table-wrap {
  max-height: calc(100vh - 245px);
}

#dbTable {
  min-width: 980px;
}

#dbTable input,
#dbTable select,
#usersTable input,
#usersTable select {
  min-width: 120px;
  height: 34px;
  min-height: 34px;
  padding: 5px 8px;
}

#dbTable td.actions,
#usersTable td.actions {
  min-width: 150px;
  white-space: nowrap;
}

#usersTable {
  min-width: 1120px;
}

#usersTable .permissions-cell {
  min-width: 360px;
  max-width: 520px;
}

.permission-editor {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fbfcfb;
}

.permission-note {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
}

.permission-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(150px, 1fr));
  gap: 8px;
}

.permission-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  margin: 0;
  background: white;
}

.permission-group label {
  min-height: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 5px 0;
  font-size: 12px;
}

.permission-group input {
  width: 16px;
  height: 16px;
  min-width: 16px;
}

.permission-view {
  font-weight: 800;
  color: #20352d;
}

.permission-editor.is-admin .permission-grid {
  display: none;
}

#dbTable .readonly-cell {
  display: inline-block;
  min-width: 80px;
  color: #405049;
}

#dbTable .link-cell {
  white-space: nowrap;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 640px;
  background: white;
}

th,
td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 13px;
}

th {
  background: var(--surface-2);
  color: #405049;
  font-size: 12px;
  letter-spacing: 0;
  white-space: nowrap;
}

td.num,
th.num {
  text-align: right;
}

tbody tr:last-child td {
  border-bottom: 0;
}

.empty-row,
.formula-summary.empty {
  color: var(--muted);
}

.ingredients table {
  min-width: 520px;
}

.ingredients select {
  min-width: 260px;
}

.ingredients input {
  min-width: 120px;
}

.remove-row {
  width: 38px;
  padding: 0;
}

.formula-summary {
  display: grid;
  gap: 12px;
}

.summary-strip,
.metric-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fbfcfb;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  font-weight: 800;
  background: #e6f3ec;
  color: #0d6b44;
  white-space: nowrap;
}

.badge.bad {
  background: #fde8e8;
  color: var(--bad);
}

.badge.warn {
  background: #fff1d9;
  color: var(--warn);
}

.file-list {
  display: grid;
  gap: 8px;
}

.file-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  text-decoration: none;
  color: var(--ink);
  background: #fbfcfb;
}

.file-link span {
  color: var(--muted);
  font-size: 12px;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 8px;
  background: #17251d;
  color: white;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 980px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
  }

  .nav {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .nav-item {
    text-align: center;
  }

  .grid.two,
  .grid.three,
  .kpi-grid,
  .summary-strip,
  .metric-list,
  .permission-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 680px) {
  .install-banner {
    align-items: stretch;
    display: grid;
    gap: 10px;
    padding: 10px 14px;
  }

  .install-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .main {
    padding: 14px;
  }

  .topbar {
    display: grid;
  }

  .nav,
  .grid.two,
  .grid.three,
  .kpi-grid,
  .summary-strip,
  .metric-list,
  .permission-grid,
  .form-grid,
  .form-grid.compact {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: span 1;
  }

  .toolbar label {
    min-width: 100%;
  }
}
