:root {
  --navy: #1f4e78;
  --blue: #5b9bd5;
  --ink: #1f2933;
  --muted: #607489;
  --line: #d8e2ee;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --green: #2f7d4b;
  --yellow: #a96800;
  --red: #b42318;
  --brand-color: #1f4e78;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

button, input, select {
  font: inherit;
}

.hidden { display: none !important; }

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 32px;
  background:
    linear-gradient(120deg, rgba(31, 78, 120, 0.92), rgba(91, 155, 213, 0.78)),
    linear-gradient(90deg, #f5f7fb, #d9e2f3);
}

.login-panel {
  width: min(460px, 100%);
  background: #fff;
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 8px;
  padding: 34px;
  box-shadow: 0 18px 60px rgba(18, 38, 63, 0.22);
}

.brand-mark {
  width: 74px;
  height: 74px;
  display: grid;
  place-items: center;
  background: var(--brand-color);
  color: #fff;
  border-radius: 8px;
  font-weight: 800;
  font-size: 23px;
  letter-spacing: 0;
  margin-bottom: 20px;
}

.brand-mark.has-logo {
  overflow: hidden;
  padding: 5px;
  background: #fff;
  border: 1px solid var(--line);
}

.brand-mark.has-logo img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transform: scale(1.24);
}

.brand-mark.has-logo:not(.small):not(.preview) {
  width: 104px;
  height: 104px;
}

.brand-mark.small.has-logo {
  width: 52px;
  height: 52px;
}

.brand-mark.preview {
  width: 88px;
  height: 88px;
  margin: 0;
  flex: 0 0 auto;
}

.brand-mark.small {
  width: 44px;
  height: 44px;
  margin: 0;
  font-size: 15px;
}

.login-panel h1 {
  margin: 0 0 8px;
  font-size: 26px;
  line-height: 1.16;
}

.login-panel p {
  margin: 0 0 28px;
  color: var(--muted);
  line-height: 1.45;
}

.login-form, .dialog-form {
  display: grid;
  gap: 16px;
}

label {
  display: grid;
  gap: 7px;
  color: #31465a;
  font-size: 13px;
  font-weight: 700;
}

input, select {
  width: 100%;
  border: 1px solid #c8d5e3;
  border-radius: 6px;
  padding: 11px 12px;
  background: #fff;
  color: var(--ink);
}

input:focus, select:focus {
  outline: 3px solid rgba(91, 155, 213, 0.22);
  border-color: var(--blue);
}

button {
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 800;
}

.login-form button, .primary-button {
  background: var(--navy);
  color: #fff;
  padding: 11px 16px;
}

.secondary-button {
  border: 1px solid #b9c9db;
  background: #fff;
  color: var(--navy);
  padding: 10px 14px;
}

.danger-button {
  border: 1px solid #efb5ad;
  background: #fff5f3;
  color: var(--red);
  padding: 8px 10px;
}

.row-actions {
  display: flex;
  gap: 7px;
  white-space: nowrap;
}

.row-actions .secondary-button,
.row-actions .danger-button {
  padding: 7px 9px;
}

.icon-button {
  border: 1px solid #c8d5e3;
  background: #fff;
  color: var(--ink);
  padding: 8px 10px;
}

.form-error {
  min-height: 18px;
  color: var(--red);
  font-weight: 700;
  font-size: 13px;
}

.login-hint {
  margin-top: 18px;
  padding: 12px;
  background: #eef4fb;
  border: 1px solid #d8e2ee;
  border-radius: 6px;
  color: #415970;
  font-size: 13px;
  line-height: 1.55;
}

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

body.sidebar-wide .app-shell {
  grid-template-columns: 310px minmax(0, 1fr);
}

body.density-compact .sidebar {
  padding: 16px 14px;
  gap: 15px;
}

body.density-compact .workspace {
  padding: 20px 24px;
}

body.density-compact .kpi-card,
body.density-compact .panel {
  padding: 16px;
}

body.density-compact th,
body.density-compact td {
  padding-top: 9px;
  padding-bottom: 9px;
}

.sidebar {
  background: var(--brand-color);
  color: #fff;
  padding: 22px 18px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  min-height: 100vh;
}

.sidebar-brand {
  display: flex;
  gap: 12px;
  align-items: center;
}

.sidebar-brand strong {
  display: block;
  font-size: 16px;
}

.sidebar-brand span {
  color: #bed6ee;
  font-size: 12px;
}

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

.nav-item, .logout-button {
  width: 100%;
  text-align: left;
  color: #e7f0fa;
  background: transparent;
  padding: 11px 12px;
  border-radius: 6px;
}

.nav-item.active, .nav-item:hover {
  background: rgba(255,255,255,0.14);
  color: #fff;
}

.logout-button {
  margin-top: auto;
  border: 1px solid rgba(255,255,255,0.22);
}

.workspace {
  min-width: 0;
  padding: 24px;
}

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

.eyebrow {
  margin: 0 0 3px;
  text-transform: uppercase;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.topbar h2 {
  margin: 0;
  font-size: 28px;
}

.topbar-actions {
  display: flex;
  gap: 10px;
}

.view { display: none; }
.active-view { display: block; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.kpi-card, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(18, 38, 63, 0.06);
}

.kpi-card {
  padding: 18px;
  border-top: 4px solid var(--blue);
}

.kpi-card.warning { border-top-color: #ffc000; }
.kpi-card.danger { border-top-color: #c00000; }

.kpi-card span {
  display: block;
  color: var(--muted);
  font-weight: 800;
  font-size: 12px;
  text-transform: uppercase;
  margin-bottom: 10px;
}

.kpi-card strong {
  font-size: 25px;
  color: var(--navy);
}

.dashboard-grid, .report-grid, .settings-grid {
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  gap: 16px;
}

.panel {
  padding: 18px;
  min-width: 0;
}

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

.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  margin-bottom: 16px;
}

.panel h3, .panel-header h3 {
  margin: 0;
  font-size: 17px;
}

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

.bar-chart {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  align-items: end;
  gap: 10px;
  min-height: 230px;
  padding-top: 12px;
}

.bar-item {
  display: grid;
  gap: 8px;
  align-items: end;
  min-width: 0;
}

.bar {
  background: var(--blue);
  min-height: 6px;
  border-radius: 5px 5px 0 0;
}

.bar-label {
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

.rank-list {
  display: grid;
  gap: 12px;
}

.rank-row {
  display: grid;
  grid-template-columns: minmax(95px, 1fr) 2fr auto;
  gap: 10px;
  align-items: center;
  font-size: 13px;
}

.rank-track {
  height: 9px;
  border-radius: 999px;
  background: #e6edf5;
  overflow: hidden;
}

.rank-fill {
  height: 100%;
  background: var(--blue);
}

.table-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.table-toolbar input, .table-toolbar select {
  max-width: 420px;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 920px;
}

th, td {
  padding: 12px 13px;
  border-bottom: 1px solid #e5edf5;
  text-align: left;
  font-size: 13px;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  background: var(--navy);
  color: #fff;
  z-index: 1;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 12px;
  font-weight: 800;
}

.badge.good { background: #e2f0d9; color: var(--green); }
.badge.warn { background: #fff2cc; color: var(--yellow); }
.badge.bad { background: #fce4d6; color: var(--red); }
.badge.info { background: #d9e2f3; color: var(--navy); }

dialog {
  border: 0;
  border-radius: 8px;
  width: min(860px, calc(100vw - 32px));
  padding: 0;
  box-shadow: 0 24px 80px rgba(18, 38, 63, 0.26);
}

dialog::backdrop {
  background: rgba(13, 31, 48, 0.55);
}

.dialog-form {
  padding: 20px;
}

.dialog-form.compact {
  width: min(430px, 100%);
}

.dialog-form header, .dialog-form footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.dialog-form h3 {
  margin: 0;
  font-size: 20px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.lifetime-policy-fields {
  display: contents;
}

.policy-commission-preview {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 1px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--line);
}

.policy-commission-preview > div {
  min-width: 0;
  display: grid;
  gap: 5px;
  padding: 12px;
  background: var(--panel);
}

.policy-commission-preview span {
  color: var(--muted);
  font-size: 12px;
}

.policy-commission-preview strong {
  font-size: 16px;
}

.summary-lines {
  display: grid;
  gap: 10px;
}

.summary-line {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid #e5edf5;
  padding-bottom: 9px;
}

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

.settings-tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 18px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
}

.settings-tab {
  flex: 0 0 auto;
  padding: 10px 14px;
  border-radius: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
}

.settings-tab.active {
  border-bottom-color: var(--brand-color);
  color: var(--brand-color);
}

[data-settings-pane] {
  display: none;
}

[data-settings-pane].settings-pane-active {
  display: block;
}

.system-settings-form {
  margin-bottom: 18px;
}

.sync-status {
  margin-bottom: 16px;
  padding: 10px 12px;
  border-left: 3px solid var(--brand-color);
  background: #eef4fb;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

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

.appearance-form {
  display: grid;
  grid-template-columns: minmax(280px, 1.5fr) repeat(3, minmax(150px, 0.65fr));
  gap: 16px;
  align-items: end;
}

.logo-control {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-control small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
}

.appearance-actions {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.commission-vitalicia-tool {
  margin-bottom: 18px;
}

.lifetime-commission-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(145px, 1fr)) minmax(160px, 0.8fr) auto;
  gap: 14px;
  align-items: end;
}

.lifetime-commission-form button {
  min-height: 42px;
  white-space: nowrap;
}

.commission-calculation {
  min-height: 42px;
  display: grid;
  align-content: center;
  gap: 3px;
  padding: 6px 12px;
  border-left: 3px solid var(--brand-color);
}

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

.commission-calculation strong {
  font-size: 18px;
}

.settings-form button {
  align-self: end;
}

.inline-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  margin-bottom: 14px;
}

.settings-list {
  display: grid;
  gap: 8px;
  max-height: 310px;
  overflow: auto;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid #e5edf5;
  border-radius: 6px;
  padding: 9px 10px;
  background: #fbfdff;
}

.settings-row strong {
  font-size: 13px;
}

.compact-table table {
  min-width: 760px;
}

.maintenance-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

ul {
  margin: 12px 0 0;
  padding-left: 18px;
  line-height: 1.7;
}

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

  .sidebar {
    min-height: auto;
  }

  .nav-list {
    grid-template-columns: repeat(3, 1fr);
  }

  .kpi-grid, .dashboard-grid, .report-grid, .settings-grid, .form-grid, .settings-form, .appearance-form, .lifetime-commission-form {
    grid-template-columns: 1fr;
  }

  .logo-control {
    align-items: flex-start;
  }

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

  .panel.wide {
    grid-column: auto;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }
}

@media print {
  .sidebar, .topbar-actions, .table-toolbar, dialog {
    display: none !important;
  }

  .app-shell {
    display: block;
  }

  .workspace {
    padding: 0;
  }

  .view {
    display: block;
    page-break-after: always;
  }
}
